def get_trailer(self, title_date): ''' Gets trailer embed url from Youtube. :param title_date: str movie title and date ("Movie Title 2016") Attempts to connect 3 times in case Youtube is down or not responding Can fail if no response is recieved. Returns str or None ''' search_term = Url.encode((title_date + '+trailer')) search_string = u"https://www.googleapis.com/youtube/v3/search?part=snippet&q={}&maxResults=1&key={}".format(search_term, _k('youtube')) request = Url.request(search_string) tries = 0 while tries < 3: try: response = Url.open(request) results = json.loads(response) return results['items'][0]['id']['videoId'] except (SystemExit, KeyboardInterrupt): raise except Exception, e: # noqa if tries == 2: logging.error(u'Unable to get trailer from Youtube.', exc_info=True) tries += 1
def fuzzy_title(self, titles): ''' Score and remove results based on title match titles: list of titles to match against If titles is an empty list every result is treated as a perfect match Iterates through self.results and removes any entry that does not fuzzy match 'title' > 60. Adds fuzzy_score / 20 points to ['score'] *If title is passed as None, assumes perfect match and scores +20 Does not return ''' logging.info(u'Checking title match.') lst = [] if titles == []: for result in self.results: result['score'] += 20 lst.append(result) else: for result in self.results: if result['type'] == 'import' and result not in lst: result['score'] += 20 lst.append(result) continue test = Url.encode(result['title']) matches = [ fuzz.partial_ratio(Url.encode(title), test) for title in titles ] if any([match > 70 for match in matches]): result['score'] += (max(matches) / 5) lst.append(result) else: logging.debug( u'{} best title match was {}%, removing search result.' .format(test, max(matches))) self.results = lst logging.info(u'Keeping {} results.'.format(len(self.results)))
def get_imdbid(self, tmdbid=None, title=None, year=''): ''' Gets imdbid from tmdbid tmdbid: str TMDB movie id # title: str movie title year str year of movie release MUST supply either tmdbid or title. Year is optional with title, but results are more reliable with it. Returns str imdbid or None on failure ''' if not tmdbid and not title: logging.warning(u'Neither tmdbid or title supplied. Unable to find imdbid.') return None if not tmdbid: title = Url.encode(title) year = Url.encode(year) url = u'https://api.themoviedb.org/3/search/movie?api_key={}&language=en-US&query={}&year={}&page=1&include_adult=false'.format(_k('tmdb'), title, year) request = Url.request(url) while self.get_tokens() < 3: sleep(0.3) self.use_token() try: response = Url.open(request) results = json.loads(response) results = results['results'] if results: tmdbid = results[0]['id'] else: return None except (SystemExit, KeyboardInterrupt): raise except Exception, e: # noqa logging.error(u'Error attempting to get TMDBID from TMDB.', exc_info=True) return None
def _search_title(self, title): ''' Search TMDB for title title: str movie title Title can include year ie Move Title 2017 Returns list results or str error/fail message ''' title = Url.encode(title) url = u'https://api.themoviedb.org/3/search/movie?page=1&include_adult=false&' if title[-4:].isdigit(): query = u'query={}&year={}'.format(title[:-5], title[-4:]) else: query = u'query={}'.format(title) url = url + query logging.info('Searching TMDB {}'.format(url)) url = url + '&api_key={}'.format(_k('tmdb')) request = Url.request(url) while self.get_tokens() < 3: sleep(0.3) self.use_token() try: response = Url.open(request) results = json.loads(response) if results.get('success') == 'false': return None else: return results['results'][:6] except (SystemExit, KeyboardInterrupt): raise except Exception, e: # noqa logging.error(u'Error searching for title on TMDB.', exc_info=True) return ['']
def search_rss(self, title_year): ''' Searches predb rss for title_year :param title_year: str movie title and year 'Black Swan 2010' Returns list of found rss entries or None if not found. ''' title_year = Url.encode(title_year) url = u'http://predb.me/?cats=movies&search={}&rss=1'.format( title_year) request = Url.request(url) try: response = Url.open(request) results_xml = response.replace('&', '%26') items = self.parse_predb_xml(results_xml) return items except (SystemExit, KeyboardInterrupt): raise except Exception, e: # noqa logging.error(u'Predb.me search failed.', exc_info=True) return None
def search_all(self, imdbid, title, year): ''' Search all Torrent indexers. imdbid: string imdb movie id. title: str movie title year: str year of movie release Returns list of dicts with sorted nzb information. ''' torz_indexers = core.CONFIG['Indexers']['TorzNab'].values() self.imdbid = imdbid results = [] term = '{}+{}'.format(title, year).replace(' ', '+') for indexer in torz_indexers: if indexer[2] is False: continue url_base = indexer[0] if url_base[-1] != u'/': url_base = url_base + '/' apikey = indexer[1] r = self.search_newznab(url_base, apikey, t='search', cat=2000, q=imdbid) for i in r: results.append(i) torrent_indexers = core.CONFIG['Indexers']['Torrent'] title = Url.encode(title) year = Url.encode(year) if torrent_indexers['rarbg']: rarbg_results = Rarbg.search(imdbid) for i in rarbg_results: if i not in results: results.append(i) if torrent_indexers['limetorrents']: lime_results = LimeTorrents.search(imdbid, term) for i in lime_results: if i not in results: results.append(i) if torrent_indexers['extratorrent']: extra_results = ExtraTorrent.search(imdbid, term) for i in extra_results: if i not in results: results.append(i) if torrent_indexers['skytorrents']: sky_results = SkyTorrents.search(imdbid, term) for i in sky_results: if i not in results: results.append(i) if torrent_indexers['bitsnoop']: bit_results = BitSnoop.search(imdbid, term) for i in bit_results: if i not in results: results.append(i) if torrent_indexers['torrentz2']: torrentz_results = Torrentz2.search(imdbid, term) for i in torrentz_results: if i not in results: results.append(i) if torrent_indexers['thepiratebay']: tpb_results = ThePirateBay.search(imdbid, term) for i in tpb_results: if i not in results: results.append(i) self.imdbid = None return results