Skip to content

Instantly share code, notes, and snippets.

@MitchRatquest
Last active January 25, 2022 01:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MitchRatquest/d41a1607d4477b02b81869be85cefc21 to your computer and use it in GitHub Desktop.
Save MitchRatquest/d41a1607d4477b02b81869be85cefc21 to your computer and use it in GitHub Desktop.
Calishot downloader
#!/usr/bin/python3
# See here for more info: https://www.reddit.com/r/opencalibre/comments/s72l4r/calishot_202201_find_ebooks_among_373_calibre/
#https://calishot-eng-5.herokuapp.com/index-eng/summary.json?_search=Lilith%E2%80%99s+Brood+Octavia+Butler&_sort=uuid
#pip install pyfzf
from pyfzf.pyfzf import FzfPrompt
import requests, json
from pprint import pprint as p
MOBIS_ONLY = False
OUTPUT_DIRECTORY = '/home/raul/books2import/'
fzf = FzfPrompt()
# Subject to change, maybe?
baseurl='https://calishot-eng-5.herokuapp.com/index-eng/summary.json?_search='
# Easier to write them all out if you want more stuff to deal with
row_names = ['uuid','cover','title','authors','year','series','language','links','publisher','tags','identifiers','formats']
formats = row_names.index('formats')
links = row_names.index('links')
titles = row_names.index('title')
authors = row_names.index('authors')
def search_for_books(search_string):
response = requests.get(baseurl + search_string)
all_books = response.json().get('rows',[])
filtered_books = []
for m in all_books:
this_book = dict()
if isinstance(m[links], list):
these_links = m[links]
else:
these_links = json.loads(m[links])
for l in these_links:
link_type = l.pop('label').split(' ')[0]
l['type'] = link_type
if MOBIS_ONLY:
this_book['links'] = list(filter(lambda x: 'mobi' in x.get('type'), these_links))
else:
this_book['links'] = these_links
this_title = json.loads(m[titles]).get('label')
this_author = ' '.join(json.loads(m[authors]))
this_book['title'] = '{} by {}'.format(this_title, this_author).strip()
filtered_books.append(this_book)
return filtered_books
'''
A list of books will look like this:
[{'links': [{'href': 'http://174.49.207.156:8090/get/pdf/61394/Calibre',
'type': 'pdf'}],
'title': 'The Art of the Infinite: The Pleasures of Mathematics by Robert '
'Kaplan Ellen Kaplan'},
{'links': [{'href': 'http://174.49.207.156:8090/get/epub/53544/Calibre',
'type': 'epub'}],
'title': 'The Art of the Infinite: The Pleasures of Mathematics by Robert '
'Kaplan Ellen Kaplan'},
{'links': [{'href': 'http://174.49.207.156:8090/get/pdf/6138/Calibre',
'type': 'pdf'}],
'title': 'The Art of the Infinite: The Pleasures of Mathematics by Robert '
'Kaplan Ellen Kaplan'},
{'links': [{'href': 'http://174.49.207.156:8090/get/pdf/6138/Calibre',
'type': 'mobi'}, {'href': 'blahb/lah', 'type': 'txt'}],
'title': 'The Art of the Infinite: The Pleasures of Mathematics by Robert '
'Kaplan Ellen Kaplan'}]
'''
def get_best_format(some_dict):
# This is the order in which we want the formats
favor = ['mobi','epub','azw3','cbr','pdf','txt']
links = some_dict.get('links')
types = [x.get('type') for x in links]
best_match = float('inf')
for t in types:
i = favor.index(t)
if i < best_match:
best_match = i
return best_match
def download_one_book(book_dict):
title = book_dict.get('title')
for link in book_dict.get('links'):
try:
book_content = requests.get(link.get('href'), timeout=10)
if book_content.ok:
book_type = link.get('type')
print("New book downloaded: {}.{}".format(title, book_type))
with open(OUTPUT_DIRECTORY+'{}.{}'.format(title, book_type), 'wb') as newbook:
newbook.write(book_content.content)
return True
except Exception as e:
p(e)
continue
return False
if __name__ == '__main__':
print("Enter search query")
user_input = input().strip()
list_of_books = search_for_books(user_input)
print("Found {} results".format(len(list_of_books)))
print("Please choose a title")
chosen_book = fzf.prompt([x.get('title') for x in list_of_books])
print("You chose: {}".format(chosen_book[0]))
title_matches = [x for x in list_of_books if x.get('title') == chosen_book[0]]
best_books = sorted(title_matches, key=get_best_format)
got_one = False
for book in best_books:
got_one = download_one_book(book)
if got_one:
exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment