def parse_pocket(): pocket = db.search(Query().type == "pocket_key")[0] if request.args.get("new") == "1": auth_data = { "consumer_key": pocket["consumer_key"], "code": pocket["code"]} resp = requests.post( "https://getpocket.com/v3/oauth/authorize", json=auth_data, headers={ "X-Accept": "application/json", "Content-Type": "application/json"}) db.update( operations.set( "access_token", resp.json()["access_token"]), Query().type == "pocket_key") flash(f"{resp.json()['username']} Signed in!") pocket_data = { "consumer_key": pocket["consumer_key"], "access_token": pocket["access_token"], "sort": "newest"} # get date of latest call to pocket api since = datetime(1970, 1, 1) for post in data.get_items(types=["pocket_bookmark"], structured=False): date = datetime.strptime(post["date"].replace("-", "/"), "%x") since = max(date, since) since = datetime.timestamp(since) if since: pocket_data["since"] = since bookmarks = requests.post( "https://getpocket.com/v3/get", json=pocket_data).json() # api spec: https://getpocket.com/developer/docs/v3/retrieve for pocket_bookmark in bookmarks["list"].values(): if int(pocket_boomark["status"]) != 2: desc = pocket_bookmark["excerpt"] if int(pocket_bookmark["is_article"]) else None bookmark = DataObj( desc=desc, url=pocket_bookmark["resolved_url"], date=datetime.now(), tags="", type="pocket_bookmarks") print(bookmark.insert()) return redirect("/")
ELASTIC_SEARCH = Elasticsearch([app.config["ELASTICSEARCH_URL"]]) with open("elasticsearch.json", "r") as search_data: elastic_conf = json.load(search_data) # create index if not already existing try: print( ELASTIC_SEARCH.indices.create(index=app.config["INDEX_NAME"], body=elastic_conf)) except: print("Elasticsearch index already created") app.jinja_options["extensions"].append("jinja2.ext.do") Scss(app) # small db to store pocket creds db = TinyDB("db.json") # create dir that will hold data if it doesn"t already exist DIRNAME = "data/" Path(DIRNAME).mkdir(parents=True, exist_ok=True) from main import data from main import routes, models # get max id app.config["max_id"] = 0 for dataobj in data.get_items(structured=False): app.config["max_id"] = max(app.config["max_id"], dataobj["id"]) app.config["max_id"] += 1
def index(): dataobjs = data.get_items() return render_template("home.html", title="Home", dataobjs=dataobjs)
def index(): dataobjs = data.get_items() return render_template('home.html', title='Home', dataobjs=dataobjs)