🛠️ Il ne s’agit pas vraiment d’un article, mais plutôt d’un partage du script Python que j’utilise pour publier automatiquement sur Nostr 🚀 #nostrfr
📋 Pré-requis
1 - 🔑 Générer une paire de clés secrète/publique pour le compte qui publiera les articles.
2 - ⏰ Accéder au fichier crontab afin d’automatiser la récupération des articles à intervalle régulier (sous Linux ou Mac, bien sûr 😁).
3 - 🐍 Installer les bibliothèques Python importées au début du script.
⚙️ Explications
Ce script :
-
🔐 utilise la clé secrète NSEC nsecXXXXX ;
-
🌐 publie sur 5 relais : nos.lol, relay.damus.io, relay.snort.social, relay.primal.net et relay.mostr.pub;
-
📰 récupère le flux RSS www.01net.com/actualites/feed ;
-
💾 stocke la liste des articles déjà publiés — afin d’éviter les doublons — dans /home/nostr/01net_deja_publies.txt ;
-
📣 publie automatiquement les nouveaux articles sur Nostr.
💡 Libre à vous de l’adapter à vos propres flux RSS, relais ou besoins spécifiques !
import feedparser
import time
import os
import pytz
from datetime import datetime
from nostr.event import Event
from nostr.key import PrivateKey
from nostr.relay_manager import RelayManager
# --- CONFIGURATION ---
NSEC = "nsecXXXXX"
FICHIER_LOG = "/home/nostr/01net_deja_publies.txt"
FUSEAU_PARIS = pytz.timezone('Europe/Paris')
# On limite à 4 relais
RELAIS = [
"wss://nos.lol",
"wss://relay.damus.io",
"wss://relay.snort.social",
"wss://relay.primal.net",
"wss://relay.mostr.pub"
]
def envoyer_une_note(message, url_article, pk):
"""Envoie la note avec un tag r pointant vers l'article original."""
event = Event(
content=message,
public_key=pk.public_key.hex(),
tags=[
["r", url_article]
]
)
pk.sign_event(event)
succes_un_relais = False
for r in RELAIS:
manager = RelayManager()
manager.add_relay(r)
try:
print(f"📡 Tentative individuelle sur {r}...")
manager.open_connections()
time.sleep(5)
manager.publish_event(event)
time.sleep(2)
print(f" ✅ Succès probable sur {r}")
succes_un_relais = True
except Exception as e:
print(f" ❌ Échec sur {r} : {e}")
finally:
manager.close_connections()
return succes_un_relais
def publier_sur_nostr():
try:
pk = PrivateKey.from_nsec(NSEC)
except Exception:
print("❌ Clé NSEC invalide.")
return
if not os.path.exists(FICHIER_LOG):
open(FICHIER_LOG, "w").close()
with open(FICHIER_LOG, "r") as f:
liens_connus = set(line.strip() for line in f)
print(f"📡 Analyse du flux - {datetime.now(FUSEAU_PARIS).strftime('%H:%M:%S')}")
flux = feedparser.parse("https://www.01net.com/actualites/feed/")
maintenant_paris = datetime.now(FUSEAU_PARIS)
envoyes = 0
for article in flux.entries:
ts_article = time.mktime(article.published_parsed)
date_article_paris = datetime.fromtimestamp(ts_article, FUSEAU_PARIS)
difference = (maintenant_paris - date_article_paris).total_seconds()
# Fenêtre de 24h
if 0 <= difference < 86400:
if article.link not in liens_connus:
print(f"🆕 Article : {article.title[:50]}...")
message = (
f"📰 {article.title}\n\n"
f"#actualites #tech #01net\n\n"
f"{article.link}"
)
if envoyer_une_note(message, article.link, pk):
with open(FICHIER_LOG, "a") as f:
f.write(f"{article.link}\n")
envoyes += 1
elif difference >= 86400:
break
print(f"🏁 Session terminée. Articles postés : {envoyes}")
if __name__ == "__main__":
publier_sur_nostr()
os._exit(0)

