Created
October 29, 2023 20:43
-
-
Save EllieTheYeen/931004c78808b3d52daf7723cb6453fd to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/python3 | |
from bs4 import BeautifulSoup as bs | |
import httpx | |
import datetime | |
import pathlib | |
import json | |
class Thingy: | |
def __init__(self, file): | |
self.file = pathlib.Path(file) | |
try: | |
self.data = { | |
k: datetime.datetime.fromisoformat(v) | |
for k, v in json.loads(self.file.read_text()).items() | |
} | |
except Exception: | |
self.data = {} | |
def save(self, feed): | |
if feed: | |
self.data[feed] = datetime.datetime.now() | |
self.file.write_text( | |
json.dumps({k: v.isoformat() for k, v in self.data.items()}) | |
) | |
def newerthan(self, feed, seconds=3600): | |
if feed not in self.data: | |
return False | |
return (datetime.datetime.now() - self.data[feed]).total_seconds() < seconds | |
def getmast(): | |
global mast | |
try: | |
return mast | |
except NameError: | |
pass | |
import mastodon | |
access_token = ... # Your access token as a string here | |
api_base_url = "https://toot.cat" # Or whatever Mastodon instance you use | |
mast = mastodon.Mastodon( | |
mastodon_version="1.4.3", | |
access_token=access_token, | |
api_base_url=api_base_url, | |
) | |
return mast | |
def handleblogfeed(doc: bs): | |
for d in doc.select("entry"): | |
title = d.select("title")[0].text | |
url = d.select('link[href^="http"][href$=".html"]')[0].attrs["href"] | |
# url = d.select('id')[0].text | |
if url in post: | |
continue | |
posttext = f"New blog post: {title} {url}" | |
print(posttext) | |
mast = getmast() | |
pos = mast.status_post(status=posttext) | |
post[url] = pos["url"] | |
savepostfile() | |
print(pos) | |
exit() | |
feeds = {"https://ellietheyeen.github.io/feed.xml": handleblogfeed} | |
# feeds = {"http://192.168.0.21:4000/feed.xml": handleblogfeed} | |
basepath = pathlib.Path(__file__).parent | |
datafile = basepath / "date.json" | |
postfile = basepath / "post.json" | |
try: | |
post = json.loads(postfile.read_text()) | |
except Exception: | |
post = {} | |
def savepostfile(): | |
postfile.write_text(json.dumps(post)) | |
t = Thingy(datafile) | |
for feed, handler in feeds.items(): | |
if t.newerthan(feed, 60): | |
continue | |
q = httpx.get(feed) | |
q.raise_for_status() | |
doc = bs(q.text, features="xml") | |
t.save(feed) | |
handler(doc) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment