-
Notifications
You must be signed in to change notification settings - Fork 2
/
bfanscount_json.py
135 lines (112 loc) · 5.94 KB
/
bfanscount_json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# coding: utf-8
import requests
from datetime import datetime
import time
import os.path
from interval import Interval
import json
# jump = int(input('时间间隔:') or 43200)
jump = int(42200)
# uid = input('uid:') or 2100679
wait = 5
my_path = os.path.abspath(os.path.dirname(__file__))
path = os.path.join(my_path, "")
i = 0
def timer(n):
global json, data
while True:
while True:
# 当前时间
now_localtime = time.strftime("%H:%M:%S", time.localtime())
# 当前时间(以时间区间的方式表示)
now_time = Interval(now_localtime, now_localtime)
time_interval_1 = Interval("06:00:00", "06:01:00")
time_interval_2 = Interval("18:00:00", "18:01:00")
if now_time in time_interval_1 or now_time in time_interval_2:
print("新一轮记录开始")
break
else:
print("等待下一轮记录")
time.sleep(40)
with open(path + '.config', "r") as f:
uids = json.loads(f.read()) # 每轮都刷新表
if not os.path.exists(path + "/json/"):
os.makedirs(path + "/json/")
if not os.path.exists(path + "/up_info/"):
os.makedirs(path + "/up_info/")
for index in range(len(uids)):
if not os.path.exists(path + "/json/" + str(uids[index]) + '.json'):
with open(path + "/json/" + str(uids[index]) + '.json', 'w'):
print("创建了" + str(uids[index]) + '.json')
web = 'https://api.bilibili.com/x/web-interface/card?mid=' + str(uids[index])
retry_time = 0
while True:
ifnext = 0
try:
data = requests.get(web)
break
except:
retry_time = retry_time + 1
if retry_time >= 3:
print(str(uids[index]) + ":放弃重试")
with open(path + "/json/" + str(uids[index]) + '.json', "r") as f:
if os.path.getsize(path + "/json/" + str(uids[index]) + '.json'):
content = json.loads(f.read())
timenow = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
json_content = {'fans': content[-1]['fans'], 'time': timenow}
if not content[:-1] == []:
rate1 = int(content[-1]['fans']) - int(content[-2]['fans'])
json_content = {'fans': content[-1]['fans'], 'time': timenow, 'rate1': rate1}
if not content[:-13] == []:
rate7 = int(content[-1]['fans']) - int(content[-14]['fans'])
json_content = {'fans': content[-1]['fans'], 'time': timenow, 'rate1': rate1, 'rate7': rate7}
content.append(json_content)
else:
content = [{'fans': 0, 'time': datetime.now().strftime("%Y-%m-%d %H:%M:%S")}]
with open(path + "/json/" + str(uids[index]) + '.json', "w") as f:
json.dump(content, f)
ifnext = 1
break
print(str(uids[index]) + ":将进行第" + str(retry_time) + "次重试……")
time.sleep(2)
if ifnext == 1:
continue
info = json.loads(data.text)
# print(info)
if info['code'] == 0:
fanss = info['data']['card']['fans']
names = info['data']['card']['name']
if names != '账号已注销': # 账号注销则不再更新info
with open(path + "/up_info/" + str(uids[index]) + '.json', 'w', encoding='utf-8') as f:
if not os.path.exists(path + "/json/" + str(uids[index]) + '.json'):
f.truncate() # 清空文件
json.dump(info, f, ensure_ascii=False)
else:
fanss = -1
names = "Null"
timenow = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
#timenow = int(time.time())
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
print(names + " 的粉丝数量为:" + str(fanss))
# str = '|-'+'\n'+'|'+datetime.now().strftime("%Y-%m-%d %H:%M:%S")+' ' + "||"+str(fanss2[index])+' ||
# '+str(fanss2[index]-fanss[index])+'\n' str2 = bytes(str2, encoding = "utf8")
json_content = {'fans': fanss, 'time': timenow}
# ff = open(path + "/json/" + str(uids[index]) + " " + str(names[index]) + '.json', mode='a')
# ff.write(str2)
# ff.close()
with open(path + "/json/" + str(uids[index]) + '.json', "r") as f:
if os.path.getsize(path + "/json/" + str(uids[index]) + '.json'):
content = json.loads(f.read())
if not content[:-1] == []:
rate1 = int(fanss) - int(content[-2]['fans'])
json_content = {'fans': fanss, 'time': timenow, 'rate1': rate1}
if not content[:-13] == []:
rate7 = int(fanss) - int(content[-14]['fans'])
json_content = {'fans': fanss, 'time': timenow, 'rate1': rate1, 'rate7': rate7}
content.append(json_content)
else:
content = [json_content]
with open(path + "/json/" + str(uids[index]) + '.json', "w") as f:
json.dump(content, f)
time.sleep(wait)
timer(jump)