Ivan000 commited on
Commit
5be97e9
·
verified ·
1 Parent(s): fa93e8f

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +30 -19
main.py CHANGED
@@ -1,18 +1,17 @@
1
- # duckopds/main.py
2
 
3
  from fastapi import FastAPI, Query, Response
4
  from fastapi.responses import XMLResponse
5
  import requests
6
  from bs4 import BeautifulSoup
7
- from urllib.parse import urlencode, quote
8
  import xml.etree.ElementTree as ET
9
- import re
10
 
11
  app = FastAPI()
12
 
13
  # ========== FB2 Generator ==========
14
  def html_to_fb2(title: str, body: str) -> str:
15
- clean_text = BeautifulSoup(body, "html.parser").get_text()
16
  fb2 = f"""<?xml version='1.0' encoding='utf-8'?>
17
  <FictionBook xmlns:xlink='http://www.w3.org/1999/xlink'>
18
  <description>
@@ -24,15 +23,22 @@ def html_to_fb2(title: str, body: str) -> str:
24
  </title-info>
25
  </description>
26
  <body>
27
- <section><title><p>{title}</p></title><p>{clean_text}</p></section>
 
 
28
  </body>
29
- </FictionBook>
30
- """
31
  return fb2
32
 
33
  # ========== DuckDuckGo Search ==========
34
  def duckduckgo_search(query: str):
35
- res = requests.get("https://html.duckduckgo.com/html/", data={"q": query}, headers={"User-Agent": "Mozilla/5.0"})
 
 
 
 
 
 
36
  soup = BeautifulSoup(res.text, "html.parser")
37
  results = []
38
  for a in soup.select("a.result__a"):
@@ -44,12 +50,13 @@ def duckduckgo_search(query: str):
44
  break
45
  return results
46
 
47
- # ========== OPDS Feed ==========
48
  def generate_opds(query: str, results):
49
  ns = "http://www.w3.org/2005/Atom"
50
  ET.register_namespace("", ns)
51
  feed = ET.Element("feed", xmlns=ns)
52
  ET.SubElement(feed, "title").text = f"Search results for '{query}'"
 
53
  for title, url in results:
54
  entry = ET.SubElement(feed, "entry")
55
  ET.SubElement(entry, "title").text = title
@@ -57,7 +64,7 @@ def generate_opds(query: str, results):
57
  ET.SubElement(entry, "updated").text = "2025-07-31T12:00:00Z"
58
  ET.SubElement(entry, "link", {
59
  "rel": "http://opds-spec.org/acquisition",
60
- "href": f"/download?url={quote(url)}",
61
  "type": "application/fb2+xml"
62
  })
63
  return ET.tostring(feed, encoding="utf-8", xml_declaration=True)
@@ -70,12 +77,16 @@ def opds_catalog(q: str = Query(..., description="Search query")):
70
 
71
  @app.get("/download")
72
  def download_fb2(url: str):
73
- try:
74
- res = requests.get(url, headers={"User-Agent": "Mozilla/5.0"}, timeout=10)
75
- soup = BeautifulSoup(res.text, "html.parser")
76
- title = soup.title.string if soup.title else "No Title"
77
- body = str(soup.body)
78
- fb2 = html_to_fb2(title, body)
79
- return Response(content=fb2, media_type="application/fb2+xml", headers={"Content-Disposition": f"attachment; filename={re.sub(r'[^a-zA-Z0-9]+', '_', title)[:30]}.fb2"})
80
- except Exception as e:
81
- return {"error": str(e)}
 
 
 
 
 
1
+ # File: main.py
2
 
3
  from fastapi import FastAPI, Query, Response
4
  from fastapi.responses import XMLResponse
5
  import requests
6
  from bs4 import BeautifulSoup
7
+ from urllib.parse import quote
8
  import xml.etree.ElementTree as ET
 
9
 
10
  app = FastAPI()
11
 
12
  # ========== FB2 Generator ==========
13
  def html_to_fb2(title: str, body: str) -> str:
14
+ clean_text = BeautifulSoup(body, "html.parser").get_text(separator="\n")
15
  fb2 = f"""<?xml version='1.0' encoding='utf-8'?>
16
  <FictionBook xmlns:xlink='http://www.w3.org/1999/xlink'>
17
  <description>
 
23
  </title-info>
24
  </description>
25
  <body>
26
+ <section><title><p>{title}</p></title>
27
+ <p>{clean_text}</p>
28
+ </section>
29
  </body>
30
+ </FictionBook>"""
 
31
  return fb2
32
 
33
  # ========== DuckDuckGo Search ==========
34
  def duckduckgo_search(query: str):
35
+ res = requests.post(
36
+ "https://html.duckduckgo.com/html/",
37
+ data={"q": query},
38
+ headers={"User-Agent": "Mozilla/5.0"},
39
+ timeout=10
40
+ )
41
+ res.raise_for_status()
42
  soup = BeautifulSoup(res.text, "html.parser")
43
  results = []
44
  for a in soup.select("a.result__a"):
 
50
  break
51
  return results
52
 
53
+ # ========== OPDS Feed Generator ==========
54
  def generate_opds(query: str, results):
55
  ns = "http://www.w3.org/2005/Atom"
56
  ET.register_namespace("", ns)
57
  feed = ET.Element("feed", xmlns=ns)
58
  ET.SubElement(feed, "title").text = f"Search results for '{query}'"
59
+ ET.SubElement(feed, "updated").text = "2025-07-31T12:00:00Z"
60
  for title, url in results:
61
  entry = ET.SubElement(feed, "entry")
62
  ET.SubElement(entry, "title").text = title
 
64
  ET.SubElement(entry, "updated").text = "2025-07-31T12:00:00Z"
65
  ET.SubElement(entry, "link", {
66
  "rel": "http://opds-spec.org/acquisition",
67
+ "href": f"/download?url={quote(url, safe='')}",
68
  "type": "application/fb2+xml"
69
  })
70
  return ET.tostring(feed, encoding="utf-8", xml_declaration=True)
 
77
 
78
  @app.get("/download")
79
  def download_fb2(url: str):
80
+ res = requests.get(url, headers={"User-Agent": "Mozilla/5.0"}, timeout=10)
81
+ res.raise_for_status()
82
+ soup = BeautifulSoup(res.text, "html.parser")
83
+ title = soup.title.string.strip() if soup.title and soup.title.string else "article"
84
+ body = str(soup.body)
85
+ fb2 = html_to_fb2(title, body)
86
+ filename = f"{quote(title, safe='').replace('%20','_')[:30]}.fb2"
87
+ return Response(
88
+ content=fb2,
89
+ media_type="application/fb2+xml",
90
+ headers={"Content-Disposition": f"attachment; filename={filename}"}
91
+ )
92
+