diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | app.py | 29 | ||||
-rw-r--r-- | downloader.py | 54 | ||||
-rw-r--r-- | static/zips/306013.zip | bin | 0 -> 7142770 bytes | |||
-rw-r--r-- | templates/nhdl.html | 6 |
5 files changed, 78 insertions, 12 deletions
@@ -1,6 +1,7 @@ edaweb.conf markdowns/ static/images/random.jpg +zips/*.zip # Byte-compiled / optimized / DLL files __pycache__/ @@ -3,6 +3,7 @@ from waitress import serve from PIL import Image import configparser import webbrowser +import downloader import datetime import database import services @@ -51,7 +52,6 @@ def get_template_items(title, db): @app.route("/") def index(): with database.Database() as db: - recentTweets = [] with open(os.path.join("static", "index.md"), "r") as f: return flask.render_template( "index.html", @@ -144,14 +144,29 @@ def serve_image(filename): @app.route("/nhdl") def serve_nhdl(): with database.Database() as db: - return flask.render_template( - "nhdl.html", - **get_template_items("Hentai Downloader", db) - ) + try: + nhentai_id = int(flask.request.args["id"]) + with downloader.CompressedImages(nhentai_id) as zippath: + # return app.send_static_file(os.path.split(zippath)[-1]) + return flask.redirect("/zip/%s" % os.path.split(zippath)[-1]) + + except (KeyError, ValueError): + return flask.render_template( + "nhdl.html", + **get_template_items("Hentai Downloader", db) + ) + +@app.route("/zip/<zipfile>") +def serve_zip(zipfile): + return flask.send_from_directory(os.path.join(".", "static", "zips"), zipfile) @app.route("/nhdlredirect", methods = ["POST"]) def redirect_nhdl(): - if flask.request.form["domain"] == "nhentai": + req = dict(flask.request.form) + try: + return flask.redirect("/nhdl?id=%i" % int(req["number_input"])) + except (TypeError, ValueError, KeyError): + flask.abort(400) @app.route("/random") @@ -195,7 +210,7 @@ if __name__ == "__main__": try: if sys.argv[1] == "--production": #serve(TransLogger(app), host='127.0.0.1', port = 6969) - serve(TransLogger(app), host='0.0.0.0', port = 6969, threads = 8) + serve(TransLogger(app), host='0.0.0.0', port = 6969, threads = 16) else: app.run(host = "0.0.0.0", port = 5001, debug = True) except IndexError: diff --git a/downloader.py b/downloader.py new file mode 100644 index 0000000..4b2af2f --- /dev/null +++ b/downloader.py @@ -0,0 +1,54 @@ +from dataclasses import dataclass +from lxml import html +import requests +import shutil +import urllib +import os + +@dataclass +class CompressedImages: + nhentai_id: int + + def __enter__(self): + self.folderpath = os.path.join("static", str(self.nhentai_id)) + self.zippath = os.path.join("static", "zips", "%i.zip" % self.nhentai_id) + os.mkdir(self.folderpath) + + self.num_downloaded = self.download_images("https://nhentai.net/g/%i" % self.nhentai_id, self.folderpath, "nhentai.net") + + shutil.make_archive(self.zippath[:-4], "zip", self.folderpath) + + return self.zippath + + def __exit__(self, type, value, traceback): + # os.remove(self.zippath) + shutil.rmtree(self.folderpath) + + def download_images(self, url:str, out:str, domain:str) -> int: + tree = html.fromstring(requests.get(url).content) + for i, element in enumerate(tree.xpath("//a[@class='gallerythumb']"), 1): + imurl = self.get_img("https://%s%s" % (domain, element.get("href")), i) + print(imurl) + self.dl_img(imurl, out) + + return i + + def get_img(self, srcurl:str, num:int) -> str: + tree = html.fromstring(requests.get(srcurl).content) + for element in tree.xpath("//img"): + try: + if num == int(os.path.splitext(element.get("src").split("/")[-1])[0]): + return element.get("src") + except ValueError: + pass + + def dl_img(self, imurl, outpath:str): + req = urllib.request.Request(imurl, headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_5_8) AppleWebKit/534.50.2 (KHTML, like Gecko) Version/5.0.6 Safari/533.22.3'}) + mediaContent = urllib.request.urlopen(req).read() + with open(os.path.join(outpath, imurl.split("/")[-1]), "wb") as f: + f.write(mediaContent) + +if __name__ == "__main__": + with CompressedImages(306013) as zippath: + import subprocess + subprocess.run(["cp", zippath, "/home/eden/Downloads"])
\ No newline at end of file diff --git a/static/zips/306013.zip b/static/zips/306013.zip Binary files differnew file mode 100644 index 0000000..d63f47d --- /dev/null +++ b/static/zips/306013.zip diff --git a/templates/nhdl.html b/templates/nhdl.html index 9b619ef..6702113 100644 --- a/templates/nhdl.html +++ b/templates/nhdl.html @@ -1,12 +1,8 @@ {% extends "template.html" %} {% block content %} <form action="/nhdlredirect" method="POST"> - <input type="radio" name="domain" value="nhentai" id="nhentai"> <label for="nhentai">nHentai.net number:</label> - <input type="text" id="number_input" name="number_input"><br> - <input type="radio" name="domain" value="other" id="other"> - <label for="other">Use full URL:</label> - <input type="text" id="url_input" name="url_input"><br><br> + <input type="text" id="number_input" name="number_input"><br><br> <input type="submit" value="Download"> </form> {% endblock %}
\ No newline at end of file |