├── LICENSE ├── README.md ├── __pycache__ └── pybel.cpython-311.pyc ├── example.py └── pybel.py /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Victor Cortez 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Library-of-Babel-Python-API 2 | A Python API for automated usage of the Library of Babel (https://libraryofbabel.info/) 3 | 4 | #The Project 5 | https://libraryofbabel.info/ 6 | 7 | #Usage 8 | Just put the script inside the folder of your program and use "import pybel" to use it and "pybel.getbook(hexagon,wall,shelf,volume)" will return you the text of the book. (ALL THE ARGUMENTS ARE STRINGS, NOT INTS) 9 | 10 | #Development 11 | The module is in early development, soon i will make inprovements. If you want to talk about or help, send me an email: victorcortezcb@gmail.com 12 | Just to make clear, i am in no way associated with the project or any of the creators of it. 13 | -------------------------------------------------------------------------------- /__pycache__/pybel.cpython-311.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/victor-cortez/Library-of-Babel-Python-API/5809336ef59f3a11536b139a268a0f4558277d37/__pycache__/pybel.cpython-311.pyc -------------------------------------------------------------------------------- /example.py: -------------------------------------------------------------------------------- 1 | import pybel 2 | 3 | # Gets a book from the library 4 | a = pybel.browser("hello",1,1,1,1) 5 | # Prints the entire book 6 | print(a) 7 | 8 | # Returns 4 results for searching the string "hello" in the library 9 | c = pybel.search("hello") 10 | print(c) 11 | print(c[0].hexagon) 12 | 13 | # Returns a random book 14 | r = pybel.random() 15 | print(r) -------------------------------------------------------------------------------- /pybel.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | __author__ = "Victor Barros" 3 | __version__ = "1.1" 4 | __maintainer__ = "Victor Barros" 5 | __email__ = "victorcortezcb@gmail.com" 6 | __status__ = "In Production" 7 | # We are going to use only these two modules 8 | import requests 9 | from bs4 import BeautifulSoup, element 10 | import re 11 | import random as rnd 12 | # Simple function to help checking if the inputs are correct 13 | def check(string,target): 14 | # Filtering the incompatible characters 15 | newstring = "".join([i for i in string if i in target]) 16 | # Comparing the two string to check if there was any modification 17 | if newstring == string: 18 | return True 19 | else: 20 | return False 21 | # If where was not, return true, if there was, return false 22 | # Main function to retrieve you the book desired 23 | def browse(hexagon,wall,shelf,volume): 24 | # The title is expandable, in the inputs of the fuction you can see very easily what you need 25 | # Just formatting the volume variable to fulfill the protocol the site wants 26 | if int(volume) <= 9: 27 | volume = "0" + volume 28 | # Alphabet and numbers allow in the website, just for checking 29 | alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"] 30 | numbers = ["0","1","2","3","4","5","6","7","8","9"] 31 | # Basic checking to make sure your inputs are correct 32 | if check(hexagon,alphabet + numbers) is False: 33 | raise Exception('Hexagon data format incorrect') 34 | if check(wall,numbers) is False or int(wall) > 4 or int(wall) < 1: 35 | raise Exception('wall data format incorrect or in incorrect range(1-4)') 36 | if check(shelf,numbers) is False or int(shelf) > 5 or int(shelf) < 1: 37 | raise Exception('shelf data format incorrect or in incorrect range (1-5)') 38 | if check(volume,numbers) is False or int(volume) > 32 or int(volume) < 1: 39 | raise Exception('volume data format incorrect or in incorrect range (1-32)') 40 | # Making the request with the data provided 41 | form = {"hex":hexagon,"wall":wall,"shelf":shelf,"volume":volume,"page":"1","title":"startofthetext"} 42 | url = "https://libraryofbabel.info/download.cgi" 43 | text = requests.post(url,data=form) 44 | # Cleaning the raw text, so "content" turns into the pure book 45 | content = text.text[len("startofthetext")+ 2::].rsplit('\n', 4)[0] 46 | return content 47 | 48 | class SearchResult: 49 | def __init__(self, hexagon, wall, shelf, volume,page, position): 50 | self.type = type 51 | self.page = page 52 | self.hexagon = hexagon 53 | self.wall = wall 54 | self.shelf = shelf 55 | self.volume = volume 56 | self.page = page 57 | self.position = position 58 | 59 | def process_search_result(result): 60 | try: 61 | type = result.find("h3").text 62 | except Exception as e: 63 | raise Exception("Result parsing error: " + str(e)) 64 | title_and_page = [i.text for i in result.find_all("b")] 65 | if (len(title_and_page) == 2): 66 | title,page = title_and_page 67 | elif (len(title_and_page) == 1): 68 | title = title_and_page[0] 69 | page = None 70 | else: 71 | raise Exception("Unexpected title and page format") 72 | info = result.find("a",{"class":"intext"})["onclick"] 73 | data_points = re.findall(r"'(\w+)'", info) 74 | if (len(data_points) not in [5,7]): 75 | raise Exception("Unexpected book address format") 76 | hexagon,wall,shelf,volume,page = data_points[0:5] 77 | if len(data_points) == 7: 78 | position = data_points[5] 79 | else: 80 | position = None 81 | return SearchResult(hexagon, wall, shelf, volume,page,position) 82 | 83 | 84 | def search(book_text): 85 | form = {"find":book_text} 86 | url = "https://libraryofbabel.info/search.cgi" 87 | text = requests.post(url,data=form) 88 | content_soup = BeautifulSoup(text.text) 89 | search_raw_results = content_soup.find_all("div", {"class": "location"}) 90 | return [process_search_result(i) for i in search_raw_results if type(i) == element.Tag] 91 | 92 | def random(hexagon_name_length=3200): 93 | hexagon = "".join([rnd.choice("abcdefghijklmnopqrstuvwxyz0123456789") for i in range(hexagon_name_length)]) 94 | wall = str(rnd.randint(1,4)) 95 | shelf = str(rnd.randint(1,5)) 96 | volume = str(rnd.randint(1,32)) 97 | return browse(hexagon,wall,shelf,volume) 98 | --------------------------------------------------------------------------------