54 lines
2.1 KiB
Python
54 lines
2.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Search libgen.vg (Library Genesis). Usage: lg.sh "query" [limit]
|
|
Polite mode: one request per query."""
|
|
import sys, time, urllib.parse, urllib.request, re
|
|
|
|
UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36"
|
|
|
|
def fetch(url, tries=3):
|
|
last = None
|
|
for i in range(tries):
|
|
try:
|
|
req = urllib.request.Request(url, headers={"User-Agent": UA, "Accept-Language": "ru-RU,ru;q=0.9"})
|
|
return urllib.request.urlopen(req, timeout=90).read().decode("utf-8", "replace")
|
|
except Exception as e:
|
|
last = e
|
|
time.sleep(5 + 5 * i)
|
|
raise SystemExit(f"fetch failed after {tries} tries: {last}")
|
|
|
|
def main():
|
|
q = sys.argv[1]
|
|
limit = int(sys.argv[2]) if len(sys.argv) > 2 else 10
|
|
url = "https://libgen.vg/index.php?" + urllib.parse.urlencode({
|
|
"req": q, "res": "100", "dlt": "0", "ln": "0",
|
|
"columns[]": ["t","a","s","y","p","i","l","x","sz"]
|
|
})
|
|
html = fetch(url)
|
|
# pagination hint
|
|
m = re.search(r'page=(\d+)', html)
|
|
if m and int(m.group(1)) > 1:
|
|
print(f"[more pages available, max page {m.group(1)} — rerun with &page=N]")
|
|
# parse result rows
|
|
rows = re.findall(r"<tr[^>]*>\s*<td[^>]*>.*?</tr>", html, re.S)
|
|
count = 0
|
|
for r in rows:
|
|
tds = re.findall(r"<td[^>]*>(.*?)</td>", r, re.S)
|
|
tds = [re.sub(r"<[^>]+>", "", t).replace("&","&").strip() for t in tds]
|
|
tds = [re.sub(r"\s+", " ", t) for t in tds]
|
|
if len(tds) < 6:
|
|
continue
|
|
# first cell is usually a link with id, title is the big cell
|
|
title = max(tds, key=len) if tds else ""
|
|
line = " | ".join(tds[:9])
|
|
print(line[:300])
|
|
count += 1
|
|
if count >= limit:
|
|
break
|
|
if count == 0:
|
|
# maybe "No results" or error page
|
|
m = re.search(r"No results|Ничего не найдено|Error[^<]*", html)
|
|
print("(no results)" if m else "(unparsed — check manually)")
|
|
print(html[:500].replace("\n", " "))
|
|
|
|
if __name__ == "__main__":
|
|
main()
|