Repos / pytaku / 79e93fa088
commit 79e93fa088292f2f8de0820966e39b9645de764f
Author: Bùi Thành Nhân <hi@imnhan.com>
Date: Sat Aug 1 18:28:21 2020 +0700
implement proxy to get around CORS restriction
diff --git a/src/mangoapi/__init__.py b/src/mangoapi/__init__.py
index f90608f..36b053a 100644
--- a/src/mangoapi/__init__.py
+++ b/src/mangoapi/__init__.py
@@ -13,8 +13,9 @@ def _parse_chapter_number(string):
def get_title(title_id):
- md_resp = requests.get(f"https://mangadex.org/api/?id={title_id}&type=manga")
- assert md_resp.status_code == 200
+ url = f"https://mangadex.org/api/?id={title_id}&type=manga"
+ md_resp = requests.get(url)
+ assert md_resp.status_code == 200, md_resp.text
md_json = md_resp.json()
assert md_json["status"] == "OK"
@@ -45,7 +46,7 @@ def get_chapter(chapter_id):
md_resp = requests.get(
f"https://mangadex.org/api/?id={chapter_id}&type=chapter&saver=0"
)
- assert md_resp.status_code == 200
+ assert md_resp.status_code == 200, md_resp.text
md_json = md_resp.json()
assert md_json["status"] == "OK"
diff --git a/src/pytaku/main.py b/src/pytaku/main.py
index 631deae..78c1d01 100644
--- a/src/pytaku/main.py
+++ b/src/pytaku/main.py
@@ -1,10 +1,19 @@
-from flask import Flask, render_template
+import base64
+import re
+
+import requests
+from flask import Flask, make_response, render_template, url_for
from mangoapi import get_chapter, get_title
app = Flask(__name__)
+@app.route("/")
+def home_view():
+ return render_template("home.html")
+
+
@app.route("/title/mangadex/<int:title_id>")
def title_view(title_id):
title = get_title(title_id)
@@ -14,9 +23,26 @@ def title_view(title_id):
@app.route("/chapter/mangadex/<int:chapter_id>")
def chapter_view(chapter_id):
chapter = get_chapter(chapter_id)
+ chapter["pages"] = [
+ url_for("proxy_view", b64_url=base64.urlsafe_b64encode(p.encode()).decode())
+ for p in chapter["pages"]
+ ]
return render_template("chapter.html", **chapter)
@app.route("/search")
def search_view():
return "TODO"
+
+
+@app.route("/proxy/<b64_url>")
+def proxy_view(b64_url):
+ """Fine I'll do it"""
+ url = base64.urlsafe_b64decode(b64_url).decode()
+ if not re.match(r"^https://\w+\.mangadex\.org/data/.+$", url):
+ return "Nope"
+ print("Proxying", url)
+ md_resp = requests.get(url)
+ resp = make_response(md_resp.content, md_resp.status_code)
+ resp.headers.extend(**md_resp.headers)
+ return resp
diff --git a/src/pytaku/templates/home.html b/src/pytaku/templates/home.html
new file mode 100644
index 0000000..1f05a23
--- /dev/null
+++ b/src/pytaku/templates/home.html
@@ -0,0 +1,13 @@
+{% extends 'base.html' %}
+
+{% block title %}
+Home
+{% endblock %}
+
+{% block content %}
+<h1>Sample links</h1>
+<ul>
+ <li><a href="/title/mangadex/23811">Title</a></li>
+ <li><a href="/chapter/mangadex/976942">Chapter</a></li>
+</ul>
+{% endblock %}