Abilty to get book from just IA url

This commit is contained in:
bipinkrish
2023-02-26 19:30:51 +05:30
parent b6a000b7b9
commit d4999b9c48
4 changed files with 231 additions and 51 deletions

135
setup/ia.py Normal file
View File

@@ -0,0 +1,135 @@
from os import path, mkdir
import requests
import random
import string
import pickle
SESSION_FILE = 'account/session.pkl'
session = None
if path.exists(SESSION_FILE):
with open(SESSION_FILE, 'rb') as f: session = pickle.load(f)
# print error
def display_error(response, message):
print(message)
print(response)
print(response.text)
# login and format
def format_data(content_type, fields):
data = ""
for name, value in fields.items():
data += f"--{content_type}\x0d\x0aContent-Disposition: form-data; name=\"{name}\"\x0d\x0a\x0d\x0a{value}\x0d\x0a"
data += content_type+"--"
return data
def login(email, password):
session = requests.Session()
session.get("https://archive.org/account/login")
content_type = "----WebKitFormBoundary"+"".join(random.sample(string.ascii_letters + string.digits, 16))
headers = {'Content-Type': 'multipart/form-data; boundary='+content_type}
data = format_data(content_type, {"username":email, "password":password, "submit_by_js":"true"})
response = session.post("https://archive.org/account/login", data=data, headers=headers)
if "bad_login" in response.text:
print("[-] Invalid credentials!")
return None
elif "Successful login" in response.text:
print("[+] Successful login")
return session
else:
display_error(response, "[-] Error while login:")
return None
# get book
def loan(book_id):
global session
if not session:
with open(SESSION_FILE, 'rb') as f: session = pickle.load(f)
data = {
"action": "grant_access",
"identifier": book_id
}
response = session.post("https://archive.org/services/loans/loan/searchInside.php", data=data)
data['action'] = "browse_book"
response = session.post("https://archive.org/services/loans/loan/", data=data)
if response.status_code == 400 :
if response.json()["error"] == "This book is not available to borrow at this time. Please try again later.":
print("This book doesn't need to be borrowed")
return session
else :
display_error(response, "Something went wrong when trying to borrow the book.")
return None
data['action'] = "create_token"
response = session.post("https://archive.org/services/loans/loan/", data=data)
if "token" in response.text:
print("[+] Successful loan")
return session
else:
display_error(response, "Something went wrong when trying to borrow the book, maybe you can't borrow this book.")
return None
# acsm file
def get_acsmfile(bookid,format="pdf"):
global session
if not session:
with open(SESSION_FILE, 'rb') as f: session = pickle.load(f)
response = session.get(f"https://archive.org/services/loans/loan/?action=media_url&format={format}&redirect=1&identifier={bookid}")
if response.status_code == 200:
with open(f"{bookid}.acsm","w") as af: af.write(response.text)
return f"{bookid}.acsm"
else:
display_error(response, "Something went wrong when trying to get ACSM")
return None
# return the book
def return_loan(book_id):
global session
if not session:
with open(SESSION_FILE, 'rb') as f: session = pickle.load(f)
data = {
"action": "return_loan",
"identifier": book_id
}
response = session.post("https://archive.org/services/loans/loan/", data=data)
if response.status_code == 200 and response.json()["success"]:
print("[+] Book returned")
return True
else:
display_error(response, "Something went wrong when trying to return the book")
return None
# manage
def manage_login(email,password):
global session
if not path.exists('account'): mkdir('account')
sess = login(email,password)
if sess is not None:
with open(SESSION_FILE, 'wb') as f: pickle.dump(sess, f)
session = sess
def get_book(url,format):
global session
bookid = url.split("/")[4]
sess = loan(bookid)
if sess is not None:
with open(SESSION_FILE, 'wb') as f: pickle.dump(sess, f)
session = sess
return get_acsmfile(bookid,format)
return None
def return_book(url):
bookid = url.split("/")[4]
return return_loan(bookid)

View File

@@ -16,21 +16,7 @@ from decrypt.params import KEYPATH
#################################################################
def takeInput():
global VAR_MAIL
global VAR_PASS
VAR_MAIL = input("Enter Mail: ")
VAR_PASS = input("Enter Password: ")
if VAR_MAIL == "" or VAR_MAIL == "":
print("It cannot be empty")
print()
exit(1)
def loginAndGetKey():
def loginAndGetKey(email, password):
global VAR_MAIL
global VAR_PASS
@@ -40,31 +26,32 @@ def loginAndGetKey():
# acc files
if True:
takeInput()
VAR_MAIL = email
VAR_PASS = password
print("Logging in")
createDeviceKeyFile()
success = createDeviceFile(True, VAR_VER)
if (success is False):
print("Error, couldn't create device file.")
exit(1)
return
success, resp = createUser(VAR_VER, None)
if (success is False):
print("Error, couldn't create user: %s" % resp)
exit(1)
return
success, resp = signIn("AdobeID", VAR_MAIL, VAR_PASS)
if (success is False):
print("Login unsuccessful: " + resp)
exit(1)
return
success, resp = activateDevice(VAR_VER, None)
if (success is False):
print("Couldn't activate device: " + resp)
exit(1)
return
print("Authorized to account " + VAR_MAIL)
@@ -79,16 +66,15 @@ def loginAndGetKey():
success = exportAccountEncryptionKeyDER(filename)
if (success is False):
print("Couldn't export key.")
exit(1)
return
print("Successfully exported key for account " + VAR_MAIL + " to file " + filename)
else:
print("failed")
exit(1)
except Exception as e:
print(e)
exit(1)
print('All Set')
print()