import re from loguru import logger import markdownify import msgspec from selectolax.lexbor import LexborHTMLParser import tqdm decoder = msgspec.json.Decoder() encoder = msgspec.json.Encoder() md = markdownify.MarkdownConverter(escape_misc=False, escape_asterisks=False) with open("ImperialRoad/royalroad-merge.jsonl", "rb") as f, open( "TheRoyalCarpet.jsonl", "wb" ) as f2: for line in tqdm.tqdm(f, desc="Processing Lines..."): k = decoder.decode(line) nodes = LexborHTMLParser(k["html"]) anti_air = None for style in nodes.css("head > style"): style_text = style.text(strip=True).replace("\n", " ") if "display" in style_text and "speak" in style_text: anti = re.search(r"(\..*)\{", style_text) # logger.debug(f"Anti Detection: {}") anti_air = anti.group(1) if not anti_air: logger.warning("Missing Anti Air defense.") continue for node in nodes.css(anti_air): # logger.debug(f"Shooting bird... {node.text()}") node.decompose() novel_title = nodes.select("title") chapter_data = nodes.select("div.chapter-inner.chapter-content") if not novel_title or not chapter_data: continue # The actual novel title novel_title = nodes.css_first('div > a[href*="/fiction/" i] > h2') if not novel_title: logger.warning("? No novel title?") continue royal_data = {"fiction": {}, "chapter": {}, "html": "", "text": ""} royal_data["fiction"]["title"] = novel_title.text().rstrip() novel_id = nodes.css_first('head > link[rel*="canon"i]') # logger.debug(novel_id) if novel_id: royal_data["fiction"]["id"] = int(novel_id.attributes["href"].split("/")[4]) if novel_title and novel_title.parent and novel_title.parent.parent: meta_root = novel_title.parent.parent author_title = meta_root.css_first("h3.font-white.inline-block") if not author_title: logger.warning("? No novel title? Parents exist") continue royal_data["fiction"]["author"] = author_title.text(strip=True) rating = meta_root.css_first("h4.font-red-thunderbird") if rating and "class" in rating.attributes: stars = [ i for i in rating.attributes["class"].split(" ") if i.startswith("star-") ][0] pointer_rating: float = int(stars.split("-")[-1]) / 10 royal_data["fiction"]["rating"] = pointer_rating # Chapter Title chapter_title = meta_root.css_first("h1.font-white.break-word") if chapter_title: royal_data["chapter"]["title"] = chapter_title.text(strip=True) # Chapter ID chapter_id = nodes.css_first( 'a.red-thunderbird[href*="/report/chapter/" i]' ) if chapter_id: royal_data["chapter"]["id"] = int( chapter_id.attributes["href"].split("/")[-1] ) # Next & prev chapter selectors next_chapter = nodes.css_first( 'div.row.nav-buttons > .col-lg-offset-6 > a[href*="/fiction/"]' ) if next_chapter: if not next_chapter.attributes["href"].split("/")[-2].isdigit(): logger.warning(next_chapter.attributes["href"]) whomst = int( next_chapter.attributes["href"].split("/")[3].split("?")[0] ) royal_data["chapter"]["next"] = whomst royal_data["chapter"]["next_info"] = "Expect missing" else: royal_data["chapter"]["next"] = int( next_chapter.attributes["href"].split("/")[-2] ) royal_data["chapter"]["next_info"] = "" else: royal_data["chapter"]["next"] = None prev_chapter = nodes.css_first( 'div.row.nav-buttons > div.col-xs-6.col-md-4.col-lg-3.col-xl-2 > a[href*="/fiction/"]' ) if prev_chapter: if not prev_chapter.attributes["href"].split("/")[-2].isdigit(): logger.warning(prev_chapter.attributes["href"]) whomst = int( prev_chapter.attributes["href"].split("/")[3].split("?")[0] ) royal_data["chapter"]["prev"] = whomst royal_data["chapter"]["prev_info"] = "Expect missing" else: royal_data["chapter"]["prev"] = int( prev_chapter.attributes["href"].split("/")[-2] ) royal_data["chapter"]["prev_info"] = "" else: royal_data["chapter"]["prev"] = None else: logger.warning("? No novel author?") # meta["html"] = chapter_data.matches[0].html md_text = md.convert(chapter_data.matches[0].html) royal_data["html"] = chapter_data.matches[0].html royal_data["text"] = md_text.replace(" \n \n", "\n\n").strip() f2.write(encoder.encode(royal_data) + b"\n")