Last active
January 3, 2017 04:22
-
-
Save srob650/4b339abcdb926df1ebecef51e9263153 to your computer and use it in GitHub Desktop.
Iterate over Plex TV library and mark episodes "watched" or "acquired" on www.tvmaze.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from plexapi.server import PlexServer | |
import pytvmaze | |
tvm = pytvmaze.TVMaze('YOUR_TVMAZE_USERNAME', 'YOUR_TVMAZE_API_KEY') | |
plex_token = 'YOUR_PLEX_TOKEN' | |
baseurl = 'http://localhost:32400' | |
plex = PlexServer(baseurl, plex_token) | |
# CHANGE THIS TO THE NAME OF YOUR TV LIBRARY | |
tv = plex.library.section('TV Shows') | |
shows = tv.all() | |
def match_show_by_premier(show_list, earliest_episode): | |
if not int(earliest_episode.season_number) == 1 and int(earliest_episode.index) == 1: | |
return None | |
for show in show_list: | |
if show.premiered and earliest_episode.originallyAvailableAt: | |
if show.premiered == earliest_episode.originallyAvailableAt.strftime('%Y-%m-%d'): | |
s = tvm.get_show(maze_id=show.maze_id, embed='episodes') | |
return s | |
def match_show_by_earliest_episode(show_list, earliest_episode): | |
for show in show_list: | |
if earliest_episode.originallyAvailableAt: | |
s = tvm.get_show(maze_id=show.maze_id, embed='episodes') | |
earliest_episode_airdate = earliest_episode.originallyAvailableAt.strftime('%Y-%m-%d') | |
try: | |
show_airdate = s[int(earliest_episode.season_number)][int(earliest_episode.index)].airdate | |
except (pytvmaze.SeasonNotFound, pytvmaze.EpisodeNotFound): | |
return None | |
if earliest_episode_airdate == show_airdate: | |
return s | |
# Get earliest non-special episode. | |
# Helps confirm we have the correct show when dealing with shows that have similar names | |
def get_earliest_episode(show): | |
for season in show.seasons(): | |
season_num = season.index | |
if int(season_num) == 0: | |
continue | |
for episode in season.episodes(): | |
episode_num = int(episode.index) | |
if episode_num == 0: | |
continue | |
episode.season_number = season_num | |
return episode | |
# Mark all non-special episodes | |
def mark_episodes(show, matched_show): | |
print(show.title) | |
for season in show.seasons(): | |
season_num = int(season.index) | |
if season_num == 0: | |
continue | |
for episode in season.episodes(): | |
episode_num = int(episode.index) | |
if episode_num == 0: | |
continue | |
episode_id = matched_show[season_num][episode_num].maze_id | |
if episode.isWatched: | |
print(' Marking episode {} as watched.'.format(episode_id)) | |
tvm.mark_episode(episode_id, 'watched') | |
else: | |
print(' Marking episode {} as acquired.'.format(episode_id)) | |
tvm.mark_episode(episode_id, 'acquired') | |
def main(): | |
for show in shows: | |
earliest_episode = get_earliest_episode(show) | |
show_list = pytvmaze.get_show_list(show.title) | |
matched_show = match_show_by_premier(show_list, earliest_episode) | |
if not matched_show: | |
matched_show = match_show_by_earliest_episode(show_list, earliest_episode) | |
if not matched_show: | |
continue | |
mark_episodes(show, matched_show) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment