Skip to content

Instantly share code, notes, and snippets.

@binwiederhier
Created July 12, 2023 02:14
Show Gist options
  • Save binwiederhier/70f13b7c7338a2b75e15438b5567a6d6 to your computer and use it in GitHub Desktop.
Save binwiederhier/70f13b7c7338a2b75e15438b5567a6d6 to your computer and use it in GitHub Desktop.
import json
import os
import requests
def download_file(url, file_path):
response = requests.get(url)
with open(file_path, 'wb') as file:
file.write(response.content)
def find_new_entries(current_file, previous_file):
with open(current_file, 'r') as current:
current_data = json.load(current)
if os.path.exists(previous_file):
with open(previous_file, 'r') as previous:
previous_data = json.load(previous)
previous_ids = [entry['post']['id'] for entry in previous_data['posts']]
new_entries = [entry['post'] for entry in current_data['posts'] if entry['post']['id'] not in previous_ids]
else:
new_entries = [] # First run, consider all entries as old
return new_entries
ntfy_topic_url = "https://ntfy.sh/lemmy"
lemmy_post_url = "https://discuss.ntfy.sh/api/v3/post/list?sort=New"
current_file = "/tmp/lemmy-ntfy-current.json"
previous_file = "/tmp/lemmy-ntfy-previous.json"
download_file(lemmy_post_url, current_file)
new_entries = find_new_entries(current_file, previous_file)
os.rename(current_file, previous_file)
if len(new_entries) == 1:
post_content = f"{new_entries[0]['name']}\n{new_entries[0]['ap_id']}"
print(post_content)
requests.post(ntfy_topic_url, data=post_content, headers={ "Title": "New Lemmy post" })
elif len(new_entries) > 1:
post_content = ""
for entry in new_entries:
post_content += f"{entry['name']}\n{entry['ap_id']}\n\n"
print(post_content.strip())
requests.post(ntfy_topic_url, data=post_content.strip(), headers={ "Title": "New Lemmy posts" })
else:
print("No new posts")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment