更新用户data key 脚本
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
# -*- coding: utf8 -*-
|
||||
import base64
|
||||
import hashlib
|
||||
import hmac
|
||||
import time
|
||||
import requests
|
||||
import mysql.connector
|
||||
from mysql.connector import Error
|
||||
|
||||
# 腾讯云API配置
|
||||
secret_id = "AKIDNdqxra6OXZOylQr42nQq20lJdnjRvIdk"
|
||||
secret_key = "vc4KSM2VklZ9ZljTyGqFQO9h1OxKTO5X"
|
||||
|
||||
# 数据库配置
|
||||
db_config = {
|
||||
'host': '101.227.74.8',
|
||||
'port': 3306,
|
||||
'user': 'root',
|
||||
'password': '9lYgSVPPDjh6sq2C',
|
||||
'database': 'ss2_migrated',
|
||||
'charset': 'utf8mb4',
|
||||
'collation': 'utf8mb4_unicode_ci'
|
||||
}
|
||||
|
||||
|
||||
def get_string_to_sign(method, endpoint, params):
|
||||
s = method + endpoint + "/?"
|
||||
query_str = "&".join("%s=%s" % (k, params[k]) for k in sorted(params))
|
||||
return s + query_str
|
||||
|
||||
|
||||
def sign_str(key, s, method):
|
||||
hmac_str = hmac.new(key.encode("utf8"), s.encode("utf8"), method).digest()
|
||||
return base64.b64encode(hmac_str)
|
||||
|
||||
|
||||
def call_api_with_device_name(device_name):
|
||||
"""调用API并返回响应"""
|
||||
endpoint = "mna.tencentcloudapi.com"
|
||||
data = {
|
||||
'Action': 'AddDevice',
|
||||
'DeviceName': device_name,
|
||||
'LicensePayMode': 0,
|
||||
'FlowTrunc': 0,
|
||||
'Nonce': 11886,
|
||||
'SecretId': secret_id,
|
||||
'Timestamp': int(time.time()),
|
||||
'Version': '2021-01-19'
|
||||
}
|
||||
|
||||
s = get_string_to_sign("GET", endpoint, data)
|
||||
data["Signature"] = sign_str(secret_key, s, hashlib.sha1)
|
||||
|
||||
try:
|
||||
resp = requests.get("https://" + endpoint, params=data)
|
||||
return resp.json()
|
||||
except Exception as e:
|
||||
print(f"API调用失败: {e}")
|
||||
return None
|
||||
|
||||
|
||||
def connect_database():
|
||||
"""连接数据库"""
|
||||
try:
|
||||
connection = mysql.connector.connect(**db_config)
|
||||
if connection.is_connected():
|
||||
print("数据库连接成功")
|
||||
return connection
|
||||
return None
|
||||
except Error as e:
|
||||
print(f"数据库连接失败: {e}")
|
||||
return None
|
||||
|
||||
|
||||
def get_all_users(connection):
|
||||
"""获取所有用户数据"""
|
||||
try:
|
||||
cursor = connection.cursor(dictionary=True)
|
||||
query = "SELECT id, username, datakey, datakey_status FROM users where datakey_status=0"
|
||||
cursor.execute(query)
|
||||
users = cursor.fetchall()
|
||||
cursor.close()
|
||||
return users
|
||||
except Error as e:
|
||||
print(f"查询用户数据失败: {e}")
|
||||
return []
|
||||
|
||||
|
||||
def update_user_datakey(connection, user_id, datakey):
|
||||
"""更新用户的datakey和datakey_status"""
|
||||
try:
|
||||
cursor = connection.cursor()
|
||||
query = "UPDATE users SET datakey = %s, datakey_status = 1 WHERE id = %s"
|
||||
cursor.execute(query, (datakey, user_id))
|
||||
connection.commit()
|
||||
cursor.close()
|
||||
print(f"用户ID {user_id} 的datakey更新成功")
|
||||
return True
|
||||
except Error as e:
|
||||
print(f"更新用户 {user_id} 的datakey失败: {e}")
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
# 输出当前时间
|
||||
print(f"开始处理时间: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())}")
|
||||
"""主函数"""
|
||||
# 连接数据库
|
||||
connection = connect_database()
|
||||
if not connection:
|
||||
return
|
||||
|
||||
try:
|
||||
# 获取所有用户
|
||||
users = get_all_users(connection)
|
||||
print(f"找到 {len(users)} 个用户")
|
||||
|
||||
for user in users:
|
||||
user_id = user['id']
|
||||
device_name = f"newdev_{user_id}"
|
||||
|
||||
print(f"处理用户ID: {user_id}, 设备名: {device_name}")
|
||||
|
||||
# 调用API
|
||||
response = call_api_with_device_name(device_name)
|
||||
|
||||
if response and 'Response' in response:
|
||||
data_key = response['Response'].get('DataKey')
|
||||
if data_key:
|
||||
# 更新数据库
|
||||
success = update_user_datakey(connection, user_id, data_key)
|
||||
if success:
|
||||
print(f"用户 {user_id} 处理完成,DataKey: {data_key}")
|
||||
else:
|
||||
print(f"用户 {user_id} 数据库更新失败")
|
||||
else:
|
||||
print(f"用户 {user_id} API响应中没有DataKey")
|
||||
else:
|
||||
print(f"用户 {user_id} API调用失败或响应格式错误")
|
||||
|
||||
# 添加延迟避免API限制
|
||||
time.sleep(1)
|
||||
|
||||
except Exception as e:
|
||||
print(f"处理过程中发生错误: {e}")
|
||||
|
||||
finally:
|
||||
if connection.is_connected():
|
||||
connection.close()
|
||||
print("数据库连接已关闭")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user