Guest User

trans.py

a guest
Jan 24th, 2022
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import re
  4. import requests
  5. import yaml
  6.  
  7. from json import loads
  8. from sys import argv
  9.  
  10. from bs4 import BeautifulSoup
  11.  
  12. with open("rules.yaml") as f:
  13.     rules = yaml.safe_load(f.read())["rules"]
  14.  
  15. rules = {k: v for r in rules.values() for k,v in r.items()}
  16.  
  17. no_fix = False
  18. print_only = True
  19.  
  20. def fix(line):
  21.     note = []
  22.     for k, v in rules.items():
  23.         old = line
  24.         line = re.sub(k, v["result"] if type(v) == dict else v, line)
  25.         if line != old and type(v) == dict:
  26.             note.append(v["note"])
  27.     return line, note
  28.  
  29. if len(argv) < 2:
  30.     print("Argument should be syosetu chapter number, ie last part of url")
  31.     exit(1)
  32.  
  33. url = "https://ncode.syosetu.com/n2267be/{}/".format(argv[1])
  34. headers = {"User-Agent": "ReZeroBot"}
  35. result = requests.get(url, headers=headers)
  36.  
  37. contents = result.content.decode()
  38.  
  39. soup = BeautifulSoup(contents, "html.parser")
  40. body = [str(i.text) for i in soup.find("div", {"id": "novel_honbun"}).find_all("p")]
  41.  
  42. for line in body:
  43.     if no_fix:
  44.         print(line)
  45.         continue
  46.  
  47.     line, note = fix(line)
  48.  
  49.     print("{}{}".format(line.lstrip().rstrip(), " ({})".format(note) if note else ""))
  50.     continue
Advertisement
Add Comment
Please, Sign In to add comment