123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- # -*- coding: utf-8 -*-
- from consts import *
- from utils import *
- class __api__(object):
- @staticmethod
- def __check_hash__(__json__):
- if __json__["error"]:
- if __json__["errno"] == -14:
- output("[API] Need to login")
- clear_session()
- return False
- elif __json__["errno"] in (-1, -2, -3, -8):
- hide_busy_dialog()
- output("[API] Version not valid, err code %s" % __json__["errno"])
- clear_session()
- hide_busy_dialog()
- xbmc.executebuiltin('Container.Update("library://video/addons.xml/",replace)')
- #xbmc.executebuiltin("ActivateWindow(Home)")
- PLUGIN.dialog(u"Leider ist die Version [B]%s[/B] ist nicht gültig!" % PLUGIN_VERSION)
- sys.exit(1)
- return False
- else:
- output("[API] ErrorCode: %s" % __json__["errno"])
- return True
- @staticmethod
- def __login__(__auto_login__=True):
- global PORTAL_USERID, PORTAL_SESSION
- PORTAL_USERID, PORTAL_SESSION = load_session()
- if PORTAL_USERID:
- return True
- else:
- if not __auto_login__:
- clear_session()
- return False
- request = __ApiRequest__("auth", __data__={
- "username": PORTAL_USERNAME,
- "password": PORTAL_PASSWORD
- })
- try:
- response = request.__execute__()
- except urllib_request.HTTPError as e:
- response = e.read()
- except Exception as e:
- output("[API] Login Error: %s " % repr(e))
- return False
- if not response:
- output("[API] No response")
- return False
- json = None
- try:
- json = string_to_json(response)
- except Exception as e:
- output("[API] Error: %s " % repr(e))
- return False
- if json["error"]:
- xbmc.log("GOT ERROR! %s" % str(json))
- if not __api__.__check_hash__(json) and __auto_login__:
- return __api__.__login__(False)
- output("[API] Error: %s" % json["error"])
- return False
- #ifdef DEBUG
- if DEBUG:
- PLUGIN.notify("Erneut eingelogt...")
- #endif
- PORTAL_USERID = int(json["data"]["userid"])
- PORTAL_SESSION = json["data"]["sessionhash"]
- save_session(PORTAL_USERID, PORTAL_SESSION)
- return True
- @staticmethod
- def __get__(__endpoint__, __data__=None, __first__=True):
- """
- @param __endpoint__:
- @param __data__:
- @param __first__:
- @return:
- """
- global PORTAL_USERID, PORTAL_SESSION
- PORTAL_USERID, PORTAL_SESSION = load_session()
- if __data__ is None:
- __data__ = {}
- if not PORTAL_USERID:
- if not __api__.__login__():
- return False
- output("[API] UserID: %s" % PORTAL_USERID)
- #ifdef DEBUG
- output("[API] Session: %s" % PORTAL_SESSION)
- #endif
- request = __ApiRequest__(__endpoint__, __data__=__data__)
- response = None
- try:
- response = request.__execute__()
- except urllib_request.HTTPError as e:
- response = e.read()
- except:
- return False
- if not response:
- return False
- json = None
- try:
- json = string_to_json(response)
- except Exception as e:
- output("[API] Error: %s " % repr(e))
- return False
- if json["error"]:
- if not __api__.__check_hash__(json) and __first__:
- return __api__.__get__(__endpoint__, __data__, False)
- output("[API] Error: %s" % json["errno"])
- return False
- return json
- @staticmethod
- def __url__(__endpoint__, __data__=None):
- if __data__ is None:
- __data__ = {}
- if not PORTAL_USERID:
- if not __api__.__login__():
- return None
- request = __ApiRequest__(__endpoint__, __data__=__data__)
- return request._request.get_full_url()
|