This commit is contained in:
vladislav 2026-09-05 22:31:44 -04:00
parent 1c77c40b39
commit 95465e82f6
Signed by: Vladislav
GPG Key ID: D97C9E5E4860EE45
2 changed files with 112 additions and 59 deletions

165
vk.ipynb
View File

@ -53,7 +53,8 @@
" load_env('env_example')\n", " load_env('env_example')\n",
" except Exception:\n", " except Exception:\n",
" pass" " pass"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -85,7 +86,8 @@
"from fastapi import APIRouter, Depends, Request, Response\n", "from fastapi import APIRouter, Depends, Request, Response\n",
"from fastapi.responses import RedirectResponse\n", "from fastapi.responses import RedirectResponse\n",
"from images import insert_logo\n" "from images import insert_logo\n"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -103,7 +105,8 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"celery_app_vk = Celery('vk', broker=f'{redis_uri}1', backend=f'{redis_uri}1', task_track_started=True )\n" "celery_app_vk = Celery('vk', broker=f'{redis_uri}1', backend=f'{redis_uri}1', task_track_started=True )\n"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -113,7 +116,8 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"token_manager = TokenManager()\n" "token_manager = TokenManager()\n"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -154,7 +158,8 @@
"\n", "\n",
" # Привязка соцсети к уже залогиненному пользователю\n", " # Привязка соцсети к уже залогиненному пользователю\n",
" return RedirectResponse(url='/profile/social', status_code=status.HTTP_303_SEE_OTHER)" " return RedirectResponse(url='/profile/social', status_code=status.HTTP_303_SEE_OTHER)"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -184,7 +189,8 @@
" images[_id] = (\"photo\", (name, background, \"image/jpeg\"))\n", " images[_id] = (\"photo\", (name, background, \"image/jpeg\"))\n",
" \n", " \n",
" return images\n" " return images\n"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -257,7 +263,8 @@
" await mongo_client[db_name][item.get('col')].update_one({'_id': ObjectId(item.get('_id'))}, update_spec)\n", " await mongo_client[db_name][item.get('col')].update_one({'_id': ObjectId(item.get('_id'))}, update_spec)\n",
" return {\"ok\": True}\n", " return {\"ok\": True}\n",
"\n" "\n"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -333,7 +340,8 @@
" await mongo_client[db_name][item.get('col')].update_one({'_id': ObjectId(item.get('_id'))}, update_spec)\n", " await mongo_client[db_name][item.get('col')].update_one({'_id': ObjectId(item.get('_id'))}, update_spec)\n",
" return {\"ok\": True}\n", " return {\"ok\": True}\n",
"\n" "\n"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -386,7 +394,8 @@
" )\n", " )\n",
"\n", "\n",
" return {\"ok\": True}\n" " return {\"ok\": True}\n"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -410,7 +419,7 @@
" try:\n", " try:\n",
" logging.info(f\"VK начало edit {item} {file_ids}\")\n", " logging.info(f\"VK начало edit {item} {file_ids}\")\n",
" res = httpx.post(\n", " res = httpx.post(\n",
" 'http://127.0.0.1:30080/api/v1/social/vk/edit', \n", " 'http://127.0.0.1:30081/api/v1/social/vk/edit', \n",
" headers={\"Authorization\": f\"bearer {ATOKEN}\"}, \n", " headers={\"Authorization\": f\"bearer {ATOKEN}\"}, \n",
" json={'post_id': post_id, 'item': item, 'text': text, 'file_ids': file_ids, 'doc': social, 'sign_avatar': sign_avatar},\n", " json={'post_id': post_id, 'item': item, 'text': text, 'file_ids': file_ids, 'doc': social, 'sign_avatar': sign_avatar},\n",
" timeout=100\n", " timeout=100\n",
@ -421,7 +430,8 @@
" \n", " \n",
" except Exception as exc:\n", " except Exception as exc:\n",
" raise self.retry(exc=exc)\n" " raise self.retry(exc=exc)\n"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -445,7 +455,7 @@
" try:\n", " try:\n",
" logging.info(f\"VK удаления публикации {doc} {post_id}\")\n", " logging.info(f\"VK удаления публикации {doc} {post_id}\")\n",
" res = httpx.post(\n", " res = httpx.post(\n",
" 'http://127.0.0.1:30080/api/v1/social/vk/delete',\n", " 'http://127.0.0.1:30081/api/v1/social/vk/delete',\n",
" headers={\"Authorization\": f\"bearer {ATOKEN}\"},\n", " headers={\"Authorization\": f\"bearer {ATOKEN}\"},\n",
" json={'item': item, 'post_id': post_id, \"doc\": doc},\n", " json={'item': item, 'post_id': post_id, \"doc\": doc},\n",
" timeout=100\n", " timeout=100\n",
@ -456,7 +466,8 @@
" \n", " \n",
" except Exception as exc:\n", " except Exception as exc:\n",
" raise self.retry(exc=exc)\n" " raise self.retry(exc=exc)\n"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -480,7 +491,7 @@
" try:\n", " try:\n",
" logging.info(f\"VK начало публикации {item} {file_ids}\")\n", " logging.info(f\"VK начало публикации {item} {file_ids}\")\n",
" res = httpx.post(\n", " res = httpx.post(\n",
" 'http://127.0.0.1:30080/api/v1/social/vk/send',\n", " 'http://127.0.0.1:30081/api/v1/social/vk/send',\n",
" headers={\"Authorization\": f\"bearer {ATOKEN}\"},\n", " headers={\"Authorization\": f\"bearer {ATOKEN}\"},\n",
" json={'item': item, 'text': text, 'file_ids': file_ids, 'doc': social, 'sign_avatar': sign_avatar},\n", " json={'item': item, 'text': text, 'file_ids': file_ids, 'doc': social, 'sign_avatar': sign_avatar},\n",
" timeout=100\n", " timeout=100\n",
@ -491,7 +502,8 @@
" \n", " \n",
" except Exception as exc:\n", " except Exception as exc:\n",
" raise self.retry(exc=exc)\n" " raise self.retry(exc=exc)\n"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -522,7 +534,8 @@
"# timeout=100\n", "# timeout=100\n",
"# ).json()\n", "# ).json()\n",
"# await asyncio.sleep(1)\n" "# await asyncio.sleep(1)\n"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -553,7 +566,8 @@
"\n", "\n",
" with open(f'{filename_without_extension}.py', 'w') as f:\n", " with open(f'{filename_without_extension}.py', 'w') as f:\n",
" f.write('\\n\\n'.join(app))\n" " f.write('\\n\\n'.join(app))\n"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -567,7 +581,8 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"!celery -A vk.celery_app_vk worker --loglevel=info" "!celery -A vk.celery_app_vk worker --loglevel=info"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -581,7 +596,8 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"!docker build -t registry.spiritofweb.ru/avtopost-api:latest ." "!docker build -t registry.spiritofweb.ru/avtopost-api:latest ."
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -595,7 +611,8 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"!sudo docker image push registry.spiritofweb.ru/avtopost-api:latest" "!sudo docker image push registry.spiritofweb.ru/avtopost-api:latest"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -609,7 +626,8 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"!kubectl rollout restart deployments/vk-celery -n kultmost" "!kubectl rollout restart deployments/vk-celery -n kultmost"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -623,7 +641,8 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"!echo $REGISTRY_HOST" "!echo $REGISTRY_HOST"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -641,7 +660,8 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"stop" "stop"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -651,7 +671,8 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"user_id = '681684390000000000000000'" "user_id = '681684390000000000000000'"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -662,7 +683,8 @@
"source": [ "source": [
"await redis_client.delete(f\"access_token:{user_id}\")\n", "await redis_client.delete(f\"access_token:{user_id}\")\n",
"await redis_client.delete(f\"tokens:{user_id}\")" "await redis_client.delete(f\"tokens:{user_id}\")"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -686,7 +708,8 @@
" },\n", " },\n",
" upsert=True,\n", " upsert=True,\n",
")\n" ")\n"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -696,7 +719,8 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"import token_manager as tm" "import token_manager as tm"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -707,7 +731,8 @@
"source": [ "source": [
"from token_manager import TokenManager\n", "from token_manager import TokenManager\n",
"token_manager = TokenManager()" "token_manager = TokenManager()"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -719,7 +744,8 @@
"token_manager = TokenManager()\n", "token_manager = TokenManager()\n",
"access_token = await token_manager.get_access_token('644fa8bb0000000000000000')\n", "access_token = await token_manager.get_access_token('644fa8bb0000000000000000')\n",
"access_token" "access_token"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -730,7 +756,8 @@
"source": [ "source": [
"item ={'_id': '6a21559cca3ad8a73b652bcb', 'db': 'competitions', 'col': 'competitions'} \n", "item ={'_id': '6a21559cca3ad8a73b652bcb', 'db': 'competitions', 'col': 'competitions'} \n",
"file_ids = ['681684390000000000000000', '6a21559cca3ad8a73b652bc6']" "file_ids = ['681684390000000000000000', '6a21559cca3ad8a73b652bc6']"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -741,7 +768,8 @@
"source": [ "source": [
"docs =[ doc async for doc in mongo_client['social']['social'].find({'user_id':'681684390000000000000000'})]\n", "docs =[ doc async for doc in mongo_client['social']['social'].find({'user_id':'681684390000000000000000'})]\n",
"doc = docs[1]" "doc = docs[1]"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -751,7 +779,8 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"competition = await competitions_col.find_one({'_id':ObjectId('6a21559cca3ad8a73b652bcb')})" "competition = await competitions_col.find_one({'_id':ObjectId('6a21559cca3ad8a73b652bcb')})"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -761,7 +790,8 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"images = await get_images(db_name, file_ids, sign_avatar)" "images = await get_images(db_name, file_ids, sign_avatar)"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -803,7 +833,8 @@
" })\n", " })\n",
" res = resp.json()\n", " res = resp.json()\n",
" res.update({'doc': doc})" " res.update({'doc': doc})"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -813,7 +844,8 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"res" "res"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -880,7 +912,8 @@
" update_spec = {\"$push\": {\"vk\": res}}\n", " update_spec = {\"$push\": {\"vk\": res}}\n",
" await mongo_client[db_name][item.get('col')].update_one({'_id': ObjectId(item.get('_id'))}, update_spec)\n", " await mongo_client[db_name][item.get('col')].update_one({'_id': ObjectId(item.get('_id'))}, update_spec)\n",
" return {\"ok\": True}\n" " return {\"ok\": True}\n"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -919,7 +952,8 @@
" })\n", " })\n",
" print(resp_wall.json())\n", " print(resp_wall.json())\n",
" \n" " \n"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -929,7 +963,8 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"await competitions_col.find_one({'_id': competition['_id']})" "await competitions_col.find_one({'_id': competition['_id']})"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -944,7 +979,8 @@
" \"$push\": {\"vk\": resp_wall.json()}\n", " \"$push\": {\"vk\": resp_wall.json()}\n",
" },\n", " },\n",
" )" " )"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -959,7 +995,8 @@
" \"v\": \"5.199\",\n", " \"v\": \"5.199\",\n",
" })\n", " })\n",
"resp.json()['response'][0]" "resp.json()['response'][0]"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -969,7 +1006,8 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"[doc async for doc in mongo_client['social']['social'].find({'user_id':competition['user']['id']})]" "[doc async for doc in mongo_client['social']['social'].find({'user_id':competition['user']['id']})]"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -987,7 +1025,8 @@
"})\n", "})\n",
"\n", "\n",
"z.json()['response']" "z.json()['response']"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -997,7 +1036,8 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"#!pip freeze > requirements.txt" "#!pip freeze > requirements.txt"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -1017,7 +1057,8 @@
" \"body\": \"group_id=191599084&user_id=392892935&role=moderator&is_contact=0&contact_position=&contact_phone=&contact_email=&permissions=&access_token=vk1.a.-kG07OUZgX5FrxtsQrU5QJh8HaP_suYXkqcg\",\n", " \"body\": \"group_id=191599084&user_id=392892935&role=moderator&is_contact=0&contact_position=&contact_phone=&contact_email=&permissions=&access_token=vk1.a.-kG07OUZgX5FrxtsQrU5QJh8HaP_suYXkqcg\",\n",
" \"method\": \"POST\",\n", " \"method\": \"POST\",\n",
"});" "});"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -1035,7 +1076,8 @@
"VK_CLIENT_ID = 51852444\n", "VK_CLIENT_ID = 51852444\n",
"SCOPES = 'offline,wall,photos,groups'\n", "SCOPES = 'offline,wall,photos,groups'\n",
"redirect_uri = 'https://oauth.vk.ru/blank.html'" "redirect_uri = 'https://oauth.vk.ru/blank.html'"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -1066,7 +1108,8 @@
"url = f'{AUTHORIZE_URL}?responsetype_item=code&client_id={VK_CLIENT_ID}&scope={SCOPES}&redirect_uri={redirect_uri}&state={state}&code_challenge={code_challenge}&code_challenge_method=S256'\n", "url = f'{AUTHORIZE_URL}?responsetype_item=code&client_id={VK_CLIENT_ID}&scope={SCOPES}&redirect_uri={redirect_uri}&state={state}&code_challenge={code_challenge}&code_challenge_method=S256'\n",
"\n", "\n",
"print(f\"\\n\\n{url}\\n\\n\")" "print(f\"\\n\\n{url}\\n\\n\")"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -1078,7 +1121,8 @@
"\n", "\n",
"code='vk2.a.---'\n", "code='vk2.a.---'\n",
"device_id=''" "device_id=''"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -1089,7 +1133,8 @@
"source": [ "source": [
"refresh_token = \"vk2.a.-----\"\n", "refresh_token = \"vk2.a.-----\"\n",
"device_id = \"-\"\n" "device_id = \"-\"\n"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -1108,7 +1153,8 @@
" 'state': os.urandom(32).hex(),\n", " 'state': os.urandom(32).hex(),\n",
"})\n", "})\n",
"z.json()" "z.json()"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -1119,7 +1165,8 @@
"source": [ "source": [
"CLIENT_ID = 51852444\n", "CLIENT_ID = 51852444\n",
"OAUTH_URL = 'https://id.vk.com/oauth2/auth'" "OAUTH_URL = 'https://id.vk.com/oauth2/auth'"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -1140,7 +1187,8 @@
" 'redirect_uri': 'https://oauth.vk.com/blank.html'\n", " 'redirect_uri': 'https://oauth.vk.com/blank.html'\n",
"})\n", "})\n",
"r.json()\n" "r.json()\n"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -1161,7 +1209,8 @@
"\n", "\n",
"z=T()\n", "z=T()\n",
"z.json()" "z.json()"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -1172,7 +1221,8 @@
"source": [ "source": [
"refresh_token=r.json()['refresh_token']\n", "refresh_token=r.json()['refresh_token']\n",
"refresh_token" "refresh_token"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -1191,7 +1241,8 @@
" 'state': os.urandom(32).hex(),\n", " 'state': os.urandom(32).hex(),\n",
"})\n", "})\n",
"z.json()" "z.json()"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -1201,7 +1252,8 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"print(f'\\n\\n{z.json()['refresh_token']}\\n\\n')" "print(f'\\n\\n{z.json()['refresh_token']}\\n\\n')"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "code", "cell_type": "code",
@ -1211,7 +1263,8 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"users.search" "users.search"
] ],
"display_name": ".venv"
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",

6
vk.py
View File

@ -319,7 +319,7 @@ def edit(self, post_id: int, item: dict, text: str, file_ids: list, social: dict
try: try:
logging.info(f"VK начало edit {item} {file_ids}") logging.info(f"VK начало edit {item} {file_ids}")
res = httpx.post( res = httpx.post(
'http://127.0.0.1:30080/api/v1/social/vk/edit', 'http://127.0.0.1:30081/api/v1/social/vk/edit',
headers={"Authorization": f"bearer {ATOKEN}"}, headers={"Authorization": f"bearer {ATOKEN}"},
json={'post_id': post_id, 'item': item, 'text': text, 'file_ids': file_ids, 'doc': social, 'sign_avatar': sign_avatar}, json={'post_id': post_id, 'item': item, 'text': text, 'file_ids': file_ids, 'doc': social, 'sign_avatar': sign_avatar},
timeout=100 timeout=100
@ -347,7 +347,7 @@ def delete_wall(self, post_id: int, doc: dict, item: dict|None = None):
try: try:
logging.info(f"VK удаления публикации {doc} {post_id}") logging.info(f"VK удаления публикации {doc} {post_id}")
res = httpx.post( res = httpx.post(
'http://127.0.0.1:30080/api/v1/social/vk/delete', 'http://127.0.0.1:30081/api/v1/social/vk/delete',
headers={"Authorization": f"bearer {ATOKEN}"}, headers={"Authorization": f"bearer {ATOKEN}"},
json={'item': item, 'post_id': post_id, "doc": doc}, json={'item': item, 'post_id': post_id, "doc": doc},
timeout=100 timeout=100
@ -375,7 +375,7 @@ def notify(self, item: dict, text: str, file_ids: list, social: dict, sign_avata
try: try:
logging.info(f"VK начало публикации {item} {file_ids}") logging.info(f"VK начало публикации {item} {file_ids}")
res = httpx.post( res = httpx.post(
'http://127.0.0.1:30080/api/v1/social/vk/send', 'http://127.0.0.1:30081/api/v1/social/vk/send',
headers={"Authorization": f"bearer {ATOKEN}"}, headers={"Authorization": f"bearer {ATOKEN}"},
json={'item': item, 'text': text, 'file_ids': file_ids, 'doc': social, 'sign_avatar': sign_avatar}, json={'item': item, 'text': text, 'file_ids': file_ids, 'doc': social, 'sign_avatar': sign_avatar},
timeout=100 timeout=100