Skip to content

Commit

Permalink
better exception handling for low bandwidth scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
ozmartian committed Jan 8, 2017
1 parent 455be0b commit 24193d1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 26 deletions.
19 changes: 15 additions & 4 deletions hosters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,18 @@
from PyQt5.QtCore import QTimer, QUrl, Qt, pyqtSignal, pyqtSlot
from PyQt5.QtGui import QCloseEvent, QDesktopServices, QIcon, QPixmap, QShowEvent
from PyQt5.QtWidgets import (QBoxLayout, QButtonGroup, QDialog, QGroupBox, QHBoxLayout, QLabel, QProgressDialog,
QPushButton, QSizePolicy, QSpacerItem, QStyleFactory, QVBoxLayout, qApp)
QProxyStyle, QPushButton, QSizePolicy, QSpacerItem, QStyle, QStyleFactory, QVBoxLayout,
qApp)


class OverrideProxyStyle(QProxyStyle):
def __init__(self):
super(OverrideProxyStyle, self).__init__('Fusion')

def styleHint(self, hint, option, widget, returnData) -> int:
if hint == QStyle.SH_UnderlineShortcut:
return 0;
return super(OverrideProxyStyle, self).styleHint(hint, option, widget, returnData)


class HosterLinks(QDialog):
Expand Down Expand Up @@ -53,19 +64,19 @@ def show_hosters(self, hosters: list) -> None:
hoster_logo.setAlignment(Qt.AlignCenter)
copy_btn = QPushButton(self, icon=self.copy_icon, text=' COPY', toolTip='Copy to clipboard',
autoDefault=False, default=False, cursor=Qt.PointingHandCursor)
copy_btn.setStyle(QStyleFactory.create('Fusion'))
copy_btn.setStyle(FusionProxyStyle())
copy_btn.setStyleSheet('padding:2px 10px;')
copy_btn.setMinimumHeight(35)
self.copy_group.addButton(copy_btn, index)
open_btn = QPushButton(self, icon=self.open_icon, text=' OPEN', toolTip='Open in browser',
autoDefault=False, default=False, cursor=Qt.PointingHandCursor)
open_btn.setStyle(QStyleFactory.create('Fusion'))
open_btn.setStyle(FusionProxyStyle())
open_btn.setStyleSheet('padding:2px 10px;')
open_btn.setMinimumHeight(35)
self.open_group.addButton(open_btn, index)
download_btn = QPushButton(self, icon=self.download_icon, text=' DOWNLOAD', toolTip='Download link',
autoDefault=False, default=False, cursor=Qt.PointingHandCursor)
download_btn.setStyle(QStyleFactory.create('Fusion'))
download_btn.setStyle(FusionProxyStyle())
download_btn.setStyleSheet('padding:2px 10px;')
download_btn.setMinimumHeight(35)
self.download_group.addButton(download_btn, index)
Expand Down
56 changes: 34 additions & 22 deletions threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import sys
from urllib.parse import urlencode
from urllib.request import Request, urlopen
from urllib.error import HTTPError
from urllib.error import HTTPError, URLError

from PyQt5.QtCore import QSettings, QThread, pyqtSignal
from PyQt5.QtWidgets import QMessageBox, qApp
Expand All @@ -30,14 +30,16 @@ def scrape_links(self) -> None:
for page in range(1, self.maxpages + 1):
url = self.source_url % page
req = Request(url, headers={'User-Agent': self.user_agent})
res = urlopen(req)
if sys.platform == 'win32':
try:
res = urlopen(req)
except URLError:
print(sys.exc_info())
QMessageBox.critical(self, 'ERROR NOTIFICATION', sys.exc_info(), QMessageBox.Ok)
self.exit()
try:
bs = BeautifulSoup(res.read(), 'lxml')
except FeatureNotFound:
bs = BeautifulSoup(res.read(), 'html.parser')
else:
try:
bs = BeautifulSoup(res.read(), 'lxml')
except FeatureNotFound:
bs = BeautifulSoup(res.read(), 'html.parser')
links = bs.find_all('table', class_='posts_table')
for link_table in links:
cols = link_table.tr.find_all('td')
Expand Down Expand Up @@ -68,17 +70,19 @@ def __del__(self) -> None:
def get_hoster_links(self) -> None:
hosters = []
req = Request(self.link_url, headers={'User-Agent': self.user_agent})
res = urlopen(req)
if sys.platform == 'win32':
try:
res = urlopen(req)
except URLError:
print(sys.exc_info())
QMessageBox.critical(self, 'ERROR NOTIFICATION', sys.exc_info(), QMessageBox.Ok)
self.exit()
try:
bs = BeautifulSoup(res.read(), 'lxml')
except FeatureNotFound:
bs = BeautifulSoup(res.read(), 'html.parser')
else:
try:
bs = BeautifulSoup(res.read(), 'lxml')
except FeatureNotFound:
bs = BeautifulSoup(res.read(), 'html.parser')
dltable = bs.find('table', id='download_table').find_all('tr')
for hoster_html in dltable:
hosters.append([hoster_html.td.img.get('src'), hoster_html.find('td', class_='td_cols').a.get('href')])
hosters.append([hoster_html.td.img.get('src'), hoster_html.find('td', class_='td_cols').a.sget('href')])
self.setHosters.emit(hosters)

def run(self) -> None:
Expand Down Expand Up @@ -119,7 +123,9 @@ def connect(self, endpoint: str, payload: object=None) -> object:
res = urlopen(req).read().decode('utf-8')
return json.loads(res)
except HTTPError:
QMessageBox.critical(self, 'Real-Debrid API Error',
print(sys.exc_info())
QMessageBox.critical(self, 'ERROR NOTIFICATION',
'<h3>Real-Debrid API Error</h3>' +
'A problem occurred whilst communicating with Real-Debrid. Please check your '
'Internet connection.<br/><br/>' +
'<b>ERROR LOG:</b><br/>(Error Code %s) %s<br/>%s'
Expand Down Expand Up @@ -177,12 +183,18 @@ def add_uri(self) -> None:
payload = json.dumps({'jsonrpc': '2.0', 'id': 1, 'method': 'aria2.addUri',
'params': ['%s:%s' % (user, passwd), [self.link_url]]}, sort_keys=False).encode('utf-8')
req = Request(aria2_endpoint, headers=headers, data=payload)
res = urlopen(req).read().decode('utf-8')
jsonres = json.loads(res)
if 'result' in jsonres.keys():
self.aria2Confirmation.emit(True)
else:
try:
res = urlopen(req).read().decode('utf-8')
jsonres = json.loads(res)
if 'result' in jsonres.keys():
self.aria2Confirmation.emit(True)
else:
self.aria2Confirmation.emit(False)
except (URLError, HTTPError) as e:
print(sys.exc_info())
QMessageBox.critical(self, 'ERROR NOTIFICATION', e.reason, QMessageBox.Ok)
self.aria2Confirmation.emit(False)
# self.exit()

def run(self) -> None:
self.add_uri()
Expand Down

0 comments on commit 24193d1

Please sign in to comment.