Repos / pytaku / 887cadec11
commit 887cadec11a7d141dfa0b6328a79b2772bc7aa6e
Author: Bùi Thành Nhân <hi@imnhan.com>
Date:   Wed Aug 5 19:32:04 2020 +0700

    site as url param

diff --git a/src/mangoapi/__init__.py b/src/mangoapi/__init__.py
index 50ff31c..9e66dd7 100644
--- a/src/mangoapi/__init__.py
+++ b/src/mangoapi/__init__.py
@@ -108,5 +108,8 @@ def search_title(user_cookies, query):
     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]
+    titles = [
+        {"id": int(id), "name": name.strip(), "site": "mangadex"}
+        for id, name in matches
+    ]
     return titles
diff --git a/src/pytaku/main.py b/src/pytaku/main.py
index bbfb064..61b98c0 100644
--- a/src/pytaku/main.py
+++ b/src/pytaku/main.py
@@ -38,6 +38,13 @@ def home_view():
     return render_template("home.html")
 
 
+@app.route("/follow", methods=["POST"])
+def follow_view():
+    title_id = request.form.get("title_id")
+    site = request.form.get("site", "mangadex")
+    return redirect(url_for(""))
+
+
 @app.route("/logout", methods=["POST"])
 def logout_view():
     session.pop("user")
@@ -125,9 +132,9 @@ def auth_view():
     return render_template("auth.html")
 
 
-@app.route("/title/mangadex/<title_id>")
-def title_view(title_id):
-    title = load_title(title_id)
+@app.route("/title/<site>/<title_id>")
+def title_view(site, title_id):
+    title = load_title(site, title_id)
     if not title:
         print("Getting title", title_id)
         title = get_title(title_id)
@@ -135,12 +142,13 @@ def title_view(title_id):
         save_title(title)
     else:
         print("Loading title", title_id, "from db")
+    title["site"] = site
     return render_template("title.html", **title)
 
 
-@app.route("/chapter/mangadex/<chapter_id>")
-def chapter_view(chapter_id):
-    chapter = load_chapter(chapter_id)
+@app.route("/chapter/<site>/<chapter_id>")
+def chapter_view(site, chapter_id):
+    chapter = load_chapter(site, chapter_id)
     if not chapter:
         print("Getting chapter", chapter_id)
         chapter = get_chapter(chapter_id)
@@ -153,11 +161,12 @@ def chapter_view(chapter_id):
     ]
 
     # YIIIIKES
-    title = load_title(chapter["title_id"])
+    title = load_title(site, chapter["title_id"])
     prev_chapter, next_chapter = get_prev_next_chapters(title, chapter)
     chapter["prev_chapter"] = prev_chapter
     chapter["next_chapter"] = next_chapter
 
+    chapter["site"] = site
     return render_template("chapter.html", **chapter)
 
 
diff --git a/src/pytaku/persistence.py b/src/pytaku/persistence.py
index 61bfde4..f8c81ee 100644
--- a/src/pytaku/persistence.py
+++ b/src/pytaku/persistence.py
@@ -47,7 +47,7 @@ def save_title(title):
     )
 
 
