79 lines
2.3 KiB
Python
79 lines
2.3 KiB
Python
import os
|
|
import requests
|
|
from bs4 import BeautifulSoup
|
|
import argparse
|
|
from mastodon import Mastodon
|
|
from dotenv import load_dotenv
|
|
|
|
# Chargement du token d'identification depuis le fichier.env
|
|
load_dotenv()
|
|
TOKEN_CURATOR = os.getenv('TOKEN_CURATOR')
|
|
ID_CURATOR = os.getenv('ID_CURATOR')
|
|
SECRET_CURATOR = os.getenv('SECRET_CURATOR')
|
|
|
|
def get_first_image_url(title):
|
|
"""
|
|
Récupère l'URL de la première image de l'article MediaWiki.
|
|
"""
|
|
response = requests.get("https://wiki.openstreetmap.org/wiki/"+title)
|
|
soup = BeautifulSoup(response.content, 'html.parser')
|
|
images = soup.find_all('img')
|
|
for i in images:
|
|
source = i['src']
|
|
print(source)
|
|
if '.svg.png' not in source:
|
|
return source
|
|
else:
|
|
return None
|
|
|
|
def post_mastodon(image_url, message, dry_run=False):
|
|
"""
|
|
Poste un message avec une image sur l'instance Mastodon.
|
|
"""
|
|
|
|
# Récupération de l'image
|
|
print("poster avec l'image_url ",image_url)
|
|
image_response = requests.get(image_url)
|
|
image_data = image_response.content
|
|
|
|
if dry_run:
|
|
print("Dry run, pas de post réel")
|
|
return
|
|
|
|
|
|
|
|
# Préparation des données pour l'API Mastodon
|
|
mastodon = Mastodon(
|
|
client_id=ID_CURATOR,
|
|
client_secret=SECRET_CURATOR,
|
|
access_token=TOKEN_CURATOR,
|
|
api_base_url='https://mastodon.cipherbliss.com/api/v1/'
|
|
)
|
|
|
|
# Envoi de la requête à l'API Mastodon
|
|
response = mastodon.status_post(message, media=image_data)
|
|
|
|
if response.status_code == 200:
|
|
print("Post envoyé avec succès!")
|
|
else:
|
|
print("Erreur lors de l'envoi du post")
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description='Poste un message avec une image sur Mastodon')
|
|
parser.add_argu
|
|
de la page MediaWiki')
|
|
parser.add_argument('--no-dry-run', action='store_false', help='Envoyer réellement le post (par défaut : dry run)')
|
|
args = parser.parse_args()
|
|
|
|
image_url = get_first_image_url(args.title) main() to view a complete err
|
|
|
|
if image_url:
|
|
message = "${args.title} \n
|
|
#rtfw #openstreetmap #wiki"
|
|
post_mastodon(image_url, message, dry_run=args.no_dry_run)
|
|
else:
|
|
print("Aucune image trouvée dans l'article")
|
|
|
|
if __name__ == "__main__":
|
|
main() to view a complete err
|