def pocket_settings(): form = PocketForm() pocket = Query() if form.validate_on_submit(): if db.search(pocket.type == "pocket_key"): redirect("/parse_pocket") request_data = { "consumer_key": form.api_key.data, "redirect_uri": "http://localhost:5000/parse_pocket?new=1", } resp = requests.post( "https://getpocket.com/v3/oauth/request", json=request_data, headers={ "X-Accept": "application/json", "Content-Type": "application/json"}) new_data = { "type": "pocket_key", "consumer_key": form.api_key.data, "code": resp.json()["code"]} if db.search(pocket.type == "pocket_key"): db.insert(new_data) else: db.update(new_data, pocket.type == "pocket_key") flash("Settings Saved") return redirect( f"https://getpocket.com/auth/authorize?request_token={r.json()['code']}&redirect_uri=http://localhost:5000/parse_pocket?new=1") return render_template( "pocket/new.html", title="Pocket Settings", form=form)
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("/")
async def true_group_name(message: Message, state: FSMContext): """ Функция для проверки валидности названия группы пользователя. """ await state.update_data(group=message.text.upper()) needed_user = db.search(User.id == message.chat.id) async with state.proxy() as data: if needed_user: db.update(set('group', data['group']), User.id == message.chat.id) else: db.insert({'id': message.chat.id, 'group': data['group']}) await state.finish() success_text = 'Ваш помощник всё запомнил, спасибо. Пользуйтесь функционалом на здоровье!' await message.answer(success_text, reply_markup=MAIN_MENU)