Created
April 18, 2023 20:00
-
-
Save danromero/a0e8e53a1e3f878a3475ba6ca584dd80 to your computer and use it in GitHub Desktop.
ChatGPT created script to auto post top Hacker News stories to Farcaster ever 10 minutes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import time | |
# Define the Hacker News API endpoint for top stories | |
hn_api_url = "https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty" | |
# Define the Warpcast API endpoint for sending new casts | |
warpcast_api_url = "https://api.warpcast.com/v2/casts" | |
# Define the bearer token for the Warpcast API | |
bearer_token = "ABCXYZ" | |
# Initialize a set to store the IDs of stories we've already seen | |
seen_story_ids = set() | |
# Define a function to send a new cast to the Warpcast API | |
def send_warpcast(text): | |
headers = { | |
"Authorization": f"Bearer {bearer_token}", | |
"Content-Type": "application/json", | |
"Accept": "application/json" | |
} | |
data = { | |
"text": text | |
} | |
response = requests.post(warpcast_api_url, json=data, headers=headers) | |
if response.status_code == 200: | |
print(f"Successfully sent cast: {text}") | |
else: | |
print(f"Failed to send cast: {response.status_code}, {response.text}") | |
# Continuously check for new stories every 10 minutes | |
while True: | |
# Get the latest stories from the Hacker News API | |
response = requests.get(hn_api_url) | |
latest_story_ids = response.json() | |
# Check for new stories and send new casts | |
for story_id in latest_story_ids: | |
if story_id not in seen_story_ids: | |
# Get the details of the new story | |
story_api_url = f"https://hacker-news.firebaseio.com/v0/item/{story_id}.json?print=pretty" | |
story_response = requests.get(story_api_url) | |
story_data = story_response.json() | |
# Extract the title and URL of the new story | |
title = story_data.get("title", "") | |
url = story_data.get("url", "") | |
# Format the text for the new cast | |
text = f"{title}\n{url}" | |
# Send a new cast to the Warpcast API | |
send_warpcast(text) | |
# Add the story ID to the set of seen stories | |
seen_story_ids.add(story_id) | |
# Sleep for 10 minutes (600 seconds) before checking for new stories again | |
time.sleep(600) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Needs to handle 201 response from Warpcast.