api.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # -*- coding: utf-8 -*-
  2. from consts import *
  3. from utils import *
  4. class __api__(object):
  5. @staticmethod
  6. def __check_hash__(__json__):
  7. if __json__["error"]:
  8. if __json__["errno"] == -14:
  9. output("[API] Need to login")
  10. clear_session()
  11. return False
  12. elif __json__["errno"] in (-1, -2, -3, -8):
  13. hide_busy_dialog()
  14. output("[API] Version not valid, err code %s" % __json__["errno"])
  15. clear_session()
  16. hide_busy_dialog()
  17. xbmc.executebuiltin('Container.Update("library://video/addons.xml/",replace)')
  18. #xbmc.executebuiltin("ActivateWindow(Home)")
  19. PLUGIN.dialog(u"Leider ist die Version [B]%s[/B] ist nicht gültig!" % PLUGIN_VERSION)
  20. sys.exit(1)
  21. return False
  22. else:
  23. output("[API] ErrorCode: %s" % __json__["errno"])
  24. return True
  25. @staticmethod
  26. def __login__(__auto_login__=True):
  27. global PORTAL_USERID, PORTAL_SESSION
  28. PORTAL_USERID, PORTAL_SESSION = load_session()
  29. if PORTAL_USERID:
  30. return True
  31. else:
  32. if not __auto_login__:
  33. clear_session()
  34. return False
  35. request = __ApiRequest__("auth", __data__={
  36. "username": PORTAL_USERNAME,
  37. "password": PORTAL_PASSWORD
  38. })
  39. try:
  40. response = request.__execute__()
  41. except urllib_request.HTTPError as e:
  42. response = e.read()
  43. except Exception as e:
  44. output("[API] Login Error: %s " % repr(e))
  45. return False
  46. if not response:
  47. output("[API] No response")
  48. return False
  49. json = None
  50. try:
  51. json = string_to_json(response)
  52. except Exception as e:
  53. output("[API] Error: %s " % repr(e))
  54. return False
  55. if json["error"]:
  56. xbmc.log("GOT ERROR! %s" % str(json))
  57. if not __api__.__check_hash__(json) and __auto_login__:
  58. return __api__.__login__(False)
  59. output("[API] Error: %s" % json["error"])
  60. return False
  61. #ifdef DEBUG
  62. if DEBUG:
  63. PLUGIN.notify("Erneut eingelogt...")
  64. #endif
  65. PORTAL_USERID = int(json["data"]["userid"])
  66. PORTAL_SESSION = json["data"]["sessionhash"]
  67. save_session(PORTAL_USERID, PORTAL_SESSION)
  68. return True
  69. @staticmethod
  70. def __get__(__endpoint__, __data__=None, __first__=True):
  71. """
  72. @param __endpoint__:
  73. @param __data__:
  74. @param __first__:
  75. @return:
  76. """
  77. global PORTAL_USERID, PORTAL_SESSION
  78. PORTAL_USERID, PORTAL_SESSION = load_session()
  79. if __data__ is None:
  80. __data__ = {}
  81. if not PORTAL_USERID:
  82. if not __api__.__login__():
  83. return False
  84. output("[API] UserID: %s" % PORTAL_USERID)
  85. #ifdef DEBUG
  86. output("[API] Session: %s" % PORTAL_SESSION)
  87. #endif
  88. request = __ApiRequest__(__endpoint__, __data__=__data__)
  89. response = None
  90. try:
  91. response = request.__execute__()
  92. except urllib_request.HTTPError as e:
  93. response = e.read()
  94. except:
  95. return False
  96. if not response:
  97. return False
  98. json = None
  99. try:
  100. json = string_to_json(response)
  101. except Exception as e:
  102. output("[API] Error: %s " % repr(e))
  103. return False
  104. if json["error"]:
  105. if not __api__.__check_hash__(json) and __first__:
  106. return __api__.__get__(__endpoint__, __data__, False)
  107. output("[API] Error: %s" % json["errno"])
  108. return False
  109. return json
  110. @staticmethod
  111. def __url__(__endpoint__, __data__=None):
  112. if __data__ is None:
  113. __data__ = {}
  114. if not PORTAL_USERID:
  115. if not __api__.__login__():
  116. return None
  117. request = __ApiRequest__(__endpoint__, __data__=__data__)
  118. return request._request.get_full_url()