2023-03-28 17:56:26 +00:00
|
|
|
from libgen_api import *
|
2023-03-29 02:10:40 +00:00
|
|
|
import requests, sys, getopt, configparser, shutil, os
|
|
|
|
|
|
|
|
config = configparser.ConfigParser()
|
|
|
|
try:
|
|
|
|
config.read('config.ini')
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
print('Config file not found. Please ensure conf.ini is in the same \
|
|
|
|
location as bookrequest.py')
|
2023-03-28 17:56:26 +00:00
|
|
|
|
2023-03-28 20:56:48 +00:00
|
|
|
def searchBook(book, unattended=False):
|
2023-03-28 17:56:26 +00:00
|
|
|
s = LibgenSearch()
|
|
|
|
filter = {"Extension": "epub"}
|
|
|
|
results = s.search_title_filtered(book, filter)
|
|
|
|
j = 0
|
|
|
|
if len(results) == 0:
|
|
|
|
print('No results found. Quiting...')
|
|
|
|
sys.exit()
|
|
|
|
for i in results:
|
|
|
|
i = i['Title']
|
2023-03-28 20:56:48 +00:00
|
|
|
if unattended == False:
|
|
|
|
print(f'[{j}]: {i}')
|
2023-03-28 17:56:26 +00:00
|
|
|
j+=1
|
2023-03-28 20:56:48 +00:00
|
|
|
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
|
2023-03-28 17:56:26 +00:00
|
|
|
download = results[int(choice)]
|
|
|
|
download = s.resolve_download_links(download)
|
|
|
|
link = download['GET']
|
|
|
|
result = {"title": results[int(choice)]['Title'],
|
|
|
|
"author": results[int(choice)]['Author'],
|
|
|
|
"link": link
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def downloadBook(bookDict):
|
|
|
|
print('Downloading book. Please wait...')
|
|
|
|
r = requests.get(bookDict['link'], allow_redirects=True)
|
|
|
|
open(bookDict['title']+'.epub', 'wb').write(r.content)
|
|
|
|
print('Book Downloaded!')
|
2023-03-29 02:10:40 +00:00
|
|
|
os.makedirs(config['data']['folder_location'] + '\\' + bookDict['author'])
|
|
|
|
shutil.move(bookDict['title']+'.epub', config['data']['folder_location'] + '\\' + bookDict['author'] + '\\' + bookDict['title']+'.epub')
|
2023-03-28 17:56:26 +00:00
|
|
|
|
|
|
|
|
2023-03-28 20:56:48 +00:00
|
|
|
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"):
|
2023-03-29 01:41:47 +00:00
|
|
|
print('bookrequest.py -b <booktitle>')
|
2023-03-28 20:56:48 +00:00
|
|
|
elif opt in ('-b', '--book'):
|
|
|
|
book = arg
|
|
|
|
results = searchBook(book, unattended=True)
|
|
|
|
downloadBook(results)
|
2023-03-28 17:56:26 +00:00
|
|
|
|
|
|
|
|
2023-03-28 20:56:48 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main(sys.argv[1:])
|