forked from Coloryr/BotBiliBili
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckThread.cs
176 lines (170 loc) · 5.51 KB
/
CheckThread.cs
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using BotBiliBili.Config;
using BotBiliBili.PicGen;
using BotBiliBili.Utils;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Threading;
namespace BotBiliBili
{
class CheckThread
{
private static Thread thread;
private static bool IsRun;
private static bool save;
public static void Start()
{
thread = new(Task);
IsRun = true;
thread.Start();
}
private static void GetDynamic(KeyValuePair<string, List<long>> item)
{
var item1 = item.Key;
Program.Log($"检查用户{item1}动态");
var obj1 = HttpUtils.GetDynamicUid(item1);
Thread.Sleep(ConfigUtils.Config.CheckDelay);
if (!IsRun)
return;
if (obj1 == null)
return;
var obj2 = obj1["data"]["cards"] as JArray;
if (obj2.Count == 0)
return;
string first = obj2[0]["desc"]["dynamic_id"].ToString();
long time = (long)obj2[0]["desc"]["timestamp"];
if (!ConfigUtils.UidLast.Dynamic.ContainsKey(item1))
{
var data1 = HttpUtils.GetDynamic(first);
if (data1 == null)
{
return;
}
DynamicObj obj = new()
{
ID = first,
Time = time
};
ConfigUtils.UidLast.Dynamic.Add(item1, obj);
save = true;
string temp1 = DynamicPicGen.Gen(data1);
Program.Log($"已生成{temp1}");
foreach (var item3 in item.Value)
{
Program.SendGroupImage(temp1, item3);
}
}
else
{
foreach (var item2 in obj2)
{
var obj3 = item2["desc"]["dynamic_id"].ToString();
var time1 = (long)item2["desc"]["timestamp"];
if (ConfigUtils.UidLast.Dynamic[item1].ID == obj3)
break;
if (ConfigUtils.UidLast.Dynamic[item1].Time > time1)
break;
var data1 = HttpUtils.GetDynamic(obj3);
if (data1 == null)
{
continue;
}
string temp1 = DynamicPicGen.Gen(data1);
Program.Log($"已生成{temp1}");
foreach (var item3 in item.Value)
{
Program.SendGroupImage(temp1, item3);
}
}
ConfigUtils.UidLast.Dynamic[item1] = new()
{
Time = time,
ID = first
};
save = true;
}
}
private static void GetLive(KeyValuePair<string, List<long>> item)
{
var item1 = item.Key;
Program.Log($"检查用户{item1}直播");
var obj1 = HttpUtils.GetLiveUID(item1);
Thread.Sleep(ConfigUtils.Config.CheckDelay);
if (!IsRun)
return;
if (obj1 == null)
return;
var obj2 = (int)obj1["data"]["roomStatus"];
if (obj2 == 0)
return;
bool obj3 = (int)obj1["data"]["liveStatus"] == 1;
if (ConfigUtils.UidLast.Live.ContainsKey(item1))
{
if (ConfigUtils.UidLast.Live[item1] == obj3)
return;
else
ConfigUtils.UidLast.Live[item1] = obj3;
save = true;
}
else
{
ConfigUtils.UidLast.Live.Add(item1, obj3);
save = true;
}
if (!obj3)
return;
var data1 = HttpUtils.GetLive(obj1["data"]["roomid"].ToString());
if (data1 == null)
{
return;
}
string temp1 = LivePicGen.Gen(data1);
Program.Log($"已生成{temp1}");
foreach (var item2 in item.Value)
{
Program.SendGroupImage(temp1, item2);
}
}
private static void Task()
{
while (IsRun)
{
try
{
save = false;
foreach (var item in ConfigUtils.Subscribes.Uids)
{
if (!IsRun)
return;
GetDynamic(item);
}
foreach (var item in ConfigUtils.Subscribes.Lives)
{
if (!IsRun)
return;
GetLive(item);
}
if (save)
{
ConfigUtils.SaveTemp();
}
Program.Log("检查暂停");
for (int a = 0; a < ConfigUtils.Config.WaitTime; a++)
{
Thread.Sleep(1000);
if (!IsRun)
return;
}
}
catch (Exception e)
{
Program.Error(e);
}
}
}
public static void Stop()
{
IsRun = false;
}
}
}