From c087a6b49b154634f1aaa88e6741ad96e261221f Mon Sep 17 00:00:00 2001 From: Frank Fenton Date: Mon, 17 Sep 2012 22:46:29 +1000 Subject: [PATCH] Add Trakt notification --- .../core/notifications/trakt/__init__.py | 43 +++++++++++++++++ couchpotato/core/notifications/trakt/main.py | 48 +++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 couchpotato/core/notifications/trakt/__init__.py create mode 100644 couchpotato/core/notifications/trakt/main.py diff --git a/couchpotato/core/notifications/trakt/__init__.py b/couchpotato/core/notifications/trakt/__init__.py new file mode 100644 index 00000000..5a3ec725 --- /dev/null +++ b/couchpotato/core/notifications/trakt/__init__.py @@ -0,0 +1,43 @@ +from .main import Trakt + +def start(): + return Trakt() + +config = [{ + 'name': 'trakt', + 'groups': [ + { + 'tab': 'notifications', + 'name': 'trakt_notification', + 'label': 'Trakt', + 'description': 'add movies to your collection once downloaded', + 'options': [ + { + 'name': 'notification_enabled', + 'default': False, + 'type': 'enabler', + }, + { + 'name': 'remove_watchlist_enabled', + 'label': 'Remove from watchlist', + 'default': False, + 'type': 'bool', + }, + { + 'name': 'automation_api_key', + 'label': 'Apikey', + }, + { + 'name': 'automation_username', + 'label': 'Username', + }, + { + 'name': 'automation_password', + 'label': 'Password', + 'type': 'password', + 'description': 'Required even if your account is unprotected.', + }, + ], + } + ], +}] \ No newline at end of file diff --git a/couchpotato/core/notifications/trakt/main.py b/couchpotato/core/notifications/trakt/main.py new file mode 100644 index 00000000..94fecb11 --- /dev/null +++ b/couchpotato/core/notifications/trakt/main.py @@ -0,0 +1,48 @@ +from couchpotato.core.logger import CPLog +from couchpotato.core.notifications.base import Notification +import json +import urllib2 + +log = CPLog(__name__) + +class Trakt(Notification): + + urls = { + 'base': 'http://api.trakt.tv/', + 'library': 'movie/library/%s', + 'unwatchlist': 'movie/unwatchlist/%s', + } + + listen_to = ['movie.downloaded'] + + def notify(self, message = '', data = {}, listener = None): + if self.isDisabled(): return + if not data: return + post_data = { + 'username': self.conf('automation_username'), + 'password' : self.conf('automation_password'), + 'movies': [ { + 'imdb_id': data['library']['identifier'], + 'title': data['library']['titles'][0]['title'], + 'year': data['library']['year'] + } ] + } + result = False + result = self.call((self.urls['library'] % self.conf('automation_api_key')), post_data) + if self.conf('remove_watchlist_enabled'): + result = result and self.call((self.urls['unwatchlist'] % self.conf('automation_api_key')), post_data) + + return result + + def call(self, method_url, post_data): + log.info('opening url: ' + self.urls['base'] + method_url + ', post: ' + str(post_data)) + try: + response = urllib2.urlopen(self.urls['base'] + method_url, json.dumps(post_data)) + if response: + if json.load(response).get('status') == "success": + log.info('Successfully called Trakt') + return True + except: + pass + log.error('Failed to call trakt, check your login.') + return False \ No newline at end of file