Repos / pytaku / 91b50d86be
commit 91b50d86be316b229929c23905014f53ff01c067
Author: Bùi Thành Nhân <hi@imnhan.com>
Date: Sat Aug 1 20:48:19 2020 +0700
login & search
diff --git a/.gitignore b/.gitignore
index 32201b5..68e2f20 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
+/debug.py
__pycache__
*.pyc
*.json
diff --git a/src/mangoapi/__init__.py b/src/mangoapi/__init__.py
index 36b053a..ce64c4c 100644
--- a/src/mangoapi/__init__.py
+++ b/src/mangoapi/__init__.py
@@ -1,5 +1,13 @@
+import re
+
import requests
+# Titles regex slightly adapted from https://github.com/md-y/mangadex-full-api
+# Thanks!
+TITLES_PATTERN = re.compile(
+ r"""<a[^>]*href=["']\/title\/(\d+)\/\S+["'][^>]*manga_title[^>]*>([^<]*)<"""
+)
+
def _parse_chapter_number(string):
nums = string.split(".")
@@ -62,3 +70,33 @@ def get_chapter(chapter_id):
**_parse_chapter_number(md_json["chapter"]),
}
return chapter
+
+
+def login(username, password):
+ """
+ Returns cookies of a logged in user.
+ """
+ form_data = {
+ "login_username": username,
+ "login_password": password,
+ "two_factor": "",
+ "remember_me": "1",
+ }
+ md_resp = requests.post(
+ "https://mangadex.org/ajax/actions.ajax.php?function=login",
+ data=form_data,
+ headers={"X-Requested-With": "XMLHttpRequest"},
+ )
+ assert md_resp.status_code == 200, md_resp.text
+ return dict(md_resp.cookies)
+
+
+def search_title(user_cookies, query):
+ md_resp = requests.get(
+ f"https://mangadex.org/quick_search/{query}", cookies=user_cookies,
+ )
+ assert md_resp.status_code == 200, md_resp.text
+
+ matches = TITLES_PATTERN.findall(md_resp.text)
+ titles = [{"id": int(id), "name": name.strip()} for id, name in matches]
+ return titles