Skip to content

Commit d735e1a

Browse files
committed
New login dialog
1 parent 3105f5d commit d735e1a

File tree

7 files changed

+97
-17
lines changed

7 files changed

+97
-17
lines changed

resources/language/English/strings.po

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ msgid "Simkl login"
4949
msgstr ""
5050

5151
msgctxt "#32022"
52-
msgid "Enter to the following URL: {}"
52+
msgid "Visit {} on your device and enter this code"
5353
msgstr ""
5454

5555
msgctxt "#32023"

resources/lib/engine.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def synclibrary(self):
4444
}))
4545
xbmc.log("Simkl: Ret: {}".format(kodilibrary))
4646
kodilibrary = json.loads(kodilibrary)
47-
47+
4848
if kodilibrary["result"]["limits"]["total"] > 0:
4949
for movie in kodilibrary["result"]["movies"]:
5050
#Dont do that, upload all at once
@@ -84,6 +84,7 @@ def onPlayBackEnded(self):
8484
xbmc.log("Simkl: ONPLAYBACKENDED")
8585
self.onPlayBackStopped()
8686
def onPlayBackStopped(self):
87+
'''Gets the info needed to pass to the api'''
8788
try:
8889
movie = self.getVideoInfoTag()
8990
imdb = movie.getIMDBNumber().strip(" ")

resources/lib/interface.py

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,73 @@ def getstr(strid): return __addon__.getLocalizedString(strid)
1414
def notify(txt="Test"):
1515
not_dialog.notification("Simkl", txt, __icon__) #Put an icon
1616

17-
#import simklapi
17+
PIN_LABEL = 201
18+
INSTRUCTION_ID = 202
19+
CANCEL_BUTTON = 203
1820

21+
#xmlfile = ""
22+
#script = __addon__.getAddonInfo("path").decode("utf-8")
23+
class loginDialog(xbmcgui.WindowXMLDialog):
24+
def __init__(self, xmlFilename, scriptPath, pin, url, check_login, log,
25+
exp=900, inter=5, api=None):
26+
self.pin = pin
27+
self.url = url
28+
self.check_login = check_login
29+
self.log = log
30+
self.exp = exp
31+
self.inter = inter
32+
self.api = api
33+
self.waiting = True
34+
self.canceled = False
35+
36+
def onInit(self):
37+
instruction = self.getControl(INSTRUCTION_ID)
38+
instruction.setLabel(
39+
getstr(32022).format("[COLOR yellow]" + self.url + "[/COLOR]"))
40+
self.getControl(PIN_LABEL).setLabel(self.pin)
41+
42+
cnt = 0
43+
while self.waiting:
44+
if cnt % (self.inter+1) == 0 and cnt>1:
45+
xbmc.log("Simkl: Still waiting... {}".format(cnt))
46+
if self.check_login(self.pin, self.log):
47+
48+
xbmc.log(str(self.api.USERSETTINGS))
49+
notify("Hello {}".format(self.api.USERSETTINGS["user"]["name"]))
50+
self.waiting = False
51+
#Now check the user has done what it has to be done
52+
cnt += 1
53+
time.sleep(1)
54+
if self.canceled or cnt >= self.exp:
55+
self.waiting = False
56+
notify("Couldn't log in")
57+
58+
xbmc.log("Simkl: Stop waiting")
59+
self.close()
60+
61+
def onAction(self, action):
62+
#xbmc.log("Simkl: onaction {}".format(action))
63+
if action == True: #TODO
64+
self.close()
65+
66+
def onControl(self, controlID):
67+
pass
68+
def onFocus(self, controlID):
69+
pass
70+
71+
def onClick(self, controlID):
72+
xbmc.log("Simkl: onclick {}".format(controlID))
73+
if controlID == CANCEL_BUTTON:
74+
self.canceled = True
75+
self.close()
76+
77+
'''
1978
class loginDialog:
2079
def __init__(self, url, pin, check_login, log, exp=900, inter=5, api=None):
2180
#TODO: If user is loged in, show a confirmation dialog
2281
API = api
2382
self.dialog = xbmcgui.DialogProgress()
24-
self.dialog.create(getstr(32021),
83+
self.dialog.create(getstr(32021),
2584
getstr(32022).format(url), "PIN: {}".format(pin))
2685
waiting = True
2786
cnt = 0
@@ -43,3 +102,4 @@ def __init__(self, url, pin, check_login, log, exp=900, inter=5, api=None):
43102
if self.dialog.iscanceled() or cnt >= exp:
44103
waiting = False
45104
#raise Not logged in
105+
'''

resources/lib/simklapi.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import xbmc
1313
import interface
1414
import httplib
15+
import xbmcgui
1516

1617
__addon__ = interface.__addon__
1718
def getstr(strid): return interface.getstr(strid)
@@ -32,20 +33,20 @@ def getstr(strid): return interface.getstr(strid)
3233
xbmc.log("APIKEY: {}".format(APIKEY))
3334
ATOKEN = 0 #Get atoken from file
3435
headers = {"Content-Type": "application-json",
35-
"simkl-api-key": APIKEY
36-
}
36+
"simkl-api-key": APIKEY}
3737

3838
class 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

resources/settings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
<setting label="32006" type="slider" id="scr-pct" default="70" range="0,100" option="percent" enable="gt(-1,-2)" />
1313
<setting label="32007" type="slider" id="sct-w-pct" default="10" range="0,100" option="
1414
percent"/>
15-
<setting label="32008" type="bool" id="bubble" default="false" enable="true"/>
15+
<setting label="32008" type="bool" id="bubble" default="true" enable="true"/>
1616
</category>
1717
</settings>

script.simkl.zip

1.32 MB
Binary file not shown.

service.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ def getstr(strid): return simkl_addon.getLocalizedString(strid)
2626
player.addon = simkl_addon
2727
eng = engine.Engine(API.api, player)
2828
#Remember: if getTime() is more than x% scrobble file
29+
2930

3031
if not API.api.is_user_logged():
31-
interface.notify(getstr(32026))
32+
API.api.login() #Add "remind me tomorrow button"
33+
#interface.notify(getstr(32026))
3234
else:
3335
interface.notify(getstr(32025).format(API.api.USERSETTINGS["user"]["name"]))
3436

0 commit comments

Comments
 (0)