Utils
hentai.Utils(object)
This class provides a handful of miscellaneous static methods that
extend the functionality of the
Hentai
class.
Static Methods
Utils.exists(verbose: bool=False) -> int
The utils decorator. This type of method invocation is more secure and returns a
red-colored error message to the screen if error_msg
is enabled:
from hentai import Hentai, Utils
@Utils.exists(error_msg=True)
def main():
doujin = Hentai(123456789)
print(doujin.title())
if __name__ == '__main__':
main()
print('done!')
# 404 Client Error: Not Found for url: https://nhentai.net/api/gallery/123456789
# done!
This construct is equivalent to calling
from hentai import Hentai
from colorama import Fore, init
from requests import HTTPError
init(autoreset=True)
def main(error_msg: bool=False):
try:
doujin = Hentai(123456789)
print(doujin.title())
except HTTPError as error:
if error_msg:
print(f"{Fore.RED}{error}")
if __name__ == '__main__':
main(error_msg=True)
print('done!')
# 404 Client Error: Not Found for url: https://nhentai.net/api/gallery/123456789
# done!
Utils.get_random_id(handler: RequestHandler=RequestHandler()) -> int
Returns a random ID.
from hentai import Utils
# 177013 or ... ?
print(Utils.get_random_id())
Utils.get_random_hentai(handler: RequestHandler=RequestHandler()) -> Hentai
Returns a random Hentai
object.
from hentai import Utils
# METAMORPHOSIS or ... ?
print(Utils.get_random_hentai())
Utils.download(doujins: List[Hentai],
delay: float=0,
progressbar: bool=False) -> None
Downloads a list of Hentai
objects. See also
self.download
for more information on this method.
from hentai import Utils
# downloads 25 doujin from the front page
homepage = Utils.get_homepage()
Utils.download([doujin for doujin in homepage.popular_now])
Utils.browse_homepage(start_page: int,
end_page: int,
handler: RequestHandler=RequestHandler(),
progressbar: bool=False) -> Set[Hentai]
Returns a set of Hentai
objects that are currently featured
on the homepage in range of [start_page, end_page]
. Each page
contains as much as 25 results. Homepage doujins are the most recently added
items on
nhentai.net.
Set progressbar
to True
to enable progressbar
feedback for terminal applications.
from hentai import Utils
# get the first 5 pages from the homepage, i.e. 5*25=125 doujins
homepage5 = Utils.browse_homepage(start_page=1, end_page=5)
Utils.get_homepage(handler: RequestHandler=RequestHandler()) -> Homepage
Return a Homepage
object which contains two properties,
popular_now
and new_uploads
. There are always
5 doujins featured in the popular now section, while new_uploads
returns the last 25 doujins added to the DB online.
from hentai import Hentai, Utils
from typing import List
from itertools import chain
homepage = Utils.get_homepage()
popular_now: Set[Hentai] = homepage.popular_now
new_uploads: Set[Hentai] = homepage.new_uploads
for doujin in chain(popular_now, new_uploads):
print(f"{doujin.upload_date}\t{doujin.title()}")
Utils.search_by_query(query: str,
page: int=1,
sort: Sort=Sort.Popular,
handler: RequestHandler=RequestHandler()) -> Set[Hentai]
Returns a set of Hentai
objects on this page
that match this search query
sorted by this sort
option.
from hentai import Sort, Format, Utils
# fetches the first 25 responses that match this query
for doujin in Utils.search_by_query('tag:loli', sort=Sort.PopularWeek):
print(doujin.title(Format.Pretty))
Utils.search_by_tag(id_: int,
page: int=1,
sort: Sort=Sort.Popular,
handler: RequestHandler=RequestHandler()) -> Set[Hentai]
Returns a set of Hentai
objects on this page
that match this tag id_
sorted by this sort
option.
from hentai import Sort, Format, Utils
# fetches the first 25 responses that match holo's character id
for doujin in Utils.search_by_tag(33918, sort=Sort.PopularWeek):
print(doujin.title(Format.Pretty))
Utils.search_all_by_query(query: str,
sort: Sort=Sort.Popular,
handler: RequestHandler=RequestHandler(),
progressbar: bool=False) -> Set[Hentai]
Returns a set of all Hentai
objects that match this search
query
sorted by this sort
option. Set
progressbar
to True
to enable progressbar
feedback for terminal applications.
from hentai import Sort, Format, Utils
# fetches all responses that match this query
for doujin in Utils.search_all_by_query(query="tag:3d", sort=Sort.PopularWeek):
print(doujin.title(Format.Pretty))
See also this site for more information on search queries.
Utils.export(iterable: List[Hentai],
filename: Union[str, Path],
options=List[Option]=None) -> None
Exports a list of Hentai
objects. See also
self.export
for more information on this method.
from hentai import Sort, Utils, Option
# fetches the first 25 responses that match this query
popular_loli = Utils.search_by_query('tag:loli', sort=Sort.PopularWeek)
# store ids, titles and upload dates for this query to disk
custom = [Option.ID, Option.Title, Option.Epos]
Utils.export(popular_loli, filename=Path('popular_loli.json'), options=custom)
Utils.compress(folder: Union[str, Path]) -> None
Archive folder
as ZipFile
(Windows) or TarFile
(Linux and macOS) using the highest compression levels available.