-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
209 lines (178 loc) · 6.79 KB
/
Copy pathmain.py
File metadata and controls
209 lines (178 loc) · 6.79 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import time
import json
import threading
from datetime import datetime, timezone
from feedgen.feed import FeedGenerator
from http.server import HTTPServer, BaseHTTPRequestHandler
import requests
from bs4 import BeautifulSoup
news_list = []
LAST_UPDATE = ""
def fetch_news():
global news_list, LAST_UPDATE
session = requests.Session()
headers = {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"accept-language": "fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7",
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36",
}
try:
response = session.get(
"https://www.mediapart.fr/journal/fil-dactualites", headers=headers
)
response.raise_for_status()
soup = BeautifulSoup(response.content, "html.parser")
articles = soup.find_all("div", class_="teaser")
new_news_list = []
for article in articles:
title_tag = article.find("h2", class_="teaser__title")
titre = (
title_tag.get_text(separator=" ", strip=True)
if title_tag
else "Non trouvé"
)
body_tag = article.find("div", class_="teaser__body")
resume = (
body_tag.get_text(separator=" ", strip=True)
if body_tag
else "Non trouvé"
)
date_tag = article.find("time", class_="teaser__timestamp")
date_iso = (
date_tag.get("datetime", datetime.now(timezone.utc).isoformat())
if date_tag
else datetime.now(timezone.utc).isoformat()
)
link_tag = article.find("a")
link = (
f"https://www.mediapart.fr{link_tag['href']}"
if link_tag and "href" in link_tag.attrs
else "https://www.mediapart.fr"
)
# Use link as id
new_news_list.append(
{
"title": titre,
"content": resume,
"date_modified": date_iso,
"url": link,
"id": link,
}
)
news_list = new_news_list
LAST_UPDATE = datetime.now(timezone.utc).isoformat()
print(f"[{LAST_UPDATE}] News fetched successfully ({len(news_list)} items).")
except Exception as e:
print(f"[{datetime.now(timezone.utc).isoformat()}] Error fetching news: {e}")
def update_loop():
while True:
fetch_news()
time.sleep(3600) # Update periodically (every hour)
def build_atom():
fg = FeedGenerator()
fg.id("https://www.mediapart.fr/journal/fil-dactualites")
fg.title("Mediapart Fil d'actualités")
fg.author({"name": "Mediapart"})
fg.link(href="https://www.mediapart.fr", rel="alternate")
fg.updated(LAST_UPDATE)
for item in news_list:
fe = fg.add_entry()
fe.id(item["id"])
fe.title(item["title"])
fe.link(href=item["url"], rel="alternate")
fe.published(item["date_modified"])
fe.updated(item["date_modified"])
fe.content(item["content"], type="html")
return fg.atom_str(pretty=True)
def build_json():
data = {
"version": "https://jsonfeed.org/version/1",
"title": "Mediapart Fil d'actualités",
"home_page_url": "https://www.mediapart.fr",
"items": [
{
"id": item["id"],
"title": item["title"],
"date_modified": item["date_modified"],
"url": item["url"],
"content_text": item["content"],
}
for item in news_list
],
}
return json.dumps(data, indent=4).encode("utf-8")
def build_txt():
# Emulate PHP print_r output for text like in the example
lines = []
lines.append("Array")
lines.append("(")
lines.append(" [name] => Mediapart Fil d'actualités")
lines.append(" [uri] => https://www.mediapart.fr")
lines.append(" [items] => Array")
lines.append(" (")
for i, item in enumerate(news_list):
lines.append(f" [{i}] => Array")
lines.append(" (")
lines.append(f" [uri] => {item['url']}")
lines.append(f" [title] => {item['title']}")
lines.append(f" [timestamp] => {item['date_modified']}")
lines.append(f" [content] => {item['content']}")
lines.append(" )")
lines.append("")
lines.append(" )")
lines.append(")")
return "\n".join(lines).encode("utf-8")
class RSSRequestHandler(BaseHTTPRequestHandler):
# Hide log requests to not spam console
def log_message(self, format, *args):
pass
def do_GET(self):
if self.path == "/rss.atom":
self.send_response(200)
self.send_header("Content-Type", "application/atom+xml; charset=utf-8")
self.end_headers()
self.wfile.write(build_atom())
elif self.path == "/rss.json":
self.send_response(200)
self.send_header("Content-Type", "application/json; charset=utf-8")
self.end_headers()
self.wfile.write(build_json())
elif self.path == "/rss.txt":
self.send_response(200)
self.send_header("Content-Type", "text/plain; charset=utf-8")
self.end_headers()
self.wfile.write(build_txt())
else:
self.send_response(200)
self.send_header("Content-Type", "text/html; charset=utf-8")
self.end_headers()
html = """
<html><body>
<h1>Mediapart RSS</h1>
<ul>
<li><a href="/rss.atom">Atom Feed</a></li>
<li><a href="/rss.json">JSON Feed</a></li>
<li><a href="/rss.txt">Plaintext Feed</a></li>
</ul>
</body></html>
"""
self.wfile.write(html.encode("utf-8"))
def main():
print("Starting initial fetch...")
fetch_news()
thread = threading.Thread(target=update_loop, daemon=True)
thread.start()
server_address = ("", 8080)
httpd = HTTPServer(server_address, RSSRequestHandler)
print("Serving feeds at port 8080.")
print(
"Paths available: http://localhost:8080/rss.atom, http://localhost:8080/rss.json, http://localhost:8080/rss.txt"
)
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\nShutting down server.")
httpd.server_close()
if __name__ == "__main__":
main()