bot-replication/code/models/updateIssuesClosedat.py

93 lines
2.8 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import requests
import time
import pymysql
from util import config
from util import util
# 更新仓部分库closed_at
# 打开数据库连接
db = pymysql.connect(**config.db_config)
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
# 程序开始时间
start_time = time.time()
requests.adapters.DEFAULT_RETRIES = 5
# 功能更新仓部分库closed_at
def update_issues_closed_at():
# SQL查询语句 查询在第一二次bot检测中存在bot的所有仓库
sql_query = "SELECT a.id,b.name,b.owner,a.number from issuesbot as a,repos as b " \
"where a.repos_id=b.repos_id and a.id < 64041 AND a.state='closed' AND a.closed_at is NULL"
try:
# 执行SQL语句
cursor.execute(sql_query)
# 获取所有记录列表
results = cursor.fetchall()
print('length:')
print(len(results))
for i in range(0, len(results)):
# 拼接出 owner/name
owner_name = results[i][2] + '/' + results[i][1]
# 为避免api访问受限根据开始与当前时间利用多个token进行轮换使用
token = config.token_list[util.chose_token(start_time)]
# 检测仓库的issues包括pulls)下的评论者
get_issue_detail(owner_name, results[i][3], token, results[i][0])
except Exception as e:
# 如果发生错误则回滚
db.rollback()
print("ERR0R2:")
print(e)
# 功能获取制定issue的关闭时间
def get_issue_detail(owner_name, number, token, issue_id):
url = 'https://api.github.com/repos/{owner_name}/issues/{number}'
url = url.format(owner_name=owner_name, number=number)
headers = {'User-Agent': 'Mozilla/5.0',
'Authorization': 'token ' + token,
'Content-Type': 'application/json',
'Accept': 'application/json'
}
try:
sess = requests.Session()
sess.keep_alive = False
response = requests.get(url, headers=headers, timeout=(5, 5))
if response.status_code != 200:
print(response.json())
print(url)
print('get_issue_detail error: fail to request')
response = response.json()
# SQL更新仓库的检测情况
sql_update = "UPDATE issuesbot SET closed_at = '%s' WHERE id = '%d' " \
% (util.str_to_datetime(response['closed_at']), issue_id)
try:
cursor.execute(sql_update)
db.commit()
except Exception as e:
db.rollback()
print("ERR0R1:")
print(e)
except Exception as e:
print("ERR0R!get_issue_detail error:")
print(e)
def run():
update_issues_closed_at()
if __name__ == '__main__':
run()
# 关闭数据库连接
db.close()