2022-11-19 17:53:20 +00:00
|
|
|
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}"}
|
|
|
|
|
2022-11-19 17:54:50 +00:00
|
|
|
HYPOTHESIS_USER=os.getenv("HYPOTHESIS_USER")
|
|
|
|
|
2022-11-19 17:53:20 +00:00
|
|
|
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")
|
2022-11-20 09:56:03 +00:00
|
|
|
r = session.post(f"{endpoint}", json={
|
2022-11-19 17:53:20 +00:00
|
|
|
"type": ["h-entry"],
|
|
|
|
"properties":{
|
|
|
|
"published": [entry.published.strftime("%Y-%m-%dT%H:%M:%S")],
|
|
|
|
"content":[{
|
|
|
|
"html": entry.content.value
|
|
|
|
}],
|
2022-11-20 10:06:31 +00:00
|
|
|
"category": annotation['tags'] + ['hypothesis'],
|
2022-11-20 09:56:03 +00:00
|
|
|
"in-reply-to": [annotation['target'][0]['source']],
|
|
|
|
"hypothesis-link": [json_link]
|
2022-11-19 17:53:20 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-11-20 09:56:03 +00:00
|
|
|
if r.status_code > 300:
|
|
|
|
raise Exception(f"Request failed: {r.status_code}: {r.text}")
|
|
|
|
|
2022-11-19 17:53:20 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
|
2022-11-19 17:54:50 +00:00
|
|
|
if HYPOTHESIS_USER is None:
|
|
|
|
raise Exception("You must set HYPOTHESIS_USER environment variable")
|
|
|
|
|
|
|
|
response = requests.get(f"https://hypothes.is/stream.atom?user={HYPOTHESIS_USER}")
|
2022-11-19 17:53:20 +00:00
|
|
|
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()
|