1212import xbmc
1313import interface
1414import httplib
15+ import xbmcgui
1516
1617__addon__ = interface .__addon__
1718def getstr (strid ): return interface .getstr (strid )
@@ -32,20 +33,20 @@ def getstr(strid): return interface.getstr(strid)
3233 xbmc .log ("APIKEY: {}" .format (APIKEY ))
3334ATOKEN = 0 #Get atoken from file
3435headers = {"Content-Type" : "application-json" ,
35- "simkl-api-key" : APIKEY
36- }
36+ "simkl-api-key" : APIKEY }
3737
3838class API :
3939 def __init__ (self ):
40+ self .scrobbled_dict = {} #So it doesn't scrobble 5 times the same chapter
41+ #{"Label":expiration_time}
4042 with open (USERFILE , "r" ) as f :
4143 self .token = f .readline ().strip ("\n " )
4244 headers ["authorization" ] = "Bearer " + self .token
43- self .scrobbled_dict = {} #So it doesn't scrobble 5 times the same chapter
44- #{"Label":expiration_time}
4545 try :
4646 self .con = httplib .HTTPSConnection ("api.simkl.com" )
4747 self .con .request ("GET" , "/users/settings" , headers = headers )
4848 self .USERSETTINGS = json .loads (self .con .getresponse ().read ().decode ("utf-8" ))
49+ xbmc .log ("Simkl: " + str (self .USERSETTINGS ))
4950 self .internet = True
5051 if not os .path .exists (USERFILE ):
5152 api .login ()
@@ -63,9 +64,20 @@ def login(self):
6364 r = log .getresponse ().read ().decode ("utf-8" )
6465 xbmc .log (r )
6566 rdic = json .loads (r )
66- dialog = interface .loginDialog (rdic ["verification_url" ],
67- rdic ["user_code" ], self .check_login , log , rdic ["expires_in" ],
68- rdic ["interval" ], self )
67+ #interface.loginDialog(rdic["verification_url"],
68+ # rdic["user_code"], self.check_login, log, rdic["expires_in"],
69+ # rdic["interval"], self)
70+
71+ pin = rdic ["user_code" ]
72+ url = rdic ["verification_url" ]
73+ exp = int (rdic ["expires_in" ])
74+ ntv = int (rdic ["interval" ])
75+
76+ self .logindialog = interface .loginDialog ("simkl-LoginDialog.xml" ,
77+ __addon__ .getAddonInfo ("path" ), pin = pin , url = url ,
78+ check_login = self .check_login , log = log , exp = exp , inter = ntv , api = self )
79+ self .logindialog .doModal ()
80+ del self .logindialog
6981
7082 def set_atoken (self , token ):
7183 global ATOKEN
@@ -85,13 +97,15 @@ def check_login(self, ucode, log): #Log is the connection
8597 log .request ("GET" , "/users/settings" , headers = headers )
8698 r = json .loads (log .getresponse ().read ().decode ("utf-8" ))
8799 self .USERSETTINGS = r
88- xbmc .log (str (self .USERSETTINGS ))
89100 return True
90101 elif r ["result" ] == "KO" :
91102 return False
92103
93104 def is_user_logged (self ):
94- if self .token == "" :
105+ """ Checks if user is logged in """
106+ failed = False
107+ if "error" in self .USERSETTINGS .keys (): failed = self .USERSETTINGS ["error" ]
108+ if self .token == "" or failed == "user_token_failed" :
95109 xbmc .log ("Simkl: User not logged in" )
96110 return False
97111 else :
@@ -121,17 +135,20 @@ def watched(self, filename, mediatype, duration, date=time.strftime('%Y-%m-%d %H
121135 mediadict = {"movie" : "movies" , "episode" :"episodes" }
122136
123137 if filename [:2 ] == "tt" :
124- imdb = filename
125138 toappend = {"ids" :{"imdb" :filename }, "watched_at" :date }
126139 media = mediadict [mediatype ]
127140 else :
128141 xbmc .log ("Simkl: Filename - {}" .format (filename ))
129142 values = {"file" :filename }
130143 values = json .dumps (values )
144+ xbmc .log ("Simkl: Getting info about: " .format (values ))
131145 con .request ("GET" , "/search/file/" , body = values , headers = headers )
132146 r1 = con .getresponse ().read ().decode ("utf-8" )
133147 r = json .loads (r1 )
134- xbmc .log ("Simkl: {}" .format (r ))
148+ if r == []:
149+ xbmc .log ("Simkl: Couldn't scrobble: Null Response" )
150+ return 0
151+ xbmc .log ("Simkl: Response: {}" .format (r ))
135152 media = mediadict [r ["type" ]]
136153 toappend = {"ids" : r [r ["type" ]]["ids" ], "watched_at" :date }
137154
0 commit comments