bot-replication/code/models/getAppsInfor.py

79 lines
2.5 KiB
Python

import requests
import time
import pymysql
from util import config
from util import util
# 打开数据库连接
db = pymysql.connect(**config.db_config)
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
# 程序开始时间
start_time = time.time()
# 获取bots中的apps基本信息
def get_bots_inf():
sql_query = "SELECT id,bot_name FROM bots WHERE bot_type=1"
try:
# 执行SQL语句
cursor.execute(sql_query)
# 获取所有记录列表
results = cursor.fetchall()
return results
except Exception as e:
# 如果发生错误则回滚
db.rollback()
print("get_bots_inf error:")
print(e)
# 获取bots表中apps类bot的详细信息 item->(id,app_slug)
def save_apps_inf(item, token):
url = 'https://api.github.com/apps/{app_slug}'
url = url.format(app_slug=(item[1].replace('[bot]', '')))
headers = {'User-Agent': 'Mozilla/5.0',
'Authorization': 'token '+token,
'Content-Type': 'application/json',
'Accept': 'application/json'
}
try:
response = requests.get(url, headers=headers, timeout=(5, 5))
if response.status_code != 200:
print(response.json())
print(url)
print('get_apps_inf error: fail to request')
res = response.json()
value = (item[0], res['slug'], res['owner']['login'], res['owner']['id'], res['owner']['type'], res['name'],
res['description'], res['external_url'], util.str_to_datetime(res['created_at']),
util.str_to_datetime(res['updated_at']), str(res['permissions']), str(res['events']))
sql = """INSERT INTO apps_bot(bots_id,slug,owner_login,owner_id,owner_type,name,description,external_url,
created_at,updated_at,permissions,events) VALUES (%s, %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) """
try:
cursor.execute(sql,value)
db.commit()
except Exception as e:
db.rollback()
print("ERR0R!"'Insert apps bot error:')
print(e)
except Exception as e:
print("ERR0R! save_apps_inf error:")
print(e)
def run():
apps_list = get_bots_inf()
for it in apps_list:
token = config.token_list[util.chose_token(start_time)]
save_apps_inf(it, token)
if __name__ == '__main__':
#run()
token = config.token_list[util.chose_token(start_time)]
save_apps_inf((59, 'pull'), token)
print('hello world')
# 关闭数据库连接
db.close()