import flask import json import os import dotenv import requests import atoma from datetime import datetime from filelock import Timeout, FileLock from tqdm import tqdm dotenv.load_dotenv() app = flask.Flask("hypopub") session = requests.session() token = os.getenv("MICROPUB_TOKEN") session.headers = {"Authorization": f"Bearer {token}"} def send_micropub(entry: atoma.atom.AtomEntry): # get the json corresponding to the annotation json_link = None for link in entry.links: if (link.rel == 'alternate') and (link.type_ == "application/json"): json_link = link.href if json_link is None: raise Exception("Cannot find json for annotation") annotation = requests.get(json_link).json() endpoint = os.getenv("MICROPUB_ENDPOINT") session.post(f"{endpoint}", json={ "type": ["h-entry"], "properties":{ "published": [entry.published.strftime("%Y-%m-%dT%H:%M:%S")], "content":[{ "html": entry.content.value }], "in-reply-to": [annotation['target'][0]['source']] } }) def main(): response = requests.get("https://hypothes.is/stream.atom?user=ravenscroftj") feed = atoma.parse_atom_bytes(response.content) print(f"Processing {feed.title.value}") state = {} last_run = datetime(1991,1,1).timestamp() if os.path.exists("state.json"): with open("state.json") as f: state = json.load(f) last_run = state['last_run'] for entry in tqdm(feed.entries): if entry.published.timestamp() > last_run: tqdm.write(f"Send micropub payload for '{entry.title.value}'") send_micropub(entry) else: tqdm.write(f"Entry older than last run time: '{entry.title.value}'") state['last_run'] = datetime.now().timestamp() with open("state.json",'w') as f: json.dump(state, f) if __name__ == "__main__": main()