Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import re
- import requests
- import yaml
- from json import loads
- from sys import argv
- from bs4 import BeautifulSoup
- with open("rules.yaml") as f:
- rules = yaml.safe_load(f.read())["rules"]
- rules = {k: v for r in rules.values() for k,v in r.items()}
- no_fix = False
- print_only = True
- def fix(line):
- note = []
- for k, v in rules.items():
- old = line
- line = re.sub(k, v["result"] if type(v) == dict else v, line)
- if line != old and type(v) == dict:
- note.append(v["note"])
- return line, note
- if len(argv) < 2:
- print("Argument should be syosetu chapter number, ie last part of url")
- exit(1)
- url = "https://ncode.syosetu.com/n2267be/{}/".format(argv[1])
- headers = {"User-Agent": "ReZeroBot"}
- result = requests.get(url, headers=headers)
- contents = result.content.decode()
- soup = BeautifulSoup(contents, "html.parser")
- body = [str(i.text) for i in soup.find("div", {"id": "novel_honbun"}).find_all("p")]
- for line in body:
- if no_fix:
- print(line)
- continue
- line, note = fix(line)
- print("{}{}".format(line.lstrip().rstrip(), " ({})".format(note) if note else ""))
- continue
Advertisement
Add Comment
Please, Sign In to add comment