import urllib2
import json
import random
import webbrowser
songsbytemp= {
'scorching':
{
'ss-collie':
{
'url': 'https://www.youtube.com/watch?v=TLcmtBA-W_s',
'title': 'Collie Man',
'artist': 'Slightly Stoopid',
'mood': 'happy',
'tempo': 'andante'
},
'beck-loser':
{
'url': 'https://www.youtube.com/watch?v=YgSPaXgAdzE',
'title': 'Loser',
'artist': 'Beck',
'mood': 'happy',
'tempo': 'allegro'
}
}, #end of scorching
'hot':
{
'ween-voodoo':
{
'url': 'https://www.youtube.com/watch?v=sRCRi8qfKu0',
'artist': 'Ween',
'title': 'Voodoo Lady',
'mood': 'getdown',
'genre': 'rock',
'tempo': 'andante'
},
'ween-voodoo':
{
'url': 'https://www.youtube.com/watch?v=sRCRi8qfKu0',
'artist': 'Ween',
'title': 'Voodoo Lady',
'mood': 'getdown',
'genre': 'rock',
'tempo': 'andante'
}
}, #end of hot
'nice':
{
'al-weneedhelp':
{
'url': 'https://www.youtube.com/watch?v=d93_iILNji8',
'artist': 'The Album Leaf',
'title': 'We Need Help',
'mood': 'sad',
'genre': 'rock',
'tempo': 'andante'
},
'am-when the sun goes down':
{
'url': 'ww.youtube.com/watch?v=d93_iILNji8',
'artist': 'The Arctic Monkeys',
'title': 'When the Sun Goes Down',
'mood': 'sad',
'genre': 'rock',
'tempo': 'andante'
}
}, #end of warm
'lukewarm':
{
'bb-drytherain':
{
'url': 'https://www.youtube.com/watch?v=wsbR2dEmHGc',
'title': 'Dry the Rain',
'artist': 'Beta Band',
'mood': 'happy',
'genre': 'indie',
'tempo': 'andante'
},
'bb-castars':
{
'url': 'https://www.youtube.com/watch?v=gxzMbAMO73k',
'title': 'California Stars',
'artist': 'Billy Brag and Wilco',
'mood': 'happy',
'genre': 'americana',
'tempo': 'andante'
}
}, #end of lukewarmm
'frigid':
{
'es-bars':
{
'url': 'http://www.youtube.com/watch?v=zH8-lQ9CeyI',
'title': 'Between the Bars',
'artist': 'eliott smith',
'mood': 'sad',
'genre': 'rock',
'tempo': 'andante'
},
'so-mag':
{
'url': 'https://www.youtube.com/watch?v=q0IeGx5-gsk',
'title': 'Hold On Magnolia',
'artist': 'Songs: Ohia',
'mood': 'sad',
'genre': 'indie',
'tempo': 'lento'
}
}, # end of cold
'freezing':
{
'so-lion':
{
'url': 'http://www.youtube.com/watch?v=zH8-lQ9CeyI',
'title': 'Between the Bars',
'artist': 'eliott smith',
'mood': 'sad',
'genre': 'rock',
'tempo': 'andante'
},
'rh-lotus':
{
'url': 'https://www.youtube.com/watch?v=cfOa1a8hYP8',
'title': 'Lotus Flower',
'artist': 'Radiohead',
'mood': 'sad',
'genre': 'rock',
'tempo': 'lento'
},
'so-mag':
{
'url': 'https://www.youtube.com/watch?v=wxAaf16xXRk',
'title': 'Lioness',
'artist': 'Songs: Ohia',
'mood': 'sad',
'tempo': 'lento',
}
} # end of freezeing
} # end of temp ranges
#pull api from wunderground
def gettemp():
f = urllib2.urlopen('http://api.wunderground.com/api/e7fa1fe6de662dfd/geolookup/conditions/q/CA/San_Francisco.json')
json_string = f.read()
parsed_json = json.loads(json_string)
location = parsed_json['location']['city']
temp_f = parsed_json['current_observation']['temp_f']
print "Current temperature in %s is: %s" % (location, temp_f)
f.close()
return temp_f
def getsongurl(temprange):
songs=songsbytemp[temprange]
mysong=random.choice(songs.keys())
for key in songs[mysong]:
print key,
print songs[mysong][key]
url=songs[mysong]['url']
print 'url =', url
return url
#get the current temp
temp=gettemp()
# Chose a song from the proper temp range
if temp > 90:
print "above 90 and scorching"
url=getsongurl('scorching')
elif temp > 82:
print 'over 82, less than 90, and hot'
url=getsongurl('hot')
elif temp > 72:
print "over 72, less than 82, and nice"
url=getsongurl('nice')
elif temp > 65:
print "over 65, under 72, and lukewarm"
url=getsongirl('lukewarm')
elif temp > 55:
print "over 55, under 65, and frigid"
url=getsongurl('frigid')
elif temp > 45:
print "over 45, under 55, and cold!"
url=getsongurl('freezing')
else:
print "Just go back to bed. It's too cold to be alive"
# Play it!
webbrowser.open_new(url)
I need to be able to have a background music for my weather app that I am making in javascript. I I am not familiar with python and I wanted to see if anyone can translate it for me. It would be kindly appreciated. It is the only weather based music api that I have come across after researching for some time now.