-def load_title(title_id):
+def load_title(site, title_id):
     conn = get_conn()
     result = list(
         conn.cursor().execute(
@@ -55,15 +55,16 @@ def load_title(title_id):
     SELECT id, name, site, cover_ext, chapters, alt_names, descriptions
     FROM title
     WHERE id = ?
+      AND site = ?
       AND datetime(updated_at) > datetime('now', '-6 hours');
     """,
-            (title_id,),
+            (title_id, site),
         )
     )
     if not result:
         return None
     elif len(result) > 1:
-        raise Exception(f"Found multiple results for title_id {title_id}!")
+        raise Exception(f"Found multiple results for title_id {title_id} on {site}!")
     else:
         title = result[0]
         return {
@@ -117,16 +118,16 @@ def save_chapter(chapter):
     )
 
 
-def load_chapter(chapter_id):
+def load_chapter(site, chapter_id):
     conn = get_conn()
     result = list(
         conn.cursor().execute(
             """
     SELECT id, title_id, num_major, num_minor, name, pages, groups, is_webtoon
     FROM chapter
-    WHERE id = ?;
+    WHERE id = ? AND site=?;
     """,
-            (chapter_id,),
+            (chapter_id, site),
         )
     )
     if not result:
diff --git a/src/pytaku/templates/chapter.html b/src/pytaku/templates/chapter.html
index b6a5665..04aa572 100644
--- a/src/pytaku/templates/chapter.html
+++ b/src/pytaku/templates/chapter.html
@@ -9,7 +9,7 @@
 {% block head %}
 
 {% if next_chapter %}
-<link rel="prefetch" href="{{ url_for('chapter_view', chapter_id=next_chapter['id'])}}">
+<link rel="prefetch" href="{{ url_for('chapter_view', site=site, chapter_id=next_chapter['id'])}}">
 {% endif %}
 
 <style>
@@ -62,15 +62,15 @@ <h1>{{ self.title() }}</h1>
 {% block buttons %}
 <div class="buttons">
   {% if prev_chapter %}
-  {{ ibutton(href=url_for('chapter_view', chapter_id=prev_chapter['id']), left_icon='chevrons-left', text='Prev') }}
+  {{ ibutton(href=url_for('chapter_view', site=site, chapter_id=prev_chapter['id']), left_icon='chevrons-left', text='Prev') }}
   {% else %}
   {{ ibutton(left_icon='chevrons-left', text='Prev', disabled=True) }}
   {% endif %}
 
-  {{ ibutton(href=url_for('title_view', title_id=title_id), left_icon='list', text='Chapter list', color='blue') }}
+  {{ ibutton(href=url_for('title_view', title_id=title_id, site=title_site), left_icon='list', text='Chapter list', color='blue') }}
 
   {% if next_chapter %}
-  {{ ibutton(href=url_for('chapter_view', chapter_id=next_chapter['id']), right_icon='chevrons-right', text='Next') }}
+  {{ ibutton(href=url_for('chapter_view', site=site, chapter_id=next_chapter['id']), right_icon='chevrons-right', text='Next') }}
   {% else %}
   {{ ibutton(right_icon='chevrons-right', text='Next', disabled=True) }}
   {% endif %}
diff --git a/src/pytaku/templates/search.html b/src/pytaku/templates/search.html
index f5240e6..feb098d 100644
--- a/src/pytaku/templates/search.html
+++ b/src/pytaku/templates/search.html
@@ -46,7 +46,7 @@ <h1>No results for "{{ query }}".</h1>
 
   <div class="results">
   {% for title in titles %}
-    <a class="result" href="{{ url_for('title_view', title_id=title['id']) }}"
+    <a class="result" href="{{ url_for('title_view', title_id=title['id'], site=title['site']) }}"
        title="{{ title['name'] }}">
       <img src="https://mangadex.org/images/manga/{{ title['id'] }}.large.jpg" alt="">
       <span>{{ title['name'] | truncate(50) }}</span>
diff --git a/src/pytaku/templates/title.html b/src/pytaku/templates/title.html
index d0d098a..5c52ef3 100644
--- a/src/pytaku/templates/title.html
+++ b/src/pytaku/templates/title.html
@@ -30,7 +30,7 @@ <h1>{{ name }}</h1>
   {% for chapter in chapters %}
   <tr>
     <td>
-      <a href="{{ url_for('chapter_view', chapter_id=chapter['id']) }}">
+      <a href="{{ url_for('chapter_view', chapter_id=chapter['id'], site=site) }}">
         Chapter {{ chapter['number'] }}
         {% if chapter['volume'] %}Volume {{ chapter['volume'] }} {% endif %}
         {% if chapter['name'] %}- {{ chapter['name'] }} {% endif %}