Created
June 25, 2018 22:11
-
-
Save Krazybug/f012e8d03a2598da1f98deb8caf0a8b9 to your computer and use it in GitHub Desktop.
calibre-opendir-bot.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
import os | |
import humanfriendly | |
server='http://207.244.104.206:8080/' | |
max_size=0 | |
#max_size=10*1024*1024 | |
offset=0 | |
num=100 | |
count_b=count_f=total_size=0 | |
format_size = {} | |
format_count = {} | |
my_formats={} | |
# my_formats=['azw3', 'epub', 'pdf', 'mobi', 'doc', 'zip', 'txt', 'chm', 'cbr', 'cbz'] | |
formats = set() | |
url=server+'/ajax/search?num=0' | |
print(f"Scanned server: {url}") | |
try: | |
r=requests.get(url) | |
total_num=int(r.json()["total_num"]) | |
# total_num=100 | |
print(f"Total ebooks count to scan={total_num}") | |
except: | |
sys.exit(1) | |
range=offset+1 | |
offset=18 | |
while offset < total_num: | |
print("offset=", str(offset)) | |
url=server+'/ajax/search?num='+str(num)+'&offset='+str(offset) | |
print(url) | |
try: | |
r=requests.get(url) | |
except: | |
continue | |
print("from: ", str(offset), " to: ", str(offset+int(r.json()['num']))) | |
book_ids=r.json()["book_ids"] | |
books_s=",".join(str(i) for i in r.json()['book_ids']) | |
url=server+'/ajax/books?ids='+books_s | |
print(url) | |
try: | |
r=requests.get(url) | |
except: | |
continue | |
print(len(r.json())) | |
for id in r.json().keys(): | |
print (f'-> range={count_b}/{total_num}') | |
if my_formats: | |
b_formats=list(set(r.json()[id]['formats']) & set(my_formats)) | |
else: | |
b_formats=r.json()[id]['formats'] | |
formats=formats | set(b_formats) | |
title=r.json()[id]['title'] | |
b_f_count=0 | |
for f in b_formats: | |
size=int(r.json()[id]['format_metadata'][f]['size']) if 'size' in r.json()[id]['format_metadata'][f] else 0 | |
if not size or (max_size and max_size < size): | |
print (f"'{f}' format ignored for {id}:'{title}' too large") | |
else: | |
print (f"({id}):'{title}'' -> size={size}") | |
if f in format_count: | |
format_count[f]+=1 | |
else: format_count[f]=1 | |
if f in format_size: | |
format_size[f]+=size | |
else: format_size[f]=size | |
total_size+=size | |
count_f+=1 | |
b_f_count+=1 | |
if b_f_count: | |
count_b+=1 | |
offset=offset+num | |
print() | |
print("--------> Total books count:", count_b) | |
print("----------> Total files count:", count_f) | |
print("----------> Total size:", humanfriendly.format_size(total_size)) | |
for f in formats: | |
print("--------------> {} '{}' file(s): Total size= {}".format(str(format_count[f]), str(f), humanfriendly.format_size(format_size[f]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment