Last active
May 1, 2017 02:48
-
-
Save hamidzr/f44daf151f0d2845f6c0d413012948de to your computer and use it in GitHub Desktop.
searches youtube with your search query and starts playing the first 50 results
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
#python3 | |
import urllib.request | |
from urllib.parse import quote | |
import json | |
import sys | |
# configs | |
YOUTUBE_API='update me' | |
KODI_ADDRESS = 'http://localhost: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) | |
# 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) | |
return res.status | |
# 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: | |
id = res['id']['videoId'] | |
filesArr.append({"file": url}) | |
requestJson = {"jsonrpc":"2.0","id":"1","method":"Playlist.Add","params":{"playlistid":1, "item":filesArr}} | |
requestKodi(requestJson) | |
def playPlaylist(id=1): | |
# play the playlist | |
requestKodi({"jsonrpc":"2.0","id":1,"method":"Player.Open","params":{"item":{"playlistid":1},"options":{"repeat":"all"}}}) | |
clearPlaylist() | |
itemCount = int(sys.argv[1]) # number of results to add for each search query | |
for query in sys.argv[2:]: | |
# search youtube and create a list of urls | |
searchResultsJson = ytSearch(query); | |
urls = [] | |
for idx, res in enumerate(searchResultsJson['items']): | |
id = res['id']['videoId'] | |
if idx < itemCount: | |
print(str(idx+1) + '. ' + res['snippet']['title']) | |
urls.append("plugin://plugin.video.youtube/?action=play_video&videoid={}".format(id)) | |
else: | |
break | |
createPlaylist(urls) | |
playPlaylist() |
Author
hamidzr
commented
Apr 30, 2017
•
- limit to only 10
- option to add to existing playlist ( no clear )
- multiple search terms
./playYtSearch.py 5 'pitbull greenlight' 'walk kawbs'
- play as soon as the first results comes back
- limit results to certain categories
- player.shuffle?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment