-
Notifications
You must be signed in to change notification settings - Fork 0
/
mixcloud.py
54 lines (42 loc) · 1.49 KB
/
mixcloud.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
50
51
52
53
54
#!/bin/env python
import urllib2
import json
import sys
import os
def harvest(path, idxname, **kwargs):
base_url = "http://api.mixcloud.com"
username = kwargs.get('username', '')
if not username:
return
jsonidx = open("%s/%s.json" % (path, idxname), 'w')
jsonidxobj = {"name": idxname, "playlists": {}}
if not os.path.exists("%s/%s" % (path, idxname)):
os.makedirs("%s/%s" % (path, idxname))
try:
result = urllib2.urlopen("%s/%s/playlists/?limit=1000&offset=0" % (base_url, username))
playlists = json.loads(result.read())
except:
playlists = {"data": []}
for plist in playlists["data"]:
js = open("%s/%s/%s.json" % (path, idxname, plist['slug']), 'w')
try:
result = urllib2.urlopen("%s/%s/playlists/%s/cloudcasts/?limit=1000&offset=0" % (base_url, username, plist['slug']))
js.write(result.read())
jsonidxobj["playlists"][plist["slug"]] = "%s/%s.json" % (idxname, plist['slug'])
except:
pass
finally:
js.close()
try:
recents = urllib2.urlopen("%s/%s/cloudcasts/" % (base_url, username))
jsonidxobj["recents"] = json.loads(recents.read())
except:
jsonidxobj["recents"] = {"data": []}
jsonidx.write(json.dumps(jsonidxobj))
jsonidx.close()
if __name__ == "__main__":
if not len(sys.argv):
sys.exit(0)
path = sys.argv[1]
idx = sys.argv[2]
harvest(path, idx, username='radiocicletta')