#!/usr/bin/env python # -*- coding: utf-8 -*- # A bot that takes an RSS feed and posts to iceshrimp.NET # Parses the latest entry in the RSS feed and if it doesn't match the last note, sends the latest entry from secrets import * from pathlib import Path import requests import feedparser RSS_FEED = feedparser.parse(FEED_URL) RSS_ENTRY = RSS_FEED.entries[0] # Function to build the note, send it and update the 'last_note.txt' file def send_note(): # Build the headers including the ICESHRIMP_TOKEN headers = { 'accept': 'application/json', 'Content-Type': 'application/json', 'Authorization' : f'Bearer {ICESHRIMP_TOKEN}', } # Build the text part of the note from the chosen 'RSS_ENTRY' note_str = '' note_str += f"{RSS_ENTRY['title']}\n\n" note_str += f"{RSS_ENTRY['description']}\n" note_str += f"\n\n{RSS_ENTRY['link']}\n\n" note_str += f"\n\nThis is an automated post from the {GAME_HASHTAGS} RSS feed dated {RSS_ENTRY['published']}\n\n" note_str += f"\n\n{NOTE_HASHTAGS}\n\n" # Build the payload payload = { 'text': note_str, 'visibility': 'public', } # POST the payload to the ICESHRIMP_URL response = requests.post(ICESHRIMP_NOTE, headers=headers, json=payload) # Update the 'last_note.txt' file with 'latest_entry' last_note = open(Path(FILE_PATH) / "last_note.txt","w") last_note.write(latest_entry) last_note.close() # Update the 'last_rss.txt' file with 'published' date last_rss = open(Path(FILE_PATH) / "last_rss.txt","w") last_rss.write(published) last_rss.close() # Output a response print("New note sent from the " + GAME_HASHTAGS + " RSS feed!") # Find the latest RSS entry latest_entry = RSS_ENTRY.id # Find the latest published date published = RSS_ENTRY.published # Find the 'last_note' sent with open(Path(FILE_PATH) / "last_note.txt") as last_note: last_note = last_note.read() # If 'latest_entry' does not equal 'last_note' then send a new note based on 'latest_entry' if latest_entry != last_note: send_note() elif latest_entry == last_note: # Output a response print("Nothing new to add from the " + GAME_HASHTAGS + " RSS feed!")