-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.py
More file actions
76 lines (62 loc) · 2.05 KB
/
Copy pathprocess.py
File metadata and controls
76 lines (62 loc) · 2.05 KB
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
65
66
67
68
69
70
71
72
73
74
75
"""
Read in all pickled things in saves/*, process from there
"""
import os
import pickle
import re
import string
import traceback
def read_all_the_things():
firere = re.compile("^[0-9]{4}-[0-9]{2}-[0-9]{2}$")
results = {}
for f in os.listdir("saves"):
try:
if not firere.match(f):
continue
results[f] = pickle.load(open("saves/%s" % f, "rb"))
except:
traceback.print_exc()
return results
def compile(results):
songs = {}
for date,week in results.items():
for song in week:
key = "%s-%s" % (song["artist"], song["song"])
if key in songs:
songs = update_song(songs, key, song, date)
else:
songs[key] = song
try:
songs[key]['relative'] = 41 - int(song["position"])
except:
print key, songs['key']
traceback.print_exc()
songs[key]['first'] = date
songs[key]['weeks'] = 1
songs[key]['last'] = date
return songs
def update_song(songs, key, song, date):
try:
songs[key]['relative'] += 41 - int(song["position"])
except:
print songs[key]
traceback.print_exc()
songs[key]['peak'] = min(int(songs[key]['peak']), int(song['peak']))
songs[key]['twc'] = max(int(songs[key]['twc']), int(song['twc']))
songs[key]['first'] = min(songs[key]['first'], date)
songs[key]['last'] = max(songs[key]['last'], date)
songs[key]['weeks'] += 1
return songs
def sanitize_song(name):
trans = string.maketrans("\t\n\r", " ")
return str(name).translate(trans)
def output(songs):
print "first\tlast\tpeak\trelative\tweeks\tkey"
for k,v in songs.items():
k = sanitize_song(k)
v["key"] = k
print "%(first)s\t%(last)s\t%(peak)s\t%(relative)s\t%(weeks)s\t%(key)s" % v
if __name__ == "__main__":
results = read_all_the_things()
songs = compile(results)
output(songs)