Created
March 24, 2023 17:23
-
-
Save lobstrio/8d1c87203755d569da7ce2433179f099 to your computer and use it in GitHub Desktop.
Download free anarchist ebooks from an .onion site with Python3 and requests 🧅
This file contains 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 | |
from lxml import html | |
import time | |
print('~~ start') | |
anarchist_library_onion_link = "http://libraryqxxiqakubqv3dc2bend2koqsndbwox2johfywcatxie26bsad.onion/special/index" | |
latest_books_library_onion_link = "http://libraryqxxiqakubqv3dc2bend2koqsndbwox2johfywcatxie26bsad.onion/latest?bare=1" | |
session = requests.session() | |
session.proxies = {'http': 'socks5h://localhost:9050', 'https': 'socks5h://localhost:9050'} | |
tor_ip = session.get("https://api.ipify.org/").text | |
local_ip = requests.get("https://api.ipify.org/").text | |
assert all([tor_ip, local_ip]) | |
assert tor_ip != local_ip | |
response = session.get("http://libraryqxxiqakubqv3dc2bend2koqsndbwox2johfywcatxie26bsad.onion/latest?bare=1", timeout=100) | |
doc = html.fromstring(response.text) | |
items = doc.xpath('//div[@class="list-group"]//div[@class="amw-listing-item"]') | |
for i, item in enumerate(items[:10]): | |
link = "".join(item.xpath('./a/@href')) | |
print(link) | |
response = session.get(link, timeout=100) | |
assert response.ok | |
item_page_doc = html.fromstring(response.text) | |
pdf_link = "".join(item_page_doc.xpath('//span[@id="download-format-pdf"]/a/@href')) | |
assert pdf_link | |
pdf_name = pdf_link.split('/')[-1] | |
pdf_response = session.get(pdf_link, stream=True) | |
with open(pdf_name, 'wb') as fd: | |
for chunk in pdf_response.iter_content(2000): | |
fd.write(chunk) | |
print('✅ %s (%s)' % (pdf_name, i+1)) | |
time.sleep(1) | |
print('~~ success') | |
print(""" _ _ _ | |
| | | | | | | |
| | ___ | |__ ___| |_ __ __ | |
| |/ _ \| '_ \/ __| __/| '__| | |
| | (_) | |_) \__ \ |_ | | | |
|_|\___/|_.__/|___/\__||_| | |
""") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment