Skip to content

Instantly share code, notes, and snippets.

@CatherineH
Created March 2, 2016 04:05
Show Gist options
  • Save CatherineH/09c9545251b698876378 to your computer and use it in GitHub Desktop.
Save CatherineH/09c9545251b698876378 to your computer and use it in GitHub Desktop.
import os
import urllib
import urllib2
import ast
import wget
from PIL import Image
from collections import Counter
service = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13"
# you will need to generate your own client id and key in Azure
clientId = "chusic"
clientSecret = os.environ.get('BING_API_KEY')
artist = 'Boston'
album = 'Don\'t Look Back'
postData = {"client_id": clientId, "client_secret": clientSecret,
"scope": "http://music.xboxlive.com",
"grant_type": "client_credentials"}
data = urllib.urlencode(postData)
req = urllib2.Request(service, data)
response = urllib2.urlopen(req)
responseString = response.read()
responseString = ast.literal_eval(responseString)
token = urllib.quote_plus(responseString['access_token'])
query = urllib.quote_plus(artist)
music_url = "https://music.xboxlive.com/1/content/music/search?q" \
"="+query+"&accessToken=Bearer+" + token
request = urllib2.Request(music_url)
response = urllib2.urlopen(request)
literal_data = response.read()
literal_data = literal_data.replace(":false", ":False")
literal_data = literal_data.replace(":true", ":True")
data = ast.literal_eval(literal_data)
for _album in data['Albums']['Items']:
if Counter(_album['Name']) == Counter(album):
temp_filename = wget.download(_album['ImageUrl'])
img = Image.open(temp_filename)
img.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment