From 3e2d4c5d7bba2ff3b70727d70d1f71fa85dfc8b3 Mon Sep 17 00:00:00 2001 From: Ruud Date: Sun, 19 Feb 2012 12:48:54 +0100 Subject: [PATCH] Initial trailer support --- couchpotato/core/plugins/trailer/__init__.py | 34 ++++++++++++++++ couchpotato/core/plugins/trailer/main.py | 32 +++++++++++++++ couchpotato/core/providers/trailer/base.py | 8 ++++ .../core/providers/trailer/hdtrailers/main.py | 40 +++++++++---------- 4 files changed, 92 insertions(+), 22 deletions(-) create mode 100644 couchpotato/core/plugins/trailer/__init__.py create mode 100644 couchpotato/core/plugins/trailer/main.py diff --git a/couchpotato/core/plugins/trailer/__init__.py b/couchpotato/core/plugins/trailer/__init__.py new file mode 100644 index 00000000..49b0cb9e --- /dev/null +++ b/couchpotato/core/plugins/trailer/__init__.py @@ -0,0 +1,34 @@ +from .main import Trailer + +def start(): + return Trailer() + +config = [{ + 'name': 'trailer', + 'groups': [ + { + 'tab': 'metadata', + 'name': 'trailer', + 'options': [ + { + 'name': 'enabled', + 'label': 'Search and download trailers', + 'default': False, + 'type': 'enabler', + }, + { + 'name': 'quality', + 'default': '720p', + 'type': 'dropdown', + 'values': [('1080P', '1080p'), ('720P', '720p'), ('480P', '480p')], + }, + { + 'name': 'automatic', + 'default': False, + 'type': 'bool', + 'description': 'Automaticly search & download for movies in library', + }, + ], + }, + ], +}] diff --git a/couchpotato/core/plugins/trailer/main.py b/couchpotato/core/plugins/trailer/main.py new file mode 100644 index 00000000..8f8e4ab2 --- /dev/null +++ b/couchpotato/core/plugins/trailer/main.py @@ -0,0 +1,32 @@ +from couchpotato.core.event import addEvent, fireEvent +from couchpotato.core.helpers.variable import getExt +from couchpotato.core.logger import CPLog +from couchpotato.core.plugins.base import Plugin +import os + +log = CPLog(__name__) + + +class Trailer(Plugin): + + def __init__(self): + addEvent('renamer.after', self.searchSingle) + + def searchSingle(self, group): + + if self.isDisabled() or len(group['files']['trailer']) > 0: return + + trailers = fireEvent('trailer.search', group = group, merge = True) + + for trailer in trailers.get(self.conf('quality'), []): + destination = '%s-trailer.%s' % (self.getRootName(group), getExt(trailer)) + if not os.path.isfile(destination): + fireEvent('file.download', url = trailer, dest = destination, urlopen_kwargs = {'headers': {'User-Agent': 'Quicktime'}}, single = True) + else: + log.debug('Trailer already exists: %s' % destination) + + # Download first and break + break + + def getRootName(self, data = {}): + return os.path.join(data['destination_dir'], data['filename']) diff --git a/couchpotato/core/providers/trailer/base.py b/couchpotato/core/providers/trailer/base.py index 17d2e5cb..338ca9b3 100644 --- a/couchpotato/core/providers/trailer/base.py +++ b/couchpotato/core/providers/trailer/base.py @@ -1,5 +1,13 @@ +from couchpotato.core.event import addEvent +from couchpotato.core.logger import CPLog from couchpotato.core.providers.base import Provider +log = CPLog(__name__) + class TrailerProvider(Provider): + type = 'trailer' + + def __init__(self): + addEvent('trailer.search', self.search) diff --git a/couchpotato/core/providers/trailer/hdtrailers/main.py b/couchpotato/core/providers/trailer/hdtrailers/main.py index 5fe476dd..e6628760 100644 --- a/couchpotato/core/providers/trailer/hdtrailers/main.py +++ b/couchpotato/core/providers/trailer/hdtrailers/main.py @@ -1,4 +1,5 @@ from BeautifulSoup import SoupStrainer, BeautifulSoup +from couchpotato.core.helpers.variable import mergeDicts from couchpotato.core.logger import CPLog from couchpotato.core.providers.trailer.base import TrailerProvider from string import letters, digits @@ -16,47 +17,40 @@ class HDTrailers(TrailerProvider): } providers = ['apple.ico', 'yahoo.ico', 'moviefone.ico', 'myspace.ico', 'favicon.ico'] - def find(self, movie): + def search(self, group): - movie_name = movie['library']['titles'][0]['title'] + movie_name = group['library']['titles'][0]['title'] - url = self.url['api'] % self.movieUrlName(movie_name) - try: - data = self.urlopen(url) - except: - return {} + url = self.urls['api'] % self.movieUrlName(movie_name) + data = self.getCache('hdtrailers.%s' % group['library']['identifier'], url) + + result_data = {} - p480 = [] - p720 = [] - p1080 = [] did_alternative = False for provider in self.providers: results = self.findByProvider(data, provider) # Find alternative if results.get('404') and not did_alternative: - results = self.findViaAlternative(movie_name) + results = self.findViaAlternative(group) did_alternative = True - p480.extend(results.get('480p')) - p720.extend(results.get('720p')) - p1080.extend(results.get('1080p')) + result_data = mergeDicts(result_data, results) - return {'480p':p480, '720p':p720, '1080p':p1080} + return result_data - def findViaAlternative(self, movie): + def findViaAlternative(self, group): results = {'480p':[], '720p':[], '1080p':[]} - url = "%s?%s" % (self.url['backup'], urlencode({'s':movie})) - try: - data = self.urlopen(url) - except: - return results + movie_name = group['library']['titles'][0]['title'] + + url = "%s?%s" % (self.url['backup'], urlencode({'s':movie_name})) + data = self.getCache('hdtrailers.alt.%s' % group['library']['identifier'], url) try: tables = SoupStrainer('div') html = BeautifulSoup(data, parseOnlyThese = tables) - result_table = html.findAll('h2', text = re.compile(movie)) + result_table = html.findAll('h2', text = re.compile(movie_name)) for h2 in result_table: if 'trailer' in h2.lower(): @@ -88,6 +82,8 @@ class HDTrailers(TrailerProvider): break if 'trailer' in trtext and not 'clip' in trtext and provider in trtext: nr = 0 + if 'trailer' not in tr.find('span', 'standardTrailerName').text.lower(): + continue resolutions = tr.findAll('td', attrs = {'class':'bottomTableResolution'}) for res in resolutions: results[str(res.a.contents[0])].insert(0, res.a['href'])