forked from firsttris/plugin.video.sendtokodi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.py
91 lines (65 loc) · 2.18 KB
/
service.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
# -*- coding: utf-8 -*-
import os
import sys
import xbmc
import xbmcaddon
import xbmcgui
import xbmcplugin
from youtube_dl import YoutubeDL
class replacement_stderr(sys.stderr.__class__):
def isatty(self): return False
sys.stderr.__class__ = replacement_stderr
# Get the plugin url in plugin:// notation.
__url__ = sys.argv[0]
# Get the plugin handle as an integer number.
__handle__ = int(sys.argv[1])
def debug(content):
log(content, xbmc.LOGDEBUG)
def notice(content):
log(content, xbmc.LOGNOTICE)
def log(msg, level=xbmc.LOGNOTICE):
addon = xbmcaddon.Addon()
addonID = addon.getAddonInfo('id')
xbmc.log('%s: %s' % (addonID, msg), level)
def showInfoNotification(message):
xbmcgui.Dialog().notification("SendToKodi", message, xbmcgui.NOTIFICATION_INFO, 5000)
def showErrorNotification(message):
xbmcgui.Dialog().notification("SendToKodi", message,
xbmcgui.NOTIFICATION_ERROR, 5000)
def getParams():
paramstring = sys.argv[2]
cleanedparams = paramstring[1:]
return cleanedparams
def createListItemFromVideo(video):
debug(video)
url = video['url']
thumbnail = video.get('thumbnail')
title = video['title']
list_item = xbmcgui.ListItem(title, path=url)
list_item.setInfo(type='Video', infoLabels={'Title': title})
if thumbnail is not None:
list_item.setArt({'thumb': thumbnail})
return list_item
ydl_opts = {
'format': 'best'
}
params = getParams()
url = str(params)
ydl = YoutubeDL(ydl_opts)
ydl.add_default_info_extractors()
with ydl:
showInfoNotification("resolving stream(s) for " + url)
result = ydl.extract_info(url, download=False)
if 'entries' in result:
# Playlist
pl = xbmc.PlayList(1)
pl.clear()
for video in result['entries']:
list_item = createListItemFromVideo(video);
xbmc.PlayList(1).add(list_item.getPath(), list_item)
xbmc.Player().play(pl)
showInfoNotification("playing playlist " + result['title'])
else:
# Just a video, pass the item to the Kodi player.
showInfoNotification("playing title " + result['title'])
xbmcplugin.setResolvedUrl(__handle__, True, listitem=createListItemFromVideo(result))