Created
August 30, 2016 14:16
-
-
Save edigiacomo/33b3f785232bed9f447dca36770f49ad to your computer and use it in GitHub Desktop.
Music videos directed by Michel Gondry and Spike Jonze on Youtube
This file contains hidden or 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
| import urllib.request | |
| import urllib.parse | |
| import re | |
| from SPARQLWrapper import SPARQLWrapper, JSON | |
| sparql = SPARQLWrapper("http://dbpedia.org/sparql") | |
| sparql.setQuery(""" | |
| SELECT ?songName WHERE { | |
| ?song dct:subject ?subj . | |
| ?song dbp:name ?songName . | |
| filter | |
| ( | |
| ?subj = dbc:Music_videos_directed_by_Michel_Gondry || | |
| ?subj = dbc:Music_videos_directed_by_Spike_Jonze | |
| ) | |
| } GROUP BY ?song | |
| """) | |
| sparql.setReturnFormat(JSON) | |
| results = sparql.query().convert() | |
| songs = ["{} official video".format(r["songName"]["value"]) | |
| for r in results["results"]["bindings"]] | |
| for song in songs: | |
| q = urllib.parse.urlencode({"search_query": song}) | |
| html = urllib.request.urlopen("http://www.youtube.com/results?" + q) | |
| r = re.findall(r'href=\"\/watch\?v=(.{11})', html.read().decode()) | |
| print(song, "http://www.youtube.com/watch?v=" + r[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment