From 8780d86ed4d25f0c9c5f63d406248e7825b1cb16 Mon Sep 17 00:00:00 2001 From: Ruud Date: Sat, 14 Jan 2012 00:23:46 +0100 Subject: [PATCH 1/4] py2app fixes --- couchpotato/core/_base/_core/main.py | 18 ++++++++---------- couchpotato/core/_base/desktop/main.py | 19 +++++++++++-------- couchpotato/environment.py | 2 +- libs/guessit/fileutils.py | 3 +-- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/couchpotato/core/_base/_core/main.py b/couchpotato/core/_base/_core/main.py index e9b4bdf5..4c7e75b5 100644 --- a/couchpotato/core/_base/_core/main.py +++ b/couchpotato/core/_base/_core/main.py @@ -1,4 +1,3 @@ -from couchpotato import app from couchpotato.api import addApiView from couchpotato.core.event import fireEvent, addEvent from couchpotato.core.helpers.request import jsonified @@ -7,7 +6,6 @@ from couchpotato.core.logger import CPLog from couchpotato.core.plugins.base import Plugin from couchpotato.environment import Env from flask import request -from flask.helpers import url_for import os import time import traceback @@ -29,6 +27,7 @@ class Core(Plugin): addEvent('app.crappy_restart', self.crappyRestart) addEvent('app.load', self.launchBrowser, priority = 1) addEvent('app.base_url', self.createBaseUrl) + addEvent('app.api_url', self.createApiUrl) addEvent('setting.save.core.password', self.md5Password) @@ -43,16 +42,10 @@ class Core(Plugin): }) def crappyShutdown(self): - ctx = app.test_request_context() - ctx.push() - self.urlopen('%s%sapp.shutdown' % (fireEvent('app.base_url', single = True), url_for('api.index'))) - ctx.pop() + self.urlopen('%sapp.shutdown' % self.createApiUrl()) def crappyRestart(self): - ctx = app.test_request_context() - ctx.push() - self.urlopen('%s%sapp.restart' % (fireEvent('app.base_url', single = True), url_for('api.index'))) - ctx.pop() + self.urlopen('%sapp.restart' % self.createApiUrl()) def shutdown(self): self.initShutdown() @@ -63,6 +56,7 @@ class Core(Plugin): return 'restarting' def initShutdown(self, restart = False): + log.info('Shutting down' if not restart else 'Restarting') fireEvent('app.shutdown') @@ -122,3 +116,7 @@ class Core(Plugin): port = Env.setting('port') return '%s:%d' % (cleanHost(host).rstrip('/'), int(port)) + + def createApiUrl(self): + + return '%s/%s/' % (self.createBaseUrl(), Env.setting('api_key')) diff --git a/couchpotato/core/_base/desktop/main.py b/couchpotato/core/_base/desktop/main.py index bd52dcd5..ce1ff282 100644 --- a/couchpotato/core/_base/desktop/main.py +++ b/couchpotato/core/_base/desktop/main.py @@ -7,25 +7,28 @@ log = CPLog(__name__) if Env.get('desktop'): - #import os - #import sys - import wx - class Desktop(Plugin): def __init__(self): desktop = Env.get('desktop') desktop.setSettings({ - 'url': fireEvent('app.base_url', single = True) + 'base_url': fireEvent('app.base_url', single = True), + 'api_url': fireEvent('app.api_url', single = True), + 'api': Env.setting('api'), }) - def onClose(event): - return fireEvent('app.crappy_shutdown') - desktop.close_handler = onClose + # Events from desktop + desktop.addEvents({ + 'onClose': self.onClose, + }) + # Events to desktop addEvent('app.after_shutdown', desktop.afterShutdown) + def onClose(self, event): + return fireEvent('app.crappy_shutdown', single = True) + else: class Desktop(Plugin): diff --git a/couchpotato/environment.py b/couchpotato/environment.py index c6b427e5..39adb3df 100644 --- a/couchpotato/environment.py +++ b/couchpotato/environment.py @@ -14,7 +14,7 @@ class Env(object): _args = None _quiet = False _deamonize = False - _version = 0.5 + _desktop = None ''' Data paths and directories ''' _app_dir = "" diff --git a/libs/guessit/fileutils.py b/libs/guessit/fileutils.py index 7c07af71..1c263b81 100644 --- a/libs/guessit/fileutils.py +++ b/libs/guessit/fileutils.py @@ -18,7 +18,6 @@ # along with this program. If not, see . # -import ntpath import os.path @@ -45,7 +44,7 @@ def split_path(path): """ result = [] while True: - head, tail = ntpath.split(path) + head, tail = os.path.split(path) # on Unix systems, the root folder is '/' if head == '/' and tail == '': From e9a77f8da7d55ef5a18c846987ac16f6f4662c94 Mon Sep 17 00:00:00 2001 From: Ruud Date: Sat, 14 Jan 2012 11:45:55 +0100 Subject: [PATCH 2/4] Add headers to urlopener --- couchpotato/core/plugins/base.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/couchpotato/core/plugins/base.py b/couchpotato/core/plugins/base.py index 05826609..3f9519cb 100644 --- a/couchpotato/core/plugins/base.py +++ b/couchpotato/core/plugins/base.py @@ -93,6 +93,12 @@ class Plugin(object): socket.setdefaulttimeout(timeout) + # Fill in some headers + if not headers.get('Referer'): + headers['Referer'] = urlparse(url).hostname + if not headers.get('User-Agent'): + headers['User-Agent'] = '' + host = urlparse(url).hostname self.wait(host) From ab5d9e29d6bead5c9ccfebc8bb4df20e5c80025d Mon Sep 17 00:00:00 2001 From: Ruud Date: Sat, 14 Jan 2012 11:46:26 +0100 Subject: [PATCH 3/4] Add year to x264 provider search --- couchpotato/core/providers/nzb/x264/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/couchpotato/core/providers/nzb/x264/main.py b/couchpotato/core/providers/nzb/x264/main.py index 9224acc9..c4d80531 100644 --- a/couchpotato/core/providers/nzb/x264/main.py +++ b/couchpotato/core/providers/nzb/x264/main.py @@ -23,7 +23,7 @@ class X264(NZBProvider): if self.isDisabled() or not self.isAvailable(self.urls['search']) or not quality.get('hd', False): return results - q = '%s %s' % (movie['library']['titles'][0]['title'], quality.get('identifier')) + q = '%s %s %s' % (movie['library']['titles'][0]['title'], movie['library']['year'], quality.get('identifier')) url = self.urls['search'] % quote_plus(q) cache_key = 'x264.%s' % q From f1a682c0261fe988f29e1b646eba9cc443a6a097 Mon Sep 17 00:00:00 2001 From: Ruud Date: Sat, 14 Jan 2012 11:46:53 +0100 Subject: [PATCH 4/4] Log errors via API --- couchpotato/core/plugins/log/main.py | 21 ++++++++++++++++++++- couchpotato/templates/_desktop.html | 16 ++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/couchpotato/core/plugins/log/main.py b/couchpotato/core/plugins/log/main.py index 25bb783d..00d2baf0 100644 --- a/couchpotato/core/plugins/log/main.py +++ b/couchpotato/core/plugins/log/main.py @@ -1,5 +1,5 @@ from couchpotato.api import addApiView -from couchpotato.core.helpers.request import jsonified, getParam +from couchpotato.core.helpers.request import jsonified, getParam, getParams from couchpotato.core.logger import CPLog from couchpotato.core.plugins.base import Plugin from couchpotato.environment import Env @@ -14,6 +14,7 @@ class Logging(Plugin): def __init__(self): addApiView('logging.get', self.get) addApiView('logging.clear', self.clear) + addApiView('logging.log', self.log) def get(self): @@ -67,3 +68,21 @@ class Logging(Plugin): return jsonified({ 'success': True }) + + def log(self): + + params = getParams() + + try: + log_message = 'API log: %s' % params + try: + getattr(log, params.get('type', 'error'))(log_message) + except: + log.error(log_message) + except: + log.error('Couldn\'t log via API: %s' % params) + + + return jsonified({ + 'success': True + }) diff --git a/couchpotato/templates/_desktop.html b/couchpotato/templates/_desktop.html index 637aa61f..edef184b 100644 --- a/couchpotato/templates/_desktop.html +++ b/couchpotato/templates/_desktop.html @@ -52,6 +52,22 @@ 'is_remote': false }); + // Catch errors + window.onerror = function(message, file, line){ + + Api.request('logging.log', { + 'data': { + 'type': 'error', + 'message': Browser.name + ' ' + Browser.version + ': \n' + message, + 'page': window.location.href, + 'file': file, + 'line': line + } + }); + + return true; + } + Quality.setup({ 'profiles': {{ fireEvent('profile.all', single = True)|tojson|safe }}, 'qualities': {{ fireEvent('quality.all', single = True)|tojson|safe }}