2014-03-10 01:18:05 -04:00
|
|
|
# Author: Frank Fenton
|
|
|
|
# URL: http://code.google.com/p/sickbeard/
|
|
|
|
#
|
2014-05-23 08:37:22 -04:00
|
|
|
# This file is part of SickRage.
|
2014-03-10 01:18:05 -04:00
|
|
|
#
|
2014-05-23 08:37:22 -04:00
|
|
|
# SickRage is free software: you can redistribute it and/or modify
|
2014-03-10 01:18:05 -04:00
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
2014-05-23 08:37:22 -04:00
|
|
|
# SickRage is distributed in the hope that it will be useful,
|
2014-03-10 01:18:05 -04:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2014-05-23 08:37:22 -04:00
|
|
|
# along with SickRage. If not, see <http://www.gnu.org/licenses/>.
|
2014-07-27 21:01:26 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
import os
|
2014-07-17 21:06:42 -04:00
|
|
|
import traceback
|
2014-09-24 08:22:56 -04:00
|
|
|
import datetime
|
2015-01-25 15:14:57 -05:00
|
|
|
import json
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
import sickbeard
|
|
|
|
from sickbeard import encodingKludge as ek
|
2014-12-06 01:51:24 -05:00
|
|
|
from sickbeard.exceptions import ex
|
2014-03-10 01:18:05 -04:00
|
|
|
from sickbeard import logger
|
|
|
|
from sickbeard import helpers
|
|
|
|
from sickbeard import search_queue
|
2014-07-27 21:01:26 -04:00
|
|
|
from sickbeard.common import SKIPPED, WANTED
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-11-28 17:13:06 -05:00
|
|
|
from lib.trakt import *
|
|
|
|
from trakt.exceptions import traktException, traktAuthException, traktServerBusy
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-11-28 17:07:26 -05:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
class TraktChecker():
|
2014-11-28 17:07:26 -05:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def __init__(self):
|
|
|
|
self.todoWanted = []
|
2015-01-27 14:26:53 -05:00
|
|
|
self.trakt_api = TraktAPI(sickbeard.TRAKT_API_KEY, sickbeard.TRAKT_USERNAME, sickbeard.TRAKT_PASSWORD, sickbeard.TRAKT_DISABLE_SSL_VERIFY)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-05-19 13:40:25 -04:00
|
|
|
def run(self, force=False):
|
2014-07-17 21:06:42 -04:00
|
|
|
try:
|
|
|
|
# add shows from trakt.tv watchlist
|
|
|
|
if sickbeard.TRAKT_USE_WATCHLIST:
|
|
|
|
self.todoWanted = [] # its about to all get re-added
|
|
|
|
if len(sickbeard.ROOT_DIRS.split('|')) < 2:
|
|
|
|
logger.log(u"No default root directory", logger.ERROR)
|
|
|
|
return
|
|
|
|
self.updateShows()
|
|
|
|
self.updateEpisodes()
|
|
|
|
|
|
|
|
# sync trakt.tv library with sickrage library
|
|
|
|
if sickbeard.TRAKT_SYNC:
|
|
|
|
self.syncLibrary()
|
|
|
|
except Exception:
|
|
|
|
logger.log(traceback.format_exc(), logger.DEBUG)
|
2014-06-28 23:28:00 -04:00
|
|
|
|
2014-07-24 00:44:11 -04:00
|
|
|
def findShow(self, indexer, indexerid):
|
2014-11-28 17:07:26 -05:00
|
|
|
traktShow = None
|
2014-06-28 23:28:00 -04:00
|
|
|
|
2014-11-28 17:07:26 -05:00
|
|
|
try:
|
2015-01-25 15:14:57 -05:00
|
|
|
library = self.trakt_api.traktRequest("sync/collection/shows") or []
|
2014-07-24 00:44:11 -04:00
|
|
|
|
2014-11-28 17:07:26 -05:00
|
|
|
if not library:
|
|
|
|
logger.log(u"Could not connect to trakt service, aborting library check", logger.ERROR)
|
|
|
|
return
|
|
|
|
|
|
|
|
if not len(library):
|
|
|
|
logger.log(u"No shows found in your library, aborting library update", logger.DEBUG)
|
|
|
|
return
|
|
|
|
|
2015-01-25 19:14:17 -05:00
|
|
|
traktShow = filter(lambda x: int(indexerid) in [int(x['show']['ids']['tvdb'] or 0), int(x['show']['ids']['tvrage'] or 0)], library)
|
2014-11-28 17:07:26 -05:00
|
|
|
except (traktException, traktAuthException, traktServerBusy) as e:
|
2014-12-06 01:51:24 -05:00
|
|
|
logger.log(u"Could not connect to Trakt service: %s" % ex(e), logger.WARNING)
|
2014-11-20 07:47:36 -05:00
|
|
|
|
2014-11-28 17:07:26 -05:00
|
|
|
return traktShow
|
2014-06-28 23:28:00 -04:00
|
|
|
|
|
|
|
def syncLibrary(self):
|
2014-07-30 19:33:28 -04:00
|
|
|
logger.log(u"Syncing Trakt.tv show library", logger.DEBUG)
|
|
|
|
|
|
|
|
for myShow in sickbeard.showList:
|
|
|
|
self.addShowToTraktLibrary(myShow)
|
2014-06-28 23:28:00 -04:00
|
|
|
|
|
|
|
def removeShowFromTraktLibrary(self, show_obj):
|
2014-07-24 00:44:11 -04:00
|
|
|
if self.findShow(show_obj.indexer, show_obj.indexerid):
|
2015-01-25 15:14:57 -05:00
|
|
|
trakt_id = sickbeard.indexerApi(show_obj.indexer).config['trakt_id']
|
2014-07-30 19:33:28 -04:00
|
|
|
|
2015-01-25 15:14:57 -05:00
|
|
|
# URL parameters
|
|
|
|
data = {
|
|
|
|
'shows': [
|
|
|
|
{
|
|
|
|
'title': show_obj.name,
|
|
|
|
'year': show_obj.startyear,
|
|
|
|
'ids': {}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
if trakt_id == 'tvdb_id':
|
|
|
|
data['shows'][0]['ids']['tvdb'] = show_obj.indexerid
|
|
|
|
else:
|
|
|
|
data['shows'][0]['ids']['tvrage'] = show_obj.indexerid
|
2014-11-28 17:07:26 -05:00
|
|
|
|
2014-12-06 01:51:24 -05:00
|
|
|
logger.log(u"Removing " + show_obj.name + " from trakt.tv library", logger.DEBUG)
|
2014-11-28 17:07:26 -05:00
|
|
|
try:
|
2015-01-25 15:14:57 -05:00
|
|
|
self.trakt_api.traktRequest("sync/collection/remove", data, method='POST')
|
2014-11-28 17:07:26 -05:00
|
|
|
except (traktException, traktAuthException, traktServerBusy) as e:
|
2014-12-06 01:51:24 -05:00
|
|
|
logger.log(u"Could not connect to Trakt service: %s" % ex(e), logger.WARNING)
|
|
|
|
pass
|
2014-06-28 23:28:00 -04:00
|
|
|
|
|
|
|
def addShowToTraktLibrary(self, show_obj):
|
|
|
|
"""
|
|
|
|
Sends a request to trakt indicating that the given show and all its episodes is part of our library.
|
|
|
|
|
|
|
|
show_obj: The TVShow object to add to trakt
|
|
|
|
"""
|
|
|
|
|
2014-07-30 19:33:28 -04:00
|
|
|
data = {}
|
|
|
|
|
2014-07-24 00:44:11 -04:00
|
|
|
if not self.findShow(show_obj.indexer, show_obj.indexerid):
|
2015-01-25 15:14:57 -05:00
|
|
|
trakt_id = sickbeard.indexerApi(show_obj.indexer).config['trakt_id']
|
2014-07-24 00:44:11 -04:00
|
|
|
# URL parameters
|
2015-01-25 15:14:57 -05:00
|
|
|
data = {
|
|
|
|
'shows': [
|
|
|
|
{
|
|
|
|
'title': show_obj.name,
|
|
|
|
'year': show_obj.startyear,
|
|
|
|
'ids': {}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
if trakt_id == 'tvdb_id':
|
|
|
|
data['shows'][0]['ids']['tvdb'] = show_obj.indexerid
|
|
|
|
else:
|
|
|
|
data['shows'][0]['ids']['tvrage'] = show_obj.indexerid
|
2014-07-30 19:33:28 -04:00
|
|
|
|
|
|
|
if len(data):
|
|
|
|
logger.log(u"Adding " + show_obj.name + " to trakt.tv library", logger.DEBUG)
|
2014-11-28 17:07:26 -05:00
|
|
|
|
|
|
|
try:
|
2015-01-25 15:14:57 -05:00
|
|
|
self.trakt_api.traktRequest("sync/collection", data, method='POST')
|
2014-11-28 17:07:26 -05:00
|
|
|
except (traktException, traktAuthException, traktServerBusy) as e:
|
2014-12-06 01:51:24 -05:00
|
|
|
logger.log(u"Could not connect to Trakt service: %s" % ex(e), logger.WARNING)
|
2014-12-03 01:13:28 -05:00
|
|
|
return
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
def updateShows(self):
|
|
|
|
logger.log(u"Starting trakt show watchlist check", logger.DEBUG)
|
2014-07-27 10:39:33 -04:00
|
|
|
|
2014-11-28 17:07:26 -05:00
|
|
|
try:
|
2015-01-25 15:14:57 -05:00
|
|
|
watchlist = self.trakt_api.traktRequest("sync/watchlist/shows")
|
2014-11-28 17:07:26 -05:00
|
|
|
except (traktException, traktAuthException, traktServerBusy) as e:
|
2014-12-06 01:51:24 -05:00
|
|
|
logger.log(u"Could not connect to Trakt service: %s" % ex(e), logger.WARNING)
|
2014-03-10 01:18:05 -04:00
|
|
|
return
|
2014-07-17 21:06:42 -04:00
|
|
|
|
2014-11-20 07:47:36 -05:00
|
|
|
if not len(watchlist):
|
|
|
|
logger.log(u"No shows found in your watchlist, aborting watchlist update", logger.DEBUG)
|
|
|
|
return
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
for show in watchlist:
|
2014-07-24 00:44:11 -04:00
|
|
|
indexer = int(sickbeard.TRAKT_DEFAULT_INDEXER)
|
|
|
|
if indexer == 2:
|
2015-01-25 15:14:57 -05:00
|
|
|
indexer_id = int(show["show"]["ids"]["tvrage"])
|
2014-07-24 00:44:11 -04:00
|
|
|
else:
|
2015-01-25 15:14:57 -05:00
|
|
|
indexer_id = int(show["show"]["ids"]["tvdb"])
|
2014-07-24 00:44:11 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
if int(sickbeard.TRAKT_METHOD_ADD) != 2:
|
2015-01-25 15:14:57 -05:00
|
|
|
self.addDefaultShow(indexer, indexer_id, show["show"]["title"], SKIPPED)
|
2014-03-10 01:18:05 -04:00
|
|
|
else:
|
2015-01-25 15:14:57 -05:00
|
|
|
self.addDefaultShow(indexer, indexer_id, show["show"]["title"], WANTED)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
if int(sickbeard.TRAKT_METHOD_ADD) == 1:
|
2014-07-24 00:44:11 -04:00
|
|
|
newShow = helpers.findCertainShow(sickbeard.showList, indexer_id)
|
2014-03-10 01:18:05 -04:00
|
|
|
if newShow is not None:
|
|
|
|
self.setEpisodeToWanted(newShow, 1, 1)
|
|
|
|
else:
|
2014-07-24 00:44:11 -04:00
|
|
|
self.todoWanted.append((indexer_id, 1, 1))
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
def updateEpisodes(self):
|
|
|
|
"""
|
|
|
|
Sets episodes to wanted that are in trakt watchlist
|
|
|
|
"""
|
|
|
|
logger.log(u"Starting trakt episode watchlist check", logger.DEBUG)
|
2014-07-27 10:39:33 -04:00
|
|
|
|
2014-11-28 17:07:26 -05:00
|
|
|
try:
|
2015-01-25 15:14:57 -05:00
|
|
|
watchlist = self.trakt_api.traktRequest("sync/watchlist/episodes")
|
2014-11-28 17:07:26 -05:00
|
|
|
except (traktException, traktAuthException, traktServerBusy) as e:
|
2014-12-06 01:51:24 -05:00
|
|
|
logger.log(u"Could not connect to Trakt service: %s" % ex(e), logger.WARNING)
|
2014-03-10 01:18:05 -04:00
|
|
|
return
|
2014-07-17 21:06:42 -04:00
|
|
|
|
2014-11-20 07:47:36 -05:00
|
|
|
if not len(watchlist):
|
|
|
|
logger.log(u"No shows found in your watchlist, aborting watchlist update", logger.DEBUG)
|
|
|
|
return
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
for show in watchlist:
|
2014-07-24 00:44:11 -04:00
|
|
|
indexer = int(sickbeard.TRAKT_DEFAULT_INDEXER)
|
|
|
|
if indexer == 2:
|
2015-01-25 15:14:57 -05:00
|
|
|
indexer_id = int(show["show"]["ids"]["tvrage"])
|
2014-07-24 00:44:11 -04:00
|
|
|
else:
|
2015-01-25 15:14:57 -05:00
|
|
|
indexer_id = int(show["show"]["ids"]["tvdb"])
|
2014-07-24 00:44:11 -04:00
|
|
|
|
2015-01-25 15:14:57 -05:00
|
|
|
self.addDefaultShow(indexer, indexer_id, show["show"]["title"], SKIPPED)
|
2014-07-24 00:44:11 -04:00
|
|
|
newShow = helpers.findCertainShow(sickbeard.showList, indexer_id)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-08-01 13:26:36 -04:00
|
|
|
try:
|
2014-09-24 08:22:56 -04:00
|
|
|
if newShow and newShow.indexer == indexer:
|
2015-01-25 15:14:57 -05:00
|
|
|
for episode in show["episode"]:
|
2014-08-01 13:26:36 -04:00
|
|
|
if newShow is not None:
|
|
|
|
self.setEpisodeToWanted(newShow, episode["season"], episode["number"])
|
|
|
|
else:
|
|
|
|
self.todoWanted.append((indexer_id, episode["season"], episode["number"]))
|
|
|
|
except TypeError:
|
2015-01-25 15:14:57 -05:00
|
|
|
logger.log(u"Could not parse the output from trakt for " + show["show"]["title"], logger.DEBUG)
|
2014-07-24 00:44:11 -04:00
|
|
|
|
|
|
|
def addDefaultShow(self, indexer, indexer_id, name, status):
|
2014-03-10 01:18:05 -04:00
|
|
|
"""
|
|
|
|
Adds a new show with the default settings
|
|
|
|
"""
|
2014-07-24 00:44:11 -04:00
|
|
|
if not helpers.findCertainShow(sickbeard.showList, int(indexer_id)):
|
|
|
|
logger.log(u"Adding show " + str(indexer_id))
|
|
|
|
root_dirs = sickbeard.ROOT_DIRS.split('|')
|
|
|
|
|
|
|
|
try:
|
|
|
|
location = root_dirs[int(root_dirs[0]) + 1]
|
|
|
|
except:
|
|
|
|
location = None
|
|
|
|
|
|
|
|
if location:
|
|
|
|
showPath = ek.ek(os.path.join, location, helpers.sanitizeFileName(name))
|
|
|
|
dir_exists = helpers.makeDir(showPath)
|
|
|
|
if not dir_exists:
|
|
|
|
logger.log(u"Unable to create the folder " + showPath + ", can't add the show", logger.ERROR)
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
helpers.chmodAsParent(showPath)
|
2014-07-10 20:20:31 -04:00
|
|
|
|
2014-07-24 00:44:11 -04:00
|
|
|
sickbeard.showQueueScheduler.action.addShow(int(indexer), int(indexer_id), showPath, status,
|
|
|
|
int(sickbeard.QUALITY_DEFAULT),
|
2014-09-24 08:22:56 -04:00
|
|
|
int(sickbeard.FLATTEN_FOLDERS_DEFAULT),
|
|
|
|
paused=sickbeard.TRAKT_START_PAUSED)
|
2014-07-09 14:41:04 -04:00
|
|
|
else:
|
2014-07-24 00:44:11 -04:00
|
|
|
logger.log(u"There was an error creating the show, no root directory setting found", logger.ERROR)
|
|
|
|
return
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
def setEpisodeToWanted(self, show, s, e):
|
|
|
|
"""
|
|
|
|
Sets an episode to wanted, only is it is currently skipped
|
|
|
|
"""
|
|
|
|
epObj = show.getEpisode(int(s), int(e))
|
2014-07-24 00:44:11 -04:00
|
|
|
if epObj:
|
|
|
|
|
|
|
|
with epObj.lock:
|
2014-09-24 08:22:56 -04:00
|
|
|
if epObj.status != SKIPPED or epObj.airdate == datetime.date.fromordinal(1):
|
2014-07-24 00:44:11 -04:00
|
|
|
return
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-07-24 00:44:11 -04:00
|
|
|
logger.log(u"Setting episode s" + str(s) + "e" + str(e) + " of show " + show.name + " to wanted")
|
|
|
|
# figure out what segment the episode is in and remember it so we can backlog it
|
2014-05-30 06:01:49 -04:00
|
|
|
|
2014-07-24 00:44:11 -04:00
|
|
|
epObj.status = WANTED
|
|
|
|
epObj.saveToDB()
|
|
|
|
|
2014-09-24 08:22:56 -04:00
|
|
|
cur_backlog_queue_item = search_queue.BacklogQueueItem(show, [epObj])
|
|
|
|
sickbeard.searchQueueScheduler.action.add_item(cur_backlog_queue_item)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-09-24 08:22:56 -04:00
|
|
|
logger.log(u"Starting backlog for " + show.name + " season " + str(
|
|
|
|
s) + " episode " + str(e) + " because some eps were set to wanted")
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
def manageNewShow(self, show):
|
2014-09-24 08:22:56 -04:00
|
|
|
logger.log(u"Checking if trakt watch list wants to search for episodes from new show " + show.name, logger.DEBUG)
|
2014-03-10 01:18:05 -04:00
|
|
|
episodes = [i for i in self.todoWanted if i[0] == show.indexerid]
|
|
|
|
for episode in episodes:
|
|
|
|
self.todoWanted.remove(episode)
|
2014-09-24 08:22:56 -04:00
|
|
|
self.setEpisodeToWanted(show, episode[1], episode[2])
|