from hypothesis_settings import USERNAME, PASSWORD import requests import json # do get to get 2 tokens CSFR # https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet # http://list.hypothes.is/archive/dev/2013-10/0000096.html # GET request /app and take the value of the beaker.session.id and XSRF-TOKEN Set-Cookie headers. # Pass these both back in the POST request in the Cookie header. # http://www.python-requests.org/en/v1.1.0/user/quickstart/#cookies url = "https://hypothes.is/app" r = requests.get(url) cookies = r.cookies payload = {"username":USERNAME,"password":PASSWORD} data = json.dumps(payload) headers = {'content-type':'application/json;charset=UTF-8'} r = requests.post(url="https://hypothes.is/app?__formid__=login", data=data, cookies=cookies, headers=headers) if r.json()['flash'].get('success') == ['You are now logged in.']: token = r.json()['model']['token'] else: token = None # https://hypothes.is/a/ByQV3a_4R9KNq8VfatbxFQ -- public # DtiypTBrTVSVAVmPNh7koQ -- private url = "https://api.hypothes.is/annotations/{a_id}".format(a_id='DtiypTBrTVSVAVmPNh7koQ') print url headers = {"X-Annotator-Auth-Token": token} r = requests.get(url, headers = headers) annotation = r.json() annotation #annotation["id"], annotation["quote"], annotation["text"], annotation["uri"] # NOT https://hypothes.is/stream#?user=rdhyee # https://api.hypothes.is/search?user=acct:rdhyee@hypothes.is from itertools import islice from urllib import urlencode def search(user, offset=0): headers = {"X-Annotator-Auth-Token": token} page_size = 10 user_acct = "acct:{user}@hypothes.is".format(user=user) limit=page_size more_results = True while more_results: search_dict = {'user':user_acct, 'limit':limit, 'offset':offset} url = "https://api.hypothes.is/search?{query}".format(query=urlencode(search_dict)) r = requests.get(url, headers=headers) rows = r.json().get("rows") if len(rows): for row in rows: yield row offset += page_size else: more_results = False for (i,row) in enumerate(search(user='rdhyee', offset=0)): print i, row