kakalot: Added search functionality.
authorFredrik Tolf <fredrik@dolda2000.com>
Mon, 13 Jan 2020 12:23:25 +0000 (13:23 +0100)
committerFredrik Tolf <fredrik@dolda2000.com>
Mon, 13 Jan 2020 12:23:25 +0000 (13:23 +0100)
manga/kakalot.py

index 96a7c28..9a5fb03 100644 (file)
@@ -1,4 +1,4 @@
-import bs4
+import bs4, json, urllib
 from urllib.parse import urljoin
 from . import lib, htcache
 soup = bs4.BeautifulSoup
@@ -108,3 +108,17 @@ class library(lib.library):
         if name is None: raise KeyError(id)
         name = name.string
         return manga(self, id, name, url)
+
+    def search(self, expr):
+        values = {"searchword": expr}
+        req = urllib.request.Request(self.base + "home_json_search",
+                                     urllib.parse.urlencode(values).encode("ascii"),
+                                     headers={"User-Agent": "automanga/1"})
+        with urllib.request.urlopen(req) as resp:
+            rc = json.loads(resp.read().decode("utf-8"))
+        for obj in rc:
+            if "nameunsigned" in obj:
+                try:
+                    yield self.byid(obj["nameunsigned"])
+                except KeyError:
+                    pass