-
Notifications
You must be signed in to change notification settings - Fork 29
/
program.py
367 lines (348 loc) · 16.3 KB
/
program.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import re
import sys
import threading
import xbmc
import xbmcgui
import xbmcaddon
import unicodedata
import json
from resources.lib.tvtime import GetCode
from resources.lib.tvtime import Authorize
from resources.lib.tvtime import IsChecked
from resources.lib.tvtime import MarkAsWatched
from resources.lib.tvtime import MarkAsUnWatched
from resources.lib.tvtime import GetUserInformations
from resources.lib.tvtime import SaveShowProgress
from resources.lib.tvtime import SaveShowsProgress
from resources.lib.tvtime import Show
from resources.lib.tvtime import GetLibrary
from resources.lib.tvtime import DeleteShowProgress
from resources.lib.tvtime import DeleteShowsProgress
from resources.lib.tvtime import FollowShows
__addon__ = xbmcaddon.Addon()
__cwd__ = __addon__.getAddonInfo('path')
__icon__ = __addon__.getAddonInfo('icon')
__scriptname__ = __addon__.getAddonInfo('name')
__version__ = __addon__.getAddonInfo('version')
__language__ = __addon__.getLocalizedString
__resource_path__ = os.path.join(__cwd__, 'resources', 'lib')
__resource__ = xbmc.translatePath(__resource_path__).decode('utf-8')
__token__ = __addon__.getSetting('token')
__facebook__ = __addon__.getSetting('facebook')
__twitter__ = __addon__.getSetting('twitter')
__notifications__ = __addon__.getSetting('notifications')
__welcome__ = __addon__.getSetting('welcome')
def start():
menuitems = []
if __token__ is '':
menuitems.append(__language__(33801))
else:
menuitems.append(__language__(33803))
menuitems.append(__language__(33802))
startmenu = xbmcgui.Dialog().select(__scriptname__, menuitems)
if startmenu < 0: return
elif startmenu == 0 and __token__ is '':
_login = GetCode()
if _login.is_code:
Authorization(_login.verification_url, _login.user_code, _login.device_code)
else:
xbmcgui.Dialog().ok(__scriptname__, __language__(33804))
elif startmenu == 1:
logout = xbmcgui.Dialog().yesno(__scriptname__, __language__(33805))
if logout == True:
__addon__.setSetting('token', '')
__addon__.setSetting('user', '')
return
start()
else:
first_step()
def Authorization(verification_url, user_code, device_code):
pDialog = xbmcgui.DialogProgress()
pDialog.create(__scriptname__, "%s: %s" % (__language__(33806), verification_url), "%s: %s" % (__language__(33807), user_code))
for i in range(0, 100):
pDialog.update(i)
xbmc.sleep(5000)
if pDialog.iscanceled(): return
_authorize = Authorize(device_code)
if _authorize.is_authorized:
__addon__.setSetting('token', _authorize.access_token)
user = GetUserInformations(_authorize.access_token)
if user.is_authenticated:
if __welcome__ == 'true':
xbmcgui.Dialog().ok(__scriptname__, '%s %s' % (__language__(32902), user.username), __language__(33808))
__addon__.setSetting('user', user.username)
return
pDialog.close()
def first_step():
which_way = xbmcgui.Dialog().select(__language__(33901), ["TV Time > Kodi", "Kodi > TV Time"])
if which_way < 0: return
scan(which_way)
return
tvshows = []
tvshowsid = []
command = '{"jsonrpc": "2.0", "method": "VideoLibrary.GetTVShows", "params": {"sort": { "order": "ascending", "method": "label" }}, "id": 1}'
result = json.loads(xbmc.executeJSONRPC(command))
for i in range(0, result['result']['limits']['total']):
tvshows.append(result['result']['tvshows'][i]['label'])
tvshowsid.append(result['result']['tvshows'][i]['tvshowid'])
tvshows.insert(0, __language__(33902))
tvshowsid.insert(0, "0")
whattvshow = xbmcgui.Dialog().select(__language__(33903), tvshows)
if whattvshow < 0: return
elif whattvshow == 0:
scan(which_way)
else:
seasons = []
command = '{"jsonrpc": "2.0", "method": "VideoLibrary.GetEpisodes", "params":{"tvshowid": %s, "properties": ["season"], "sort": { "order": "ascending", "method": "season" }}, "id": 1}' % tvshowsid[whattvshow]
result = json.loads(xbmc.executeJSONRPC(command))
for i in range(0, result['result']['limits']['total']):
seasons.append(str(result['result']['episodes'][i]['season']))
seasons = remove_duplicates(seasons)
seasons.insert(0, __language__(33904))
whatseason = xbmcgui.Dialog().select(__language__(33905), seasons)
if whatseason < 0: return
elif whatseason == 0:
scan(which_way, tvshowsid[whattvshow])
else:
scan(which_way, tvshowsid[whattvshow], whatseason)
def remove_duplicates(values):
output = []
seen = set()
for value in values:
if value not in seen:
output.append(value)
seen.add(value)
return output
def scan(way):
emotion = __addon__.getSetting('emotion')
__addon__.setSetting('emotion', 'false')
if way == 1:
log('Kodi > TV Time')
pDialog = xbmcgui.DialogProgressBG()
pDialog.create('Kodi > TV Time', __language__(33906))
pDialog.update(0, message=__language__(33906))
tvshowsList = getTvshowList()
showsSeen = []
showsNotSeen = []
cpt = 0
pc = 0
total = len(tvshowsList)
for tvshowList in tvshowsList:
cpt = cpt + 1
pc = (cpt*100)/total
pDialog.update(pc, message=tvshowList['title'])
xbmc.sleep(100)
if tvshowList['seen'] == 1:
showsSeen.append({
'show_id': int(tvshowList['show_id']),
'season': tvshowList['season'],
'episode': tvshowList['episode']
})
elif tvshowList['seen'] == 0 and tvshowList['season'] == 1 and tvshowList['episode'] == 1:
showsNotSeen.append({
'show_id': int(tvshowList['show_id'])
})
cpt = 1
pc = 0
limit = 50
total = int(len(showsSeen))+int(len(showsNotSeen))
if len(showsSeen):
tempShowsSeen = []
tempcpt = 1
pDialog.update(0, message=__language__(33908))
for showSeen in showsSeen:
cpt = cpt + 1
pc = (cpt*100)/total
tempShowsSeen.append({
'show_id': int(showSeen['show_id']),
'season': showSeen['season'],
'episode': showSeen['episode']
})
tempcpt = tempcpt + 1
if tempcpt >= limit:
log("SaveShowsProgress(*, %s)" % tempShowsSeen)
show_progress = SaveShowsProgress(__token__, json.dumps(tempShowsSeen))
log("show_progress.is_set=%s" % show_progress.is_set)
tempShowsSeen = []
tempcpt = 0
pDialog.update(pc, message=__language__(33908))
if tempShowsSeen:
log("SaveShowsProgress(*, %s)" % tempShowsSeen)
show_progress = SaveShowsProgress(__token__, json.dumps(tempShowsSeen))
log("show_progress.is_set=%s" % show_progress.is_set)
if len(showsNotSeen):
tempShowsNotSeen = []
tempcpt = 0
pDialog.update(0, message=__language__(33908))
for showNotSeen in showsNotSeen:
cpt = cpt + 1
pc = (cpt*100)/total
tempShowsNotSeen.append({
'show_id': int(showNotSeen['show_id'])
})
tempcpt = tempcpt + 1
if tempcpt >= limit:
log("DeleteShowsProgress(*, %s)" % tempShowsNotSeen)
show_progress = DeleteShowsProgress(__token__, json.dumps(tempShowsNotSeen))
log("show_progress.is_delete=%s" % show_progress.is_delete)
tempShowsNotSeen = []
tempcpt = 0
cpt = cpt +1
pDialog.update(pc, message=__language__(33908))
if tempShowsNotSeen:
log("DeleteShowsProgress(*, %s)" % tempShowsNotSeen)
show_progress = DeleteShowsProgress(__token__, json.dumps(tempShowsNotSeen))
log("show_progress.is_delete=%s" % show_progress.is_delete)
follow_shows = FollowShows(__token__, json.dumps(tempShowsNotSeen))
log("follow_shows.is_follow=%s" % follow_shows.is_follow)
pDialog.update(100, message=__language__(33907))
xbmcgui.Dialog().ok("Kodi > TV Time", __language__(33907))
else:
log('TV Time > Kodi')
pDialog = xbmcgui.DialogProgressBG()
pDialog.create('TV Time > Kodi', __language__(33906))
pDialog.update(0, message=__language__(33906))
tvshowList = getTvshowList()
tvshowTimeList = getTvshowTimeList()
if tvshowList and tvshowTimeList:
total = len(tvshowList)
for i in range(0, total):
for tvshowTime in tvshowTimeList:
if int(tvshowList[i]['show_id']) == int(tvshowTime['show_id']):
pDialog.update(((100/total)*(i+1)), message=tvshowTime['title'])
log('setTvshowProgress(%s, %s, %s)' % (tvshowTime['show_id'], tvshowTime['season'], tvshowTime['episode']))
tvshowProgress = setTvshowProgress(tvshowTime['show_id'], tvshowTime['season'], tvshowTime['episode'])
pDialog.update(100, message=__language__(33907))
xbmcgui.Dialog().ok("TV Time > Kodi", __language__(33907))
__addon__.setSetting('emotion', emotion)
pDialog.close()
def getTvshowList():
rpccmd = {'jsonrpc': '2.0', 'method': 'VideoLibrary.GetTVShows', 'params': { 'properties': ['title', 'imdbnumber'] }, 'id': 'libTvShows'}
rpccmd = json.dumps(rpccmd)
result = xbmc.executeJSONRPC(rpccmd)
tvshows = json.loads(result)
log('tvshows=%s' % tvshows)
tvshowList = []
if tvshows.has_key('result') and tvshows['result'] != None and tvshows['result'].has_key('tvshows'):
tvshows = tvshows['result']['tvshows']
for tvshow in tvshows:
rpccmd = {'jsonrpc': '2.0', 'method': 'VideoLibrary.GetEpisodes', 'params': {'tvshowid': tvshow['tvshowid'], 'properties': ['season', 'episode', 'playcount']}, 'id': 1}
rpccmd = json.dumps(rpccmd)
result = xbmc.executeJSONRPC(rpccmd)
episodes = json.loads(result)
log('tvshow=%s [%s]' % (tvshow['title'], tvshow['imdbnumber']))
log('episodes[%s]=%s' % (tvshow['imdbnumber'], episodes))
if episodes.has_key('result') and episodes['result'] != None and episodes['result'].has_key('episodes'):
episodes = episodes['result']['episodes']
lastEpisode = None
lastSeasonNr = 0
lastEpisodeNr = 0
firstEpisode = None
firstSeasonNr = 2
firstEpisodeNr = 2
for episode in episodes:
if episode['playcount'] >= 1:
if (episode['season'] > lastSeasonNr):
lastSeasonNr = episode['season']
lastEpisodeNr = episode['episode']
lastEpisode = episode
elif (episode['season'] == lastSeasonNr and episode['episode'] > lastEpisodeNr):
lastEpisodeNr = episode['episode']
lastEpisode = episode
if (episode['season'] < firstSeasonNr):
firstSeasonNr = episode['season']
firstEpisodeNr = episode['episode']
firstEpisode = episode
elif (episode['season'] == firstSeasonNr and episode['episode'] < firstEpisodeNr):
firstEpisodeNr = episode['episode']
firstEpisode = episode
if lastEpisode != None:
tvshowList.append({
'seen': 1,
'title': tvshow['title'],
'show_id': tvshow['imdbnumber'],
'season': lastEpisode['season'],
'episode': lastEpisode['episode']
})
log('dernier_vu=s%.2de%.2d' % (int(lastEpisode['season']), int(lastEpisode['episode'])))
elif firstEpisode != None:
tvshowList.append({
'seen': 0,
'title': tvshow['title'],
'show_id': tvshow['imdbnumber'],
'season': firstEpisode['season'],
'episode': firstEpisode['episode']
})
log('premier_non_vu=s%.2de%.2d' % (int(firstEpisode['season']), int(firstEpisode['episode'])))
log('list=%s' % tvshowList)
return tvshowList
def getTvshowTimeList():
tvshowTimeList = []
log('shows=Library(*, 0, 999999)')
shows = GetLibrary(__token__, 0, 999999)
for show in shows.shows:
if show['last_seen'] != None:
tvshowTimeList.append({
'title': show['name'],
'show_id': show['id'],
'season': show['last_seen']['season_number'],
'episode': show['last_seen']['number']
})
log('list=%s' % tvshowTimeList)
return tvshowTimeList
def setTvshowProgress(show_id, last_season_seen, last_episode_seen):
rpccmd = {'jsonrpc': '2.0', 'method': 'VideoLibrary.GetTVShows', 'params': { 'properties': ['title', 'imdbnumber'] }, 'id': 'libTvShows'}
rpccmd = json.dumps(rpccmd)
result = xbmc.executeJSONRPC(rpccmd)
tvshows = json.loads(result)
log('tvshows=%s' % tvshows)
if tvshows['result']['limits']['total'] == 0:
return
tvshows = tvshows['result']['tvshows']
for tvshow in tvshows:
if int(tvshow['imdbnumber']) == int(show_id):
log('tvshow=%s' % tvshow)
rpccmd = {'jsonrpc': '2.0', 'method': 'VideoLibrary.GetEpisodes', 'params': {'tvshowid': tvshow['tvshowid'], 'properties': ['title', 'season', 'episode']}, 'id': 1}
rpccmd = json.dumps(rpccmd)
result = xbmc.executeJSONRPC(rpccmd)
episodes = json.loads(result)
log('episodes=%s' % episodes)
if episodes['result']['limits']['total'] > 0:
episodes = episodes['result']['episodes']
for episode in episodes:
log('episode=%s' % episode)
if (episode['season'] < last_season_seen or (episode['season'] == last_season_seen and episode['episode'] <= last_episode_seen)):
command2 = '{"jsonrpc": "2.0", "id": 1, "method": "VideoLibrary.SetEpisodeDetails", "params": {"episodeid" : %s, "playcount": %s}}' % (episode['episodeid'], 1)
result2 = json.loads(xbmc.executeJSONRPC(command2))
log('watched=%s' % 1)
else:
command2 = '{"jsonrpc": "2.0", "id": 1, "method": "VideoLibrary.SetEpisodeDetails", "params": {"episodeid" : %s, "playcount": %s}}' % (episode['episodeid'], 0)
result2 = json.loads(xbmc.executeJSONRPC(command2))
log('watched=%s' % 0)
def formatNumber(number):
if len(number) < 2:
number = '0%s' % number
return number
def formatName(filename):
filename = filename.strip()
filename = filename.replace(' ', '.')
return normalizeString(filename)
def notif(msg, time=5000):
if __notifications__ == 'true':
xbmcgui.Dialog().notification(encode(__scriptname__), encode(msg), time=time, icon=__icon__)
def log(msg):
xbmc.log("### [%s] - %s" % (__scriptname__, encode(msg), ),
level=xbmc.LOGDEBUG) #100 #xbmc.LOGDEBUG
def encode(string):
result = ''
try:
result = string.encode('UTF-8','replace')
except UnicodeDecodeError:
result = 'UTF-8 Error'
return result
def normalizeString(str):
return unicodedata.normalize('NFKD', str).encode('ascii','ignore').encode('UTF-8','replace')
start()