/
storedata.py
58 lines (46 loc) · 1.33 KB
/
storedata.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
55
56
57
58
#!/usr/bin/env python
from album_metadata import *
import urllib2
from BeautifulSoup import BeautifulSoup as bs
import socket
import json
import os
import cPickle as pickle
def strip_tags(html):
''' Strips tags out of the html. '''
html = re.sub('<[^<]+?>', '', html)
return html
def writeJson():
user_agent = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:14.0) Gecko/20100101 Firefox/14.0.1'
headers = {'User-Agent':user_agent,}
albumsList = []
for count in range(1, 51):
url = "http://rateyourmusic.com/charts/top/album/all-time/" + str(count)
a = album_metadata()
b = a.open_url(url, headers).read()
content = bs(b)
albums = content.findAll("span", {"style" :"font-size:x-large;"})
albums = [strip_tags(str(eachAlbum)) for eachAlbum in albums]
albumsList.extend(albums)
albums = {'albums' :albumsList}
f = open("static/list_of_albums.json", 'w')
jsonEncode = json.dumps(albums)
f.write(jsonEncode)
f.close()
def readJson():
f = open("static/list_of_albums.json").read()
data = json.loads(f)
return data
def storeVar(var):
pickle.dump(var, open("userdata.p", "wb"))
def loadVar():
var = pickle.load(open("userdata.p", "rb"))
return var
def removePickle():
if os.path.exists('userdata.p'):
os.remove('userdata.p')
if __name__ == "__main__":
writeJson()
#username = "foo"
#storeVar(username)
#print loadVar(username)