From 00d887153fe1e11bf290d5c49f7f1d0944ee74bd Mon Sep 17 00:00:00 2001 From: Ruud Date: Sun, 21 Dec 2014 19:39:16 +0100 Subject: [PATCH 1/5] Return data in main thread --- couchpotato/api.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/couchpotato/api.py b/couchpotato/api.py index 3c8275dc..fbe18b4a 100644 --- a/couchpotato/api.py +++ b/couchpotato/api.py @@ -7,6 +7,7 @@ import urllib from couchpotato.core.helpers.request import getParams from couchpotato.core.logger import CPLog +from tornado.ioloop import IOLoop from tornado.web import RequestHandler, asynchronous @@ -129,6 +130,10 @@ class ApiHandler(RequestHandler): post = get def taskFinished(self, result, route): + IOLoop.current().add_callback(self.sendData, result, route) + self.unlock() + + def sendData(self, result, route): if not self.request.connection.stream.closed(): try: @@ -151,8 +156,6 @@ class ApiHandler(RequestHandler): try: self.finish({'success': False, 'error': 'Failed returning results'}) except: pass - self.unlock() - def unlock(self): try: api_locks[self.route].release() except: pass From 5e6280166689c732b3188042ad060a24946351f9 Mon Sep 17 00:00:00 2001 From: Ruud Date: Sun, 21 Dec 2014 20:19:30 +0100 Subject: [PATCH 2/5] Send data through finish not write --- couchpotato/api.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/couchpotato/api.py b/couchpotato/api.py index fbe18b4a..4efd603b 100644 --- a/couchpotato/api.py +++ b/couchpotato/api.py @@ -141,14 +141,12 @@ class ApiHandler(RequestHandler): jsonp_callback = self.get_argument('callback_func', default = None) if jsonp_callback: - self.write(str(jsonp_callback) + '(' + json.dumps(result) + ')') - self.set_header("Content-Type", "text/javascript") - self.finish() + self.set_header('Content-Type', 'text/javascript') + self.finish(str(jsonp_callback) + '(' + json.dumps(result) + ')') elif isinstance(result, tuple) and result[0] == 'redirect': self.redirect(result[1]) else: - self.write(result) - self.finish() + self.finish(result) except UnicodeDecodeError: log.error('Failed proper encode: %s', traceback.format_exc()) except: From 7a1b914824e3dc8fcb8eb87ed44f22e7fa7adbbc Mon Sep 17 00:00:00 2001 From: Ruud Date: Sun, 21 Dec 2014 20:19:53 +0100 Subject: [PATCH 3/5] Return nonblock results in main thread --- couchpotato/api.py | 26 ++++++++++----------- couchpotato/core/notifications/core/main.py | 3 ++- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/couchpotato/api.py b/couchpotato/api.py index 4efd603b..b5754d82 100644 --- a/couchpotato/api.py +++ b/couchpotato/api.py @@ -51,24 +51,22 @@ class NonBlockHandler(RequestHandler): start, stop = api_nonblock[route] self.stopper = stop - start(self.onNewMessage, last_id = self.get_argument('last_id', None)) + start(self.sendData, last_id = self.get_argument('last_id', None)) - def onNewMessage(self, response): - if self.request.connection.stream.closed(): - self.on_connection_close() - return + def sendData(self, response): + if not self.request.connection.stream.closed(): + try: + self.finish(response) + except: + log.debug('Failed doing nonblock request, probably already closed: %s', (traceback.format_exc())) + try: self.finish({'success': False, 'error': 'Failed returning results'}) + except: pass - try: - self.finish(response) - except: - log.debug('Failed doing nonblock request, probably already closed: %s', (traceback.format_exc())) - try: self.finish({'success': False, 'error': 'Failed returning results'}) - except: pass - - def on_connection_close(self): + self.removeStopper() + def removeStopper(self): if self.stopper: - self.stopper(self.onNewMessage) + self.stopper(self.sendData) self.stopper = None diff --git a/couchpotato/core/notifications/core/main.py b/couchpotato/core/notifications/core/main.py index 4055c413..771d9696 100644 --- a/couchpotato/core/notifications/core/main.py +++ b/couchpotato/core/notifications/core/main.py @@ -14,6 +14,7 @@ from couchpotato.core.logger import CPLog from couchpotato.core.notifications.base import Notification from .index import NotificationIndex, NotificationUnreadIndex from couchpotato.environment import Env +from tornado.ioloop import IOLoop log = CPLog(__name__) @@ -190,7 +191,7 @@ class CoreNotifier(Notification): while len(self.listeners) > 0 and not self.shuttingDown(): try: listener, last_id = self.listeners.pop() - listener({ + IOLoop.current().add_callback(listener, { 'success': True, 'result': [notification], }) From 387a71153815846cea659fac085a09babb64a321 Mon Sep 17 00:00:00 2001 From: Ruud Date: Sun, 21 Dec 2014 21:14:38 +0100 Subject: [PATCH 4/5] TorrentBytes not encoding name fix #4377 --- .../core/media/_base/providers/torrent/torrentbytes.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/couchpotato/core/media/_base/providers/torrent/torrentbytes.py b/couchpotato/core/media/_base/providers/torrent/torrentbytes.py index 156243eb..8e2becb2 100644 --- a/couchpotato/core/media/_base/providers/torrent/torrentbytes.py +++ b/couchpotato/core/media/_base/providers/torrent/torrentbytes.py @@ -1,7 +1,7 @@ import traceback from bs4 import BeautifulSoup -from couchpotato.core.helpers.encoding import tryUrlencode +from couchpotato.core.helpers.encoding import tryUrlencode, toUnicode from couchpotato.core.helpers.variable import tryInt from couchpotato.core.logger import CPLog from couchpotato.core.media._base.providers.torrent.base import TorrentProvider @@ -56,11 +56,12 @@ class Base(TorrentProvider): full_id = link['href'].replace('details.php?id=', '') torrent_id = full_id[:6] + name = toUnicode(link.contents[0]) results.append({ 'id': torrent_id, - 'name': link.contents[0], - 'url': self.urls['download'] % (torrent_id, link.contents[0]), + 'name': name, + 'url': self.urls['download'] % (torrent_id, name), 'detail_url': self.urls['detail'] % torrent_id, 'size': self.parseSize(cells[6].contents[0] + cells[6].contents[2]), 'seeders': tryInt(cells[8].find('span').contents[0]), From 229d67c086ce0e707eabd7bce41cdc21bc544f31 Mon Sep 17 00:00:00 2001 From: Ruud Date: Mon, 22 Dec 2014 22:01:47 +0100 Subject: [PATCH 5/5] Don't toUnicode loop --- couchpotato/core/helpers/encoding.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/couchpotato/core/helpers/encoding.py b/couchpotato/core/helpers/encoding.py index c65fe876..b0fe05cf 100644 --- a/couchpotato/core/helpers/encoding.py +++ b/couchpotato/core/helpers/encoding.py @@ -44,8 +44,7 @@ def toUnicode(original, *args): raise except: log.error('Unable to decode value "%s..." : %s ', (repr(original)[:20], traceback.format_exc())) - ascii_text = str(original).encode('string_escape') - return toUnicode(ascii_text) + return toSafeString(original) def ss(original, *args):