2014-04-22 04:02:43 -04:00
|
|
|
# Author: Nic Wolfe <nic@wolfeden.ca>
|
|
|
|
# URL: http://code.google.com/p/sickbeard/
|
2014-03-10 01:18:05 -04:00
|
|
|
#
|
|
|
|
# This file is part of Sick Beard.
|
|
|
|
#
|
|
|
|
# Sick Beard is free software: you can redistribute it and/or modify
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# Sick Beard is distributed in the hope that it will be useful,
|
|
|
|
# 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
|
|
|
|
# along with Sick Beard. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
# Created on Sep 20, 2012
|
|
|
|
# @author: Dermot Buckley <dermot@buckley.ie>
|
|
|
|
# @copyright: Dermot Buckley
|
|
|
|
#
|
|
|
|
|
|
|
|
import time
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
try:
|
|
|
|
import json
|
|
|
|
except ImportError:
|
|
|
|
from lib import simplejson as json
|
|
|
|
|
2014-03-20 01:33:34 -04:00
|
|
|
import sickbeard
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
from sickbeard import logger
|
|
|
|
from sickbeard import db
|
|
|
|
from sickbeard.exceptions import ex
|
|
|
|
from lib import requests
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
MAX_XEM_AGE_SECS = 86400 # 1 day
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-05-03 05:23:26 -04:00
|
|
|
def get_scene_numbering(indexer_id, indexer, season, episode, fallback_to_xem=True):
|
2014-03-10 01:18:05 -04:00
|
|
|
"""
|
|
|
|
Returns a tuple, (season, episode), with the scene numbering (if there is one),
|
|
|
|
otherwise returns the xem numbering (if fallback_to_xem is set), otherwise
|
2014-03-10 22:32:02 -04:00
|
|
|
returns the TVDB and TVRAGE numbering.
|
2014-03-10 01:18:05 -04:00
|
|
|
(so the return values will always be set)
|
|
|
|
|
|
|
|
@param indexer_id: int
|
|
|
|
@param season: int
|
|
|
|
@param episode: int
|
|
|
|
@param fallback_to_xem: bool If set (the default), check xem for matches if there is no local scene numbering
|
|
|
|
@return: (int, int) a tuple with (season, episode)
|
|
|
|
"""
|
|
|
|
if indexer_id is None or season is None or episode is None:
|
|
|
|
return (season, episode)
|
2014-03-20 01:33:34 -04:00
|
|
|
|
2014-05-03 05:58:04 -04:00
|
|
|
indexer_id = int(indexer_id)
|
|
|
|
indexer = int(indexer)
|
|
|
|
|
2014-05-03 05:23:26 -04:00
|
|
|
result = find_scene_numbering(indexer_id, indexer, season, episode)
|
2014-03-10 01:18:05 -04:00
|
|
|
if result:
|
|
|
|
return result
|
|
|
|
else:
|
|
|
|
if fallback_to_xem:
|
2014-05-03 05:23:26 -04:00
|
|
|
xem_result = find_xem_numbering(indexer_id, indexer, season, episode)
|
2014-03-10 01:18:05 -04:00
|
|
|
if xem_result:
|
|
|
|
return xem_result
|
|
|
|
return (season, episode)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
|
|
|
|
2014-05-03 05:23:26 -04:00
|
|
|
def find_scene_numbering(indexer_id, indexer, season, episode):
|
2014-03-10 01:18:05 -04:00
|
|
|
"""
|
|
|
|
Same as get_scene_numbering(), but returns None if scene numbering is not set
|
|
|
|
"""
|
|
|
|
if indexer_id is None or season is None or episode is None:
|
|
|
|
return (season, episode)
|
2014-03-20 01:33:34 -04:00
|
|
|
|
2014-05-03 05:58:04 -04:00
|
|
|
indexer_id = int(indexer_id)
|
|
|
|
indexer = int(indexer)
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
myDB = db.DBConnection()
|
2014-03-20 01:33:34 -04:00
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
rows = myDB.select(
|
|
|
|
"SELECT scene_season, scene_episode FROM scene_numbering WHERE indexer = ? and indexer_id = ? and season = ? and episode = ?",
|
|
|
|
[indexer, indexer_id, season, episode])
|
2014-03-10 01:18:05 -04:00
|
|
|
if rows:
|
|
|
|
return (int(rows[0]["scene_season"]), int(rows[0]["scene_episode"]))
|
2014-03-21 04:34:00 -04:00
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-05-03 05:23:26 -04:00
|
|
|
def get_indexer_numbering(indexer_id, indexer, sceneSeason, sceneEpisode, fallback_to_xem=True):
|
2014-03-10 01:18:05 -04:00
|
|
|
"""
|
2014-03-10 22:32:02 -04:00
|
|
|
Returns a tuple, (season, episode) with the TVDB and TVRAGE numbering for (sceneSeason, sceneEpisode)
|
2014-03-10 01:18:05 -04:00
|
|
|
(this works like the reverse of get_scene_numbering)
|
|
|
|
"""
|
|
|
|
if indexer_id is None or sceneSeason is None or sceneEpisode is None:
|
|
|
|
return (sceneSeason, sceneEpisode)
|
2014-03-20 01:33:34 -04:00
|
|
|
|
2014-05-03 05:58:04 -04:00
|
|
|
indexer_id = int(indexer_id)
|
|
|
|
indexer = int(indexer)
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
myDB = db.DBConnection()
|
2014-03-25 01:57:24 -04:00
|
|
|
|
|
|
|
rows = myDB.select(
|
|
|
|
"SELECT season, episode FROM scene_numbering WHERE indexer = ? and indexer_id = ? and scene_season = ? and scene_episode = ?",
|
|
|
|
[indexer, indexer_id, sceneSeason, sceneEpisode])
|
2014-03-10 01:18:05 -04:00
|
|
|
if rows:
|
|
|
|
return (int(rows[0]["season"]), int(rows[0]["episode"]))
|
|
|
|
else:
|
|
|
|
if fallback_to_xem:
|
2014-05-03 05:23:26 -04:00
|
|
|
return get_indexer_numbering_for_xem(indexer_id, indexer, sceneSeason, sceneEpisode)
|
2014-03-10 01:18:05 -04:00
|
|
|
return (sceneSeason, sceneEpisode)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
|
|
|
|
2014-05-03 05:23:26 -04:00
|
|
|
def get_scene_numbering_for_show(indexer_id, indexer):
|
2014-03-10 01:18:05 -04:00
|
|
|
"""
|
|
|
|
Returns a dict of (season, episode) : (sceneSeason, sceneEpisode) mappings
|
|
|
|
for an entire show. Both the keys and values of the dict are tuples.
|
|
|
|
Will be empty if there are no scene numbers set
|
|
|
|
"""
|
|
|
|
if indexer_id is None:
|
|
|
|
return {}
|
2014-03-20 01:33:34 -04:00
|
|
|
|
2014-05-03 05:58:04 -04:00
|
|
|
indexer_id = int(indexer_id)
|
|
|
|
indexer = int(indexer)
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
myDB = db.DBConnection()
|
2014-03-25 01:57:24 -04:00
|
|
|
|
|
|
|
rows = myDB.select(
|
|
|
|
'SELECT season, episode, scene_season, scene_episode FROM scene_numbering WHERE indexer = ? and indexer_id = ? ORDER BY season, episode',
|
|
|
|
[indexer, indexer_id])
|
2014-03-10 01:18:05 -04:00
|
|
|
result = {}
|
|
|
|
for row in rows:
|
|
|
|
result[(int(row['season']), int(row['episode']))] = (int(row['scene_season']), int(row['scene_episode']))
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
return result
|
2014-03-25 01:57:24 -04:00
|
|
|
|
|
|
|
|
2014-05-03 05:23:26 -04:00
|
|
|
def set_scene_numbering(indexer_id, indexer, season, episode, sceneSeason=None, sceneEpisode=None):
|
2014-03-10 01:18:05 -04:00
|
|
|
"""
|
|
|
|
Set scene numbering for a season/episode.
|
|
|
|
To clear the scene numbering, leave both sceneSeason and sceneEpisode as None.
|
|
|
|
|
|
|
|
"""
|
|
|
|
if indexer_id is None or season is None or episode is None:
|
|
|
|
return
|
2014-03-20 01:33:34 -04:00
|
|
|
|
2014-05-03 05:58:04 -04:00
|
|
|
indexer_id = int(indexer_id)
|
|
|
|
indexer = int(indexer)
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
myDB = db.DBConnection()
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
# sanity
|
|
|
|
#if sceneSeason == None: sceneSeason = season
|
|
|
|
#if sceneEpisode == None: sceneEpisode = episode
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
# delete any existing record first
|
2014-03-25 01:57:24 -04:00
|
|
|
myDB.action('DELETE FROM scene_numbering where indexer = ? and indexer_id = ? and season = ? and episode = ?',
|
|
|
|
[indexer, indexer_id, season, episode])
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
# now, if the new numbering is not the default, we save a new record
|
|
|
|
if sceneSeason is not None and sceneEpisode is not None:
|
2014-03-25 01:57:24 -04:00
|
|
|
myDB.action(
|
|
|
|
"INSERT INTO scene_numbering (indexer, indexer_id, season, episode, scene_season, scene_episode) VALUES (?,?,?,?,?,?)",
|
|
|
|
[indexer, indexer_id, season, episode, sceneSeason, sceneEpisode])
|
|
|
|
|
|
|
|
|
2014-05-03 05:23:26 -04:00
|
|
|
def find_xem_numbering(indexer_id, indexer, season, episode):
|
2014-03-10 01:18:05 -04:00
|
|
|
"""
|
|
|
|
Returns the scene numbering, as retrieved from xem.
|
|
|
|
Refreshes/Loads as needed.
|
|
|
|
|
|
|
|
@param indexer_id: int
|
|
|
|
@param season: int
|
|
|
|
@param episode: int
|
|
|
|
@return: (int, int) a tuple of scene_season, scene_episode, or None if there is no special mapping.
|
|
|
|
"""
|
|
|
|
if indexer_id is None or season is None or episode is None:
|
|
|
|
return None
|
2014-03-20 01:33:34 -04:00
|
|
|
|
2014-05-03 05:58:04 -04:00
|
|
|
indexer_id = int(indexer_id)
|
|
|
|
indexer = int(indexer)
|
|
|
|
|
2014-05-03 05:23:26 -04:00
|
|
|
if _xem_refresh_needed(indexer_id, indexer):
|
|
|
|
_xem_refresh(indexer_id, indexer)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
cacheDB = db.DBConnection('cache.db')
|
2014-05-11 18:24:02 -04:00
|
|
|
myDB = db.DBConnection()
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
rows = cacheDB.select(
|
|
|
|
"SELECT scene_season, scene_episode FROM xem_numbering WHERE indexer = ? and indexer_id = ? and season = ? and episode = ?",
|
|
|
|
[indexer, indexer_id, season, episode])
|
2014-05-11 18:24:02 -04:00
|
|
|
|
|
|
|
scene_seasons = cacheDB.select(
|
|
|
|
"SELECT COUNT(DISTINCT scene_season) as count FROM xem_numbering WHERE indexer = ? and indexer_id = ?",
|
|
|
|
[indexer, indexer_id])
|
|
|
|
|
|
|
|
indexer_seasons = myDB.select(
|
|
|
|
"SELECT COUNT(DISTINCT season) as count FROM tv_episodes WHERE indexer = ? and showid = ?",
|
|
|
|
[indexer, indexer_id])
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
if rows:
|
|
|
|
return (int(rows[0]["scene_season"]), int(rows[0]["scene_episode"]))
|
2014-05-11 18:24:02 -04:00
|
|
|
elif int(scene_seasons[0]["count"]) > 0 and int(indexer_seasons[0]["count"]) > int(scene_seasons[0]["count"]):
|
|
|
|
return (0,0)
|
2014-03-10 01:18:05 -04:00
|
|
|
else:
|
|
|
|
return None
|
2014-03-25 01:57:24 -04:00
|
|
|
|
|
|
|
|
2014-05-03 05:23:26 -04:00
|
|
|
def get_indexer_numbering_for_xem(indexer_id, indexer, sceneSeason, sceneEpisode):
|
2014-03-10 01:18:05 -04:00
|
|
|
"""
|
|
|
|
Reverse of find_xem_numbering: lookup a tvdb season and episode using scene numbering
|
|
|
|
|
|
|
|
@param indexer_id: int
|
|
|
|
@param sceneSeason: int
|
|
|
|
@param sceneEpisode: int
|
|
|
|
@return: (int, int) a tuple of (season, episode)
|
|
|
|
"""
|
|
|
|
if indexer_id is None or sceneSeason is None or sceneEpisode is None:
|
|
|
|
return None
|
2014-03-20 01:33:34 -04:00
|
|
|
|
2014-05-03 05:58:04 -04:00
|
|
|
indexer_id = int(indexer_id)
|
|
|
|
indexer = int(indexer)
|
|
|
|
|
2014-05-03 05:23:26 -04:00
|
|
|
if _xem_refresh_needed(indexer_id, indexer):
|
|
|
|
_xem_refresh(indexer_id, indexer)
|
2014-03-10 01:18:05 -04:00
|
|
|
cacheDB = db.DBConnection('cache.db')
|
2014-03-25 01:57:24 -04:00
|
|
|
rows = cacheDB.select(
|
|
|
|
"SELECT season, episode FROM xem_numbering WHERE indexer = ? and indexer_id = ? and scene_season = ? and scene_episode = ?",
|
|
|
|
[indexer, indexer_id, sceneSeason, sceneEpisode])
|
2014-03-10 01:18:05 -04:00
|
|
|
if rows:
|
|
|
|
return (int(rows[0]["season"]), int(rows[0]["episode"]))
|
|
|
|
else:
|
|
|
|
return (sceneSeason, sceneEpisode)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
|
|
|
|
2014-05-03 05:23:26 -04:00
|
|
|
def _xem_refresh_needed(indexer_id, indexer):
|
2014-03-10 01:18:05 -04:00
|
|
|
"""
|
|
|
|
Is a refresh needed on a show?
|
|
|
|
|
|
|
|
@param indexer_id: int
|
|
|
|
@return: bool
|
|
|
|
"""
|
|
|
|
if indexer_id is None:
|
|
|
|
return False
|
2014-03-20 01:33:34 -04:00
|
|
|
|
2014-05-03 05:58:04 -04:00
|
|
|
indexer_id = int(indexer_id)
|
|
|
|
indexer = int(indexer)
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
cacheDB = db.DBConnection('cache.db')
|
2014-03-25 01:57:24 -04:00
|
|
|
rows = cacheDB.select("SELECT last_refreshed FROM xem_refresh WHERE indexer = ? and indexer_id = ?",
|
|
|
|
[indexer, indexer_id])
|
2014-03-10 01:18:05 -04:00
|
|
|
if rows:
|
|
|
|
return time.time() > (int(rows[0]['last_refreshed']) + MAX_XEM_AGE_SECS)
|
|
|
|
else:
|
|
|
|
return True
|
2014-03-25 01:57:24 -04:00
|
|
|
|
|
|
|
|
2014-05-03 05:23:26 -04:00
|
|
|
def _xem_refresh(indexer_id, indexer):
|
2014-03-10 01:18:05 -04:00
|
|
|
"""
|
|
|
|
Refresh data from xem for a tv show
|
|
|
|
|
|
|
|
@param indexer_id: int
|
|
|
|
"""
|
|
|
|
if indexer_id is None:
|
|
|
|
return
|
2014-03-20 01:33:34 -04:00
|
|
|
|
2014-05-03 05:58:04 -04:00
|
|
|
indexer_id = int(indexer_id)
|
|
|
|
indexer = int(indexer)
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
try:
|
2014-03-25 01:57:24 -04:00
|
|
|
logger.log(
|
2014-05-03 05:58:04 -04:00
|
|
|
u'Looking up XEM scene mapping for show %s on %s' % (indexer_id, sickbeard.indexerApi(indexer).name,),
|
2014-03-25 01:57:24 -04:00
|
|
|
logger.DEBUG)
|
|
|
|
data = requests.get("http://thexem.de/map/all?id=%s&origin=%s&destination=scene" % (
|
2014-05-03 05:58:04 -04:00
|
|
|
indexer_id, sickbeard.indexerApi(indexer).config['xem_origin'],), verify=False).json()
|
2014-03-20 01:33:34 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
if data is None or data == '':
|
2014-03-25 01:57:24 -04:00
|
|
|
logger.log(u'No XEN data for show "%s on %s", trying TVTumbler' % (
|
2014-05-03 05:58:04 -04:00
|
|
|
indexer_id, sickbeard.indexerApi(indexer).name,), logger.MESSAGE)
|
2014-03-25 01:57:24 -04:00
|
|
|
data = requests.get("http://show-api.tvtumbler.com/api/thexem/all?id=%s&origin=%s&destination=scene" % (
|
2014-05-03 05:58:04 -04:00
|
|
|
indexer_id, sickbeard.indexerApi(indexer).config['xem_origin'],), verify=False).json()
|
2014-03-10 01:18:05 -04:00
|
|
|
if data is None or data == '':
|
2014-03-25 01:57:24 -04:00
|
|
|
logger.log(u'TVTumbler also failed for show "%s on %s". giving up.' % (indexer_id, indexer,),
|
|
|
|
logger.MESSAGE)
|
2014-03-10 01:18:05 -04:00
|
|
|
return None
|
2014-03-20 01:33:34 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
result = data
|
2014-05-07 03:50:49 -04:00
|
|
|
ql = []
|
|
|
|
|
|
|
|
cacheDB = db.DBConnection('cache.db')
|
2014-03-10 01:18:05 -04:00
|
|
|
if result:
|
2014-05-07 03:50:49 -04:00
|
|
|
ql.append(["INSERT OR REPLACE INTO xem_refresh (indexer, indexer_id, last_refreshed) VALUES (?,?,?)",
|
|
|
|
[indexer, indexer_id, time.time()]])
|
2014-03-10 01:18:05 -04:00
|
|
|
if 'success' in result['result']:
|
2014-05-07 03:50:49 -04:00
|
|
|
ql.append(["DELETE FROM xem_numbering where indexer = ? and indexer_id = ?", [indexer, indexer_id]])
|
2014-03-10 01:18:05 -04:00
|
|
|
for entry in result['data']:
|
|
|
|
if 'scene' in entry:
|
2014-05-07 03:50:49 -04:00
|
|
|
ql.append([
|
2014-03-25 01:57:24 -04:00
|
|
|
"INSERT INTO xem_numbering (indexer, indexer_id, season, episode, scene_season, scene_episode) VALUES (?,?,?,?,?,?)",
|
|
|
|
[indexer, indexer_id, entry[sickbeard.indexerApi(indexer).config['xem_origin']]['season'],
|
2014-05-03 05:58:04 -04:00
|
|
|
entry[sickbeard.indexerApi(indexer).config['xem_origin']]['episode'],
|
2014-05-07 03:50:49 -04:00
|
|
|
entry['scene']['season'], entry['scene']['episode']]])
|
2014-03-25 01:57:24 -04:00
|
|
|
if 'scene_2' in entry: # for doubles
|
2014-05-07 03:50:49 -04:00
|
|
|
ql.append([
|
2014-03-25 01:57:24 -04:00
|
|
|
"INSERT INTO xem_numbering (indexer, indexer_id, season, episode, scene_season, scene_episode) VALUES (?,?,?,?,?,?)",
|
|
|
|
[indexer, indexer_id, entry[sickbeard.indexerApi(indexer).config['xem_origin']]['season'],
|
2014-05-03 05:58:04 -04:00
|
|
|
entry[sickbeard.indexerApi(indexer).config['xem_origin']]['episode'],
|
2014-05-07 03:50:49 -04:00
|
|
|
entry['scene_2']['season'], entry['scene_2']['episode']]])
|
2014-03-10 01:18:05 -04:00
|
|
|
else:
|
2014-03-25 01:57:24 -04:00
|
|
|
logger.log(u'Failed to get XEM scene data for show %s from %s because "%s"' % (
|
2014-05-03 05:58:04 -04:00
|
|
|
indexer_id, sickbeard.indexerApi(indexer).name, result['message']), logger.DEBUG)
|
2014-03-10 01:18:05 -04:00
|
|
|
else:
|
2014-03-25 01:57:24 -04:00
|
|
|
logger.log(u"Empty lookup result - no XEM data for show %s on %s" % (
|
2014-05-03 05:58:04 -04:00
|
|
|
indexer_id, sickbeard.indexerApi(indexer).name,), logger.DEBUG)
|
2014-03-10 01:18:05 -04:00
|
|
|
except Exception, e:
|
2014-03-25 01:57:24 -04:00
|
|
|
logger.log(u"Exception while refreshing XEM data for show " + str(indexer_id) + " on " + sickbeard.indexerApi(
|
2014-05-03 05:58:04 -04:00
|
|
|
indexer).name + ": " + ex(e), logger.WARNING)
|
2014-03-10 01:18:05 -04:00
|
|
|
logger.log(traceback.format_exc(), logger.DEBUG)
|
|
|
|
return None
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-05-07 03:50:49 -04:00
|
|
|
if ql:
|
|
|
|
cacheDB.mass_action(ql)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-05-03 05:23:26 -04:00
|
|
|
def get_xem_numbering_for_show(indexer_id, indexer):
|
2014-03-10 01:18:05 -04:00
|
|
|
"""
|
|
|
|
Returns a dict of (season, episode) : (sceneSeason, sceneEpisode) mappings
|
|
|
|
for an entire show. Both the keys and values of the dict are tuples.
|
|
|
|
Will be empty if there are no scene numbers set in xem
|
|
|
|
"""
|
|
|
|
if indexer_id is None:
|
|
|
|
return {}
|
2014-03-20 01:33:34 -04:00
|
|
|
|
2014-05-03 05:58:04 -04:00
|
|
|
indexer_id = int(indexer_id)
|
|
|
|
indexer = int(indexer)
|
|
|
|
|
2014-05-03 05:23:26 -04:00
|
|
|
if _xem_refresh_needed(indexer_id, indexer):
|
|
|
|
_xem_refresh(indexer_id, indexer)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
cacheDB = db.DBConnection('cache.db')
|
2014-03-25 01:57:24 -04:00
|
|
|
|
|
|
|
rows = cacheDB.select(
|
|
|
|
'SELECT season, episode, scene_season, scene_episode FROM xem_numbering WHERE indexer = ? and indexer_id = ? ORDER BY season, episode',
|
|
|
|
[indexer, indexer_id])
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
result = {}
|
|
|
|
for row in rows:
|
|
|
|
result[(int(row['season']), int(row['episode']))] = (int(row['scene_season']), int(row['scene_episode']))
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
return result
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-05-03 05:23:26 -04:00
|
|
|
def get_xem_numbering_for_season(indexer_id, indexer, season):
|
2014-03-10 01:18:05 -04:00
|
|
|
"""
|
|
|
|
Returns a dict of (season, episode) : (sceneSeason, sceneEpisode) mappings
|
|
|
|
for an entire show. Both the keys and values of the dict are tuples.
|
|
|
|
Will be empty if there are no scene numbers set
|
|
|
|
"""
|
|
|
|
if indexer_id is None or season is None:
|
|
|
|
return {}
|
|
|
|
|
2014-05-03 05:58:04 -04:00
|
|
|
indexer_id = int(indexer_id)
|
|
|
|
indexer = int(indexer)
|
|
|
|
|
2014-05-03 05:23:26 -04:00
|
|
|
if _xem_refresh_needed(indexer_id, indexer):
|
|
|
|
_xem_refresh(indexer_id, indexer)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
cacheDB = db.DBConnection('cache.db')
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
rows = cacheDB.select(
|
2014-05-03 05:23:26 -04:00
|
|
|
'SELECT season, scene_season FROM xem_numbering WHERE indexer = ? and indexer_id = ? AND season = ? ORDER BY season', [indexer, indexer_id, season])
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
result = {}
|
|
|
|
if rows:
|
|
|
|
for row in rows:
|
2014-03-25 01:57:24 -04:00
|
|
|
result.setdefault(int(row['season']), []).append(int(row['scene_season']))
|
2014-03-10 01:18:05 -04:00
|
|
|
else:
|
2014-03-25 01:57:24 -04:00
|
|
|
result.setdefault(int(season), []).append(int(season))
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-05-03 18:29:00 -04:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def fix_scene_numbering():
|
|
|
|
ql = []
|
|
|
|
|
|
|
|
myDB = db.DBConnection()
|
|
|
|
|
|
|
|
sqlResults = myDB.select(
|
|
|
|
"SELECT showid, indexerid, indexer, episode_id, season, episode FROM tv_episodes WHERE scene_season = -1 OR scene_episode = -1")
|
|
|
|
|
|
|
|
for epResult in sqlResults:
|
|
|
|
|
|
|
|
indexerid = int(epResult["showid"])
|
|
|
|
indexer = int(epResult["indexer"])
|
|
|
|
season = int(epResult["season"])
|
|
|
|
episode = int(epResult["episode"])
|
|
|
|
|
|
|
|
logger.log(
|
|
|
|
u"Repairing any scene numbering issues for showid: " + str(epResult["showid"]) + u" season: " + str(
|
|
|
|
epResult["season"]) + u" episode: " + str(epResult["episode"]), logger.DEBUG)
|
|
|
|
|
|
|
|
scene_season, scene_episode = sickbeard.scene_numbering.get_scene_numbering(indexerid,
|
|
|
|
indexer,
|
|
|
|
season,
|
|
|
|
episode)
|
|
|
|
|
|
|
|
ql.append(
|
|
|
|
["UPDATE tv_episodes SET scene_season = ? WHERE indexerid = ?", [scene_season, epResult["indexerid"]]])
|
|
|
|
ql.append(
|
|
|
|
["UPDATE tv_episodes SET scene_episode = ? WHERE indexerid = ?", [scene_episode, epResult["indexerid"]]])
|
|
|
|
|
2014-05-07 03:50:49 -04:00
|
|
|
if ql:
|
2014-05-03 18:29:00 -04:00
|
|
|
myDB.mass_action(ql)
|