Skip to content

Instantly share code, notes, and snippets.

@hamidzr
Last active May 21, 2017 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hamidzr/29f0eff1b62262d5a141c37cbb525118 to your computer and use it in GitHub Desktop.
Save hamidzr/29f0eff1b62262d5a141c37cbb525118 to your computer and use it in GitHub Desktop.
Play trending youtube videos from a specific playlist randomly. The default playlist is trendin music of the week.
#!/usr/bin/python3
#python3
import urllib.request
from urllib.parse import quote
import json
import sys
# configs
YOUTUBE_API='updateme'
KODI_ADDRESS = 'http://piaddress:8080'
# pre: gets an encoded url pointing to a json endpoint
# post: returns a python dic representing the response
def getJson(url):
print('Calling url: ',url)
httpResponse = urllib.request.urlopen(url)
return json.loads(httpResponse.read().decode("utf-8"))
# pre: gets a keyword
# post: returns the first result of it's search (in yt)
def ytSearch(keyword):
keyword = quote(keyword)
url = "https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=50&order=relevance&q={}&type=video&key={}".format(keyword,YOUTUBE_API)
return getJson(url)
def getYtPlaylist(id='PLFgquLnL59alW3xmYiWRaoz0oM3H17Lth'):
url = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,contentDetails&maxResults=25&playlistId={}&key={}'.format(id,YOUTUBE_API)
return getJson(url)
# send a request object to kodi
def requestKodi(kodiReqJson):
req = urllib.request.Request(url='{}/jsonrpc'.format(KODI_ADDRESS), method='POST')
req.add_header('Content-Type','application/json')
req.data = json.dumps(kodiReqJson).encode('UTF-8')
res = urllib.request.urlopen(req, timeout=30).read().decode('UTF-8')
res = json.loads(res)
return res
# plays queues up a list of urls
def clearPlaylist(id=1):
requestKodi({"jsonrpc":"2.0","id":1,"method":"Playlist.Clear","params":{"playlistid":1}})
def createPlaylist(urls):
filesArr = []
for url in urls:
filesArr.append({"file": url})
requestJson = {"jsonrpc":"2.0","id":"1","method":"Playlist.Add","params":{"playlistid":1, "item":filesArr}}
requestKodi(requestJson)
def playPlaylist(id=1, shuffle=True):
# play the playlist
requestKodi({"jsonrpc":"2.0","id":1,"method":"Player.Open","params":{"item":{"playlistid":1},"options":{"repeat":"all"}}})
if shuffle:
# shuffle the playlist
requestKodi({"jsonrpc":"2.0","id":"1","method":"Player.SetShuffle", "params":{"playerid":1,"shuffle":True}})
clearPlaylist()
urls = []
try:
searchResultsJson = getYtPlaylist(sys.argv[1])
except Exception as e:
pass
searchResultsJson = getYtPlaylist()
for idx, res in enumerate(searchResultsJson['items']):
id = res['contentDetails']['videoId']
try:
print(str(idx+1) + '. ' + res['snippet']['title'])
except Exception as e:
print('failed to print name')
urls.append("plugin://plugin.video.youtube/?action=play_video&videoid={}".format(id))
createPlaylist(urls)
playPlaylist()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment