-
Notifications
You must be signed in to change notification settings - Fork 0
/
songza.py
executable file
·49 lines (45 loc) · 2.02 KB
/
songza.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/python2
from optparse import OptionParser
import subprocess
import requests
import sys
USER_AGENT = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36'
def play_station(num_code, cookies=None, proxy=None, download=None):
if proxy:
proxies = {
"http": proxy
}
else:
proxies = None
headers = {'User-Agent' : USER_AGENT}
url = "http://songza.com/api/1/station/%s/next" % num_code
if cookies:
response = requests.get(url, headers=headers, cookies=cookies,
proxies=proxies)
else:
response = requests.get(url, headers=headers, proxies=proxies)
cookies = response.cookies
response_json = response.json()
listen_url = response_json['listen_url']
song = response_json['song']
print "song is %s by %s" % (song[u'title'], song[u'artist'][u'name'])
cookie_str = ":".join(map(str, cookies.items()[0]))
#call(["/usr/bin/mplayer", '-cookies', cookie_str, '-really-quiet', listen_url])
subprocess.call(["/usr/bin/mplayer", '-really-quiet', listen_url])
#import ipdb;ipdb.set_trace()
#curl = subprocess.call(["/usr/bin/curl", listen_url], stdout=subprocess.PIPE)
#mplayer = subprocess.call(["/usr/bin/mplayer", '-really-quiet', "-"], stdin=curl.stdout, stdout=subprocess.PIPE)
#mplayer.communicate()[0]
#subprocess.call(['/usr/bin/wget', listen_url])
play_station(num_code, cookies=cookies, proxy=proxy, download=download)
if __name__ == "__main__":
parser = OptionParser(usage="usage: %prog [options] station_number")
parser.add_option("-p", "--proxy", dest="proxy", default=None,
type="string", help="optional http proxy")
parser.add_option("-d", "--download", dest="download", default=None,
type="string", help="download flag")
(options, args) = parser.parse_args()
try:
play_station(args[0], proxy=options.proxy, download=options.download)
except KeyboardInterrupt:
sys.exit(0)