Utils

insert_link hentai.Utils(object)

This class provides a handful of miscellaneous static methods that extend the functionality of the Hentai class.

Static Methods

insert_link 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!
            
        
insert_link Utils.get_random_id(handler=RequestHandler()) -> int

Returns a random ID.

            
                from hentai import Utils

                # 177013 or ... ?
                print(Utils.get_random_id())
            
        
insert_link Utils.get_random_hentai(handler=RequestHandler()) -> Hentai

Returns a random Hentai object.

            
                from hentai import Utils

                # METAMORPHOSIS or ... ?
                print(Utils.get_random_hentai())
            
        
insert_link 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])
            
        
insert_link Utils.browse_homepage(start_page: int, end_page: int, handler=RequestHandler(), progressbar: bool=False) -> List[Hentai]

Returns a list 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)
            
        
insert_link Utils.get_homepage(handler=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: List[Hentai] = homepage.popular_now
                new_uploads: List[Hentai] = homepage.new_uploads

                for doujin in chain(popular_now, new_uploads):
                    print(f"{doujin.upload_date}\t{doujin.title()}")
            
        
insert_link Utils.search_by_query(query: str, page: int=1, sort: Sort=Sort.Popular, handler=RequestHandler()) -> List[Hentai]

Returns a list 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))
            
        
insert_link Utils.search_by_tag(id_: int, page: int=1, sort: Sort=Sort.Popular, handler=RequestHandler()) -> List[Hentai]

Returns a list 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))
            
        
insert_link Utils.search_all_by_query(query: str, sort: Sort=Sort.Popular, handler=RequestHandler(), progressbar: bool=False) -> List[Hentai]

Returns a list 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))
            
        
warning Warning
Depending on the scope of the search query it is more or less likely that this method times out before the invocation completes. As a side effect, you might notice that this method takes a considerable amount of time before it finishes its execution. Refine your search query with a time frame or use more tags to narrow down your search scope.

See also this site for more information on search queries.

insert_link Utils.export(iterable: List[Hentai], filename: 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)