From 1e50c0f8da1853325e8b5cd1c38d1fc9910c74e1 Mon Sep 17 00:00:00 2001 From: l3uddz Date: Thu, 8 Mar 2018 16:38:45 +0000 Subject: [PATCH] added boxoffice list to movies command --- media/trakt.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++++- misc/str.py | 13 ++++++++++++ traktarr.py | 4 +++- 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/media/trakt.py b/media/trakt.py index a164352..ef7e856 100644 --- a/media/trakt.py +++ b/media/trakt.py @@ -349,7 +349,7 @@ class Trakt: if req.status_code == 200: resp_json = req.json() - # process list so it conforms to standard we expect ( e.g. {"show": {.....}} ) + # process list so it conforms to standard we expect ( e.g. {"movie": {.....}} ) for movie in resp_json: if movie not in processed_movies: processed_movies.append({'movie': movie}) @@ -377,3 +377,58 @@ class Trakt: except Exception: log.exception("Exception retrieving popular movies: ") return None + + @backoff.on_predicate(backoff.expo, lambda x: x is None, max_tries=4, on_backoff=backoff_handler) + def get_boxoffice_movies(self, limit=1000, languages=None): + try: + processed_movies = [] + + if languages is None: + languages = ['en'] + + # generate payload + payload = {'extended': 'full', 'limit': limit, 'page': 1} + if languages: + payload['languages'] = ','.join(languages) + + # make request + while True: + req = requests.get('https://api.trakt.tv/movies/boxoffice', params=payload, headers=self.headers, + timeout=30) + log.debug("Request URL: %s", req.url) + log.debug("Request Payload: %s", payload) + log.debug("Response Code: %d", req.status_code) + log.debug("Response Page: %d of %d", payload['page'], + 0 if 'X-Pagination-Page-Count' not in req.headers else int( + req.headers['X-Pagination-Page-Count'])) + + if req.status_code == 200: + resp_json = req.json() + + for movie in resp_json: + if movie not in processed_movies: + processed_movies.append(movie) + + # check if we have fetched the last page, break if so + if 'X-Pagination-Page-Count' not in req.headers or not int(req.headers['X-Pagination-Page-Count']): + log.debug("There was no more pages to retrieve") + break + elif payload['page'] >= int(req.headers['X-Pagination-Page-Count']): + log.debug("There are no more pages to retrieve results from") + break + else: + log.info("There are %d pages left to retrieve results from", + int(req.headers['X-Pagination-Page-Count']) - payload['page']) + payload['page'] += 1 + + else: + log.error("Failed to retrieve boxoffice movies, request response: %d", req.status_code) + break + + if len(processed_movies): + log.debug("Found %d boxoffice movies", len(processed_movies)) + return processed_movies + return None + except Exception: + log.exception("Exception retrieving boxoffice movies: ") + return None diff --git a/misc/str.py b/misc/str.py index f47425c..91c9cab 100644 --- a/misc/str.py +++ b/misc/str.py @@ -13,3 +13,16 @@ def get_year_from_timestamp(timestamp): except Exception: log.exception("Exception parsing year from %s: ", timestamp) return int(year) if str(year).isdigit() else 0 + + +def is_ascii(string): + try: + string.encode('ascii') + except UnicodeEncodeError: + return False + except UnicodeDecodeError: + return False + except Exception: + log.exception(u"Exception checking if %r was ascii: ", string) + return False + return True diff --git a/traktarr.py b/traktarr.py index d24f645..abb311b 100644 --- a/traktarr.py +++ b/traktarr.py @@ -150,7 +150,7 @@ def shows(list_type, add_limit=0, add_delay=2.5, no_search=False): ############################################################ @app.command(help='Add new movies to Radarr.') -@click.option('--list-type', '-t', type=click.Choice(['anticipated', 'trending', 'popular']), +@click.option('--list-type', '-t', type=click.Choice(['anticipated', 'trending', 'popular', 'boxoffice']), help='Trakt list to process.', required=True) @click.option('--add-limit', '-l', default=0, help='Limit number of movies added to Radarr.', show_default=True) @click.option('--add-delay', '-d', default=2.5, help='Seconds between each add request to Radarr.', show_default=True) @@ -198,6 +198,8 @@ def movies(list_type, add_limit=0, add_delay=2.5, no_search=False): trakt_movies_list = trakt.get_trending_movies() elif list_type.lower() == 'popular': trakt_movies_list = trakt.get_popular_movies() + elif list_type.lower() == 'boxoffice': + trakt_movies_list = trakt.get_boxoffice_movies() else: log.error("Aborting due to unknown Trakt list type") return