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
#
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-03-10 01:18:05 -04:00
#
# Created on Sep 20, 2012
# @author: Dermot Buckley <dermot@buckley.ie>
# @copyright: Dermot Buckley
#
2014-07-27 06:59:21 -04:00
2014-03-10 01:18:05 -04:00
import time
2014-07-27 06:59:21 -04:00
import datetime
2014-03-10 01:18:05 -04:00
import traceback
2014-05-28 17:13:29 -04:00
import sickbeard
2014-03-10 01:18:05 -04:00
try :
import json
except ImportError :
from lib import simplejson as json
from sickbeard import logger
from sickbeard import db
from sickbeard . exceptions import ex
2014-03-25 01:57:24 -04:00
2014-05-30 01:48:02 -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 :
2014-05-30 01:48:02 -04:00
return ( season , episode )
2014-03-20 01:33:34 -04:00
2014-05-30 01:48:02 -04:00
showObj = sickbeard . helpers . findCertainShow ( sickbeard . showList , int ( indexer_id ) )
if not showObj . is_scene :
return ( season , episode )
2014-05-03 05:58:04 -04:00
2014-05-30 01:48:02 -04:00
result = find_scene_numbering ( int ( indexer_id ) , int ( indexer ) , season , episode )
2014-03-10 01:18:05 -04:00
if result :
return result
else :
if fallback_to_xem :
2014-05-30 01:48:02 -04:00
xem_result = find_xem_numbering ( int ( indexer_id ) , int ( indexer ) , season , episode )
2014-03-10 01:18:05 -04:00
if xem_result :
return xem_result
2014-05-30 01:48:02 -04:00
return ( season , episode )
2014-03-25 01:57:24 -04:00
2014-05-30 01:48:02 -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 :
2014-05-30 01:48:02 -04:00
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-06-21 18:46:59 -04:00
myDB = db . DBConnection ( )
rows = myDB . select (
" SELECT scene_season, scene_episode FROM scene_numbering WHERE indexer = ? and indexer_id = ? and season = ? and episode = ? and (scene_season or scene_episode) != 0 " ,
[ indexer , indexer_id , season , episode ] )
2014-03-20 01:33:34 -04:00
2014-03-10 01:18:05 -04:00
if rows :
2014-05-30 01:48:02 -04:00
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-30 01:48:02 -04:00
def get_scene_absolute_numbering ( indexer_id , indexer , absolute_number , fallback_to_xem = True ) :
"""
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
returns the TVDB and TVRAGE numbering .
( so the return values will always be set )
@param indexer_id : int
2014-06-07 08:36:50 -04:00
@param absolute_number : int
2014-05-30 01:48:02 -04:00
@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 absolute_number is None :
return absolute_number
indexer_id = int ( indexer_id )
indexer = int ( indexer )
showObj = sickbeard . helpers . findCertainShow ( sickbeard . showList , indexer_id )
if not showObj . is_scene :
return absolute_number
result = find_scene_absolute_numbering ( indexer_id , indexer , absolute_number )
if result :
return result
else :
if fallback_to_xem :
xem_result = find_xem_absolute_numbering ( indexer_id , indexer , absolute_number )
if xem_result :
return xem_result
return absolute_number
def find_scene_absolute_numbering ( indexer_id , indexer , absolute_number ) :
"""
Same as get_scene_numbering ( ) , but returns None if scene numbering is not set
"""
if indexer_id is None or absolute_number is None :
return absolute_number
indexer_id = int ( indexer_id )
indexer = int ( indexer )
2014-06-21 18:46:59 -04:00
myDB = db . DBConnection ( )
rows = myDB . select (
" SELECT scene_absolute_number FROM scene_numbering WHERE indexer = ? and indexer_id = ? and absolute_number = ? and scene_absolute_number != 0 " ,
[ indexer , indexer_id , absolute_number ] )
2014-05-30 01:48:02 -04:00
if rows :
return int ( rows [ 0 ] [ " scene_absolute_number " ] )
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-06-21 18:46:59 -04:00
myDB = db . DBConnection ( )
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-25 01:57:24 -04:00
2014-03-10 01:18:05 -04:00
if rows :
2014-05-30 01:48:02 -04:00
return ( int ( rows [ 0 ] [ " season " ] ) , int ( rows [ 0 ] [ " episode " ] ) )
2014-03-10 01:18:05 -04:00
else :
if fallback_to_xem :
2014-05-30 01:48:02 -04:00
return get_indexer_numbering_for_xem ( indexer_id , indexer , sceneSeason , sceneEpisode )
return ( sceneSeason , sceneEpisode )
2014-03-25 01:57:24 -04:00
2014-05-30 01:48:02 -04:00
2014-07-03 12:15:03 -04:00
def get_indexer_absolute_numbering ( indexer_id , indexer , sceneAbsoluteNumber , fallback_to_xem = True , scene_season = None ) :
2014-05-28 17:13:29 -04:00
"""
Returns a tuple , ( season , episode , absolute_number ) with the TVDB and TVRAGE numbering for ( sceneAbsoluteNumber )
( this works like the reverse of get_absolute_numbering )
"""
if indexer_id is None or sceneAbsoluteNumber is None :
2014-05-30 01:48:02 -04:00
return sceneAbsoluteNumber
2014-05-28 17:13:29 -04:00
indexer_id = int ( indexer_id )
indexer = int ( indexer )
2014-06-21 18:46:59 -04:00
myDB = db . DBConnection ( )
2014-07-03 12:15:03 -04:00
if scene_season is None :
rows = myDB . select (
" SELECT absolute_number FROM scene_numbering WHERE indexer = ? and indexer_id = ? and scene_absolute_number = ? " ,
[ indexer , indexer_id , sceneAbsoluteNumber ] )
else :
rows = myDB . select (
" SELECT absolute_number FROM scene_numbering WHERE indexer = ? and indexer_id = ? and scene_absolute_number = ? and scene_season = ? " ,
[ indexer , indexer_id , sceneAbsoluteNumber , scene_season ] )
2014-05-28 17:13:29 -04:00
if rows :
2014-05-30 01:48:02 -04:00
return int ( rows [ 0 ] [ " absolute_number " ] )
2014-05-28 17:13:29 -04:00
else :
if fallback_to_xem :
2014-07-03 12:15:03 -04:00
return get_indexer_absolute_numbering_for_xem ( indexer_id , indexer , sceneAbsoluteNumber , scene_season )
2014-05-30 01:48:02 -04:00
return sceneAbsoluteNumber
2014-03-25 01:57:24 -04:00
2014-05-30 01:48:02 -04:00
2014-07-27 06:59:21 -04:00
def set_scene_numbering ( indexer_id , indexer , season = None , episode = None , absolute_number = None , sceneSeason = None ,
sceneEpisode = None , sceneAbsolute = None ) :
2014-03-10 01:18:05 -04:00
"""
2014-05-30 01:48:02 -04:00
Set scene numbering for a season / episode .
To clear the scene numbering , leave both sceneSeason and sceneEpisode as None .
2014-03-10 01:18:05 -04:00
"""
if indexer_id is None :
2014-05-30 01:48:02 -04:00
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-06-21 18:46:59 -04:00
myDB = db . DBConnection ( )
if season and episode :
myDB . action (
" INSERT OR IGNORE INTO scene_numbering (indexer, indexer_id, season, episode) VALUES (?,?,?,?) " ,
[ indexer , indexer_id , season , episode ] )
2014-03-25 01:57:24 -04:00
2014-06-21 18:46:59 -04:00
myDB . action (
" UPDATE scene_numbering SET scene_season = ?, scene_episode = ? WHERE indexer = ? and indexer_id = ? and season = ? and episode = ? " ,
[ sceneSeason , sceneEpisode , indexer , indexer_id , season , episode ] )
elif absolute_number :
myDB . action (
" INSERT OR IGNORE INTO scene_numbering (indexer, indexer_id, absolute_number) VALUES (?,?,?) " ,
[ indexer , indexer_id , absolute_number ] )
2014-05-26 02:29:22 -04:00
2014-06-21 18:46:59 -04:00
myDB . action (
" UPDATE scene_numbering SET scene_absolute_number = ? WHERE indexer = ? and indexer_id = ? and absolute_number = ? " ,
[ sceneAbsolute , indexer , indexer_id , absolute_number ] )
2014-03-25 01:57:24 -04:00
2014-05-30 01:48:02 -04:00
def find_xem_numbering ( indexer_id , indexer , season , episode ) :
2014-03-10 01:18:05 -04:00
"""
2014-05-30 01:48:02 -04:00
Returns the scene numbering , as retrieved from xem .
Refreshes / Loads as needed .
2014-03-10 01:18:05 -04:00
2014-05-30 01:48:02 -04:00
@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 .
2014-03-10 01:18:05 -04:00
"""
if indexer_id is None or season is None or episode is None :
2014-05-30 01:48:02 -04:00
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-30 02:49:47 -04:00
xem_refresh ( indexer_id , indexer )
2014-03-25 01:57:24 -04:00
2014-06-21 18:46:59 -04:00
myDB = db . DBConnection ( )
rows = myDB . select (
" SELECT scene_season, scene_episode FROM tv_episodes WHERE indexer = ? and showid = ? and season = ? and episode = ? and (scene_season or scene_episode) != 0 " ,
[ indexer , indexer_id , season , episode ] )
2014-03-25 01:57:24 -04:00
2014-05-30 01:48:02 -04:00
if rows :
return ( int ( rows [ 0 ] [ " scene_season " ] ) , int ( rows [ 0 ] [ " scene_episode " ] ) )
2014-03-25 01:57:24 -04:00
2014-05-30 01:48:02 -04:00
def find_xem_absolute_numbering ( indexer_id , indexer , absolute_number ) :
2014-03-10 01:18:05 -04:00
"""
Returns the scene numbering , as retrieved from xem .
Refreshes / Loads as needed .
2014-05-30 01:48:02 -04:00
2014-03-10 01:18:05 -04:00
@param indexer_id : int
2014-06-07 08:36:50 -04:00
@param absolute_number : int
@return : int
2014-03-10 01:18:05 -04:00
"""
2014-05-30 01:48:02 -04:00
if indexer_id is None or absolute_number is None :
return absolute_number
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-30 02:49:47 -04:00
xem_refresh ( indexer_id , indexer )
2014-03-25 01:57:24 -04:00
2014-06-21 18:46:59 -04:00
myDB = db . DBConnection ( )
rows = myDB . select (
" SELECT scene_absolute_number FROM tv_episodes WHERE indexer = ? and showid = ? and absolute_number = ? and scene_absolute_number != 0 " ,
[ indexer , indexer_id , absolute_number ] )
2014-05-11 18:24:02 -04:00
2014-03-10 01:18:05 -04:00
if rows :
2014-05-30 01:48:02 -04:00
return int ( rows [ 0 ] [ " scene_absolute_number " ] )
2014-03-25 01:57:24 -04:00
2014-05-30 01:48:02 -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 :
2014-05-30 01:48:02 -04:00
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-05-30 02:49:47 -04:00
xem_refresh ( indexer_id , indexer )
2014-05-28 17:13:29 -04:00
2014-06-21 18:46:59 -04:00
myDB = db . DBConnection ( )
rows = myDB . select (
" SELECT season, episode FROM tv_episodes WHERE indexer = ? and showid = ? and scene_season = ? and scene_episode = ? " ,
[ indexer , indexer_id , sceneSeason , sceneEpisode ] )
2014-05-28 17:13:29 -04:00
2014-03-10 01:18:05 -04:00
if rows :
2014-05-30 01:48:02 -04:00
return ( int ( rows [ 0 ] [ " season " ] ) , int ( rows [ 0 ] [ " episode " ] ) )
2014-03-25 01:57:24 -04:00
2014-05-30 01:48:02 -04:00
return ( sceneSeason , sceneEpisode )
2014-05-28 17:13:29 -04:00
2014-05-30 01:48:02 -04:00
2014-07-03 12:15:03 -04:00
def get_indexer_absolute_numbering_for_xem ( indexer_id , indexer , sceneAbsoluteNumber , scene_season = None ) :
2014-05-28 17:13:29 -04:00
"""
Reverse of find_xem_numbering : lookup a tvdb season and episode using scene numbering
@param indexer_id : int
2014-06-07 08:36:50 -04:00
@param sceneAbsoluteNumber : int
@return : int
2014-05-28 17:13:29 -04:00
"""
if indexer_id is None or sceneAbsoluteNumber is None :
2014-05-30 01:48:02 -04:00
return sceneAbsoluteNumber
2014-05-28 17:13:29 -04:00
indexer_id = int ( indexer_id )
indexer = int ( indexer )
2014-05-30 02:49:47 -04:00
xem_refresh ( indexer_id , indexer )
2014-05-28 17:13:29 -04:00
2014-06-21 18:46:59 -04:00
myDB = db . DBConnection ( )
2014-07-03 12:15:03 -04:00
if scene_season is None :
rows = myDB . select (
" SELECT absolute_number FROM tv_episodes WHERE indexer = ? and showid = ? and scene_absolute_number = ? " ,
[ indexer , indexer_id , sceneAbsoluteNumber ] )
else :
rows = myDB . select (
" SELECT absolute_number FROM tv_episodes WHERE indexer = ? and showid = ? and scene_absolute_number = ? and scene_season = ? " ,
2014-07-27 06:59:21 -04:00
[ indexer , indexer_id , sceneAbsoluteNumber , scene_season ] )
2014-05-28 17:13:29 -04:00
if rows :
2014-05-30 01:48:02 -04:00
return int ( rows [ 0 ] [ " absolute_number " ] )
return sceneAbsoluteNumber
def get_scene_numbering_for_show ( indexer_id , indexer ) :
"""
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 { }
indexer_id = int ( indexer_id )
indexer = int ( indexer )
2014-06-21 18:46:59 -04:00
myDB = db . DBConnection ( )
rows = myDB . select (
' SELECT season, episode, scene_season, scene_episode FROM scene_numbering WHERE indexer = ? and indexer_id = ? and (scene_season or scene_episode) != 0 ORDER BY season, episode ' ,
[ indexer , indexer_id ] )
2014-05-30 01:48:02 -04:00
result = { }
for row in rows :
season = int ( row [ ' season ' ] )
episode = int ( row [ ' episode ' ] )
scene_season = int ( row [ ' scene_season ' ] )
scene_episode = int ( row [ ' scene_episode ' ] )
result [ ( season , episode ) ] = ( scene_season , scene_episode )
return result
def get_xem_numbering_for_show ( indexer_id , indexer ) :
"""
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 { }
indexer_id = int ( indexer_id )
indexer = int ( indexer )
2014-05-30 02:49:47 -04:00
xem_refresh ( indexer_id , indexer )
2014-05-30 01:48:02 -04:00
2014-06-21 18:46:59 -04:00
myDB = db . DBConnection ( )
rows = myDB . select (
' SELECT season, episode, scene_season, scene_episode FROM tv_episodes WHERE indexer = ? and showid = ? and (scene_season or scene_episode) != 0 ORDER BY season, episode ' ,
[ indexer , indexer_id ] )
2014-05-30 01:48:02 -04:00
result = { }
for row in rows :
season = int ( row [ ' season ' ] )
episode = int ( row [ ' episode ' ] )
scene_season = int ( row [ ' scene_season ' ] )
scene_episode = int ( row [ ' scene_episode ' ] )
result [ ( season , episode ) ] = ( scene_season , scene_episode )
return result
def get_scene_absolute_numbering_for_show ( indexer_id , indexer ) :
"""
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 { }
indexer_id = int ( indexer_id )
indexer = int ( indexer )
2014-06-21 18:46:59 -04:00
myDB = db . DBConnection ( )
rows = myDB . select (
' SELECT absolute_number, scene_absolute_number FROM scene_numbering WHERE indexer = ? and indexer_id = ? and scene_absolute_number != 0 ORDER BY absolute_number ' ,
[ indexer , indexer_id ] )
2014-05-30 01:48:02 -04:00
result = { }
for row in rows :
absolute_number = int ( row [ ' absolute_number ' ] )
scene_absolute_number = int ( row [ ' scene_absolute_number ' ] )
result [ absolute_number ] = scene_absolute_number
return result
def get_xem_absolute_numbering_for_show ( indexer_id , indexer ) :
"""
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 { }
indexer_id = int ( indexer_id )
indexer = int ( indexer )
2014-05-30 02:49:47 -04:00
xem_refresh ( indexer_id , indexer )
2014-05-30 01:48:02 -04:00
result = { }
2014-06-21 18:46:59 -04:00
myDB = db . DBConnection ( )
rows = myDB . select (
' SELECT absolute_number, scene_absolute_number FROM tv_episodes WHERE indexer = ? and showid = ? and scene_absolute_number != 0 ORDER BY absolute_number ' ,
[ indexer , indexer_id ] )
2014-05-30 01:48:02 -04:00
for row in rows :
absolute_number = int ( row [ ' absolute_number ' ] )
scene_absolute_number = int ( row [ ' scene_absolute_number ' ] )
result [ absolute_number ] = scene_absolute_number
return result
2014-03-25 01:57:24 -04:00
2014-07-27 06:59:21 -04:00
2014-05-30 02:49:47 -04:00
def xem_refresh ( indexer_id , indexer , force = False ) :
2014-03-10 01:18:05 -04:00
"""
2014-05-30 02:49:47 -04:00
Refresh data from xem for a tv show
2014-03-10 01:18:05 -04:00
@param indexer_id : int
"""
if indexer_id is None :
2014-05-30 02:49:47 -04:00
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-07-27 06:59:21 -04:00
# XEM API URL
url = " http://thexem.de/map/all?id= %s &origin= %s &destination=scene " % (
indexer_id , sickbeard . indexerApi ( indexer ) . config [ ' xem_origin ' ] )
MAX_REFRESH_AGE_SECS = 86400 # 1 day
2014-06-21 18:46:59 -04:00
myDB = db . DBConnection ( )
rows = myDB . 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 :
2014-07-27 06:59:21 -04:00
lastRefresh = int ( rows [ 0 ] [ ' last_refreshed ' ] )
refresh = int ( time . mktime ( datetime . datetime . today ( ) . timetuple ( ) ) ) > lastRefresh + MAX_REFRESH_AGE_SECS
2014-03-10 01:18:05 -04:00
else :
2014-05-30 02:49:47 -04:00
refresh = True
if refresh or force :
2014-07-27 06:59:21 -04:00
logger . log (
u ' Looking up XEM scene mapping using for show %s on %s ' % ( indexer_id , sickbeard . indexerApi ( indexer ) . name , ) ,
logger . DEBUG )
# mark refreshed
myDB . upsert ( " xem_refresh " ,
{ ' indexer ' : indexer ,
' last_refreshed ' : int ( time . mktime ( datetime . datetime . today ( ) . timetuple ( ) ) ) } ,
{ ' indexer_id ' : indexer_id } )
2014-05-30 02:49:47 -04:00
try :
2014-07-27 06:59:21 -04:00
parsedJSON = sickbeard . helpers . getURL ( url , json = True )
if not parsedJSON or parsedJSON == ' ' :
logger . log ( u ' No XEN data for show " %s on %s " ' % ( indexer_id , sickbeard . indexerApi ( indexer ) . name , ) , logger . MESSAGE )
return
if ' success ' in parsedJSON [ ' result ' ] :
cl = [ ]
for entry in parsedJSON [ ' data ' ] :
if ' scene ' in entry :
cl . append ( [
" UPDATE tv_episodes SET scene_season = ?, scene_episode = ?, scene_absolute_number = ? WHERE showid = ? AND season = ? AND episode = ? " ,
[ entry [ ' scene ' ] [ ' season ' ] ,
entry [ ' scene ' ] [ ' episode ' ] ,
entry [ ' scene ' ] [ ' absolute ' ] ,
indexer_id ,
entry [ sickbeard . indexerApi ( indexer ) . config [ ' xem_origin ' ] ] [ ' season ' ] ,
entry [ sickbeard . indexerApi ( indexer ) . config [ ' xem_origin ' ] ] [ ' episode ' ]
] ] )
if ' scene_2 ' in entry : # for doubles
cl . append ( [
" UPDATE tv_episodes SET scene_season = ?, scene_episode = ?, scene_absolute_number = ? WHERE showid = ? AND season = ? AND episode = ? " ,
[ entry [ ' scene_2 ' ] [ ' season ' ] ,
entry [ ' scene_2 ' ] [ ' episode ' ] ,
entry [ ' scene_2 ' ] [ ' absolute ' ] ,
indexer_id ,
entry [ sickbeard . indexerApi ( indexer ) . config [ ' xem_origin ' ] ] [ ' season ' ] ,
entry [ sickbeard . indexerApi ( indexer ) . config [ ' xem_origin ' ] ] [ ' episode ' ]
] ] )
if len ( cl ) > 0 :
myDB = db . DBConnection ( )
myDB . mass_action ( cl )
2014-03-10 01:18:05 -04:00
else :
2014-05-30 02:49:47 -04:00
logger . log ( u " Empty lookup result - no XEM data for show %s on %s " % (
indexer_id , sickbeard . indexerApi ( indexer ) . name , ) , logger . DEBUG )
except Exception , e :
2014-07-27 06:59:21 -04:00
logger . log (
u " Exception while refreshing XEM data for show " + str ( indexer_id ) + " on " + sickbeard . indexerApi (
indexer ) . name + " : " + ex ( e ) , logger . WARNING )
2014-05-30 02:49:47 -04:00
logger . log ( traceback . format_exc ( ) , logger . DEBUG )
2014-03-10 01:18:05 -04:00
2014-05-28 17:13:29 -04:00
def fix_xem_numbering ( 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 .
2014-05-28 17:13:29 -04:00
Will be empty if there are no scene numbers set in xem
2014-03-10 01:18:05 -04:00
"""
2014-05-28 17:13:29 -04:00
if indexer_id is None :
2014-03-10 01:18:05 -04:00
return { }
2014-05-03 05:58:04 -04:00
indexer_id = int ( indexer_id )
indexer = int ( indexer )
2014-05-28 17:13:29 -04:00
# query = [{
2014-05-30 01:48:02 -04:00
# "name": self.show.name,
2014-07-27 06:59:21 -04:00
# "seasons": [{
# "episodes": [{
# "episode_number": None,
# "name": None
# }],
# "season_number": None,
2014-05-28 17:13:29 -04:00
# }],
# "/tv/tv_program/number_of_seasons": [],
# "/tv/tv_program/number_of_episodes": [],
# "/tv/tv_program/thetvdb_id": [],
# "/tv/tv_program/tvrage_id": [],
# "type": "/tv/tv_program",
# }]
#
#
# url = 'https://www.googleapis.com/freebase/v1/mqlread'
# api_key = "AIzaSyCCHNp4dhVHxJYzbLiCE4y4a1rgTnX4fDE"
# params = {
# 'query': json.dumps(query),
# 'key': api_key
# }
#
#
# def get_from_api(url, params=None):
# """Build request and return results
# """
# import xmltodict
#
# response = requests.get(url, params=params)
# if response.status_code == 200:
# try:
# return response.json()
# except ValueError:
# return xmltodict.parse(response.text)['Data']
#
# # Get query results
# tmp = get_from_api(url, params=params)['result']
2014-03-10 01:18:05 -04:00
2014-06-21 18:46:59 -04:00
myDB = db . DBConnection ( )
rows = myDB . select (
' SELECT season, episode, absolute_number, scene_season, scene_episode, scene_absolute_number FROM tv_episodes WHERE indexer = ? and showid = ? ' ,
[ indexer , indexer_id ] )
2014-05-03 18:29:00 -04:00
2014-05-28 17:13:29 -04:00
last_absolute_number = None
last_scene_season = None
last_scene_episode = None
last_scene_absolute_number = None
2014-05-03 18:29:00 -04:00
2014-05-28 17:13:29 -04:00
update_absolute_number = False
update_scene_season = False
update_scene_episode = False
update_scene_absolute_number = False
2014-05-03 18:29:00 -04:00
2014-05-28 17:13:29 -04:00
logger . log (
u ' Fixing any XEM scene mapping issues for show %s on %s ' % ( indexer_id , sickbeard . indexerApi ( indexer ) . name , ) ,
logger . DEBUG )
2014-05-03 18:29:00 -04:00
2014-07-14 22:00:53 -04:00
cl = [ ]
2014-05-28 17:13:29 -04:00
for row in rows :
season = int ( row [ ' season ' ] )
episode = int ( row [ ' episode ' ] )
2014-05-03 18:29:00 -04:00
2014-05-28 17:13:29 -04:00
if not int ( row [ ' scene_season ' ] ) and last_scene_season :
scene_season = last_scene_season + 1
update_scene_season = True
else :
scene_season = int ( row [ ' scene_season ' ] )
if last_scene_season and scene_season < last_scene_season :
scene_season = last_scene_season + 1
update_scene_season = True
if not int ( row [ ' scene_episode ' ] ) and last_scene_episode :
scene_episode = last_scene_episode + 1
update_scene_episode = True
else :
scene_episode = int ( row [ ' scene_episode ' ] )
if last_scene_episode and scene_episode < last_scene_episode :
scene_episode = last_scene_episode + 1
update_scene_episode = True
# check for unset values and correct them
if not int ( row [ ' absolute_number ' ] ) and last_absolute_number :
absolute_number = last_absolute_number + 1
update_absolute_number = True
else :
absolute_number = int ( row [ ' absolute_number ' ] )
if last_absolute_number and absolute_number < last_absolute_number :
absolute_number = last_absolute_number + 1
update_absolute_number = True
if not int ( row [ ' scene_absolute_number ' ] ) and last_scene_absolute_number :
scene_absolute_number = last_scene_absolute_number + 1
update_scene_absolute_number = True
else :
scene_absolute_number = int ( row [ ' scene_absolute_number ' ] )
if last_scene_absolute_number and scene_absolute_number < last_scene_absolute_number :
scene_absolute_number = last_scene_absolute_number + 1
update_scene_absolute_number = True
# store values for lookup on next iteration
last_absolute_number = absolute_number
last_scene_season = scene_season
last_scene_episode = scene_episode
last_scene_absolute_number = scene_absolute_number
if update_absolute_number :
2014-07-14 22:00:53 -04:00
cl . append ( [
2014-05-28 17:13:29 -04:00
" UPDATE tv_episodes SET absolute_number = ? WHERE showid = ? AND season = ? AND episode = ? " ,
[ absolute_number ,
indexer_id ,
season ,
episode
] ] )
update_absolute_number = False
if update_scene_season :
2014-07-14 22:00:53 -04:00
cl . append ( [
2014-05-28 17:13:29 -04:00
" UPDATE tv_episodes SET scene_season = ? WHERE showid = ? AND season = ? AND episode = ? " ,
[ scene_season ,
indexer_id ,
season ,
episode
] ] )
update_scene_season = False
if update_scene_episode :
2014-07-14 22:00:53 -04:00
cl . append ( [
2014-05-28 17:13:29 -04:00
" UPDATE tv_episodes SET scene_episode = ? WHERE showid = ? AND season = ? AND episode = ? " ,
[ scene_episode ,
indexer_id ,
season ,
episode
] ] )
update_scene_episode = False
if update_scene_absolute_number :
2014-07-14 22:00:53 -04:00
cl . append ( [
2014-05-28 17:13:29 -04:00
" UPDATE tv_episodes SET scene_absolute_number = ? WHERE showid = ? AND season = ? AND episode = ? " ,
[ scene_absolute_number ,
indexer_id ,
season ,
episode
] ] )
update_scene_absolute_number = False
2014-05-03 18:29:00 -04:00
2014-07-14 22:00:53 -04:00
if len ( cl ) > 0 :
2014-06-21 18:46:59 -04:00
myDB = db . DBConnection ( )
2014-07-14 22:00:53 -04:00
myDB . mass_action ( cl )