From 022e9935db0a1768b3159fd370107dc70d8df7fa Mon Sep 17 00:00:00 2001 From: Nyxordinal Date: Sun, 11 Sep 2022 00:07:00 +0700 Subject: [PATCH] feat: pdf argument --- .github/workflows/pdf.yml | 24 +++++++++++++ Makefile | 3 ++ README.md | 74 +++++++++++++++++++++------------------ tomoe/__init__.py | 2 +- tomoe/asmhentai.py | 49 ++++++-------------------- tomoe/hentai2read.py | 49 ++++++-------------------- tomoe/hentaifox.py | 49 ++++++-------------------- tomoe/nhentai.py | 49 ++++++-------------------- tomoe/pdf.py | 43 +++++++++++++++++++++++ tomoe/pururin.py | 50 ++++++-------------------- tomoe/simplyh.py | 51 +++++++-------------------- tomoe/utils/const.py | 2 ++ tomoe/utils/misc.py | 16 +++++++++ 13 files changed, 195 insertions(+), 266 deletions(-) create mode 100644 .github/workflows/pdf.yml create mode 100644 tomoe/pdf.py create mode 100644 tomoe/utils/const.py diff --git a/.github/workflows/pdf.yml b/.github/workflows/pdf.yml new file mode 100644 index 0000000..b9fa8c2 --- /dev/null +++ b/.github/workflows/pdf.yml @@ -0,0 +1,24 @@ +name: pdf render +on: + push: + branches: + - master + pull_request: + branches: + - master + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Install Python 3 + uses: actions/setup-python@v1 + with: + python-version: 3.8 + - name: Install dependencies + run: | + python -m pip install --upgrade pip + python setup.py install + - name: Download book with pdf render + run: make pdf diff --git a/Makefile b/Makefile index 709c204..1515132 100644 --- a/Makefile +++ b/Makefile @@ -24,6 +24,9 @@ asmhentai: bulk: tomoe --bulk doujin.json + +pdf: + tomoe --nhentai 255369 --pdf api-mock: # check api if something down python -m unittest test.test_api diff --git a/README.md b/README.md index 1bf13fd..3e0e692 100644 --- a/README.md +++ b/README.md @@ -7,11 +7,12 @@

