-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbabelApi.py
26 lines (23 loc) · 973 Bytes
/
babelApi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import requests
import re
class BabelApi:
def __init__(self):
self.url = "http://libraryofbabel.info/"
def search(self, data_block):
payload = {"btnSubmit": "Search",
"method": "x",
"find": data_block}
r = requests.post(self.url + "search.cgi", data=payload)
matches = re.findall('postform\(.*\)', r.text)
elem = matches[0]
elem = elem.split("postform(")[1].split(')')[0].split(',')
data = {"hex": elem[0][1:-1],
"wall": int(elem[1][1:-1]),
"shelf": int(elem[2][1:-1]),
"volume": int(elem[3][1:-1]),
"page": int(elem[4][1:-1]),
"len": len(data_block)}
return data
def lookup(self, data_struct):
r = requests.post(self.url + "book.cgi", data=data_struct)
return r.text.split('<PRE id = "textblock">')[1].split('</PRE>')[0].replace('\n', '')[:data_struct["len"]]