Chris Adams / Jan 15 2023
Creating quotebacks from hypothesis
! pip install httpx
! pip install rich
import os
import json
import httpx
import rich
4.4s
token = os.getenv("HYPOTHESIS_TOKEN")
headers = {"Authorization": f"Bearer {token}"}
chosen_page = "https://www.newyorker.com/news/us-journal/could-coal-waste-be-used-to-make-sustainable-batteries"
res_json = httpx.get(
"https://api.hypothes.is/api/search",
headers=headers,
params={"user":"mrchrisadams", "uri": chosen_page}
).json()
with open("/results/hypo.json", "w") as hypo_file:
hypo_file.write(json.dumps(res_json))
0.9s
OK, get this out, you would need to reach into the json, and pull out an object and for each object, you would need to:
pull out the
url / uri
pull out the
TextQuoteSelector
in the targetpull out the
title
from thedocument
object
This would give me enough to make something like the pic below. There is no original author in hypothesis, so you'd need to pull it out yourself.
title = res_json['rows'][0]['document']['title']
link = res_json['rows'][0]['target'][0]['source']
quoteSelector, *_ = [selector
for selector in
res_json['rows'][0]['target'][0]['selector']
if selector['type'] == 'TextQuoteSelector'
]
quote = quoteSelector['exact']
0.0s
quoteback_markdown = f"""
> {quote}
Source: [{title}]({link})
"""
rich.print(quoteback_markdown)
0.0s
OK, this wasn't too hard. You could do something like this to pull down all the highlights as quotebacks into a local file.
with open("/results/quoteback_example.md", "w") as quoteback_file:
quoteback_file.write(quoteback_markdown)
0.0s