Add diff script
[playlist-display/.git] / gen_diffs.py
1 import json
2 import glob
3
4 files = glob.glob('_data/*.json')
5
6 for f in files:
7     file1 = json.load(open(f,'r'))
8     tracks1 = sorted([x['track']['name'] for x in file1['items']])
9     for g in files:
10         file2 = json.load(open(g,'r'))
11         tracks2 = sorted([x['track']['name'] for x in file2['items']])
12
13         col1 = []
14         col2 = []
15         c = 0
16         d = 0
17         for x in range(len(tracks1)):
18             if (d >= len(tracks1)):
19                 col1.append('')
20                 col2.append(tracks2[c])
21             elif (c >= len(tracks2)):
22                 col1.append(tracks1[d])
23                 col2.append('')
24             elif tracks1[d] == tracks2[c]:
25                 col1.append(tracks1[d])
26                 col2.append(tracks2[c])
27             else:
28                 if tracks1[d] == tracks2[c+1]:
29                     col1.append('')
30                     col2.append(tracks2[c])
31                     d -= 1
32                 elif tracks1[d+1] == tracks2[c]:
33                     col1.append(tracks1[d])
34                     col2.append('')
35                     c -= 1
36             c += 1
37             d += 1
38
39         gone = []
40         added = []
41
42         for x in tracks1:
43             if x not in tracks2:
44                 gone.append(x)
45         for x in tracks2:
46             if x not in tracks1:
47                 added.append(x)
48
49         outer = open('diff/'+f[-15:-5]+'_'+g[-15:-5]+'.md','w')
50         outer.write('---\n')
51         outer.write('layout: default\n')
52         outer.write('---\n\n')
53         outer.write('[<- Back home](/)\n\n')
54         outer.write('## Diff for '+f[-15:-5]+' and '+g[-15:-5]+'\n\n')
55         outer.write('Removed | Added\n')
56         outer.write('-----|-----\n')
57         if len(gone) > len(added):
58             for x in range((len(gone) - len(added))):
59                 added.append('') 
60         elif len(added) > len(gone):
61             for x in range((len(added) - len(gone))):
62                 gone.append('')
63         for x in range(len(gone)):
64             outer.write(gone[x].encode('utf-8')+' | '+added[x].encode('utf-8')+'\n')
65         if f == g:
66             outer.write('    |    \n')
67         """
68         outer.write(f[-15:-5]+'|'+g[-15:-5]+'\n')
69         outer.write('-----|-----\n')
70         for x in range(len(col1)):
71             outer.write(col1[x].encode('utf-8')+'|'+col2[x].encode('utf-8')+'\n')
72         """
73         outer.close()