Created
February 28, 2024 14:30
-
-
Save briandeheus/6ce3b57e1cc828ae753fb1f77ded4b7d to your computer and use it in GitHub Desktop.
Mainichi poster
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 feedparser | |
import sqlite3 | |
import os | |
import logging | |
import dotenv | |
import time | |
dotenv.load_dotenv() | |
logging.basicConfig(level=logging.INFO) | |
ACCESS_TOKEN = os.environ.get("ACCESS_TOKEN") | |
MASTODON_URL = os.environ.get("MASTODON_URL") | |
SLEEP_TIME = 5 | |
con = sqlite3.connect(os.environ.get("MAINICHI_DB", "mainichi.db")) | |
def setup_db(): | |
cur = con.cursor() | |
logging.info("Creating posts table") | |
cur.execute("CREATE TABLE IF NOT EXISTS posts(url)") | |
cur.close() | |
def does_post_exist(url): | |
cur = con.cursor() | |
if cur.execute("SELECT url FROM posts WHERE url=?", [url]).fetchone(): | |
return True | |
logging.info("Storing post url=%s", url) | |
cur.execute("INSERT INTO posts VALUES(?)", [url]) | |
cur.close() | |
def post(title, url): | |
res = requests.post(url=f"{MASTODON_URL}/api/v1/statuses", json={ | |
"status": f"{title} {url}" | |
}, headers={ | |
"Authorization": f"Bearer {ACCESS_TOKEN}" | |
}) | |
if res.status_code >= 400: | |
logging.error("Failed to post url=%s because=%s", url, res.text) | |
return | |
logging.error("Successfully posted url=%s", url) | |
def main(): | |
feed = feedparser.parse("https://mainichi.jp/rss/etc/english_latest.rss") | |
for article in feed.entries: | |
url = article["link"] | |
summary = article["title"] | |
if does_post_exist(url=article["link"]): | |
logging.info("Skipping url=%s because it has already been posted", url) | |
continue | |
post(title=summary, url=url) | |
time.sleep(SLEEP_TIME) | |
con.commit() | |
if __name__ == '__main__': | |
setup_db() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment