diff --git a/main.py b/main.py index 7ea15d6..f4e298a 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,7 @@ from libgen_api import * -import requests, sys +import requests, sys, getopt -def searchBook(book): +def searchBook(book, unattended=False): s = LibgenSearch() filter = {"Extension": "epub"} results = s.search_title_filtered(book, filter) @@ -11,12 +11,16 @@ def searchBook(book): sys.exit() for i in results: i = i['Title'] - print(f'[{j}]: {i}') + if unattended == False: + print(f'[{j}]: {i}') j+=1 - choice = input('Please select the book to download or press enter to exit: ') - if choice == '': - print('Quiting...') - sys.exit() + if unattended is False: + choice = input('Please select the book to download or press enter to exit: ') + if choice == '': + print('Quiting...') + sys.exit() + elif unattended is True: + choice = 0 download = results[int(choice)] download = s.resolve_download_links(download) link = download['GET'] @@ -34,10 +38,21 @@ def downloadBook(bookDict): print('Book Downloaded!') -def main(): - book = input('Please enter the title of the book you are looking for: ') - results = searchBook(book) - downloadBook(results) +def main(argv): + if len(argv) == 0: + book = input('Please enter the title of the book you are looking for: ') + results = searchBook(book) + downloadBook(results) + else: + opts, args = getopt.getopt(argv, "hb:",["help=","book="]) + for opt, arg in opts: + if opt in ('-h', "--help"): + print('main.py -b ') + elif opt in ('-b', '--book'): + book = arg + results = searchBook(book, unattended=True) + downloadBook(results) -main() \ No newline at end of file +if __name__ == "__main__": + main(sys.argv[1:]) \ No newline at end of file diff --git a/requirments.txt b/requirments.txt new file mode 100644 index 0000000..e836f14 --- /dev/null +++ b/requirments.txt @@ -0,0 +1 @@ +libgen_api==1.0.0 \ No newline at end of file