-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from pathlib import Path | ||
from zipfile import ZipFile | ||
import re, json | ||
from datetime import date | ||
|
||
import logging | ||
logger = logging.getLogger(__name__) | ||
|
||
class Scanner: | ||
'''For https://github.com/z-mio/ehentai_bot, with <gid>.json put into the zip archive.''' | ||
|
||
def scan(self, path: Path, id: str, metadata: dict, prev_scanners: list[str]) -> bool: | ||
if not '10-zip' in prev_scanners or '20-ccloli' in prev_scanners: | ||
return False | ||
with ZipFile(path) as z: | ||
for filename in z.namelist(): | ||
if (m := re.fullmatch(r'(\d+)\.json', filename, flags=re.ASCII)): | ||
with z.open(filename) as f: | ||
json_content = json.load(f) | ||
if not isinstance(json_content, dict): | ||
break | ||
gmetadata = json_content.get('gmetadata') | ||
if not (isinstance(gmetadata, list) and len(gmetadata) == 1 and isinstance(gmetadata[0], dict)): | ||
break | ||
logger.info(f' <- {path}') | ||
gmetadata = gmetadata[0] | ||
if gmetadata.get('gid') != int(m[1]): | ||
break | ||
metadata["id"] = f"EH{gmetadata['gid']:>018}{gmetadata['token']}{id[-10:]}" | ||
metadata["title"] = gmetadata['title'] | ||
metadata["subtitle"] = gmetadata['title_jpn'] | ||
metadata["categories"] = set((gmetadata['category'],)) | ||
metadata["thumb"] = gmetadata['thumb'] | ||
metadata["pagecount"] = gmetadata['filecount'] | ||
metadata["tags"] = gmetadata['tags'] + [ | ||
f"date_posted:{date.fromtimestamp(int(gmetadata['posted']))}", | ||
] | ||
metadata["source"] = f"https://exhentai.org/g/{gmetadata['gid']}/{gmetadata['token']}/" | ||
return True | ||
return False |