scripts/pirate_program/post_mastodon_random_prog.py

47 lines
1.2 KiB
Python
Raw Normal View History

2024-06-10 15:42:10 +02:00
import random
import json
import requests
from bs4 import BeautifulSoup
from mastodon import Mastodon
# Charger les données depuis le fichier JSON
with open("all_pages.json", "r", encoding="utf-8") as f:
all_pages = json.load(f)
def truncate_words(text, max_length):
if len(text) <= max_length:
return text
else:
words = text.split(" ")
current_length = 0
truncated_text = ""
for word in words:
current_length += len(word) + 1 # +1 pour l'espace
if current_length > max_length:
break
truncated_text += word + " "
return truncated_text.strip() + ""
# Choisir un article aléatoire
page = random.choice(all_pages)
print(page['title'])
print(truncate_words(page['content'], 280))
# print(page['url'])
# Configurer Mastodon
# mastodon = Mastodon(
# access_token="your_access_token",
# api_base_url="https://your_instance.com"
# )
#
# # Publier l'article sur Mastodon
# mastodon.toot(
# f"Nouvel article sur le site du Parti Pirate : {page['title']}\n\n"
# f"{page['content'][:280]}…\n\n"
# f"Lire l'article complet : {page['url']}"
# )