Upload TheQueenDescends.py with huggingface_hub
Browse files- TheQueenDescends.py +125 -0
TheQueenDescends.py
ADDED
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
from loguru import logger
|
3 |
+
import markdownify
|
4 |
+
import msgspec
|
5 |
+
from selectolax.lexbor import LexborHTMLParser
|
6 |
+
import tqdm
|
7 |
+
|
8 |
+
decoder = msgspec.json.Decoder()
|
9 |
+
encoder = msgspec.json.Encoder()
|
10 |
+
|
11 |
+
md = markdownify.MarkdownConverter(escape_misc=False, escape_asterisks=False)
|
12 |
+
|
13 |
+
with open("ImperialRoad/royalroad-merge.jsonl", "rb") as f, open(
|
14 |
+
"TheRoyalCarpet.jsonl", "wb"
|
15 |
+
) as f2:
|
16 |
+
for line in tqdm.tqdm(f, desc="Processing Lines..."):
|
17 |
+
k = decoder.decode(line)
|
18 |
+
nodes = LexborHTMLParser(k["html"])
|
19 |
+
anti_air = None
|
20 |
+
for style in nodes.css("head > style"):
|
21 |
+
style_text = style.text(strip=True).replace("\n", " ")
|
22 |
+
if "display" in style_text and "speak" in style_text:
|
23 |
+
anti = re.search(r"(\..*)\{", style_text)
|
24 |
+
# logger.debug(f"Anti Detection: {}")
|
25 |
+
anti_air = anti.group(1)
|
26 |
+
if not anti_air:
|
27 |
+
logger.warning("Missing Anti Air defense.")
|
28 |
+
continue
|
29 |
+
for node in nodes.css(anti_air):
|
30 |
+
# logger.debug(f"Shooting bird... {node.text()}")
|
31 |
+
node.decompose()
|
32 |
+
novel_title = nodes.select("title")
|
33 |
+
chapter_data = nodes.select("div.chapter-inner.chapter-content")
|
34 |
+
if not novel_title or not chapter_data:
|
35 |
+
continue
|
36 |
+
# The actual novel title
|
37 |
+
novel_title = nodes.css_first('div > a[href*="/fiction/" i] > h2')
|
38 |
+
if not novel_title:
|
39 |
+
logger.warning("? No novel title?")
|
40 |
+
continue
|
41 |
+
royal_data = {"fiction": {}, "chapter": {}, "html": "", "text": ""}
|
42 |
+
royal_data["fiction"]["title"] = novel_title.text().rstrip()
|
43 |
+
novel_id = nodes.css_first('head > link[rel*="canon"i]')
|
44 |
+
# logger.debug(novel_id)
|
45 |
+
if novel_id:
|
46 |
+
royal_data["fiction"]["id"] = int(novel_id.attributes["href"].split("/")[4])
|
47 |
+
if novel_title and novel_title.parent and novel_title.parent.parent:
|
48 |
+
meta_root = novel_title.parent.parent
|
49 |
+
|
50 |
+
author_title = meta_root.css_first("h3.font-white.inline-block")
|
51 |
+
if not author_title:
|
52 |
+
logger.warning("? No novel title? Parents exist")
|
53 |
+
continue
|
54 |
+
royal_data["fiction"]["author"] = author_title.text(strip=True)
|
55 |
+
|
56 |
+
rating = meta_root.css_first("h4.font-red-thunderbird")
|
57 |
+
|
58 |
+
if rating and "class" in rating.attributes:
|
59 |
+
stars = [
|
60 |
+
i
|
61 |
+
for i in rating.attributes["class"].split(" ")
|
62 |
+
if i.startswith("star-")
|
63 |
+
][0]
|
64 |
+
pointer_rating: float = int(stars.split("-")[-1]) / 10
|
65 |
+
royal_data["fiction"]["rating"] = pointer_rating
|
66 |
+
# Chapter Title
|
67 |
+
chapter_title = meta_root.css_first("h1.font-white.break-word")
|
68 |
+
if chapter_title:
|
69 |
+
royal_data["chapter"]["title"] = chapter_title.text(strip=True)
|
70 |
+
|
71 |
+
# Chapter ID
|
72 |
+
chapter_id = nodes.css_first(
|
73 |
+
'a.red-thunderbird[href*="/report/chapter/" i]'
|
74 |
+
)
|
75 |
+
if chapter_id:
|
76 |
+
royal_data["chapter"]["id"] = int(
|
77 |
+
chapter_id.attributes["href"].split("/")[-1]
|
78 |
+
)
|
79 |
+
# Next & prev chapter selectors
|
80 |
+
next_chapter = nodes.css_first(
|
81 |
+
'div.row.nav-buttons > .col-lg-offset-6 > a[href*="/fiction/"]'
|
82 |
+
)
|
83 |
+
if next_chapter:
|
84 |
+
if not next_chapter.attributes["href"].split("/")[-2].isdigit():
|
85 |
+
logger.warning(next_chapter.attributes["href"])
|
86 |
+
whomst = int(
|
87 |
+
next_chapter.attributes["href"].split("/")[3].split("?")[0]
|
88 |
+
)
|
89 |
+
royal_data["chapter"]["next"] = whomst
|
90 |
+
royal_data["chapter"]["next_info"] = "Expect missing"
|
91 |
+
else:
|
92 |
+
royal_data["chapter"]["next"] = int(
|
93 |
+
next_chapter.attributes["href"].split("/")[-2]
|
94 |
+
)
|
95 |
+
royal_data["chapter"]["next_info"] = ""
|
96 |
+
else:
|
97 |
+
royal_data["chapter"]["next"] = None
|
98 |
+
|
99 |
+
prev_chapter = nodes.css_first(
|
100 |
+
'div.row.nav-buttons > div.col-xs-6.col-md-4.col-lg-3.col-xl-2 > a[href*="/fiction/"]'
|
101 |
+
)
|
102 |
+
if prev_chapter:
|
103 |
+
if not prev_chapter.attributes["href"].split("/")[-2].isdigit():
|
104 |
+
logger.warning(prev_chapter.attributes["href"])
|
105 |
+
whomst = int(
|
106 |
+
prev_chapter.attributes["href"].split("/")[3].split("?")[0]
|
107 |
+
)
|
108 |
+
royal_data["chapter"]["prev"] = whomst
|
109 |
+
royal_data["chapter"]["prev_info"] = "Expect missing"
|
110 |
+
else:
|
111 |
+
royal_data["chapter"]["prev"] = int(
|
112 |
+
prev_chapter.attributes["href"].split("/")[-2]
|
113 |
+
)
|
114 |
+
royal_data["chapter"]["prev_info"] = ""
|
115 |
+
else:
|
116 |
+
royal_data["chapter"]["prev"] = None
|
117 |
+
else:
|
118 |
+
logger.warning("? No novel author?")
|
119 |
+
|
120 |
+
# meta["html"] = chapter_data.matches[0].html
|
121 |
+
|
122 |
+
md_text = md.convert(chapter_data.matches[0].html)
|
123 |
+
royal_data["html"] = chapter_data.matches[0].html
|
124 |
+
royal_data["text"] = md_text.replace(" \n \n", "\n\n").strip()
|
125 |
+
f2.write(encoder.encode(royal_data) + b"\n")
|