-
Notifications
You must be signed in to change notification settings - Fork 1
/
ao3.py
517 lines (474 loc) · 17.6 KB
/
ao3.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
import requests
import time
import datetime
from datetime import datetime as dtime
from bs4 import BeautifulSoup
import string
import math
import numpy as np
import pandas as pd
from urllib.parse import urlencode
import ast
import sqlite3
def redate(date_string):
# convert dates from AO3 formate to YYYYMMDD integer string, via datetime
d=dtime.strptime(date_string, '%d %b %Y')
outdate = int(d.strftime('%Y%m%d'))
return outdate
def query_AO3_work(work):
# prime all the entry valies with nulls
wurl=''
node=0
title=''
creator=[]
curl=[]
gift = []
fandom=[]
rating=''
category=[]
warning=[]
complete=''
date=0
relationship_tags=[]
characters_tags=[]
freeform_tags=[]
all_tags=[]
summary=''
language=''
words=0
chapters=[1,'?']
collection=0
comments=0
kudos=0
hits=0
bookmarks=0
series=[]
series_part=[]
series_url=[]
# title level meta
a = work.find_all('h4')
v = BeautifulSoup(str(a),'html.parser')
refs = v.select('a')
for ref in refs:
ref = BeautifulSoup(str(ref),'html.parser')
url = ref.a.get('href')
if url.startswith('/works/'):
url = str(v.a.get('href'))
wurl = 'https://archiveofourown.org' + url
n = url.split('/')
node = int(n[-1])
title = str(v.a.string)
else:
if url.endswith('/gifts'):
gift.append(ref.a.string)
else:
curl.append("https://archiveofourown.org" + ref.a.get('href'))
creator.append(ref.a.string)
# fandom tags
a = work.find_all('h5')
v = BeautifulSoup(str(a),'html.parser')
if v.h5['class']==["fandoms","heading"]:
refs = v.select('a[class]')
fandom = []
for f in refs:
w = BeautifulSoup(str(f),'html.parser')
if w.a['class']==["tag"]:
fandom.append(w.a.string)
# date
a = work.find_all('div')
v = BeautifulSoup(str(a[0]),'html.parser')
date = redate(v.find('p').string)
# classifications
a = work.find_all('ul')
b = a[0].find_all('li')
for tag in b:
v = BeautifulSoup(str(tag),'html.parser')
if v.span['class']:
cl = v.span['class']
if cl[0].startswith('rating'):
rating = tag.span['title']
if cl[0].startswith('warning'):
warning = tag.span['title'].split(',')
if cl[0].startswith('category'):
category = tag.span['title'].split(',')
if cl[0].startswith('complete'):
complete = tag.span['title']
# work tags
b = a[1].find_all('li')
for tag in b:
v = BeautifulSoup(str(tag),'html.parser')
if v.li['class']==['relationships']:
relationship_tags.append(tag.string)
if v.li['class']==['characters']:
characters_tags.append(tag.string)
if v.li['class']==['freeforms']:
freeform_tags.append(tag.string)
all_tags = relationship_tags + characters_tags + freeform_tags
# series details
for ain in a[2:]:
b = ain.find_all('li')
for tag in b:
t = tag.find('strong')
series_part.append(int(t.string))
t = tag.find('a')
series.append(t.string)
v = BeautifulSoup(str(tag),'html.parser')
series_url.append('https://archiveofourown.org' + v.a.get('href'))
# work stats
a = work.find_all('dd')
for tag in a:
v = BeautifulSoup(str(tag),'html.parser')
if v.dd['class']==['language']:
language = str(tag.string)
if v.dd['class']==['words']:
if tag.string is not None:
w = tag.string.split(',')
words = int(''.join(w))
if v.dd['class']==['chapters']:
w = v.get_text()
w = w.split('/')
if w[1].isdigit():
w[1]=int(w[1])
w[0] = int(w[0])
chapters = w
if v.dd['class']==['collections']:
collection = int(tag.string)
if v.dd['class']==['kudos']:
kudos = int(tag.string)
if v.dd['class']==['comments']:
comments = int(tag.string)
if v.dd['class']==['hits']:
hits = int(tag.string)
if v.dd['class']==['bookmarks']:
bookmarks= int(tag.string)
# summary
a = work.find_all('blockquote')
if a:
for w in a[0].find_all('p'):
k = str(w).replace('<br/>','\n')
v = BeautifulSoup(k,'html.parser')
summary+= v.get_text() + '\n'
# dictionary of work information
entry = {
'url': wurl,
'Node': node,
'Title': title,
'Creator': creator,
'Gift':gift,
'cURL':curl,
'Fandom': fandom,
'Rating': rating,
'Category': category,
'Warning': warning,
'Complete': complete,
'Date': date,
'Relationship_tags': relationship_tags,
'Character_tags': characters_tags,
'Freeform_tags': freeform_tags,
'Tags': all_tags,
'Summary': summary,
'Language': language,
'Words': words,
'Chapters': chapters,
'Collection': collection,
'Comments': comments,
'Kudos': kudos,
'Hits': hits,
'Bookmarks': bookmarks,
'Series': series,
'Series_part': series_part,
'sURL': series_url}
#print entry
return entry
def work_eval_print(work):
a = work.find_all('h4')
v = BeautifulSoup(str(a),'html.parser')
# if v.h4['class']==["heading"]:
# url = str(v.a.get('href'))
# print("url: https://archiveofourown.org" + url)
# n = url.split('/')
# print('Node: ' + n[-1])
# print('Title: ' + v.a.string)
refs = v.select('a')
authors = []
aurls = []
gifts = []
for ref in refs:
ref = BeautifulSoup(str(ref),'html.parser')
url = ref.a.get('href')
if url.startswith('/works/'):
print("URL: https://archiveofourown.org" + url)
n = url.split('/')
print('Node: ' + n[-1])
print('Title: ' + ref.a.string )
else:
if url.endswith('/gifts'):
gifts.append(ref.a.string)
else:
authors.append(ref.a.string)
aurls.append("https://archiveofourown.org" + v.a.get('href'))
# for a in refs:
# v = BeautifulSoup(str(a),'html.parser')
# if v.a['rel']==["author"]:
# aurls.append("https://archiveofourown.org" + v.a.get('href'))
# authors.append(v.a.string)
print('Creator: ' + str(authors))
print('aURL: ' + str(aurls))
print('Gifts: ' + str(gifts))
a = work.find_all('h5')
v = BeautifulSoup(str(a),'html.parser')
if v.h5['class']==["fandoms","heading"]:
refs = v.select('a[class]')
fs = []
for f in refs:
w = BeautifulSoup(str(f),'html.parser')
if w.a['class']==["tag"]:
fs.append(w.a.string)
print('Fandom: ' + str(fs))
a = work.find_all('div')
v = BeautifulSoup(str(a[0]),'html.parser')
date = redate(v.find('p').string)
print('Date: ' + str(date))
a = work.find_all('ul')
b = a[0].find_all('li')
# print(b)
for tag in b:
v = BeautifulSoup(str(tag),'html.parser')
if v.span['class']:
cl = v.span['class']
if cl[0].startswith('rating'):
print('Rating: ' + tag.span['title'])
if cl[0].startswith('warning'):
print('Warning: ' + str(tag.span['title'].split(',')))
if cl[0].startswith('category'):
print('Category: ' + str(tag.span['title'].split(',')))
if cl[0].startswith('complete'):
print('Complete: ' + tag.span['title'])
b = a[1].find_all('li')
print('tags:' + str(len(b)))
Warnings = []
Ships = []
Characters = []
Freeforms = []
for tag in b:
v = BeautifulSoup(str(tag),'html.parser')
if v.li['class']==['warnings']:
Warnings.append(tag.string)
if v.li['class']==['relationships']:
Ships.append(tag.string)
if v.li['class']==['characters']:
Characters.append(tag.string)
if v.li['class']==['freeforms']:
Freeforms.append(tag.string)
Alltags = Ships + Characters + Freeforms
print('Warning: ' + str(Warnings))
print('Relationships: ' + str(Ships))
print('Characters: '+ str(Characters))
print('Freeform: ' + str(Freeforms))
print('Tags: ' + str(Alltags))
for ain in a[2:]:
b = ain.find_all('li')
print(len(b))
for tag in b:
t = tag.find('strong')
print('Series_Part: ' + t.string)
t = tag.find('a')
print('Series: ' + t.string)
v = BeautifulSoup(str(tag),'html.parser')
print("sURL: https://archiveofourown.org" + v.a.get('href'))
a = work.find_all('dd')
for tag in a:
v = BeautifulSoup(str(tag),'html.parser')
if v.dd['class']==['language']:
print('Language: ' + tag.string)
if v.dd['class']==['words']:
if tag.string is not None:
w = tag.string.split(',')
print('Words: ' + ''.join(w))
if v.dd['class']==['chapters']:
w = v.get_text()
w = w.split('/')
print('Chapters: '+ w[0] + ' of ' + w[-1])
if v.dd['class']==['collections']:
print('Collections: ' + tag.string)
if v.dd['class']==['kudos']:
print('Kudos: ' + tag.string)
if v.dd['class']==['comments']:
print('Comments: '+ tag.string)
if v.dd['class']==['hits']:
print('Hits: '+ tag.string)
if v.dd['class']==['bookmarks']:
print('Bookmarks: '+ tag.string)
Summary = ''
a = work.find_all('blockquote')
for w in a[0].find_all('p'):
k = str(w).replace('<br/>','\n')
v = BeautifulSoup(k,'html.parser')
Summary+= v.get_text() + '\n'
print('Summary: ' + Summary)
return
def db2sqlTypes(df_work):
# convert columns of database into values suitable for sql_types
# floats to int (specifically appropriate for this project)
# lists to string
df_sql = pd.DataFrame(index = df_work.index)
cols = df_work.columns
for c in cols:
A = df_work[c]
B = []
for a in A:
if type(a) is list:
B.append('list'+str(a))
if type(a) is float:
B.append(int(a))
if type(a) is str:
B.append(a)
df_sql[c] = pd.Series(B,df_work.index)
return df_sql
def sql2dbListTypes(data,cols):
# convert back sqlite db of works to pandas dataFramewith more complex objects
# namely, convert table entry strings that start with '[' into lists.
# Inputs: data is the output of the .fetchall(), a list of ordered entries
# cols are the column titles to build back into a pandas database
df_full = pd.DataFrame(columns = cols)
for r in data: # entries row by row, yeah it's slow
row = []
for i in range(len(r)):
if type(r[i]) is str:
if r[i].startswith('list['):
row.append(ast.literal_eval(r[i][4:]))
else:
if r[i].startswith('[') and r[i].endswith(']'):
if len(r[i].split(','))>1:
row.append(ast.literal_eval(r[i]))
else:
if r[i].endswith('"]'):
row.append(ast.literal_eval(r[i]))
else:
row.append(r[i])
else:
row.append(r[i])
else:
row.append(r[i])
ent = dict(zip(cols,row))
df_full=df_full.append(ent,ignore_index=True)
return df_full
def ao3_sql_2_db(sqlName):
conn = sqlite3.connect(sqlName)
c = conn.cursor()
c.execute("SELECT * FROM WORKS")
cols = [tuple[0] for tuple in c.description]
req ="SELECT * FROM WORKS"
res = pd.read_sql(req, conn)
tic = time.time()
df_DB = pd.DataFrame(index = res.index)
list_feilds = [ 'Creator', 'Gift', 'cURL', 'Fandom', 'Category', 'Warning', 'Relationship_tags', 'Character_tags', 'Freeform_tags', 'Tags', 'Chapters', 'Series', 'Series_part', 'sURL']
for feild in cols:
req ="SELECT " + feild + " FROM WORKS"
res = pd.read_sql(req, conn)
if feild in list_feilds:
res2 = pd.DataFrame(index=res.index,columns=[feild])
for index, row in res.iterrows():
res2.loc[index,feild] = ast.literal_eval(row[feild][4:])
res = res2.copy()
if feild.startswith('Date'):
res2 = pd.DataFrame(index=res.index,columns=[feild])
for index, row in res.iterrows():
res2.loc[index,feild] = to_datetime(row[feild])
res = res2.copy()
df_DB[feild] = res
return df_DB
# http://rightfootin.blogspot.com/2006/09/more-on-python-flatten.html
def flatten(l, ltypes=(list, tuple)):
ltype = type(l)
l = list(l)
i = 0
while i < len(l):
while isinstance(l[i], ltypes):
if not l[i]:
l.pop(i)
i -= 1
break
else:
l[i:i + 1] = l[i]
i += 1
return ltype(l)
def daysSince(date,dateRef):
dRef = str(dateRef)
yrRef = int(dRef[0:4])
mnRef = int(dRef[4:6])
dyRef = int(dRef[6:8])
d = str(date)
yr = int(d[0:4])
mn = int(d[4:6])
dy = int(d[6:8])
d = datetime.datetime(yr,mn,dy) - datetime.datetime(yrRef,mnRef,dyRef)
return d.days
def to_datetime(date):
d = str(date)
yr = int(d[0:4])
mn = int(d[4:6])
dy = int(d[6:8])
d = datetime.datetime(yr,mn,dy)
return d
def timeStats_numeric(df_DB,dateSeries,feildName):
# dateSeries is a date range ex: months = pd.Series(pd.date_range("2010-01-01", "2021-01-01", freq="M")
# df_DB is a pandas dataframe of fanworks metadata that includes the feild 'Date' per work, meaning the date the work was last updated in the source database.
# feildName is the feild to be evaluated in the intervals set out by the dateSeries
# at present, this feild must be numeric (float or int)
# the output dataframe
df_stats = pd.DataFrame(index = dateSeries[:-1])
work_counts = []
# if type is Numeric:
feild_totals = []
feild_median = []
# evaluate between dates of the dateSeries
for m_i in range(len(dateSeries)-1):
mask = (df_DB['Date']> dateSeries[m_i]) & (df_DB['Date']<= dateSeries[m_i+1])
interval_works = df_DB.loc[mask]
work_counts.append(len(interval_works))
# if type is Numeric:
feild_totals.append(interval_works[feildName].sum())
feild_median.append(interval_works[feildName].median())
df_stats['Work_Counts'] = work_counts
df_stats['Median_' + feildName] = feild_median
df_stats['Total_' + feildName] = feild_totals
return df_stats
def timeStats_list(df_DB,dateSeries,feildName):
# dateSeries is a date range ex: months = pd.Series(pd.date_range("2010-01-01", "2021-01-01", freq="M")
# df_DB is a pandas dataframe of fanworks metadata that includes the feild 'Date' per work, meaning the date the work was last updated in the source database.
# feildName is the feild to be evaluated in the intervals set out by the dateSeries
# at present, this feild must be numeric (float or int)
df_stats = pd.DataFrame(index = dateSeries[:-1])
# if type is list of strings, like tags, creators, etc:
work_counts = []
feild_uniqueN = [] # number of unique strings across all work lists in interval
feild_unique = [] # actual lists of unique entries
feild_meanN = [] # median number of strings per work list
feild_newN = [] # number of new unique strings in this interval, assuming the date intervals are chronological
feild_new = [] # actual lists of unique new entries this interval
list_all = []
# evaluate between dates of the dateSeries
for m_i in range(len(dateSeries)-1):
mask = (df_DB['Date']> dateSeries[m_i]) & (df_DB['Date']<= dateSeries[m_i+1])
interval_works = df_DB.loc[mask]
work_counts.append(len(interval_works))
full_list = flatten(list(interval_works[feildName].values))
flatList = np.unique(full_list)
feild_uniqueN.append(len(flatList))# number of unique strings across all work lists in interval
feild_unique.append(flatList)# actual lists of unique entries
if len(interval_works)>0:
feild_meanN.append(len(full_list)/len(interval_works)) # median number of strings per work list
else:
feild_meanN.append(0)
new_strings = np.setdiff1d(flatList,list_all)
feild_newN.append(len(new_strings))# number of new unique strings in this interval, assuming the date intervals are chronological
feild_new.append(new_strings) # actual lists of unique new entries this interval
list_all = np.union1d(flatList,list_all)
df_stats['Work_Counts'] = work_counts
df_stats['Unique_Count_' + feildName] = feild_uniqueN
df_stats['Unique_' + feildName] = feild_unique
df_stats['Mean_Count_' + feildName] = feild_meanN
df_stats['New_Count_' + feildName] = feild_newN
df_stats['New_' + feildName] = feild_new
return df_stats