-Tomoe is a CLI tool for downloading doujinshi from various doujinboards. It's also has built-in modular bulk downloads, and has auto render into PDF (**Portable Document Format**) for it's utility, hopefully will be reusable. +Tomoe is a CLI tool for downloading doujinshi from various doujinboards. It's also has built-in modular bulk download and auto render into PDF (**Portable Document Format**) for it's utility, hopefully will be reusable. 🚀 Commands • Documentation • Report Issues + - [Tomoe](#) @@ -30,29 +31,29 @@ Tomoe is a CLI tool for downloading doujinshi from various doujinboards. It's al - [Acknowledgements](./CLOSING_REMARKS.md) - [Legal](#legal) - [EoF](#eof) - + --- ## Tomoe vs. the doujinboards -Tomoe consumes [Jandapress](https://github.com/sinkaroid/jandapress) and uses [janda](https://pypi.org/project/janda/) for the doujin Python library. -**Features availability** Speed or perfomace may not accurate because internet connection or API response. Some tests has high resolve time and rendering a bit longer, because some source does not providing real extension of a images, tomoe should check and guessing it's format. +Tomoe consumes [Jandapress](https://github.com/sinkaroid/jandapress) and uses [janda](https://pypi.org/project/janda/) for the doujin Python library. -| Site | Status | Bulk download | Average response | Downloaded | -| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------ | ------------- | ---------------------- | -| `nhentai` | [![Nhentai download](https://github.com/sinkaroid/tomoe/workflows/nhentai/badge.svg)](https://github.com/sinkaroid/tomoe/actions/workflows/nhentai.yml) | `Yes` | ~0.52 minutes | ~10.39 MB (26 images) | -| `pururin` | [![Pururin download](https://github.com/sinkaroid/tomoe/workflows/pururin/badge.svg)](https://github.com/sinkaroid/tomoe/actions/workflows/pururin.yml) | `Yes` |~0.63 minutes | ~15.55 MB (20 images) | -| `hentaifox` | [![Hentaifox download](https://github.com/sinkaroid/tomoe/workflows/hentaifox/badge.svg)](https://github.com/sinkaroid/tomoe/actions/workflows/hentaifox.yml) | `Yes` |~0.33 minutes | ~8.18 MB (26 images) | -| `hentai2read` | [![Hentai2read download](https://github.com/sinkaroid/tomoe/workflows/hentai2read/badge.svg)](https://github.com/sinkaroid/tomoe/actions/workflows/hentai2read.yml) | `Yes` |~0.10 minutes | ~7.78 MB (26 images) | -| `simply-hentai` | [![Simply-hentai download](https://github.com/sinkaroid/tomoe/workflows/simplyh/badge.svg)](https://github.com/sinkaroid/tomoe/actions/workflows/simply.yml) | `Yes` |~0.29 minutes | ~42.61 MB (19 images) | -| `asmhentai` | [![Asmhentai download](https://github.com/sinkaroid/tomoe/workflows/asmhentai/badge.svg)](https://github.com/sinkaroid/tomoe/actions/workflows/asmhentai.yml) | `Yes` |~0.23 minutes | ~4.96 MB (23 images) | +**Features availability** Speed or performance may not be accurate because internet connection or API response. Some tests have high resolve time and render a bit longer, because some sources do not provide real extension of an images, tomoe should check and guess it's format. +| Site | Status | Bulk download | Average response | Downloaded | +| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------- | ---------------- | --------------------- | +| `nhentai` | [![Nhentai download](https://github.com/sinkaroid/tomoe/workflows/nhentai/badge.svg)](https://github.com/sinkaroid/tomoe/actions/workflows/nhentai.yml) | `Yes` | ~0.52 minutes | ~10.39 MB (26 images) | +| `pururin` | [![Pururin download](https://github.com/sinkaroid/tomoe/workflows/pururin/badge.svg)](https://github.com/sinkaroid/tomoe/actions/workflows/pururin.yml) | `Yes` | ~0.63 minutes | ~15.55 MB (20 images) | +| `hentaifox` | [![Hentaifox download](https://github.com/sinkaroid/tomoe/workflows/hentaifox/badge.svg)](https://github.com/sinkaroid/tomoe/actions/workflows/hentaifox.yml) | `Yes` | ~0.33 minutes | ~8.18 MB (26 images) | +| `hentai2read` | [![Hentai2read download](https://github.com/sinkaroid/tomoe/workflows/hentai2read/badge.svg)](https://github.com/sinkaroid/tomoe/actions/workflows/hentai2read.yml) | `Yes` | ~0.10 minutes | ~7.78 MB (26 images) | +| `simply-hentai` | [![Simply-hentai download](https://github.com/sinkaroid/tomoe/workflows/simplyh/badge.svg)](https://github.com/sinkaroid/tomoe/actions/workflows/simply.yml) | `Yes` | ~0.29 minutes | ~42.61 MB (19 images) | +| `asmhentai` | [![Asmhentai download](https://github.com/sinkaroid/tomoe/workflows/asmhentai/badge.svg)](https://github.com/sinkaroid/tomoe/actions/workflows/asmhentai.yml) | `Yes` | ~0.23 minutes | ~4.96 MB (23 images) | ## Features - Plenty of sites coverage - Built-in auto render into PDF -- Modular bulk downloads +- Modular bulk download - Minimalist dependencies - Download with ease, doesn't require you a lot of arguments @@ -60,7 +61,7 @@ Tomoe consumes [Jandapress](https://github.com/sinkaroid/jandapress) and uses [j ## Site support -Currently tomoe support the following websites: +Currently tomoe supports the following websites: - [nhentai.net](https://nhentai.net/) - [pururin.to](https://pururin.to/) @@ -69,8 +70,7 @@ Currently tomoe support the following websites: - [simply-hentai.com](https://simply-hentai.com/) - [asmhentai.com](https://asmhentai.com/) - -## Prerequisites +## Prequisites @@ -78,10 +78,9 @@ Currently tomoe support the following websites: ## Installation -`pip install tomoe` / `pipenv install tomoe` - -- Or manual build by cloning the repository and running `python setup.py install` +`pip install tomoe` / `pipenv install tomoe` +- Or manual build by cloning the repository and run `python setup.py install` ## Command usage @@ -97,18 +96,20 @@ After that, you could see the download results or throw you an error if somethin It's fairly simple to use tomoe -| **Argument** | **Description** | **Example** | -| --------------------------------- | --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| --nhentai, int | nhentai | [`tomoe --nhentai 255369`](https://nhentai.net/g/255369/) | -| --pururin, int | pururin | [`tomoe --pururin 47226`](https://pururin.to/gallery/47226/crot-sampe-lumpuh) | -| --hentaifox, int | hentaifox | [`tomoe --hentaifox 59026`](https://hentaifox.com/gallery/59026/) | -| --hentai2read, str chapter:number | hentai2read | [`tomoe --hentai2read chaldea_life:1`](https://hentai2read.com/chaldea_life/) | -| --simply, str | simply-hentai | [`tomoe --simply "fate-grand-order/fgo-no-ashibon-fgo-foot-book/all-pages"`](https://www.simply-hentai.com/fate-grand-order/fgo-no-ashibon-fgo-foot-book/all-pages) | -| --asmhentai, int | asmhentai | [`tomoe --asmhentai 311851`](https://asmhentai.com/g/311851/) | -| --bulk, str | custom bulk download | [`tomoe --bulk doujin.json`](/doujin.json) | +| **Argument** | **Description** | **Example** | +| --------------------------------- | ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| --nhentai, int | nhentai | [`tomoe --nhentai 255369`](https://nhentai.net/g/255369/) | +| --pururin, int | pururin | [`tomoe --pururin 47226`](https://pururin.to/gallery/47226/crot-sampe-lumpuh) | +| --hentaifox, int | hentaifox | [`tomoe --hentaifox 59026`](https://hentaifox.com/gallery/59026/) | +| --hentai2read, str chapter:number | hentai2read | [`tomoe --hentai2read chaldea_life:1`](https://hentai2read.com/chaldea_life/) | +| --simply, str | simply-hentai | [`tomoe --simply "fate-grand-order/fgo-no-ashibon-fgo-foot-book/all-pages"`](https://www.simply-hentai.com/fate-grand-order/fgo-no-ashibon-fgo-foot-book/all-pages) | +| --asmhentai, int | asmhentai | [`tomoe --asmhentai 311851`](https://asmhentai.com/g/311851/) | +| --bulk, str | custom bulk download | [`tomoe --bulk doujin.json`](/doujin.json) | +| --pdf, str | render pdf for each title | [`tomoe --nhentai 255369 --pdf`](https://nhentai.net/g/255369/) | ## Bulk download -You can passing multiple id to requests bulk download + +You can pass multiple id to request bulk download | **Sites** | **Description** | **Example** | | ----------- | ------------------------------- | -------------------------------------------------------------------------------------------------------------------------- | @@ -120,12 +121,14 @@ You can passing multiple id to requests bulk download | asmhentai | place multiple id | `tomoe --asmhentai 311851 210135 309068` | ## Bulk download using nested list -`tomoe --bulk doujin.json` -Since this tool covers plenty of website, If You need to download from each multiple different website You can make bulk download with arbitrary methods, Meant you can mix and wrap the whole book id into nested array in a JSON file. Read more about [Understanding Nested Arrays.](https://javascript.plainenglish.io/understanding-the-nested-arrays-fbf3ab13c2b4#:~:text=An%20array%20is%20an%20ordered,the%20element%20of%20an%20array) +`tomoe --bulk doujin.json` + +If you need to download multiple doujins from each different website, you can do a bulk download using an arbitrary method. Meaning that you can mix and wrap the whole book ids into nested array in a JSON file. Read more about [Understanding Nested Arrays.](https://javascript.plainenglish.io/understanding-the-nested-arrays-fbf3ab13c2b4#:~:text=An%20array%20is%20an%20ordered,the%20element%20of%20an%20array) + +Create `doujin.json` in the same directory where you want to run tomoe and follow this structure: +**Note** _You should not change the "book" property_ -Create `doujin.json` in the same directory You want to run tomoe and follow this structure: -**Note** *You should not change the "book" property* ```js { "book": [ @@ -163,13 +166,14 @@ Create `doujin.json` in the same directory You want to run tomoe and follow this } ``` -Then tells tomoe to resolve all the book id from the JSON file. +Then tell tomoe to resolve all the book ids from the JSON file. `tomoe --bulk doujin.json` ## Legal This tool can be freely copied, modified, altered, distributed without any attribution whatsoever. However, if you feel like this tool deserves an attribution, mention it. It won't hurt anybody. + > Licence: WTF. ## Pronounciation @@ -178,4 +182,4 @@ like this tool deserves an attribution, mention it. It won't hurt anybody. ## EoF -All books from those third-party doujinboards are definitely ilegal from original authors. Support the authors by buying the original book. \ No newline at end of file +All books from those third-party doujinboards are definitely illegal from original authors. Support the authors by buying the original book. diff --git a/tomoe/__init__.py b/tomoe/__init__.py index 0946551..4e703a3 100644 --- a/tomoe/__init__.py +++ b/tomoe/__init__.py @@ -1,4 +1,4 @@ -__version__ = "3.0.13" +__version__ = "3.0.14" from .pururin import get_pur from .nhentai import get_nh from .hentaifox import get_hfox diff --git a/tomoe/asmhentai.py b/tomoe/asmhentai.py index e984631..db75087 100644 --- a/tomoe/asmhentai.py +++ b/tomoe/asmhentai.py @@ -1,25 +1,27 @@ import asyncio -import janda -import requests import os import re import time + +import janda +import requests +from inputimeout import TimeoutOccurred + +from .pdf import process_pdf +from .utils.const import TOMOE_HTML from .utils.misc import ( choose, - split_name, get_size, - convert_html_to_pdf, - project, get_size_folder, log_data, log_file, log_final, log_warn, + project, + split_name, ) -from inputimeout import inputimeout, TimeoutOccurred asmh = janda.Asmhentai() -t: str = "tomoe.html" async def get_asm(ids=choose().asmhentai): @@ -101,7 +103,7 @@ async def process_asmhentai(id: int): f"{(time.time() - initial) / 60:.2f}", get_size_folder(neat_dir) ) - with open(neat_dir + "/" + t, "x", encoding="utf-8") as f: + with open(neat_dir + "/" + TOMOE_HTML, "x", encoding="utf-8") as f: f.write("
") f.write(f"

{parser['data']['id']}

") @@ -128,35 +130,6 @@ async def process_asmhentai(id: int): print(f"Directory: {os.path.abspath(neat_dir)}") try: - desired = inputimeout( - prompt="Do you want to render it all to .pdf? (y/n) ", - timeout=10, - ) - to_pdf = desired - - if to_pdf == "y": - try: - source = open(f"{neat_dir}/{t}") - output = f"{neat_dir}/{parser['data']['id']}.pdf" - filepdf = output.rsplit("/", 1)[-1] - - convert_html_to_pdf(source, output) - print( - f"Successfully rendered to {filepdf} | {get_size(output)} MB" - ) - - except Exception as e: - print(f"Something went wrong while converting to pdf: {e}") - - elif to_pdf == "n": - print("Okay") - os.remove(neat_dir + "/" + t) - return - - else: - print("Invalid input") - os.remove(neat_dir + "/" + t) - return - + process_pdf(neat_dir, parser["data"]["id"]) except TimeoutOccurred: print(f"Timeout occurred, not rendering pdf {id}") diff --git a/tomoe/hentai2read.py b/tomoe/hentai2read.py index cbabe9b..fe6a475 100644 --- a/tomoe/hentai2read.py +++ b/tomoe/hentai2read.py @@ -1,25 +1,27 @@ import asyncio -import janda -import requests import os import re import time + +import janda +import requests +from inputimeout import TimeoutOccurred + +from .pdf import process_pdf +from .utils.const import TOMOE_HTML from .utils.misc import ( choose, - split_name, get_size, - convert_html_to_pdf, - project, get_size_folder, log_data, log_file, log_final, log_warn, + project, + split_name, ) -from inputimeout import inputimeout, TimeoutOccurred h2r = janda.Hentai2read() -t: str = "tomoe.html" async def get_h2r(ids=choose().hentai2read): @@ -86,7 +88,7 @@ async def process_h2r(id: str, chapter: int): ) print(f"Directory: {os.path.abspath(neat_dir)}") - with open(neat_dir + "/" + t, "x", encoding="utf-8") as f: + with open(neat_dir + "/" + TOMOE_HTML, "x", encoding="utf-8") as f: f.write("
") f.write(f"

{parser['data']['title']}

") @@ -99,36 +101,7 @@ async def process_h2r(id: str, chapter: int): f.close() try: - desired = inputimeout( - prompt="Do you want to render it all to .pdf? (y/n) ", - timeout=10, - ) - to_pdf = desired - - if to_pdf == "y": - try: - source = open(f"{neat_dir}/{t}") - output = f"{neat_dir}/{parser['data']['title']}.pdf" - filepdf = output.rsplit("/", 1)[-1] - - convert_html_to_pdf(source, output) - print( - f"Successfully rendered to {filepdf} | {get_size(output)} MB" - ) - - except Exception as e: - print(f"Something went wrong while converting to pdf: {e}") - - elif to_pdf == "n": - print("Okay") - os.remove(neat_dir + "/" + t) - return - - else: - print("Invalid input") - os.remove(neat_dir + "/" + t) - return - + process_pdf(neat_dir, parser["data"]["title"]) except TimeoutOccurred: print( f"Timeout occurred, not rendering pdf {id}, chapter: {chapter}" diff --git a/tomoe/hentaifox.py b/tomoe/hentaifox.py index 13e29e1..f6dc650 100644 --- a/tomoe/hentaifox.py +++ b/tomoe/hentaifox.py @@ -1,25 +1,27 @@ import asyncio -import janda -import requests import os import re import time + +import janda +import requests +from inputimeout import TimeoutOccurred + +from .pdf import process_pdf +from .utils.const import TOMOE_HTML from .utils.misc import ( choose, - split_name, get_size, - convert_html_to_pdf, - project, get_size_folder, log_data, log_file, log_final, log_warn, + project, + split_name, ) -from inputimeout import inputimeout, TimeoutOccurred hfox = janda.Hentaifox() -t: str = "tomoe.html" async def get_hfox(ids=choose().hentaifox): @@ -101,7 +103,7 @@ async def process_hentaifox(id: int): f"{(time.time() - initial) / 60:.2f}", get_size_folder(neat_dir) ) - with open(neat_dir + "/" + t, "x", encoding="utf-8") as f: + with open(neat_dir + "/" + TOMOE_HTML, "x", encoding="utf-8") as f: f.write("
") f.write(f"

{parser['data']['id']}

") @@ -128,35 +130,6 @@ async def process_hentaifox(id: int): print(f"Directory: {os.path.abspath(neat_dir)}") try: - desired = inputimeout( - prompt="Do you want to render it all to .pdf? (y/n) ", - timeout=10, - ) - to_pdf = desired - - if to_pdf == "y": - try: - source = open(f"{neat_dir}/{t}") - output = f"{neat_dir}/{parser['data']['id']}.pdf" - filepdf = output.rsplit("/", 1)[-1] - - convert_html_to_pdf(source, output) - print( - f"Successfully rendered to {filepdf} | {get_size(output)} MB" - ) - - except Exception as e: - print(f"Something went wrong while converting to pdf: {e}") - - elif to_pdf == "n": - print("Okay") - os.remove(neat_dir + "/" + t) - return - - else: - print("Invalid input") - os.remove(neat_dir + "/" + t) - return - + process_pdf(neat_dir, parser["data"]["id"]) except TimeoutOccurred: print(f"Timeout occurred, not rendering pdf {id}") diff --git a/tomoe/nhentai.py b/tomoe/nhentai.py index 45e5d7b..557c8bd 100644 --- a/tomoe/nhentai.py +++ b/tomoe/nhentai.py @@ -1,25 +1,27 @@ import asyncio -import janda -import requests import os import re import time + +import janda +import requests +from inputimeout import TimeoutOccurred + +from .pdf import process_pdf +from .utils.const import TOMOE_HTML from .utils.misc import ( choose, - split_name, get_size, - convert_html_to_pdf, - project, get_size_folder, log_data, log_file, log_final, log_warn, + project, + split_name, ) -from inputimeout import inputimeout, TimeoutOccurred nh = janda.Nhentai() -t: str = "tomoe.html" async def get_nh(ids=choose().nhentai): @@ -90,7 +92,7 @@ async def process_nhentai(id: int): f"{(time.time() - initial) / 60:.2f}", get_size_folder(neat_dir) ) - with open(neat_dir + "/" + t, "x", encoding="utf-8") as f: + with open(neat_dir + "/" + TOMOE_HTML, "x", encoding="utf-8") as f: f.write("
") f.write(f"

{parser['data']['id']}

") @@ -105,35 +107,6 @@ async def process_nhentai(id: int): print(f"Directory: {os.path.abspath(neat_dir)}") try: - desired = inputimeout( - prompt="Do you want to render it all to .pdf? (y/n) ", - timeout=10, - ) - to_pdf = desired - - if to_pdf == "y": - try: - source = open(f"{neat_dir}/{t}") - output = f"{neat_dir}/{parser['data']['id']}.pdf" - filepdf = output.rsplit("/", 1)[-1] - - convert_html_to_pdf(source, output) - print( - f"Successfully rendered to {filepdf} | {get_size(output)} MB" - ) - - except Exception as e: - print(f"Something went wrong while converting to pdf: {e}") - - elif to_pdf == "n": - print("Okay") - os.remove(neat_dir + "/" + t) - return - - else: - print("Invalid input") - os.remove(neat_dir + "/" + t) - return - + process_pdf(neat_dir, parser["data"]["id"]) except TimeoutOccurred: print(f"Timeout occurred, not rendering pdf {id}") diff --git a/tomoe/pdf.py b/tomoe/pdf.py new file mode 100644 index 0000000..9b3e513 --- /dev/null +++ b/tomoe/pdf.py @@ -0,0 +1,43 @@ +import os + +from inputimeout import inputimeout + +from .utils.const import TOMOE_HTML +from .utils.misc import choose, convert_html_to_pdf, get_size + + +def process_pdf(dir: str, id: str): + if choose().pdf: + render_pdf(dir, id) + else: + desired = inputimeout( + prompt="Do you want to render it all to .pdf? (y/n) ", + timeout=10, + ) + to_pdf = desired + + if to_pdf == "y": + try: + render_pdf(dir, id) + + except Exception as e: + print(f"Something went wrong while converting to pdf: {e}") + + elif to_pdf == "n": + print("Okay") + os.remove(dir + "/" + TOMOE_HTML) + return + + else: + print("Invalid input") + os.remove(dir + "/" + TOMOE_HTML) + return + + +def render_pdf(dir: str, id: str): + source = open(f"{dir}/{TOMOE_HTML}") + output = f"{dir}/{id}.pdf" + filepdf = output.rsplit("/", 1)[-1] + + convert_html_to_pdf(source, output) + print(f"Successfully rendered to {filepdf} | {get_size(output)} MB") diff --git a/tomoe/pururin.py b/tomoe/pururin.py index e7b5eed..9b66b93 100644 --- a/tomoe/pururin.py +++ b/tomoe/pururin.py @@ -1,25 +1,27 @@ import asyncio -import janda -import requests import os import re import time + +import janda +import requests +from inputimeout import TimeoutOccurred + +from .pdf import process_pdf +from .utils.const import TOMOE_HTML from .utils.misc import ( choose, - split_name, get_size, - convert_html_to_pdf, - project, get_size_folder, log_data, log_file, log_final, log_warn, + project, + split_name, ) -from inputimeout import inputimeout, TimeoutOccurred pururin = janda.Pururin() -t: str = "tomoe.html" async def get_pur(ids=choose().pururin): @@ -93,7 +95,7 @@ async def process_pururin(id: int): ) print(f"Directory: {os.path.abspath(neat_dir)}") - with open(neat_dir + "/" + t, "x", encoding="utf-8") as f: + with open(neat_dir + "/" + TOMOE_HTML, "x", encoding="utf-8") as f: f.write("
") f.write(f"

{parser['data']['id']}

") @@ -106,36 +108,6 @@ async def process_pururin(id: int): f.close() try: - desired = inputimeout( - prompt="Do you want to render it all to .pdf? (y/n) ", - timeout=10, - ) - to_pdf = desired - - if to_pdf == "y": - try: - source = open(f"{neat_dir}/{t}") - output = f"{neat_dir}/{parser['data']['id']}.pdf" - filepdf = output.rsplit("/", 1)[-1] - - convert_html_to_pdf(source, output) - print( - f"Successfully rendered to {filepdf} | {get_size(output)} MB" - ) - - except Exception as e: - print(f"Something went wrong while converting to pdf: {e}") - - elif to_pdf == "n": - print("Okay") - os.remove(neat_dir + "/" + t) - return - - else: - print("Invalid input") - os.remove(neat_dir + "/" + t) - return - + process_pdf(neat_dir, parser["data"]["id"]) except TimeoutOccurred: print(f"Timeout occurred, not rendering pdf {id}") - os.remove(neat_dir + "/" + t) diff --git a/tomoe/simplyh.py b/tomoe/simplyh.py index 7fe4a44..d91a61a 100644 --- a/tomoe/simplyh.py +++ b/tomoe/simplyh.py @@ -1,26 +1,28 @@ import asyncio -import janda -import requests import os import re import time + +import janda +import requests +from inputimeout import TimeoutOccurred + +from .pdf import process_pdf +from .utils.const import TOMOE_HTML from .utils.misc import ( choose, - split_name, get_size, - convert_html_to_pdf, - project, get_size_folder, - nums, log_data, log_file, log_final, log_warn, + nums, + project, + split_name, ) -from inputimeout import inputimeout, TimeoutOccurred simply = janda.SimplyHentai() -t: str = "tomoe.html" async def get_sim(ids=choose().simply): @@ -92,7 +94,7 @@ async def process_simplyh(id: str): print(f"Directory: {os.path.abspath(neat_dir)}") - with open(neat_dir + "/" + t, "x", encoding="utf-8") as f: + with open(neat_dir + "/" + TOMOE_HTML, "x", encoding="utf-8") as f: f.write("
") f.write(f"

{neat_dir}

") @@ -104,35 +106,6 @@ async def process_simplyh(id: str): f.close() try: - desired = inputimeout( - prompt="Do you want to render it all to .pdf? (y/n) ", - timeout=10, - ) - to_pdf = desired - - if to_pdf == "y": - try: - source = open(f"{neat_dir}/{t}") - output = f"{neat_dir}/{set_name}.pdf" - filepdf = output.rsplit("/", 1)[-1] - - convert_html_to_pdf(source, output) - print( - f"Successfully rendered to {filepdf} | {get_size(output)} MB" - ) - - except Exception as e: - print(f"Something went wrong while converting to pdf: {e}") - - elif to_pdf == "n": - print("Okay") - os.remove(neat_dir + "/" + t) - return - - else: - print("Invalid input") - os.remove(neat_dir + "/" + t) - return - + process_pdf(neat_dir, set_name) except TimeoutOccurred: print(f"Timeout occurred, not rendering pdf {id}") diff --git a/tomoe/utils/const.py b/tomoe/utils/const.py new file mode 100644 index 0000000..4f947ba --- /dev/null +++ b/tomoe/utils/const.py @@ -0,0 +1,2 @@ +# place any constants that used throughout the app here +TOMOE_HTML: str = "tomoe.html" diff --git a/tomoe/utils/misc.py b/tomoe/utils/misc.py index 832eee7..a8a6f4c 100644 --- a/tomoe/utils/misc.py +++ b/tomoe/utils/misc.py @@ -26,6 +26,8 @@ def choose(): "--bulk", "-bulk", action="store", metavar=("BULK"), type=str ) + my_parser.add_argument("--pdf", "-pdf", action="store_true") + args = my_parser.parse_args() return args @@ -185,6 +187,20 @@ def log_warn(case: str, note: str): logging.info(f"{case}: {note}") +def log_warn(case: str, note: str): + """Logging warning + + Parameters + ---------- + case : str + Case of the log + + note : str + Note of the log + """ + logging.info(f"{case}: {note}") + + def log_final(taken: str, total_size: str): """Final log
NOTE: Python 3.7 or above