Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue #76 #79

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 78 additions & 56 deletions service.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,93 @@ def showErrorNotification(message):
xbmcgui.NOTIFICATION_ERROR, 5000)


def get_local_user_input():
kb = xbmc.Keyboard('', 'Please enter a URL')
kb.doModal() # Onscreen keyboard appears
if not kb.isConfirmed():
return ''

# User input
return kb.getText()


# 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 getParams():
result = {}
params = {}
paramstring = sys.argv[2]

if not paramstring:
paramstring = "?" + get_local_user_input()

additionalParamsIndex = paramstring.find(' ')
if additionalParamsIndex == -1:
result['url'] = paramstring[1:]
result['ydlOpts'] = {}
params['url'] = paramstring[1:]
params['ydlOpts'] = {}
else:
result['url'] = paramstring[1:additionalParamsIndex]
params['url'] = paramstring[1:additionalParamsIndex]
additionalParamsString = paramstring[additionalParamsIndex:]
additionalParams = json.loads(additionalParamsString)
result['ydlOpts'] = additionalParams['ydlOpts']
return result
params['ydlOpts'] = additionalParams['ydlOpts']
params['url'] = str(params['url'])
return params


def play(url, ydl_opts):
if xbmcplugin.getSetting(int(sys.argv[1]),"usemanifest") == 'true':
ydl_opts['format'] = 'bestvideo*+bestaudio/best'
ydl = YoutubeDL(ydl_opts)
ydl.add_default_info_extractors()

with ydl:
progress = xbmcgui.DialogProgressBG()
progress.create("Resolving " + url)
try:
result = ydl.extract_info(url, download=False)
except:
progress.close()
showErrorNotification("Could not resolve the url, check the log for more info")
import traceback
log(msg=traceback.format_exc(), level=xbmc.LOGERROR)
exit()
progress.close()

if 'entries' in result:
# more than one video
pl = xbmc.PlayList(1)
pl.clear()

# determine which index in the queue to start playing from
indexToStartAt = playlistIndex(url, result)
if indexToStartAt == None:
indexToStartAt = 0

unresolvedEntries = list(result['entries'])
startingEntry = unresolvedEntries.pop(indexToStartAt)

# populate the queue with unresolved entries so that the starting entry can be inserted
for video in unresolvedEntries:
list_item = createListItemFromFlatPlaylistItem(video)
pl.add(list_item.getPath(), list_item)

# make sure the starting ListItem has a resolved url, to avoid recursion and crashes
startingVideoUrl = startingEntry['url']
startingItem = createListItemFromVideo(ydl.extract_info(startingVideoUrl, download=False))
pl.add(startingItem.getPath(), startingItem, indexToStartAt)

#xbmc.Player().play(pl) # this probably works again
# ...but start playback the same way the Youtube plugin does it:
xbmc.executebuiltin('Playlist.PlayOffset(%s,%d)' % ('video', indexToStartAt))

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))


def extract_manifest_url(result):
Expand Down Expand Up @@ -250,55 +318,9 @@ def playlistIndex(url, playlist):
}

params = getParams()
url = str(params['url'])
url = params['url']
ydl_opts.update(params['ydlOpts'])
if xbmcplugin.getSetting(int(sys.argv[1]),"usemanifest") == 'true':
ydl_opts['format'] = 'bestvideo*+bestaudio/best'
ydl = YoutubeDL(ydl_opts)
ydl.add_default_info_extractors()

with ydl:
progress = xbmcgui.DialogProgressBG()
progress.create("Resolving " + url)
try:
result = ydl.extract_info(url, download=False)
except:
progress.close()
showErrorNotification("Could not resolve the url, check the log for more info")
import traceback
log(msg=traceback.format_exc(), level=xbmc.LOGERROR)
exit()
progress.close()

if 'entries' in result:
# more than one video
pl = xbmc.PlayList(1)
pl.clear()

# determine which index in the queue to start playing from
indexToStartAt = playlistIndex(url, result)
if indexToStartAt == None:
indexToStartAt = 0

unresolvedEntries = list(result['entries'])
startingEntry = unresolvedEntries.pop(indexToStartAt)

# populate the queue with unresolved entries so that the starting entry can be inserted
for video in unresolvedEntries:
list_item = createListItemFromFlatPlaylistItem(video)
pl.add(list_item.getPath(), list_item)

# make sure the starting ListItem has a resolved url, to avoid recursion and crashes
startingVideoUrl = startingEntry['url']
startingItem = createListItemFromVideo(ydl.extract_info(startingVideoUrl, download=False))
pl.add(startingItem.getPath(), startingItem, indexToStartAt)

#xbmc.Player().play(pl) # this probably works again
# ...but start playback the same way the Youtube plugin does it:
xbmc.executebuiltin('Playlist.PlayOffset(%s,%d)' % ('video', indexToStartAt))

showInfoNotification("Playing playlist " + result['title'])
if url == '':
showInfoNotification("Kindly provide the valid URL.")
else:
# Just a video, pass the item to the Kodi player.
showInfoNotification("Playing title " + result['title'])
xbmcplugin.setResolvedUrl(__handle__, True, listitem=createListItemFromVideo(result))
play(url, ydl_opts)