-
Notifications
You must be signed in to change notification settings - Fork 3
/
common.py
64 lines (55 loc) · 1.73 KB
/
common.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
59
60
61
62
63
64
import pickle
import os
import parse_infgen
CHARTDATA_FILENAME = 'hot-100-chartdata.pickle'
DB_FILENAME = 'hot-100.pickle'
LYRICS_DIR = 'lyrics'
OMNI_PICKLE_NAME = 'omnisongs.pickle'
class NotScrapedException(Exception):
pass
def have_lyrics(song):
k = song_key(song)
path = os.path.join(LYRICS_DIR, k+'.txt')
return os.path.exists(path)
def song_key(song):
k = song.artist[:15] + '-' + song.title[:20]
k = k.replace('/', '')
return k.replace(' ', '_')
def get_songdb():
with open(DB_FILENAME) as f:
db = pickle.load(f)
return db
def get_omnisong():
with open(OMNI_PICKLE_NAME) as f:
om = pickle.load(f)
return om
def get_chartdata():
with open(CHARTDATA_FILENAME) as f:
cd = pickle.load(f)
return cd
def get_sizes(song_or_key):
if isinstance(song_or_key, basestring):
k = song_or_key
else:
k = song_key(song_or_key)
path = os.path.join(LYRICS_DIR, k+'.txt')
try:
raw = os.path.getsize(path)
except OSError:
raise NotScrapedException
comp = os.path.getsize(path+'.gz')
return (raw, comp)
def get_inf_sizes(song_or_key):
"""Return raw/compressed sizes used when calculating the infgen-based
compression ratio. Raw size will be the same as above (i.e. just the
number you'd get from `wc -c` on the text file).
The compressed size will be an approximation of the size of the LZ-77
compressed data *before* Huffman coding. Assumes 1 byte per literal, 3
bytes per match."""
if isinstance(song_or_key, basestring):
k = song_or_key
else:
k = song_key(song_or_key)
path = os.path.join(LYRICS_DIR, k+'.txt.gz.infgen')
with open(path) as f:
return parse_infgen.parse_ratio(f)