-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.py
420 lines (371 loc) · 15.1 KB
/
parser.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
"""parser.py downloads, parses, and creates messages from FFN and AO3 pages."""
from bs4 import BeautifulSoup
import AO3
# import cloudscraper
import config
import json
import re
import requests
HEADERS = {"User-Agent": "fanfiction-abstractor-bot"}
FFN_GENRES = set()
# create scraper to bypass cloudflare, always download desktop pages
# options = {"desktop": True, "browser": "firefox", "platform": "linux"}
# scraper = cloudscraper.create_scraper(browser=options)
# generate set of possible FFN genres
genres_list = ["Adventure", "Angst", "Crime", "Drama", "Family", "Fantasy",
"Friendship", "General", "Horror", "Humor", "Hurt/Comfort",
"Mystery", "Parody", "Poetry", "Romance", "Sci-Fi", "Spiritual",
"Supernatural", "Suspense", "Tragedy", "Western"]
for g1 in genres_list:
for g2 in genres_list:
FFN_GENRES.add(g1 + "/" + g2)
FFN_GENRES.add(g1)
# dictionary of emoji to numbers, for parsing reacts
REACTS = {"1️⃣": 1, "2️⃣": 2, "3️⃣": 3, "4️⃣": 4, "5️⃣": 5,
"6️⃣": 6, "7️⃣": 7, "8️⃣": 8, "9️⃣": 9, "🔟": 10}
def generate_ao3_work_summary(link):
"""Generate the summary of an AO3 work.
link should be a link to an AO3 fic
Returns the message with the fic info, or else a blank string
"""
r = requests.get(link, headers=HEADERS)
if r.status_code != requests.codes.ok:
return ""
soup = BeautifulSoup(r.text, "lxml")
if r.url == "https://archiveofourown.org/users/login?restricted=true":
ao3_session = AO3.Session(config.AO3_USERNAME, config.AO3_PASSWORD)
soup = ao3_session.request(link)
locked_fic = True
else:
locked_fic = False
preface = soup.find(class_="preface group")
if preface is None:
r = requests.get(link+"?view_adult=true", headers=HEADERS)
soup = BeautifulSoup(r.text, "lxml")
# if chapter link, replace with work link
if "/chapters/" in link:
share = soup.find(class_="share")
work_id = share.a["href"].strip("/works/").strip("/share")
link = "https://archiveofourown.org/works/{}".format(work_id)
preface = soup.find(class_="preface group")
title = preface.h2.text.strip()
author = preface.h3.string
if author is None:
author = ", ".join(map(lambda x: x.string, preface.h3.find_all("a")))
else:
author = author.strip()
summary = preface.find(class_="summary module")
if summary:
summary = format_html(summary)
tags = soup.find(class_="work meta group")
# print(tags)
rating = tags.find("dd", class_="rating tags")
category = tags.find("dd", class_="category tags")
fandoms = tags.find("dd", class_="fandom tags")
warnings = tags.find("dd", class_="warning tags")
relationships = tags.find("dd", class_="relationship tags")
characters = tags.find("dd", class_="character tags")
freeform = tags.find("dd", class_="freeform tags")
series = tags.find("dd", class_="series")
words = tags.find("dd", class_="words").string
chapters = tags.find("dd", class_="chapters").string
kudos = tags.find("dd", class_="kudos")
if kudos:
kudos = kudos.string
else:
kudos = 0
updated = tags.find("dd", class_="status")
if updated:
updated = updated.string
else:
updated = tags.find("dd", class_="published").string
if not locked_fic:
output = "**{}** (<{}>) by **{}**\n".format(title, link, author)
else:
output = ":lock: **{}** (<{}>) by **{}**\n".format(title, link, author)
if series:
series = series.find_all(class_="position")[:2]
for s in series:
s_name = s.text.split()
if s_name[3] == "the" and s_name[-1] == "series":
s = "**Part {}** of the **{}** series (<https://archiveofourown.org{}>)\n"\
.format(s_name[1], " ".join(s_name[4:-1]), s.a["href"])
else:
s = "**Part {}** of the **{}** series (<https://archiveofourown.org{}>)\n"\
.format(s_name[1], " ".join(s_name[3:]), s.a["href"])
output += s
if fandoms:
fandoms = list(map(lambda x: x.string, fandoms.find_all("a")))
if len(fandoms) > 5:
fandoms = ", ".join(fandoms[:5]) + ", …"
else:
fandoms = ", ".join(fandoms)
output += "**Fandoms:** {}\n".format(fandoms)
if category:
rating = ", ".join(map(lambda x: x.string, rating.find_all("a")))
category = ", ".join(map(lambda x: x.string, category.find_all("a")))
output += "**Rating:** {} **Category:** {}\n".format(
rating, category)
else:
rating = ", ".join(map(lambda x: x.string, rating.find_all("a")))
output += "**Rating:** {}\n".format(rating)
warnings = ", ".join(map(lambda x: x.string, warnings.find_all("a")))
output += "**Warnings:** {}\n".format(warnings)
if relationships:
relationships = list(map(
lambda x: x.string, relationships.find_all("a")))
relationship_list = relationships
if len(relationships) > 3:
relationships = ", ".join(relationships[:3]) + ", …"
else:
relationships = ", ".join(relationships)
output += "**Relationships:** {}\n".format(relationships)
if characters:
characters = list(map(lambda x: x.string, characters.find_all("a")))
# do not list characters already listed in relationships
if relationships:
already_listed = set()
for r in relationship_list[:3]:
r = r.replace(" & ", "/")
r = r.split("/")
for c in r:
if " (" in c:
c = c.split(" (")[0]
already_listed.add(c)
chars_static = characters.copy()
for c in chars_static:
before = c
if " (" in c:
c = c.split(" (")[0]
if " - " in c:
c = c.split(" - ")[0]
if c in already_listed:
characters.remove(before)
if len(characters) > 3:
characters = ", ".join(characters[:3]) + ", …"
else:
characters = ", ".join(characters)
if len(characters) > 0:
if relationships:
output += "**Additional Characters:** {}\n".format(characters)
else:
output += "**Characters:** {}\n".format(characters)
if freeform:
freeform = list(map(lambda x: x.string, freeform.find_all("a")))
if len(freeform) > 5:
freeform = ", ".join(freeform[:5]) + ", …"
else:
freeform = ", ".join(freeform)
output += "**Tags:** {}\n".format(freeform)
if summary:
output += "**Summary:** {}\n".format(summary)
output += "**Words:** {} **Chapters:** {} **Kudos:** {} **Updated:** {}".format(
words, chapters, kudos, updated)
return output
def generate_ao3_series_summary(link):
"""Generate the summary of an AO3 work.
link should be a link to an AO3 series
Returns the message with the series info, or else a blank string
"""
r = requests.get(link, headers=HEADERS)
if r.status_code != requests.codes.ok:
return ""
soup = BeautifulSoup(r.text, "lxml")
if r.url == "https://archiveofourown.org/users/login?restricted=true":
ao3_session = AO3.Session(config.AO3_USERNAME, config.AO3_PASSWORD)
soup = ao3_session.request(link)
locked_fic = True
else:
locked_fic = False
title = soup.find("h2", class_="heading").text.strip()
preface = soup.find(class_="series meta group")
next_field = preface.dd
author = ", ".join(map(lambda x: x.string, next_field.find_all("a")))
next_field = next_field.find_next_sibling("dd")
begun = next_field.string
next_field = next_field.find_next_sibling("dd")
updated = next_field.string
next_field = next_field.find_next_sibling("dt")
if next_field.string == "Description:":
next_field = next_field.find_next_sibling("dd")
description = format_html(next_field)
next_field = next_field.find_next_sibling("dt")
else:
description = None
if next_field.string == "Notes:":
next_field = next_field.find_next_sibling("dd")
notes = format_html(next_field)
next_field = next_field.find_next_sibling("dt")
else:
notes = None
next_field = next_field.find_next_sibling("dd").dl.dd
words = next_field.string
next_field = next_field.find_next_sibling("dd")
works = next_field.string
complete = next_field.find_next_sibling("dd").string
# format output
if not locked_fic:
output = "**{}** (<{}>) by **{}**\n".format(title, link, author)
else:
output = ":lock: **{}** (<{}>) by **{}**\n".format(title, link, author)
if description:
output += "**Description:** {}\n".format(description)
# if notes:
# output += "**Notes:** {}\n".format(notes)
output += "**Begun:** {} **Updated:** {}\n".format(begun, updated)
output += "**Words:** {} **Works:** {} **Complete:** {}\n\n".format(
words, works, complete)
# Find titles and links to first few works
works = soup.find_all(class_=re.compile("work blurb group work-.*"))
for i in range(min(3, len(works))):
title = works[i].h4.a
output += "{}. __{}__: <https://archiveofourown.org{}>\n".format(
i + 1, title.string, title["href"])
if len(works) == 4:
title = works[3].h4.a
output += "4. __{}__: <https://archiveofourown.org{}>".format(
title.string, title["href"])
elif len(works) > 4:
output += "** ** [and {} more works]".format(len(works) - 3)
else:
output = output[:-1]
return output
def identify_work_in_ao3_series(link, number):
"""Do something.
link should be a link to a series, number is an int for which fic
Returns the link to that number fic in the series, or else None
"""
r = requests.get(link, headers=HEADERS)
if r.status_code != requests.codes.ok:
return None
if r.url == "https://archiveofourown.org/users/login?restricted=true":
return None
soup = BeautifulSoup(r.text, "lxml")
preface = soup.find(class_="series meta group")
next_field = preface.find("dl", class_="stats").dd
next_field = next_field.find_next_sibling("dd")
works = int(next_field.string)
if works < number:
return None
# Find link to correct work
works = soup.find_all(class_=re.compile("work blurb group work-.*"))
fic = works[number - 1]
return fic.h4.a["href"]
def generate_ffn_work_summary(link):
"""Generate summary of FFN work.
link should be a link to an FFN fic
Returns the message with the fic info, or else a blank string
"""
fichub_link = "https://fichub.net/api/v0/epub?q=" + link
MY_HEADER = {"User-Agent": config.name}
r = requests.get(fichub_link, headers=MY_HEADER)
if r.status_code != requests.codes.ok:
return None
metadata = json.loads(r.text)["meta"]
title = metadata["title"]
author = metadata["author"]
summary = metadata["description"].strip("<p>").strip("</p>")
complete = metadata["status"]
chapters = metadata["chapters"]
words = metadata["words"]
updated = metadata["updated"].replace("T", " ")
stats = metadata["extraMeta"].split(" - ")
# next field varies. have fun identifying it!
# it's much easier using ficlab's data.
# order: rating, language, genre, characters, ~chapters, words,~~
# reviews, favs, follows, ~~updated, published, status, id~~
genre = None
characters = None
reviews = 0
favs = 0
follows = 0
for field in stats:
if "Rated: " in field:
rating = field.replace("Rated: Fiction ", "")
if "Genre: " in field:
genre = field.replace("Genre: ", "")
if "Characters: " in field:
characters = field.replace("Characters: ", "")
if "Reviews: " in field:
reviews = field.replace("Reviews: ", "")
if "Favs: " in field:
favs = field.replace("Favs: ", "")
if "Follows: " in field:
follows = field.replace("Follows: ", "")
output = "**{}** (<{}>) by **{}**\n".format(title, link, author)
# output += "**Fandoms:** {}\n".format(fandoms)
if genre:
output += "**Rating:** {} **Genre:** {}\n".format(rating, genre)
else:
output += "**Rating:** {}\n".format(rating)
if characters:
output += "**Characters:** {}\n".format(characters)
if summary:
output += "**Summary:** {}\n".format(summary)
# output += "**Reviews:** {} **Favs:** {} **Follows:** {}\n".format(\
# reviews, favs, follows)
if complete == "complete":
chapters = str(chapters) + "/" + str(chapters)
else:
chapters = str(chapters) + "/?"
output += "**Words:** {} **Chapters:** {} **Favs:** {} **Updated:** {}".format(
words, chapters, favs, updated)
return output
def generate_sb_summary(link):
"""Generate summary of SpaceBattles work.
link should be a link to a spacebattles fic
Returns the message with the fic info, or else a blank string
"""
fichub_link = "https://fichub.net/api/v0/epub?q=" + link
MY_HEADER = {"User-Agent": config.name}
r = requests.get(fichub_link, headers=MY_HEADER)
if r.status_code != requests.codes.ok:
return None
metadata = json.loads(r.text)["meta"]
title = metadata["title"]
author = metadata["author"]
summary = metadata["description"].strip("<p>").strip("</p>")
complete = metadata["status"]
chapters = metadata["chapters"]
words = metadata["words"]
updated = metadata["updated"].replace("T", " ")
output = "**{}** (<{}>) by **{}**\n".format(title, link, author)
# if summary:
# output += "**Summary:** {}\n".format(summary)
if complete == "complete":
chapters = str(chapters) + "/" + str(chapters)
else:
chapters = str(chapters) + "/?"
output += "**Words:** {} **Chapters:** {} **Updated:** {}".format(
words, chapters, updated)
return output
def format_html(field):
"""Format an HTML segment for discord markdown.
field should be a note or summary from AO3.
"""
brs = field.find_all("br")
for br in brs:
br.replace_with("\n")
ols = field.find_all("ol")
for ol in ols:
ol.name = "p"
uls = field.find_all("ul")
for ul in uls:
ul.name = "p"
for li in field.find_all("li"):
li.string = "- {}".format(li.text.strip())
li.unwrap()
field = field.blockquote.find_all("p")
result = list(map(lambda x: x.text.strip(), field))
result = "\n\n".join(result)
result = result.strip()
while "\n\n\n" in result:
result = result.replace("\n\n\n", "\n\n")
if result.count("\n\n") > 2:
result = "\n\n".join(result.split("\n\n")[:3])
if len(result) > 250:
result = result[:250].strip()
# i = result.rfind(" ")
# result = result[:i]
result += "…"
return result