我这用MySQL来储存积分信息。(建议用老版本的MySQL,新版本加密方式变了,pymysql连不上。即使改了加密方式,每次开机都要重启MySQL才行。)
至于mysql安装配置,百度一下,你就知道。
sql语句也是,百度一下,你就知道。
我是提前把表建好:
使用pymysql可以用python操作MySQL
卵杰的积分库时常抽风或挂掉,这时就要调用数据库中的积分。
import pymysql
try:
credit = requests.get('http://api.xiaojie666.com/xiaojie/credit/query.do?nickName=')
credit.encoding = 'utf-8'
CREDIT = credit.text
pattern = '"credit":(.*?),"'
CREDIT = re.findall(pattern, CREDIT)
CREDIT = int(CREDIT[0])
except:
con = pymysql.connect(host='localhost', port=3306, user='root', password='Sars@945945', charset='utf8')
cur = con.cursor()
cur.execute('use xiaojie')
sql_insert = 'select * from xiaojie order by time desc limit 1'
cur.execute(sql_insert)
data_one = cur.fetchone()
CREDIT = data_one[1]
而且数据库需要时时更新:
将网上获取积分与数据库对比,若不同,则添加最新积分数据。并添加新的线程。
def point():
con = pymysql.connect(host='localhost', port=3306, user='root', password='Sars@945945', charset='utf8')
cur = con.cursor()
cur.execute('use xiaojie')
sql_insert = 'select * from xiaojie order by time desc limit 1'
cur.execute(sql_insert)
data_one = cur.fetchone()
old_point = data_one[1]
while 1:
try:
point = requests.get('http://api.xiaojie666.com/xiaojie/credit/query.do?nickName=')
point.encoding = 'utf-8'
POINT = point.text
pattern = '"credit":(.*?),"'
Point = re.findall(pattern, POINT)
point = int(Point[0])
except:
print('服务器繁忙')
if point != old_point:
differential = point - old_point
sql_insert = 'insert into xiaojie(time,point,difference) values (%s,%s,%s);'
DATA = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
DATA = str(DATA)
POINT = point
DIFFERENCE = differential
cur.execute(sql_insert, [DATA, POINT, DIFFERENCE])
con.commit()
old_point = point
print(old_point)
time.sleep(60)
def main():
init()
t1 = Thread(target=get_dm)
t2 = Thread(target=keep_live)
t3 = Thread(target=sign)
t4 = Thread(target=point)
t1.start()
t2.start()
t3.start()
t4.strat()
if __name__ == '__main__':
main()
同样在nickName=后面添加自己斗鱼id
把上述代码加到原代码中就可以了。