Skip to content

Commit 5fb04c3

Browse files
authored
try to use getUniqueID('tmdb') if possible (#21)
* context_info.py * update README.md * context_info.py less info in popup * try to use getUniqueID('tmdb') if possible * support elementum items in doTraktAction
1 parent ed666b4 commit 5fb04c3

File tree

5 files changed

+173
-95
lines changed

5 files changed

+173
-95
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
#Context Menu for Elementum
1+
# Context Menu for Elementum
2+
3+
http://elementum.surge.sh/context/
4+

addon.xml

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@
2828
</item>
2929
<item library="context_trakt.py" args="watched">
3030
<label>32018</label>
31-
<visible>Integer.IsGreater(ListItem.DBID,0)+Integer.IsEqual(ListItem.PlayCount,0)+[StringCompare(ListItem.dbtype,movie) | StringCompare(ListItem.dbtype,episode) | StringCompare(ListItem.dbtype,season) | StringCompare(ListItem.dbtype,tvshow) | String.IsEqual(ListItem.dbtype,movie) | String.IsEqual(ListItem.dbtype,episode) | String.IsEqual(ListItem.dbtype,season) | String.IsEqual(ListItem.dbtype,tvshow)]</visible>
31+
<visible>[Integer.IsGreater(ListItem.DBID,0) | Integer.IsGreater(ListItem.UniqueID(tmdb),0)]+Integer.IsEqual(ListItem.PlayCount,0)+[StringCompare(ListItem.dbtype,movie) | StringCompare(ListItem.dbtype,episode) | StringCompare(ListItem.dbtype,season) | StringCompare(ListItem.dbtype,tvshow) | String.IsEqual(ListItem.dbtype,movie) | String.IsEqual(ListItem.dbtype,episode) | String.IsEqual(ListItem.dbtype,season) | String.IsEqual(ListItem.dbtype,tvshow)]</visible>
3232
</item>
3333
<item library="context_trakt.py" args="unwatched">
3434
<label>32019</label>
35-
<visible>Integer.IsGreater(ListItem.DBID,0)+Integer.IsGreater(ListItem.PlayCount,0)+[StringCompare(ListItem.dbtype,movie) | StringCompare(ListItem.dbtype,episode) | StringCompare(ListItem.dbtype,season) | StringCompare(ListItem.dbtype,tvshow) | String.IsEqual(ListItem.dbtype,movie) | String.IsEqual(ListItem.dbtype,episode) | String.IsEqual(ListItem.dbtype,season) | String.IsEqual(ListItem.dbtype,tvshow)]</visible>
35+
<visible>[Integer.IsGreater(ListItem.DBID,0) | Integer.IsGreater(ListItem.UniqueID(tmdb),0)]+Integer.IsGreater(ListItem.PlayCount,0)+[StringCompare(ListItem.dbtype,movie) | StringCompare(ListItem.dbtype,episode) | StringCompare(ListItem.dbtype,season) | StringCompare(ListItem.dbtype,tvshow) | String.IsEqual(ListItem.dbtype,movie) | String.IsEqual(ListItem.dbtype,episode) | String.IsEqual(ListItem.dbtype,season) | String.IsEqual(ListItem.dbtype,tvshow)]</visible>
3636
</item>
37-
<item library="context_menu.py">
37+
<item library="context_info.py">
3838
<label>32001</label>
3939
<visible>System.GetBool(debug.extralogging)+[StringCompare(ListItem.dbtype,movie) | StringCompare(ListItem.dbtype,episode) | StringCompare(ListItem.dbtype,season) | StringCompare(ListItem.dbtype,tvshow) | String.IsEqual(ListItem.dbtype,movie) | String.IsEqual(ListItem.dbtype,episode) | String.IsEqual(ListItem.dbtype,season) | String.IsEqual(ListItem.dbtype,tvshow)]</visible>
4040
</item>

context_menu.py context_info.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"ListItem.TMDB",
1717
"ListItem.UniqueID(tmdb)",
1818
"ListItem.UniqueID(Elementum)",
19+
"ListItem.Property(ShowTMDBId)",
1920
"ListItem.Label",
2021
"ListItem.Label2",
2122
"ListItem.ThumbnailImage",
@@ -88,4 +89,4 @@
8889
log.info("Labels: %s" % json.dumps(all_labels["result"], indent=4))
8990

9091
log.info("truelabel: %s; infolabel: %s; dbid: %s; tmdbID: %s; mediatype: %s; path: %s;" % (truelabel, infolabel, dbid, tmdbID, mediatype, path))
91-
xbmcgui.Dialog().notification("truelabel: %s" % truelabel, "infolabel: %s; dbid: %s; tmdbID: %s; mediatype: %s; path: %s;" % (infolabel, dbid, tmdbID, mediatype, path))
92+
xbmcgui.Dialog().notification("truelabel: %s" % truelabel, "infolabel: %s; dbid: %s; tmdbID: %s; mediatype: %s;" % (infolabel, dbid, tmdbID, mediatype))

plugin.py

+163-89
Original file line numberDiff line numberDiff line change
@@ -38,45 +38,36 @@ def doAssign():
3838
except AttributeError:
3939
path = sys.listitem.getPath()
4040

41-
use_tmdb_id = False
42-
43-
if path.startswith("plugin://plugin.video.elementum"):
44-
"""plugin://plugin.video.elementum/show/1622/season/15/episode/1/links/Supernatural%20S15E01
45-
plugin://plugin.video.elementum/show/1622/season/15/episodes
46-
plugin://plugin.video.elementum/movie/628/links/Interview%20with%20the%20Vampire%20%281994%29"""
47-
use_tmdb_id = True
48-
result = re.search(r'plugin://plugin.video.elementum/[^/]+/(\d+)/.*', path)
49-
if result:
50-
tmdbID = result.group(1)
51-
else:
52-
log.error("Could not find TMDB id for %s" % path)
53-
xbmcgui.Dialog().notification(ADDON.getLocalizedString(32007), ADDON.getLocalizedString(32014), xbmcgui.NOTIFICATION_WARNING, 3000)
54-
return
41+
use_elementum_path = False
5542

56-
if mediatype == 'season':
57-
result = re.search(r'plugin://plugin.video.elementum/[^/]+/\d+/season/(\d+)/.*', path)
58-
if result:
59-
season_number = result.group(1)
60-
else:
61-
log.error("Could not find season number for %s" % path)
62-
xbmcgui.Dialog().notification(ADDON.getLocalizedString(32007), ADDON.getLocalizedString(32014), xbmcgui.NOTIFICATION_WARNING, 3000)
43+
try:
44+
tmdbID = sys.listitem.getUniqueID('tmdb')
45+
except AttributeError:
46+
tmdbID = ""
47+
48+
if tmdbID == "":
49+
if path.startswith("plugin://plugin.video.elementum"):
50+
use_elementum_path = True
51+
tmdbID = getTMDBidFromElementumPath(path)
52+
if not tmdbID:
6353
return
6454

65-
if mediatype == 'episode':
66-
result = re.search(r'plugin://plugin.video.elementum/[^/]+/\d+/season/(\d+)/episode/(\d+)/.*', path)
67-
if result:
68-
season_number = result.group(1)
69-
episode_number = result.group(2)
70-
else:
71-
log.error("Could not find season/episode number for %s" % path)
72-
xbmcgui.Dialog().notification(ADDON.getLocalizedString(32007), ADDON.getLocalizedString(32014), xbmcgui.NOTIFICATION_WARNING, 3000)
55+
if mediatype == 'season':
56+
season_number = getSeasonNumberFromElementumPath(path)
57+
if not season_number:
58+
return
59+
60+
if mediatype == 'episode':
61+
season_number = getSeasonNumberFromElementumPath(path)
62+
episode_number = getEpisodeNumberFromElementumPath(path)
63+
if not season_number or not episode_number:
64+
return
65+
else:
66+
dbid = getDbId()
67+
if not dbid.isdigit():
68+
log.error("Kodi library ID is wrong %s" % dbid)
69+
xbmcgui.Dialog().notification(ADDON.getLocalizedString(32007), ADDON.getLocalizedString(32016), xbmcgui.NOTIFICATION_WARNING, 3000)
7370
return
74-
else:
75-
dbid = getDbId()
76-
if not dbid.isdigit():
77-
log.error("Kodi library ID is wrong %s" % dbid)
78-
xbmcgui.Dialog().notification(ADDON.getLocalizedString(32007), ADDON.getLocalizedString(32016), xbmcgui.NOTIFICATION_WARNING, 3000)
79-
return
8071

8172
# we also can use plugin://plugin.video.elementum/torrents/
8273
file = xbmcgui.Dialog().browseSingle(1, ADDON.getLocalizedString(32010), 'files', '', False, False, 'plugin://plugin.video.elementum/history/')
@@ -96,24 +87,29 @@ def doAssign():
9687
xbmcgui.Dialog().notification(ADDON.getLocalizedString(32007), ADDON.getLocalizedString(32015), xbmcgui.NOTIFICATION_WARNING, 3000)
9788
return
9889

99-
if not use_tmdb_id:
100-
url = "plugin://plugin.video.elementum/context/torrents/assign/%s/kodi/%s/%s" % (torrentid, mediatype, dbid)
101-
log.info("Assigning torrent %s for: DBID=%s, MediaType=%s" % (torrentid, dbid, mediatype))
102-
else:
90+
if use_elementum_path:
10391
if mediatype == 'movie':
10492
url = "plugin://plugin.video.elementum/context/torrents/assign/%s/tmdb/%s/%s" % (torrentid, mediatype, tmdbID)
10593
elif mediatype == 'season':
10694
url = "plugin://plugin.video.elementum/context/torrents/assign/%s/tmdb/show/%s/%s/%s" % (torrentid, tmdbID, mediatype, season_number)
10795
elif mediatype == 'episode':
10896
url = "plugin://plugin.video.elementum/context/torrents/assign/%s/tmdb/show/%s/season/%s/%s/%s" % (torrentid, tmdbID, season_number, mediatype, episode_number)
10997
log.info("Assigning torrent %s for: TMDBID=%s, MediaType=%s" % (torrentid, tmdbID, mediatype))
98+
else:
99+
if tmdbID != "":
100+
url = "plugin://plugin.video.elementum/torrents/assign/%s/%s" % (torrentid, tmdbID)
101+
log.info("Assigning torrent %s for: TMDBID=%s, MediaType=%s" % (torrentid, tmdbID, mediatype))
102+
else:
103+
url = "plugin://plugin.video.elementum/context/torrents/assign/%s/kodi/%s/%s" % (torrentid, mediatype, dbid)
104+
log.info("Assigning torrent %s for: DBID=%s, MediaType=%s" % (torrentid, dbid, mediatype))
110105

111106
xbmcgui.Dialog().notification(ADDON.getLocalizedString(32010), sys.listitem.getLabel(), xbmcgui.NOTIFICATION_INFO, 3000)
112107

113108
log.info("Starting Elementum with: %s" % url)
114109

115110
xbmc.Player().play(url)
116111

112+
117113
def doPlay():
118114
dbid = getDbId()
119115
mediatype = getMediaType()
@@ -142,14 +138,21 @@ def doDownload():
142138

143139
def doLibraryAction(action):
144140
dbid = getDbId()
141+
try:
142+
tmdbID = sys.listitem.getUniqueID('tmdb')
143+
except AttributeError:
144+
tmdbID = ""
145145
mediatype = getMediaType()
146146

147147
heading = ADDON.getLocalizedString(32017) if action == "add" else "Unsupported action"
148148
xbmcgui.Dialog().notification(heading, sys.listitem.getLabel(), xbmcgui.NOTIFICATION_INFO, 3000)
149149

150150
log.info("%s library item: DBID=%s, MediaType=%s" % (action, dbid, mediatype))
151151

152-
url = "plugin://plugin.video.elementum/context/library/%s/%s/%s" % (mediatype, dbid, action)
152+
if tmdbID != "":
153+
url = "plugin://plugin.video.elementum/library/%s/%s/%s" % (mediatype, action, tmdbID)
154+
else:
155+
url = "plugin://plugin.video.elementum/context/library/%s/%s/%s" % (mediatype, dbid, action)
153156
log.info("Starting Elementum with: %s" % url)
154157
xbmc.Player().play(url)
155158

@@ -164,11 +167,38 @@ def doTraktAction(action):
164167
heading = ADDON.getLocalizedString(32019)
165168
else:
166169
heading = "Unsupported action"
167-
xbmcgui.Dialog().notification(heading, sys.listitem.getLabel(), xbmcgui.NOTIFICATION_INFO, 3000)
168170

169-
log.info("%s library item: DBID=%s, MediaType=%s" % (action, dbid, mediatype))
171+
if not dbid.isdigit():
172+
showtmdbid = xbmc.getInfoLabel('ListItem.Property(ShowTMDBId)')
173+
try:
174+
tmdbID = sys.listitem.getUniqueID('tmdb')
175+
except AttributeError:
176+
tmdbID = ""
177+
if tmdbID == "" or ((mediatype == 'season' or mediatype == 'episode') and showtmdbid == ""):
178+
log.error("Could not find TMDB id for %s" % dbid)
179+
xbmcgui.Dialog().notification(ADDON.getLocalizedString(32007), ADDON.getLocalizedString(32014), xbmcgui.NOTIFICATION_WARNING, 3000)
180+
return
170181

171-
url = "plugin://plugin.video.elementum/context/media/%s/%s/%s" % (mediatype, dbid, action)
182+
xbmcgui.Dialog().notification(heading, sys.listitem.getLabel(), xbmcgui.NOTIFICATION_INFO, 3000)
183+
if not dbid.isdigit():
184+
log.info("Make %s non-library item: tmdbID=%s, MediaType=%s" % (action, tmdbID, mediatype))
185+
else:
186+
log.info("Make %s library item: DBID=%s, MediaType=%s" % (action, dbid, mediatype))
187+
188+
if not dbid.isdigit():
189+
if mediatype == 'movie':
190+
url = "plugin://plugin.video.elementum/movie/%s/%s" % (tmdbID, action)
191+
if mediatype == 'tvshow':
192+
url = "plugin://plugin.video.elementum/show/%s/%s" % (tmdbID, action)
193+
elif mediatype == 'season':
194+
season = xbmc.getInfoLabel('ListItem.Season')
195+
url = "plugin://plugin.video.elementum/show/%s/season/%s/%s" % (showtmdbid, season, action)
196+
elif mediatype == 'episode':
197+
season = xbmc.getInfoLabel('ListItem.Season')
198+
episode = xbmc.getInfoLabel('ListItem.Episode')
199+
url = "plugin://plugin.video.elementum/show/%s/season/%s/episode/%s/%s" % (showtmdbid, season, episode, action)
200+
else:
201+
url = "plugin://plugin.video.elementum/context/media/%s/%s/%s" % (mediatype, dbid, action)
172202
log.info("Starting Elementum with: %s" % url)
173203
xbmc.Player().play(url)
174204

@@ -212,7 +242,7 @@ def getMediaType():
212242
if xbmc.getInfoLabel('ListItem.DBTYPE'):
213243
# Seasons and calls from library will work
214244
return xbmc.getInfoLabel('ListItem.DBTYPE')
215-
elif version >= 18:
245+
elif version >= 17:
216246
# Will work on Kodi 17 and further
217247
return getVideoTag().getMediaType()
218248
else:
@@ -248,45 +278,6 @@ def getVideoTag():
248278
return sys.listitem.getVideoInfoTag()
249279

250280

251-
def getTMDBId(mediatype, id):
252-
if isTMDBId(mediatype, id):
253-
return id
254-
else:
255-
if mediatype == 'movie':
256-
url = "https://api.themoviedb.org/3/find/%s?api_key=%s&language=en-US&external_source=%s" % (id, api_key, 'imdb_id')
257-
elif mediatype == 'show':
258-
url = "https://api.themoviedb.org/3/find/%s?api_key=%s&language=en-US&external_source=%s" % (id, api_key, 'tvdb_id')
259-
260-
response = getJSON(url)
261-
262-
if 'status_code' in response:
263-
xbmcgui.Dialog().notification(ADDON.getLocalizedString(32007), ADDON.getLocalizedString(32008), xbmcgui.NOTIFICATION_WARNING, 3000)
264-
return None
265-
266-
if response['movie_results']:
267-
return response['movie_results'][0]['id']
268-
elif response['tv_results']:
269-
return response['tv_results'][0]['id']
270-
elif response['tv_episode_results']:
271-
return response['tv_episode_results'][0]['id']
272-
else:
273-
return None
274-
275-
276-
def isTMDBId(mediatype, id):
277-
if mediatype == 'show':
278-
url = "https://api.themoviedb.org/3/tv/%s?api_key=%s&language=en-US" % (id, api_key)
279-
elif mediatype == 'movie':
280-
url = "https://api.themoviedb.org/3/movie/%s?api_key=%s&language=en-US" % (id, api_key)
281-
282-
response = getJSON(url)
283-
284-
if 'status_code' in response:
285-
return False
286-
else:
287-
return True
288-
289-
290281
def xbmcVersion():
291282
build = xbmc.getInfoLabel('System.BuildVersion')
292283

@@ -312,6 +303,14 @@ def getJSONResponse(message):
312303
return simplejson.loads(xbmc.executeJSONRPC(message))
313304

314305

306+
def getJSON(url):
307+
page = requests.get(url).content
308+
try:
309+
return simplejson.loads(page)
310+
except:
311+
return simplejson.loads(page.decode('utf-8'))
312+
313+
315314
def configureTMDB():
316315
global api_key
317316

@@ -327,9 +326,84 @@ def configureTMDB():
327326
return True
328327

329328

330-
def getJSON(url):
331-
page = requests.get(url).content
332-
try:
333-
return simplejson.loads(page)
334-
except:
335-
return simplejson.loads(page.decode('utf-8'))
329+
def isTMDBId(mediatype, id):
330+
if mediatype == 'show':
331+
url = "https://api.themoviedb.org/3/tv/%s?api_key=%s&language=en-US" % (id, api_key)
332+
elif mediatype == 'movie':
333+
url = "https://api.themoviedb.org/3/movie/%s?api_key=%s&language=en-US" % (id, api_key)
334+
335+
response = getJSON(url)
336+
337+
if 'status_code' in response:
338+
return False
339+
else:
340+
return True
341+
342+
343+
def getTMDBId(mediatype, id):
344+
if isTMDBId(mediatype, id):
345+
return id
346+
else:
347+
if mediatype == 'movie':
348+
url = "https://api.themoviedb.org/3/find/%s?api_key=%s&language=en-US&external_source=%s" % (id, api_key, 'imdb_id')
349+
elif mediatype == 'show':
350+
url = "https://api.themoviedb.org/3/find/%s?api_key=%s&language=en-US&external_source=%s" % (id, api_key, 'tvdb_id')
351+
352+
response = getJSON(url)
353+
354+
if 'status_code' in response:
355+
xbmcgui.Dialog().notification(ADDON.getLocalizedString(32007), ADDON.getLocalizedString(32008), xbmcgui.NOTIFICATION_WARNING, 3000)
356+
return None
357+
358+
if response['movie_results']:
359+
return response['movie_results'][0]['id']
360+
elif response['tv_results']:
361+
return response['tv_results'][0]['id']
362+
elif response['tv_episode_results']:
363+
return response['tv_episode_results'][0]['id']
364+
else:
365+
return None
366+
367+
368+
def getTMDBidFromElementumPath(path):
369+
"""plugin://plugin.video.elementum/movie/628/links/Interview%20with%20the%20Vampire%20%281994%29"""
370+
result = re.search(r'plugin://plugin.video.elementum/[^/]+/(\d+)/.*', path)
371+
if result:
372+
tmdbID = result.group(1)
373+
return tmdbID
374+
else:
375+
log.error("Could not find TMDB id for %s" % path)
376+
xbmcgui.Dialog().notification(ADDON.getLocalizedString(32007), ADDON.getLocalizedString(32014), xbmcgui.NOTIFICATION_WARNING, 3000)
377+
return ""
378+
379+
380+
def getSeasonNumberFromElementumPath(path):
381+
"""plugin://plugin.video.elementum/show/1622/season/15/episodes"""
382+
season_number = xbmc.getInfoLabel('ListItem.Season')
383+
if season_number:
384+
return season_number
385+
else:
386+
result = re.search(r'plugin://plugin.video.elementum/[^/]+/\d+/season/(\d+)/.*', path)
387+
if result:
388+
season_number = result.group(1)
389+
return season_number
390+
else:
391+
log.error("Could not find season number for %s" % path)
392+
xbmcgui.Dialog().notification(ADDON.getLocalizedString(32007), ADDON.getLocalizedString(32014), xbmcgui.NOTIFICATION_WARNING, 3000)
393+
return ""
394+
395+
396+
def getEpisodeNumberFromElementumPath(path):
397+
"""plugin://plugin.video.elementum/show/1622/season/15/episode/1/links/Supernatural%20S15E01"""
398+
episode_number = xbmc.getInfoLabel('ListItem.Episode')
399+
if episode_number:
400+
return episode_number
401+
else:
402+
result = re.search(r'plugin://plugin.video.elementum/[^/]+/\d+/season/\d+/episode/(\d+)/.*', path)
403+
if result:
404+
episode_number = result.group(1)
405+
return episode_number
406+
else:
407+
log.error("Could not find episode number for %s" % path)
408+
xbmcgui.Dialog().notification(ADDON.getLocalizedString(32007), ADDON.getLocalizedString(32014), xbmcgui.NOTIFICATION_WARNING, 3000)
409+
return ""

resources/language/Russian/strings.po

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ msgstr "Скачивание в [COLOR FF46B3B3]Elementum[/COLOR]"
7676

7777
msgctxt "#32014"
7878
msgid "Could not find TMDB info"
79-
msgstr "Невозможно найти TMDB info"
79+
msgstr "Невозможно найти TMDB информацию"
8080

8181
msgctxt "#32015"
8282
msgid "Could not get torrent info"

0 commit comments

Comments
 (0)