Skip to content

Commit

Permalink
Merge pull request #35 from l3uddz/feat/watched-played
Browse files Browse the repository at this point in the history
Feat/watched played
  • Loading branch information
l3uddz authored Jun 3, 2018
2 parents eb86ef9 + a8e4f5f commit 1db4f18
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 10 deletions.
5 changes: 5 additions & 0 deletions helpers/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,8 @@ def sorted_list(original_list, list_type, sort_key, reverse=True):
item[list_type][sort_key] = 0

return sorted(prepared_list, key=lambda k: k[list_type][sort_key], reverse=reverse)


# reference: https://stackoverflow.com/a/16712886
def substring_after(s, delim):
return s.partition(delim)[2]
42 changes: 41 additions & 1 deletion media/trakt.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


class Trakt:
non_user_lists = ['anticipated', 'trending', 'popular', 'boxoffice']
non_user_lists = ['anticipated', 'trending', 'popular', 'boxoffice', 'watched', 'played']

def __init__(self, cfg):
self.cfg = cfg
Expand Down Expand Up @@ -343,6 +343,26 @@ def get_anticipated_shows(self, limit=1000, languages=None, genres=None):
genres=genres
)

def get_most_played_shows(self, limit=1000, languages=None, genres=None, most_type=None):
return self._make_items_request(
url='https://api.trakt.tv/shows/played/%s' % ('weekly' if not most_type else most_type),
limit=limit,
languages=languages,
object_name='shows',
type_name='played',
genres=genres
)

def get_most_watched_shows(self, limit=1000, languages=None, genres=None, most_type=None):
return self._make_items_request(
url='https://api.trakt.tv/shows/watched/%s' % ('weekly' if not most_type else most_type),
limit=limit,
languages=languages,
object_name='shows',
type_name='watched',
genres=genres
)

def get_watchlist_shows(self, authenticate_user=None, limit=1000, languages=None):
return self._make_items_request(
url='https://api.trakt.tv/users/{authenticate_user}/watchlist/shows',
Expand Down Expand Up @@ -407,6 +427,26 @@ def get_anticipated_movies(self, limit=1000, languages=None, genres=None):
genres=genres
)

def get_most_played_movies(self, limit=1000, languages=None, genres=None, most_type=None):
return self._make_items_request(
url='https://api.trakt.tv/movies/played/%s' % ('weekly' if not most_type else most_type),
limit=limit,
languages=languages,
object_name='movies',
type_name='played',
genres=genres
)

def get_most_watched_movies(self, limit=1000, languages=None, genres=None, most_type=None):
return self._make_items_request(
url='https://api.trakt.tv/movies/watched/%s' % ('weekly' if not most_type else most_type),
limit=limit,
languages=languages,
object_name='movies',
type_name='watched',
genres=genres
)

