Spaces:
Sleeping
Sleeping
Create api-article-soleil.py
Browse files- api-article-soleil.py +125 -0
api-article-soleil.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, jsonify
|
| 2 |
+
import requests
|
| 3 |
+
from bs4 import BeautifulSoup
|
| 4 |
+
import time
|
| 5 |
+
|
| 6 |
+
app = Flask(__name__)
|
| 7 |
+
|
| 8 |
+
KEYWORDS = ["élection présidentielle", "présidentielle", "élections présidentielles",
|
| 9 |
+
"élection législative", "législative", "élections législatives"]
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def get_articles():
|
| 13 |
+
url = "https://lesoleil.sn/rubriques/actualites/politique/"
|
| 14 |
+
headers = {
|
| 15 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
# Récupérer la page
|
| 20 |
+
response = requests.get(url, headers=headers, timeout=10)
|
| 21 |
+
if response.status_code != 200:
|
| 22 |
+
print(f"Erreur lors de la récupération de la page principale: {response.status_code}")
|
| 23 |
+
return []
|
| 24 |
+
|
| 25 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 26 |
+
article_links = []
|
| 27 |
+
|
| 28 |
+
# Trouver tous les liens d'articles
|
| 29 |
+
link_elements = soup.select("a.elementor-cta")
|
| 30 |
+
for link in link_elements:
|
| 31 |
+
href = link.get('href')
|
| 32 |
+
if href and href not in article_links:
|
| 33 |
+
article_links.append(href)
|
| 34 |
+
|
| 35 |
+
print(f"Nombre de liens trouvés: {len(article_links)}")
|
| 36 |
+
|
| 37 |
+
results = []
|
| 38 |
+
|
| 39 |
+
for link in article_links:
|
| 40 |
+
try:
|
| 41 |
+
article_response = requests.get(link, headers=headers, timeout=10)
|
| 42 |
+
if article_response.status_code != 200:
|
| 43 |
+
print(f"Erreur lors de la récupération de l'article {link}: {article_response.status_code}")
|
| 44 |
+
continue
|
| 45 |
+
|
| 46 |
+
article_soup = BeautifulSoup(article_response.text, 'html.parser')
|
| 47 |
+
|
| 48 |
+
# Essayer différentes sélections pour trouver le titre
|
| 49 |
+
title_element = article_soup.select_one("h1.td-page-title") or article_soup.select_one("h1")
|
| 50 |
+
if not title_element:
|
| 51 |
+
print(f"Pas de titre trouvé pour {link}")
|
| 52 |
+
continue
|
| 53 |
+
|
| 54 |
+
title = title_element.text.strip()
|
| 55 |
+
|
| 56 |
+
# Essayer différentes sélections pour trouver le contenu
|
| 57 |
+
content_div = article_soup.select_one("div.td-post-content") or article_soup.select_one(
|
| 58 |
+
"div.elementor-widget-theme-post-content")
|
| 59 |
+
if not content_div:
|
| 60 |
+
print(f"Pas de contenu trouvé pour {link}")
|
| 61 |
+
continue
|
| 62 |
+
|
| 63 |
+
content = content_div.text.strip()
|
| 64 |
+
combined_text = f"{title.lower()} {content.lower()}"
|
| 65 |
+
|
| 66 |
+
# Vérifier si le contenu est lié aux élections
|
| 67 |
+
if any(keyword in combined_text for keyword in KEYWORDS):
|
| 68 |
+
paragraphs = content.split("\n")
|
| 69 |
+
description = paragraphs[0] if paragraphs else ""
|
| 70 |
+
results.append({
|
| 71 |
+
"title": title,
|
| 72 |
+
"description": description,
|
| 73 |
+
"content": content,
|
| 74 |
+
"url": link
|
| 75 |
+
})
|
| 76 |
+
print(f"Article trouvé sur les élections: {title}")
|
| 77 |
+
|
| 78 |
+
# Attendre un peu pour ne pas surcharger le serveur
|
| 79 |
+
time.sleep(1)
|
| 80 |
+
|
| 81 |
+
except Exception as e:
|
| 82 |
+
print(f"Erreur pour {link}: {str(e)}")
|
| 83 |
+
|
| 84 |
+
print(f"Nombre total d'articles sur les élections trouvés: {len(results)}")
|
| 85 |
+
return results
|
| 86 |
+
|
| 87 |
+
except Exception as e:
|
| 88 |
+
print(f"Erreur générale: {str(e)}")
|
| 89 |
+
return []
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
@app.route('/', methods=['GET'])
|
| 93 |
+
def index():
|
| 94 |
+
return """
|
| 95 |
+
<html>
|
| 96 |
+
<head>
|
| 97 |
+
<title>API d'articles sur les élections</title>
|
| 98 |
+
<style>
|
| 99 |
+
body { font-family: Arial, sans-serif; margin: 40px; line-height: 1.6; }
|
| 100 |
+
h1 { color: #333; }
|
| 101 |
+
.endpoint { background: #f4f4f4; padding: 10px; border-radius: 5px; }
|
| 102 |
+
.description { margin-bottom: 20px; }
|
| 103 |
+
</style>
|
| 104 |
+
</head>
|
| 105 |
+
<body>
|
| 106 |
+
<h1>API d'articles sur les élections</h1>
|
| 107 |
+
<div class="description">
|
| 108 |
+
Cette API extrait des articles liés aux élections du site LeSOLEIL.sn
|
| 109 |
+
</div>
|
| 110 |
+
<div class="endpoint">
|
| 111 |
+
Endpoint: <a href="/api/election-articles">/api/election-articles</a> - Récupérer tous les articles sur les élections
|
| 112 |
+
</div>
|
| 113 |
+
</body>
|
| 114 |
+
</html>
|
| 115 |
+
"""
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
@app.route('/api/election-articles', methods=['GET'])
|
| 119 |
+
def get_election_articles():
|
| 120 |
+
articles = get_articles()
|
| 121 |
+
return jsonify(articles)
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
if __name__ == '__main__':
|
| 125 |
+
app.run(host="0.0.0.0", debug=True, port=5000)
|