Last active
August 29, 2015 14:07
-
-
Save nikolak/62f06ea93a0ca8f60688 to your computer and use it in GitHub Desktop.
Syncs all episodes from trakt.tv library to local sickrage database
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
import requests | |
import sqlite3 | |
import os | |
''' | |
Status ID's: | |
1 = not aired | |
3 = wanted | |
5 = skipped | |
6 = archived | |
104 = downloaded | |
''' | |
# What status to set for watched and unwatched episode | |
WATCHED_STATUS = 6 | |
UNWATCHED_STATUS = 5 | |
# API key available at: http://trakt.tv/api-docs/authentication | |
API_KEY = "25665d450ed0c00907f32be617cd2a37" | |
TRAKT_USER = "nikolak" # trakt username | |
TRAKT_URL = "http://api.trakt.tv/user/progress/watched.json/{key}/{user}".format( | |
key=API_KEY, | |
user=TRAKT_USER) | |
# list of shows that will be ignored when adding | |
IGNORED = ["Archer (1975)"] | |
# absolute or relative location of sickrage database file | |
DATABASE = "sickrage/sickbeard.db" | |
conn = sqlite3.connect(DATABASE) | |
c = conn.cursor() | |
def get_episodes(): | |
r = requests.get(TRAKT_URL) | |
if r.status_code != 200: | |
print "Error getting data from trakt: HTTP status:", r.status_code | |
return [] | |
else: | |
return r.json() | |
def update_status(title, tvdb_id, season_num, episode_num, watched): | |
ep_query = c.execute('SELECT * FROM tv_episodes WHERE showid=? AND season=? AND episode=?', | |
(tvdb_id, season_num, episode_num)).fetchone() | |
try: | |
ep_status = ep_query[11] | |
ep_id = ep_query[0] | |
except IndexError: | |
print "Index error for: ", title, tvdb_id, season_num, episode_num | |
return | |
new_status = WATCHED_STATUS if watched else UNWATCHED_STATUS | |
if new_status == ep_status: | |
return | |
print "Updating {} ({}) | S{}E{} (ID: {}) to {} (Status: {})".format(title, | |
tvdb_id, season_num, episode_num, ep_id, watched, new_status) | |
c.execute( | |
"UPDATE tv_episodes SET status=? WHERE episode_id=?", (new_status, ep_id)) | |
if __name__ == '__main__': | |
all_shows = get_episodes() | |
for show in all_shows: | |
query = c.execute( | |
'SELECT * FROM tv_episodes WHERE showid=?', (show['show']['tvdb_id'],)).fetchone() | |
if show['show']['title'] in IGNORED or not query: | |
continue | |
for season in show['seasons']: | |
for episode, seen in season['episodes'].items(): | |
update_status(show['show']['title'], | |
show['show']['tvdb_id'], season['season'], episode, seen) | |
if all_shows: | |
conn.commit() | |
conn.close() | |
print "Changes saved to database" | |
else: | |
print "No changes made to database" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment