-
-
Notifications
You must be signed in to change notification settings - Fork 288
/
mtlnation.py
87 lines (76 loc) · 2.8 KB
/
mtlnation.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# -*- coding: utf-8 -*-
import json
import logging
from urllib.parse import urlencode, urlparse
from lncrawl.core.crawler import Crawler
logger = logging.getLogger(__name__)
class MTLNation(Crawler):
base_url = [
"https://mtlnation.com/",
"https://www.mtlnation.com/",
]
has_mtl = True
def initialize(self):
self.init_executor(1)
def login(self, email: str, password: str) -> None:
self.post_json(
"https://api.mtlnation.com/api/v2/accounts/login",
data=json.dumps(
{
"identity": email,
"password": password,
}
),
)
jwt = self.cookies.get("jwt")
self.set_header("authorization", f"JWT {jwt}")
logger.info("Logged in with jwt %s", jwt)
def search_novel(self, query):
data = self.get_json(
"https://api.mtlnation.com/api/v2/novels/?"
+ urlencode(
{
"max_word_count": 0,
"min_word_count": 0,
"query": query,
"sort": "chapter_new",
}
)
)
results = []
for item in data["data"]:
results.append(
{
"title": item["title"],
"url": f"https://mtlnation.com/novel/{item['slug']}",
"info": "Chapters: %d | Rating: %d | Author: %s"
% (item["chapter_count"], item["rating"], item["author"]),
}
)
return results
def read_novel_info(self):
slug = urlparse(self.novel_url).path.rstrip("/").split("/")[-1]
data = self.get_json(f"https://api.mtlnation.com/api/v2/novels/{slug}")
self.novel_title = data["data"]["title"]
self.novel_author = data["data"]["author"]
self.novel_cover = "https://api.mtlnation.com/media/" + data["data"]["cover"]
data = self.get_json(
f"https://api.mtlnation.com/api/v2/novels/{data['data']['id']}/chapters/"
)
for item in data["data"]:
chap_id = len(self.chapters) + 1
vol_id = len(self.chapters) // 100 + 1
if vol_id > len(self.volumes):
self.volumes.append({"id": vol_id})
self.chapters.append(
{
"id": chap_id,
"volume": vol_id,
"title": item["title"],
"url": f"https://mtlnation.com/novel/{slug}/{item['slug']}",
"data_url": f"https://api.mtlnation.com/api/v2/chapters/{slug}/{item['slug']}",
}
)
def download_chapter_body(self, chapter):
data = self.get_json(chapter["data_url"])
return data["data"]["content"]