-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperformermanager.py
309 lines (253 loc) · 10.6 KB
/
performermanager.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import sys
import codecs
import re
import os
import sqlite3
import logging
import logging.handlers
import wikiatools
from pagetools import EpisodeInfo
from boop_generator import get_boop
reload(sys)
sys.setdefaultencoding('utf-8')
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
sys.stderr = codecs.getwriter('utf-8')(sys.stderr)
DEBUG = False
clean = False
# connect to database
conn = sqlite3.connect('oneshot.db')
cursor = conn.cursor()
# setup logging
log_file = 'logs/perfomers.log'
logger = logging.getLogger()
logger.setLevel(logging.INFO)
if DEBUG:
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
filehandler = logging.handlers.RotatingFileHandler(log_file, backupCount=7)
filehandler.setFormatter(formatter)
filehandler.doRollover()
logger.addHandler(filehandler)
ch = logging.StreamHandler(sys.stdout)
ch.setFormatter(formatter)
ch.setLevel(logging.DEBUG)
logger.addHandler(ch)
logger = logging.getLogger('perfomers')
# regex patterns
appearences_re = r'== ?Featured Episodes(?: and Series)? ?==(?:\n\* ?.*)*'
player_re = r'== ?Players? ?==(?:\n\* ?.*)*'
titles_re = r'(?:\* \[\[)(.*)(?:\|)(.*)(?:\]\])'
# pywikibot commands
episodes_command = 'python pwb.py listpages -cat:"Episodes" -namespace:0 -save:"../one-shot-bot/test_files/episodes"'
get_command = 'python pwb.py listpages -file:"userfiles/{0}" -save:"../one-shot-bot/test_files/{0}/"'
if not clean:
episodes_command += ' -recentchanges:100 -intersect'
get_command += ' -recentchanges:100 -intersect'
add_appearences_command = 'python pwb.py add_text -page:"%s" -text:"%s" -summary:"%s Links added by a droid" -always'
replace_appearences_command = 'python pwb.py replace -page:"%s" -regex "' + appearences_re + '" "%s" -summary:"%s Updating episodes was done by a droid" -always'
replace_players_command = 'python pwb.py replace -page:"%s" -regex "' + player_re + '" "%s" -summary:"%s Updating episodes was done by a droid" -always'
file_output = '../core/userfiles/%s'
episodes_path = 'test_files/episodes/'
perfomers_path = 'test_files/performers/'
series_path = 'test_files/series/'
mainpages_path = 'test_files/mainpages/'
def episode_sort(episode):
if '|' in episode:
episode = episode.split('|')[1]
return [int(t) if t.isdigit() else t.lower() for t in re.split('(\d+)', episode)]
# get episode pages
if not DEBUG:
wikiatools.run_command(episodes_command)
# clear new pages
wikiatools.clear_new_pages()
for ep in os.listdir(episodes_path):
if 'Template' in ep:
continue
episode = ep.replace('_', ' ')
episode_info = EpisodeInfo(ep)
names = episode_info.get_gm() + episode_info.get_players()
# setup performers and episodes
cursor.executemany('insert or ignore into performers(name) values(?);', [(n,) for n in names])
cursor.execute('insert or ignore into episodes(episode) values(?);', (episode,))
conn.commit()
cursor.executemany('''
insert into ep_perfs(eid,id)
select episodes.eid, performers.id
from episodes, performers
where episodes.episode=?
and performers.name=?;
''', [(episode, n) for n in names])
conn.commit()
# setup series
series_name = episode_info.get_series()
if series_name:
series_name = series_name[0]
if 'Campaign:' in series_name:
series_name = 'Campaign:Campaign'
if '(series)' not in series_name:
logger.warning('=========================== (series) not in ' + series_name)
cursor.execute('insert or ignore into series(series_name) values(?);', (series_name,))
conn.commit()
cursor.execute('''
insert into series_eps(sid,eid)
select series.sid, episodes.eid
from series, episodes
where series.series_name=?
and episodes.episode=?;
''', (series_name, episode))
conn.commit()
commands = []
# get current perfomer and series pages
with open(file_output % 'performers', 'w') as f:
cursor.execute('''select name from performers;''')
for name in sorted(p[0] for p in cursor.fetchall()):
f.write('[[%s]]' % name)
with open(file_output % 'series', 'w') as f:
cursor.execute('''select series_name from series;''')
for name in sorted(s[0] for s in cursor.fetchall()):
f.write('[[%s]]' % name)
with open(file_output % 'mainpages', 'w') as f:
for name in ['One Shot', 'Critical Success', 'First Watch', 'Backstory', 'Modifier', 'Talking TableTop', "Hero's Journey"]:
f.write('[[%s]]' % name)
# get titles of single episodes
if not DEBUG:
wikiatools.run_command(get_command.format('mainpages'))
titles = {}
for show in os.listdir(mainpages_path):
with open(os.path.join(mainpages_path, show)) as f:
page = f.read()
titles.update(re.findall(titles_re, page))
logger.debug(titles)
# refresh perfomer files
if not DEBUG:
wikiatools.run_command(get_command.format('performers'))
# generate apperences
cursor.execute('''select name from performers''')
names = cursor.fetchall()
logger.info('format appearences')
for name in [n[0] for n in names]:
# logger.info(name)
contents = '== Featured Episodes and Series =='
links = []
cursor.execute('''
select distinct
ifnull(series.series_name,episodes.episode)
from performers
join ep_perfs on performers.id = ep_perfs.id
join episodes on ep_perfs.eid = episodes.eid
left join series_eps on ep_perfs.eid = series_eps.eid
left join series on series_eps.sid = series.sid
where performers.name=?;
''', (name,))
appearences = cursor.fetchall()
for ep in [a[0] for a in appearences]:
if ep in titles:
links.append(ep + '|' + titles[ep])
else:
links.append(ep)
for l in sorted(links, key=episode_sort):
contents += '\n* [[%s]]' % l
# check if perfomerpage exists
perfomer_file = os.path.join(perfomers_path, name.replace(' ', '_'))
if os.path.exists(perfomer_file) and os.path.getsize(perfomer_file) > 0:
with open(perfomer_file) as f:
page = f.read()
if 'Featured Episodes and Series' in page:
updated_page = re.sub(appearences_re, contents, page)
if page != updated_page:
logger.info('updating ' + name)
commands.append(replace_appearences_command % (name, contents, get_boop()))
else:
logger.info('no changes to ' + name)
else:
# add appearences
logger.info('adding ' + name)
commands.append(add_appearences_command % (name, contents, get_boop()))
# new perfomer page
else:
logger.info('new perfomer ' + name)
wikiatools.write_page(title=name, content='{{Performer}}\n' + contents)
if not DEBUG:
wikiatools.run_command(get_command.format('series'))
# create new series pages
cursor.execute('''select series_name from series;''')
for name in sorted(s[0] for s in cursor.fetchall()):
# print name, eps
if 'Campaign:' in name:
continue
contents = '== Featured Episodes =='
links = []
cursor.execute('''
select distinct
episode
from series, series_eps, episodes
where series.sid=series_eps.sid
and series_eps.eid=episodes.eid
and series.series_name=?;
''', (name,))
for ep in [e[0] for e in cursor.fetchall()]:
if ep in titles:
links.append(ep + '|' + titles[ep])
else:
links.append(ep)
for ep in sorted(links, key=episode_sort):
contents += '\n* [[%s]]' % ep
player_contents = '== Players =='
cursor.execute('''
select distinct
name
from series, series_eps, ep_perfs, performers
where series.sid=series_eps.sid
and series_eps.eid=ep_perfs.eid
and ep_perfs.id=performers.id
and series.series_name=?;
''', (name,))
for p in sorted([n[0] for n in cursor.fetchall()]):
player_contents += '\n* [[%s]]' % p
logger.debug(player_contents)
# TODO: find systems in pages
# if len(series_system[name]) > 1:
# system_contents = '== Systems =='
# else:
# system_contents = '== System =='
# for s in sorted(series_system[name]):
# system_contents +='\n* [[%s]]' % s
# logger.debug(system_contents)
# update series links
series_file = os.path.join(series_path, name.replace(' ', '_').replace(':', '_'))
if os.path.exists(series_file) and os.path.getsize(series_file) > 0:
with open(series_file) as f:
page = f.read()
if 'Featured Episodes' in page:
# featured_list = re.findall(appearences_re, page)[0]
# logger.debug(' '.join([name,featured_list]))
# if abs(len(contents) - len(featured_list)) > 10:
updated_page = re.sub(appearences_re, contents, page)
if page != updated_page:
logger.info('updating episodes ' + name)
commands.append(replace_appearences_command % (name, contents, get_boop()))
else:
# add appearences
logger.info('adding episodes ' + name)
commands.append(add_appearences_command % (name, contents, get_boop()))
# series performers
if 'Players' in page:
updated_page = re.sub(player_re, player_contents, page)
if page != updated_page:
logger.info('updating players ' + name)
commands.append(replace_players_command % (name, player_contents, get_boop()))
else:
# add appearences
logger.info('adding players ' + name)
commands.append(add_appearences_command % (name, player_contents, get_boop()))
# series systems
# new series page
else:
logger.info('new series ' + name)
wikiatools.write_page(title=name, content='\n' + contents + '\n\n' + player_contents + '\n\n[[Category:Series]]')
if not DEBUG:
for c in commands:
wikiatools.run_command(c)
wikiatools.post_pages()