aggiornamento ricerca alternativa
This commit is contained in:
+109
-39
@@ -12,13 +12,15 @@ import sys
|
||||
if sys.version_info[0] >= 3: from concurrent import futures
|
||||
else: from concurrent_py2 import futures
|
||||
|
||||
client_id = "502bd1660b833c1ae69828163c0848e84e9850061e5529f30930e7356cae73b1"
|
||||
client_secret = "1d30d5b24acf223a5e1ab6c61d08b69992d98ed5b0c7e26b052b5e6a592035a4"
|
||||
host = 'https://api.trakt.tv'
|
||||
client_id = '502bd1660b833c1ae69828163c0848e84e9850061e5529f30930e7356cae73b1'
|
||||
client_secret = '1d30d5b24acf223a5e1ab6c61d08b69992d98ed5b0c7e26b052b5e6a592035a4'
|
||||
token_auth = config.get_setting("token_trakt", "trakt")
|
||||
|
||||
|
||||
def auth_trakt():
|
||||
item = Item()
|
||||
folder = (config.get_platform() == "plex")
|
||||
folder = (config.get_platform() == 'plex')
|
||||
item.folder = folder
|
||||
# Autentificación de cuenta Trakt
|
||||
headers = {'Content-Type': 'application/json', 'trakt-api-key': client_id, 'trakt-api-version': '2'}
|
||||
@@ -26,24 +28,23 @@ def auth_trakt():
|
||||
post = {'client_id': client_id}
|
||||
post = jsontools.dump(post)
|
||||
# Se solicita url y código de verificación para conceder permiso a la app
|
||||
url = "http://api.trakt.tv/oauth/device/code"
|
||||
data = httptools.downloadpage(url, post=post, headers=headers).data
|
||||
data = jsontools.load(data)
|
||||
item.verify_url = data["verification_url"]
|
||||
item.user_code = data["user_code"]
|
||||
item.device_code = data["device_code"]
|
||||
item.intervalo = data["interval"]
|
||||
url = host + '/oauth/device/code'
|
||||
data = httptools.downloadpage(url, post=post, headers=headers).json
|
||||
item.verify_url = data['verification_url']
|
||||
item.user_code = data['user_code']
|
||||
item.device_code = data['device_code']
|
||||
item.intervalo = data['interval']
|
||||
if not item.folder:
|
||||
token_trakt(item)
|
||||
|
||||
else:
|
||||
itemlist = []
|
||||
title = config.get_localized_string(60248) % item.verify_url
|
||||
itemlist.append(item.clone(title=title, action=""))
|
||||
itemlist.append(item.clone(title=title, action=''))
|
||||
title = config.get_localized_string(60249) % item.user_code
|
||||
itemlist.append(item.clone(title=title, action=""))
|
||||
itemlist.append(item.clone(title=title, action=''))
|
||||
title = config.get_localized_string(60250)
|
||||
itemlist.append(item.clone(title=title, action="token_trakt"))
|
||||
itemlist.append(item.clone(title=title, action='token_trakt'))
|
||||
return itemlist
|
||||
except:
|
||||
import traceback
|
||||
@@ -55,17 +56,17 @@ def token_trakt(item):
|
||||
|
||||
headers = {'Content-Type': 'application/json', 'trakt-api-key': client_id, 'trakt-api-version': '2'}
|
||||
try:
|
||||
if item.extra == "renew":
|
||||
refresh = config.get_setting("refresh_token_trakt", "trakt")
|
||||
url = "http://api.trakt.tv/oauth/device/token"
|
||||
if item.extra == 'renew':
|
||||
refresh = config.get_setting('refresh_token_trakt', 'trakt')
|
||||
url = host + '/oauth/device/token'
|
||||
post = {'refresh_token': refresh, 'client_id': client_id, 'client_secret': client_secret,
|
||||
'redirect_uri': 'urn:ietf:wg:oauth:2.0:oob', 'grant_type': 'refresh_token'}
|
||||
post = jsontools.dump(post)
|
||||
data = httptools.downloadpage(url, post=post, headers=headers).data
|
||||
data = jsontools.load(data)
|
||||
elif item.action == "token_trakt":
|
||||
url = "http://api.trakt.tv/oauth/device/token"
|
||||
post = "code=%s&client_id=%s&client_secret=%s" % (item.device_code, client_id, client_secret)
|
||||
elif item.action == 'token_trakt':
|
||||
url = host + '/oauth/device/token'
|
||||
post = 'code={}&client_id={}&client_secret={}'.format(item.device_code, client_id, client_secret)
|
||||
data = httptools.downloadpage(url, post=post, headers=headers).data
|
||||
data = jsontools.load(data)
|
||||
else:
|
||||
@@ -80,15 +81,15 @@ def token_trakt(item):
|
||||
time.sleep(item.intervalo)
|
||||
try:
|
||||
if dialog_auth.iscanceled():
|
||||
config.set_setting("trakt_sync", False)
|
||||
config.set_setting('trakt_sync', False)
|
||||
return
|
||||
|
||||
url = "http://api.trakt.tv/oauth/device/token"
|
||||
url = host + '/oauth/device/token'
|
||||
post = {'code': item.device_code, 'client_id': client_id, 'client_secret': client_secret}
|
||||
post = jsontools.dump(post)
|
||||
data = httptools.downloadpage(url, post=post, headers=headers).data
|
||||
data = jsontools.load(data)
|
||||
if "access_token" in data:
|
||||
if 'access_token' in data:
|
||||
# Código introducido, salimos del bucle
|
||||
break
|
||||
except:
|
||||
@@ -99,16 +100,16 @@ def token_trakt(item):
|
||||
except:
|
||||
pass
|
||||
|
||||
token = data["access_token"]
|
||||
refresh = data["refresh_token"]
|
||||
token = data['access_token']
|
||||
refresh = data['refresh_token']
|
||||
|
||||
config.set_setting("token_trakt", token, "trakt")
|
||||
config.set_setting("refresh_token_trakt", refresh, "trakt")
|
||||
config.set_setting('token_trakt', token, 'trakt')
|
||||
config.set_setting('refresh_token_trakt', refresh, 'trakt')
|
||||
if not item.folder:
|
||||
platformtools.dialog_notification(config.get_localized_string(60255), config.get_localized_string(60256))
|
||||
if config.is_xbmc():
|
||||
import xbmc
|
||||
xbmc.executebuiltin("Container.Refresh")
|
||||
xbmc.executebuiltin('Container.Refresh')
|
||||
return
|
||||
|
||||
except:
|
||||
@@ -116,13 +117,13 @@ def token_trakt(item):
|
||||
logger.error(traceback.format_exc())
|
||||
if not item.folder:
|
||||
return platformtools.dialog_notification(config.get_localized_string(60527), config.get_localized_string(60258))
|
||||
token = ""
|
||||
token = ''
|
||||
|
||||
itemlist = []
|
||||
if token:
|
||||
itemlist.append(item.clone(title=config.get_localized_string(60256), action=""))
|
||||
itemlist.append(item.clone(title=config.get_localized_string(60256), action=''))
|
||||
else:
|
||||
itemlist.append(item.clone(title=config.get_localized_string(60260), action=""))
|
||||
itemlist.append(item.clone(title=config.get_localized_string(60260), action=''))
|
||||
|
||||
return itemlist
|
||||
|
||||
@@ -138,34 +139,35 @@ def set_trakt_info(item):
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
def get_trakt_watched(id_type, mediatype, update=False):
|
||||
logger.debug()
|
||||
|
||||
id_list = []
|
||||
id_dict = dict()
|
||||
|
||||
token_auth = config.get_setting("token_trakt", "trakt")
|
||||
token_auth = config.get_setting('token_trakt', 'trakt')
|
||||
|
||||
if token_auth:
|
||||
sync_path = os.path.join(config.get_data_path(), 'settings_channels', 'trakt')
|
||||
|
||||
if os.path.exists(sync_path) and not update:
|
||||
trakt_node = jsontools.get_node_from_file('trakt', "TRAKT")
|
||||
trakt_node = jsontools.get_node_from_file('trakt', 'TRAKT')
|
||||
if mediatype == 'shows':
|
||||
return trakt_node['shows']
|
||||
if mediatype == 'movies':
|
||||
return trakt_node['movies']
|
||||
|
||||
else:
|
||||
token_auth = config.get_setting("token_trakt", "trakt")
|
||||
token_auth = config.get_setting('token_trakt', 'trakt')
|
||||
if token_auth:
|
||||
try:
|
||||
token_auth = config.get_setting("token_trakt", "trakt")
|
||||
token_auth = config.get_setting('token_trakt', 'trakt')
|
||||
headers = [['Content-Type', 'application/json'], ['trakt-api-key', client_id],
|
||||
['trakt-api-version', '2']]
|
||||
if token_auth:
|
||||
headers.append(['Authorization', "Bearer %s" % token_auth])
|
||||
url = "https://api.trakt.tv/sync/watched/%s" % mediatype
|
||||
headers.append(['Authorization', 'Bearer ' + token_auth])
|
||||
url = host + '/sync/watched/' + mediatype
|
||||
data = httptools.downloadpage(url, headers=headers).data
|
||||
watched_dict = jsontools.load(data)
|
||||
|
||||
@@ -233,7 +235,7 @@ def get_sync_from_file():
|
||||
sync_path = os.path.join(config.get_data_path(), 'settings_channels', 'trakt_data.json')
|
||||
trakt_node = {}
|
||||
if os.path.exists(sync_path):
|
||||
trakt_node = jsontools.get_node_from_file('trakt', "TRAKT")
|
||||
trakt_node = jsontools.get_node_from_file('trakt', 'TRAKT')
|
||||
|
||||
trakt_node['movies'] = get_trakt_watched('tmdb', 'movies')
|
||||
trakt_node['shows'] = get_trakt_watched('tmdb', 'shows')
|
||||
@@ -245,7 +247,7 @@ def update_trakt_data(mediatype, trakt_data):
|
||||
|
||||
sync_path = os.path.join(config.get_data_path(), 'settings_channels', 'trakt_data.json')
|
||||
if os.path.exists(sync_path):
|
||||
trakt_node = jsontools.get_node_from_file('trakt', "TRAKT")
|
||||
trakt_node = jsontools.get_node_from_file('trakt', 'TRAKT')
|
||||
trakt_node[mediatype] = trakt_data
|
||||
jsontools.update_node(trakt_node, 'trakt', 'TRAKT')
|
||||
|
||||
@@ -257,7 +259,7 @@ def ask_install_script():
|
||||
|
||||
respuesta = platformtools.dialog_yesno(config.get_localized_string(20000), config.get_localized_string(70521))
|
||||
if respuesta:
|
||||
xbmc.executebuiltin("InstallAddon(script.trakt)")
|
||||
xbmc.executebuiltin('InstallAddon(script.trakt)')
|
||||
return
|
||||
else:
|
||||
config.set_setting('install_trakt', False)
|
||||
@@ -283,3 +285,71 @@ def update_all():
|
||||
trakt_data = get_trakt_watched('tmdb', mediatype, True)
|
||||
update_trakt_data(mediatype, trakt_data)
|
||||
|
||||
|
||||
def context(item):
|
||||
Type = item.contentType.replace("tv", "") + "s"
|
||||
item.action = 'traktResults'
|
||||
title = config.get_localized_string(30122 if item.contentType == 'movie' else 30123)
|
||||
context = []
|
||||
commands = []
|
||||
condition = "'tmdb': " + item.infoLabels["tmdb_id"]
|
||||
try:
|
||||
result = execute(item.clone(url="/sync/watched/" + Type))
|
||||
post = {Type: [{"ids": {"tmdb": item.infoLabels["tmdb_id"]}}]}
|
||||
if condition in str(result):
|
||||
context.append(config.get_localized_string(60016 if item.contentType == 'movie' else 60020))
|
||||
commands.append(item.clone(url="/sync/history/remove", post=post))
|
||||
else:
|
||||
context.append(config.get_localized_string(60017 if item.contentType == 'movie' else 60021))
|
||||
commands.append(item.clone(url="/sync/history", post=post))
|
||||
except:
|
||||
pass
|
||||
|
||||
try:
|
||||
from core.support import dbg;dbg()
|
||||
result = execute(item.clone(url="/sync/watchlist/" + Type))
|
||||
post = {Type: [{"ids": {"tmdb": item.infoLabels["tmdb_id"]}}]}
|
||||
if condition in str(result):
|
||||
context.append(config.get_localized_string(70343) % title)
|
||||
commands.append(item.clone(url="/sync/watchlist/remove", post=post))
|
||||
else:
|
||||
context.append(config.get_localized_string(70344) % title)
|
||||
commands.append(item.clone(url="/sync/watchlist", post=post))
|
||||
except:
|
||||
pass
|
||||
|
||||
try:
|
||||
result = execute(item.clone(url="/sync/collection/" + Type))
|
||||
post = {Type: [{"ids": {"tmdb": item.infoLabels["tmdb_id"]}}]}
|
||||
if condition in str(result):
|
||||
context.append(config.get_localized_string(70345) % title)
|
||||
commands.append(item.clone(url="/sync/collection/remove", post=post))
|
||||
else:
|
||||
context.append(config.get_localized_string(70346) % title)
|
||||
commands.append(item.clone(url="/sync/collection", post=post))
|
||||
except:
|
||||
pass
|
||||
|
||||
if context:
|
||||
import xbmcgui
|
||||
index = xbmcgui.Dialog().contextmenu(context)
|
||||
if index > -1:
|
||||
execute(commands[index])
|
||||
|
||||
def execute(item):
|
||||
from platformcode.platformtools import dialog_notification
|
||||
url = host + item.url
|
||||
|
||||
headers = [['Content-Type', 'application/json'], ['trakt-api-key', client_id], ['trakt-api-version', '2']]
|
||||
if token_auth: headers.append(['Authorization', 'Bearer {}'.format(token_auth)])
|
||||
|
||||
post = None
|
||||
if item.post: post = jsontools.dump(item.post)
|
||||
|
||||
data = httptools.downloadpage(url, post=post, headers=headers).json
|
||||
|
||||
if not post:
|
||||
return data
|
||||
else:
|
||||
if 'not_found' in data: return dialog_notification('Trakt', config.get_localized_string(70347))
|
||||
else: return dialog_notification('Trakt', config.get_localized_string(70348))
|
||||
Reference in New Issue
Block a user