add sync_xbot_activity_info
continuous-integration/drone/push Build is passing

This commit is contained in:
jianxin committed 2024-02-20 20:04:04 +08:00
1 parent 57e98728eb
commit f6202d551e
13 files changed
+386 -41

No files matched your search

+9 -5
View File
@@ -5,6 +5,7 @@ import time
import lark_oapi as lark
from celery import Celery
from celery import Task
from celery.schedules import crontab
from flask import Flask
@@ -101,11 +102,6 @@ def create_app() -> Flask:
resp = handler.do(parse_req())
return parse_resp(resp)
@app.route("/test", methods=["GET"])
def test():
add.delay(1, 2)
return {"test": "ok"}
@app.route("/index", methods=["GET"])
def index():
return {"test": "ok"}
@@ -122,5 +118,13 @@ def celery_init_app(app: Flask) -> Celery:
celery_app = Celery(app.name, task_cls=FlaskTask)
celery_app.config_from_object(app.config["CELERY"])
celery_app.set_default()
celery_app.conf.timezone = "Asia/Shanghai"
celery_app.conf.beat_schedule = {
"sync-office-xbot-info": {
"task": "app.task.sync_xbot_activity_info",
"schedule": crontab(hour=19, minute=57),
},
}
app.extensions["celery"] = celery_app
return celery_app
Binary file not shown.
+7 -7
View File
@@ -2,10 +2,10 @@ from app import create_app
flask_app = create_app()
celery_app = flask_app.extensions["celery"]
celery_app.conf.beat_schedule = {
"add-every-30-seconds": {
"task": "app.task.sync_xbot_activity_info",
"schedule": 1800,
}
}
celery_app.conf.timezone = "UTC"
# celery_app.conf.beat_schedule = {
# "add-every-30-seconds": {
# "task": "app.task.sync_xbot_activity_info",
# "schedule": 1800,
# }
# }
# celery_app.conf.timezone = "UTC"
View File
Whitespace-only changes.
+71
View File
@@ -0,0 +1,71 @@
import os
import json
import lark_oapi as lark
from flask import Flask
from lark_oapi.adapter.flask import *
from lark_oapi.api.im.v1 import *
from lark_oapi.api.bitable.v1 import *
import lark_oapi as lark
from lark_oapi.api.drive.v1 import *
from lark_oapi.api.bitable.v1 import *
from app.utils.yd_utils import get_activity_info, get_token, get_office_app_uuids
from app.utils.utils import get_filtered_record_by_uuid, update_record, add_record
app_id = os.environ["APP_ID"]
app_secret = os.environ["APP_SECRET"]
bitable_token = os.environ["BITABLE_TOKEN"]
table_id = os.environ["TABLE_ID"]
xbot_username = os.environ["XBOT_USERNAME"]
xbot_password = os.environ["XBOT_PASSWORD"]
bitable_token = "XDmHbhbZcaY4gZsOvBJcLcHonKh"
table_id = "tbl2GcBvkvMtOBnl"
view_id = "vewIAewVD2"
client = lark.Client.builder().app_id(app_id).app_secret(app_secret).build()
token = get_token(xbot_username, xbot_password)
handle_list = []
# 同步应用数据
# office_app_uuids = get_office_app_uuids()
# print(f"office_activity_uuids count: {len(office_app_uuids)}")
# 同步指令数据
office_activity_uuids = get_activity_info(token)
print(f"office_activity_uuids count: {len(office_activity_uuids)}")
for item in office_activity_uuids:
robot_uuid = item.get("robotUuid")
package_view_name = item.get("packageViewName")
print("=====================================")
print(f"同步信息: {robot_uuid}, {package_view_name}")
record_id = get_filtered_record_by_uuid(
client, bitable_token, table_id, view_id, "UUID", robot_uuid
)
if record_id:
print(f"{robot_uuid}已同步多维表格中")
update_record(
client, bitable_token, table_id, record_id, {"名称": package_view_name}
)
else:
handle_list.append(robot_uuid)
print(f"{robot_uuid}: {package_view_name} 未同步多维表格中")
add_record(
client,
bitable_token,
table_id,
{"UUID": robot_uuid, "名称": package_view_name, "分类": "官方指令"},
)
print(f"新增的数量: {len(handle_list)}")
print("新增的uuid: ", handle_list)
+45 -23
View File
@@ -10,25 +10,6 @@ from app.utils.utils import *
from app.utils.yd_utils import get_token, get_activity_info
@shared_task(ignore_result=False)
def add(a: int, b: int) -> int:
return a + b
@shared_task()
def block() -> None:
time.sleep(5)
@shared_task(bind=True, ignore_result=False)
def process(self: Task, total: int) -> object:
for i in range(total):
self.update_state(state="PROGRESS", meta={"current": i + 1, "total": total})
time.sleep(1)
return {"current": total, "total": total}
@shared_task(include_result=False)
def process_message_callback(
app_id, app_secret, bitable_token, table_id, cache_dir, data
@@ -93,11 +74,52 @@ def process_message_callback(
@shared_task()
def sync_xbot_activity_info():
app_id = os.environ["APP_ID"]
app_secret = os.environ["APP_SECRET"]
bitable_token = os.environ["OFFICE_XBOT_BITABLE_TOKEN"]
table_id = os.environ["OFFICE_XBOT_TABLE_ID"]
view_id = os.environ["OFFICE_XBOT_VIEW_ID"]
xbot_username = os.environ["XBOT_USERNAME"]
xbot_password = os.environ["XBOT_PASSWORD"]
client = lark.Client.builder().app_id(app_id).app_secret(app_secret).build()
token = get_token(xbot_username, xbot_password)
office_activity_uuids: list = get_activity_info(token)
handle_list = []
# 同步应用数据
# office_app_uuids = get_office_app_uuids()
# print(f"office_activity_uuids count: {len(office_app_uuids)}")
# 同步指令数据
office_activity_uuids = get_activity_info(token)
print(f"office_activity_uuids count: {len(office_activity_uuids)}")
for item in office_activity_uuids:
uuid = item.get("uuid")
package_viewName = item.get("packageViewName")
print(uuid, package_viewName)
robot_uuid = item.get("robotUuid")
package_view_name = item.get("packageViewName")
print("=====================================")
print(f"同步信息: {robot_uuid}, {package_view_name}")
record_id = get_filtered_record_by_uuid(
client, bitable_token, table_id, view_id, "UUID", robot_uuid
)
if record_id:
print(f"{robot_uuid}已同步多维表格中")
update_record(
client, bitable_token, table_id, record_id, {"名称": package_view_name}
)
else:
handle_list.append(robot_uuid)
print(f"{robot_uuid}: {package_view_name} 未同步多维表格中")
add_record(
client,
bitable_token,
table_id,
{"UUID": robot_uuid, "名称": package_view_name, "分类": "官方指令"},
)
print(f"新增的数量: {len(handle_list)}")
print("新增的uuid: ", handle_list)
View File
Whitespace-only changes.
+84
View File
@@ -11,6 +11,7 @@ from lark_oapi.api.im.v1 import *
from lark_oapi.api.bitable.v1 import *
import lark_oapi as lark
from lark_oapi.api.drive.v1 import *
from lark_oapi.api.bitable.v1 import *
def update_record(client, bitable_token, table_id, record_id, data):
@@ -197,3 +198,86 @@ def send_message(client, receive_id, content):
# 处理业务结果
lark.logger.info(lark.JSON.marshal(response.data, indent=4))
def get_filtered_record_by_uuid(client, bitable_token, table_id, view_id, field_name, uuid):
request: SearchAppTableRecordRequest = (
SearchAppTableRecordRequest.builder()
.app_token(bitable_token)
.table_id(table_id)
.user_id_type("user_id")
.page_size(20)
.request_body(
SearchAppTableRecordRequestBody.builder()
.view_id(view_id)
.field_names([field_name])
.sort([Sort.builder().field_name(field_name).desc(True).build()])
.filter(
FilterInfo.builder()
.conjunction("and")
.conditions(
[
Condition.builder()
.field_name(field_name)
.operator("is")
.value([uuid])
.build()
]
)
.build()
)
.automatic_fields(False)
.build()
)
.build()
)
# 发起请求
option = (
lark.RequestOption.builder()
.user_access_token("u-caH0NMIK50kUMfBMUqgMtr1l6blBghh1Mi0015.82GbP")
.build()
)
response: SearchAppTableRecordResponse = client.bitable.v1.app_table_record.search(
request, option
)
# 处理失败返回
if not response.success():
# lark.logger.error(
# f"client.bitable.v1.app_table_record.search failed, code: {response.code}, msg: {response.msg}, log_id: {response.get_log_id()}"
# )
return
# 处理业务结果
lark.logger.info(lark.JSON.marshal(response.data))
if response.data.total == 1:
return response.data.items[0].record_id
if response.data.total == 2:
print("找到了多条记录")
if __name__ == "__main__":
app_id = os.environ["APP_ID"]
app_secret = os.environ["APP_SECRET"]
bitable_token = os.environ["BITABLE_TOKEN"]
table_id = os.environ["TABLE_ID"]
bitable_token = "XDmHbhbZcaY4gZsOvBJcLcHonKh"
table_id = "tbl2GcBvkvMtOBnl"
uuid = "4e478138-c8d6-426c-9761-3d1f4fd3cb05"
view_id = "vewIAewVD2"
# 创建client
client = (
lark.Client.builder()
.app_id(app_id)
.app_secret(app_secret)
.log_level(lark.LogLevel.DEBUG)
.build()
)
get_filtered_record_by_uuid(client, bitable_token, table_id, view_id, uuid)
+31 -1
View File
@@ -90,6 +90,36 @@ def get_activity_detail(token, package_name, package_version):
return response.json().get("data")
def get_office_app_uuids() -> list:
"""
获取官方应用的 uuid,
:return: list, 官方应用 uuid
"""
uuids = []
tmp_list = []
page = 1
url = "https://api.winrobot360.com/api/noauth/v1/group/official/queryOfficialRobotList?page=%s&size=200"
page_response = requests.request("GET", url % page)
page_info = page_response.json().get("page").get("pages")
for item in range(1, page_info + 1):
response = requests.request("GET", url % item)
for item in response.json().get("data"):
# if item.get("robotGradeName") == "简单应用":
# continue
if item.get("uiTags") == "极速":
continue
print(item.get("name"), item.get("robotGradeName"))
uuids.append(item.get("uuid"))
name = item.get("name")
uuid = item.get("uuid")
return uuids
if __name__ == "__main__":
dotenv.load_dotenv()
xbot_username = os.environ["XBOT_USERNAME"]
@@ -97,4 +127,4 @@ if __name__ == "__main__":
token = get_token(xbot_username, xbot_password)
office_activity_uuids: list = get_activity_info(token)
for item in office_activity_uuids:
print(item)
print(item.get("packageViewName"), item.get("uuid"))