-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.py
62 lines (44 loc) · 1.65 KB
/
query.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
import requests
import os
import re
import json
import csv
from bs4 import BeautifulSoup
from iterfzf import iterfzf
def search():
if "manga_index.csv" not in os.listdir():
dl_manga_index()
mangas = []
with open("manga_index.csv", mode="r", newline="", encoding="utf-8") as file:
reader = csv.DictReader(file)
for row in reader:
mangas.append(row)
selected = iterfzf([manga["s"] for manga in mangas], exact=True, multi=True)
if selected is None:
return None
names = [manga["i"] for manga in mangas if manga["s"] in selected]
return names
def get_updatables():
if "manga_index.csv" not in os.listdir():
dl_manga_index()
mangas = []
with open("manga_index.csv", mode="r", newline="", encoding="utf-8") as file:
reader = csv.DictReader(file)
for row in reader:
if row["ps"] == "Ongoing":
mangas.append(row["i"])
return mangas
def dl_manga_index():
query_url = "https://manga4life.com/search/"
soup = BeautifulSoup(requests.get(query_url).content, "html.parser")
pattern = re.compile(r"vm.Directory\s+=\s+\[(.*)\];")
script = soup.find("script", text=pattern)
directory = pattern.search(script.__str__())
if directory is None:
raise Exception("Manga index not found.")
data = '{ "mangas": ' + ("".join(directory.group().split(" = ")[1:])[:-1]) + "}"
mangas = json.loads(data)["mangas"]
with open("manga_index.csv", mode="w", newline="", encoding="utf-8") as file:
writer = csv.DictWriter(file, fieldnames=mangas[0].keys())
writer.writeheader()
writer.writerows(mangas)