-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibgen_api.py
More file actions
56 lines (46 loc) · 1.68 KB
/
libgen_api.py
File metadata and controls
56 lines (46 loc) · 1.68 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from flask import Flask, jsonify, request
from libgen_api_enhanced import LibgenSearch
import requests
from bs4 import BeautifulSoup
app = Flask(__name__)
@app.route('/search_book', methods=['GET'])
def search_book():
# Get the book title from the query parameters
title = request.args.get('title')
if not title:
return jsonify({"error": "Title is required"}), 400
s = LibgenSearch()
results = s.search_default(title)
books = []
for result in results:
mirror_1 = result.get('Mirror_1')
if not mirror_1:
continue
page = requests.get(mirror_1)
soup = BeautifulSoup(page.text, "html.parser")
links = []
res = soup.find_all("li")
for i in res:
a_tag = i.find("a")
if a_tag and 'href' in a_tag.attrs:
next_link = a_tag['href']
links.append(next_link)
img_tag = soup.find("img")
img_link = img_tag['src'] if img_tag and 'src' in img_tag.attrs else None
book = {
'id': result.get('ID'),
'title': result.get('Title'),
'author': result.get('Author'),
'year': result.get('Year'),
'pages': result.get('Pages'),
'lang': result.get('Language'),
'extension': result.get('Extension'),
'Cloudflare': links[0] if len(links) > 0 else None,
'Ipfs': links[1] if len(links) > 1 else None,
'Pinata': links[2] if len(links) > 2 else None,
'image': f"https://library.lol{img_link}" if img_link else None
}
books.append(book)
return jsonify(books)
if __name__ == '__main__':
app.run(debug=True)