def get_boxoffice_movies(self, limit=1000, languages=None):
return self._make_items_request(
url='https://api.trakt.tv/movies/boxoffice',
Expand Down
40 changes: 31 additions & 9 deletions traktarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ def get_objects(pvr, type, notifications):
if notifications:
callback_notify({'event': 'error', 'reason': 'Failure to retrieve %s shows list' % type})
exit()
log.info("Retrieved %s shows list, shows found: %d", type, len(objects_list))
objects_type = 'movies' if type.lower() == 'radarr' else 'shows'
log.info("Retrieved %s %s list, %s found: %d", type, objects_type, objects_type, len(objects_list))
return objects_list


Expand Down Expand Up @@ -171,8 +172,8 @@ def show(show_id, folder=None, no_search=False):

@app.command(help='Add multiple shows to Sonarr.')
@click.option('--list-type', '-t',
help='Trakt list to process. For example, anticipated, trending, popular, watchlist or any URL to a list',
required=True)
help='Trakt list to process. For example, anticipated, trending, popular, watched, played, watchlist '
'or any URL to a list', required=True)
@click.option('--add-limit', '-l', default=0, help='Limit number of shows added to Sonarr.', show_default=True)
@click.option('--add-delay', '-d', default=2.5, help='Seconds between each add request to Sonarr.', show_default=True)
@click.option('--sort', '-s', default='votes', type=click.Choice(['votes', 'rating', 'release']),
Expand Down Expand Up @@ -221,6 +222,14 @@ def shows(list_type, add_limit=0, add_delay=2.5, sort='votes', genre=None, folde
trakt_objects_list = trakt.get_trending_shows(genres=genre, languages=cfg.filters.shows.allowed_languages)
elif list_type.lower() == 'popular':
trakt_objects_list = trakt.get_popular_shows(genres=genre, languages=cfg.filters.shows.allowed_languages)
elif list_type.lower().startswith('played'):
most_type = misc_helper.substring_after(list_type.lower(), "_")
trakt_objects_list = trakt.get_most_played_shows(genres=genre, languages=cfg.filters.shows.allowed_languages,
most_type=most_type if most_type else None)
elif list_type.lower().startswith('watched'):
most_type = misc_helper.substring_after(list_type.lower(), "_")
trakt_objects_list = trakt.get_most_watched_shows(genres=genre, languages=cfg.filters.shows.allowed_languages,
most_type=most_type if most_type else None)
elif list_type.lower() == 'watchlist':
trakt_objects_list = trakt.get_watchlist_shows(authenticate_user)
else:
Expand Down Expand Up @@ -357,8 +366,8 @@ def movie(movie_id, folder=None, no_search=False):

@app.command(help='Add multiple movies to Radarr.')
@click.option('--list-type', '-t',
help='Trakt list to process. For example, anticipated, trending, popular, boxoffice, watchlist '
'or any URL to a list',
help='Trakt list to process. For example, anticipated, trending, popular, boxoffice, watched, played, '
'watchlist or any URL to a list',
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)
Expand Down Expand Up @@ -409,6 +418,14 @@ def movies(list_type, add_limit=0, add_delay=2.5, sort='votes', genre=None, fold
trakt_objects_list = trakt.get_popular_movies(genres=genre, languages=cfg.filters.movies.allowed_languages)
elif list_type.lower() == 'boxoffice':
trakt_objects_list = trakt.get_boxoffice_movies()
elif list_type.lower().startswith('played'):
most_type = misc_helper.substring_after(list_type.lower(), "_")
trakt_objects_list = trakt.get_most_played_movies(genres=genre, languages=cfg.filters.movies.allowed_languages,
most_type=most_type if most_type else None)
elif list_type.lower().startswith('watched'):
most_type = misc_helper.substring_after(list_type.lower(), "_")
trakt_objects_list = trakt.get_most_watched_movies(genres=genre, languages=cfg.filters.movies.allowed_languages,
most_type=most_type if most_type else None)
elif list_type.lower() == 'watchlist':
trakt_objects_list = trakt.get_watchlist_movies(authenticate_user)
else:
Expand Down Expand Up @@ -532,7 +549,8 @@ def automatic_shows(add_delay=2.5, sort='votes', no_search=False, notifications=
if list_type.lower() == 'interval':
continue

if list_type.lower() in Trakt.non_user_lists:
if list_type.lower() in Trakt.non_user_lists or (
'_' in list_type and list_type.lower().partition("_")[0] in Trakt.non_user_lists):
limit = value

if limit <= 0:
Expand Down Expand Up @@ -620,7 +638,8 @@ def automatic_movies(add_delay=2.5, sort='votes', no_search=False, notifications
if list_type.lower() == 'interval':
continue

if list_type.lower() in Trakt.non_user_lists:
if list_type.lower() in Trakt.non_user_lists or (
'_' in list_type and list_type.lower().partition("_")[0] in Trakt.non_user_lists):
limit = value

if limit <= 0:
Expand Down Expand Up @@ -720,8 +739,8 @@ def run(add_delay=2.5, sort='votes', no_search=False, run_now=False, no_notifica
if run_now:
movie_schedule.run()

# Sleep between tasks
time.sleep(add_delay)
# Sleep between tasks
time.sleep(add_delay)

if cfg.automatic.shows.interval:
shows_schedule = schedule.every(cfg.automatic.shows.interval).hours.do(
Expand All @@ -735,6 +754,9 @@ def run(add_delay=2.5, sort='votes', no_search=False, run_now=False, no_notifica
if run_now:
shows_schedule.run()

# Sleep between tasks
time.sleep(add_delay)

# Enter running schedule
while True:
try:
Expand Down

0 comments on commit 1db4f18

Please sign in to comment.