-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_changes.py
More file actions
104 lines (91 loc) · 3.26 KB
/
Copy pathextract_changes.py
File metadata and controls
104 lines (91 loc) · 3.26 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"""Track cube changes over time."""
import collections
import datetime
import itertools
import os
import pprint
import bs4
def GetChanges():
"""Extract changes from CubeTutor blog html files."""
results = []
for name in os.listdir('ctdata'):
if name.endswith('.html'):
soup = bs4.BeautifulSoup(open(f'ctdata/{name}'), 'html.parser')
for mode in ['ADD', 'REMOVE', 'SIDEBOARD', 'ADD_TO_SIDEBOARD']:
for a in soup.select(f'.{mode}Icon'):
stamp = datetime.datetime.strptime(
a.parent.select('.date')[0].text, '%Y-%m-%d %H:%M')
card = a.select('a')[0].text
results.append((stamp, mode, card))
mode = 'REPLACE'
for a in soup.select(f'.{mode}Icon'):
stamp = datetime.datetime.strptime(
a.parent.select('.date')[0].text, '%Y-%m-%d %H:%M')
card_out = a.select('a')[0].text
card_in = a.select('a')[1].text
results.append((stamp, 'REMOVE', card_out))
results.append((stamp, 'ADD', card_in))
return results
def ConditionalZip(badges, cards):
badges = iter(badges)
cards = iter(cards)
mapping = {'+': 'ADD', '-': 'REMOVE'}
for badge in badges:
if badge in ('+', '-'):
yield (mapping[badge], next(cards))
elif badge in ('→',):
yield (mapping['-'], next(cards))
yield (mapping['+'], next(cards))
def GetChangesCobra():
"""Extract changes from CubeCobra blog html files."""
results = []
for name in os.listdir('ccdata'):
if name.endswith('.html'):
soup = bs4.BeautifulSoup(open(f'ccdata/{name}'), 'html.parser')
posts = soup.select('.mt-3')
for post in posts:
if 'Cube Bulk Import' in post.select('.card-title')[0].text:
continue
deltatext = soup.select('h6.mb-2')[0].text.split('-')[-1].strip()
count, unit, unused_ago = deltatext.split()
if not unit.endswith('s'):
unit += 's'
delta = datetime.timedelta(**{unit: int(count)})
stamp = datetime.datetime.now() - delta
badges = [span.text for span in post.select('span.badge')]
cards = [a.text for a in post.select('a.dynamic-autocard')]
results.extend(
(stamp,) + change for change in ConditionalZip(badges, cards))
return results
# pprint.pprint(GetChanges(), stream=open('changes.txt', 'w'))
def TrackCube():
"""Construct the intermediate lists."""
all_lists = {}
counter = collections.Counter(
line.strip().split(' // ')[0] for line in open('ChrissVintageCube.txt'))
changes = GetChanges() + GetChangesCobra()
changes.sort(reverse=True)
for stamp, g in itertools.groupby(changes, key=lambda c: c[0]):
all_lists[stamp] = counter.copy()
for change in g:
_, mode, card = change
card = card.split(' // ')[0]
# We're going backwards from the most recent list; operations are reversed
# from their natural descriptions.
if mode in ('SIDEBOARD', 'REMOVE'):
counter.update([card])
elif mode == 'ADD':
counter.subtract([card])
elif mode == 'ADD_TO_SIDEBOARD':
pass
else:
raise ValueError('Unknown mode.')
all_lists[datetime.datetime.fromtimestamp(0)] = counter.copy()
return all_lists
try:
x = TrackCube()
except:
import pdb
pdb.post_mortem()
import IPython
IPython.embed()