forked from tuxzz/bilidraw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbili.py
155 lines (142 loc) · 6.32 KB
/
bili.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import requests, json, time, os, asyncio, threading
import numpy as np
import cv2
from common import *
writePos = (0,0)
# Waiting to be written: pull remote png from cloud.
img = cv2.imread("aria_q.png")
mask = cv2.imread("aria_qmask.png")
mask = (~mask.transpose(2, 0, 1)[0]).astype(np.bool)
#mask = ~np.zeros(img.shape[:2], dtype = np.bool)
imgData = imgToData(img)
globalCanvas = np.zeros((*canvasShape, 3), dtype = np.uint8)
globalDiffCache = []
globalCanvasLocker = threading.Lock()
# load cookie and delete cookies that in blacklist
oCookiePathList = os.listdir("./accounts")
cookiePathList = []
cookieList = []
blacklistedCookieList = []
for cookies in oCookiePathList:
if(os.path.splitext(cookies)[-1] != ".cookie"):
continue
cookiePathList.append(cookies)
cookieList.append(loadCookie(os.path.join("./accounts", cookies)))
del cookies
del oCookiePathList
if(os.path.isfile("black.json")):
blacklistedCookieList = json.load(open("black.json", "r"))
for x in blacklistedCookieList:
if(x in cookiePathList):
print("%s is blacklisted." % (x))
i = cookiePathList.index(x)
del cookieList[i], cookiePathList[i], i
else:
print("%s is not in cookieList and blacklisted." % (x))
del x
if(len(cookieList) == 0):
raise ValueError("No account available.")
cookieLock = threading.Lock()
print("********** Loaded %d cookie **********" % len(cookieList))
def placeIntoBlackList(cookieName):
with cookieLock:
assert not cookieName in blacklistedCookieList
if(cookieName in cookiePathList):
print("%s is blacklisted." % (cookieName))
i = cookiePathList.index(cookieName)
del cookieList[i], cookiePathList[i], i
else:
print("%s is not in cookieList and blacklisted." % (cookieName))
blacklistedCookieList.append(cookieName)
with open("black.json", "w") as f:
f.write(json.dumps(blacklistedCookieList))
async def procressAccount(cookies, cookieName):
timeRemain = await getTimeRemain(cookies, cookieName)
if(timeRemain == "BLACKLIST"):
placeIntoBlackList(cookieName)
return
latency = 0.0
forceSleep = False
while(True):
try:
if(forceSleep):
await asyncio.sleep(120)
forceSleep = False
timeRemain = max(0.0, timeRemain - latency)
if(timeRemain > 0.0):
print("%s: Time remain: %lfs(Without latency %lfs)" % (cookieName, timeRemain, latency))
await asyncio.sleep(timeRemain)
t1 = time.time()
print("%s: Woken up" % (cookieName))
with globalCanvasLocker:
if(len(globalDiffCache) == 0):
print("%s: Nothing to do" % (cookieName))
forceSleep = True
continue
((y, x), color), _ = selectPixFromCache(globalDiffCache)
nUnpainted = len(globalDiffCache)
print("%s: Selected pixel x = %d, y = %d, color = %s, Unpainted %d" % (cookieName, x, y, color, nUnpainted))
dur = time.time() - t1
latency = latency * 0.9 + dur * 0.1
drawResult = await drawPix(x, y, color, cookies, cookieName)
if(drawResult == "BLACKLIST"):
placeIntoBlackList(cookieName)
return
elif(drawResult != "SKIP"):
print("%s: Draw operation success." % (cookieName))
timeRemain = await getTimeRemain(cookies, cookieName)
if(timeRemain == "BLACKLIST"):
placeIntoBlackList(cookieName)
return
except Exception as e:
print("%s: Exception occurred => %s" % (cookieName, str(e)))
async def main():
loop = asyncio.get_event_loop()
print("Main: Load canvas data from remote")
canvas = await loadCanvas()
print("Main: Convert canvas data")
canvas = dataToImg(canvas, canvasShape)
print("Main: Update local canvas data and pixel cache")
with globalCanvasLocker:
globalCanvas[:] = canvas
globalDiffCache[:] = genDiffCache(globalCanvas, img, mask, writePos)
print("Main: Canvas updated")
del canvas
futureList = []
for i, cookieName in enumerate(cookiePathList):
futureList.append(asyncio.ensure_future(procressAccount(cookieList[i], cookieName)))
print("Main: everything created.")
while(True):
newCookiePathList = os.listdir("./accounts")
try:
print("Main: Load canvas data from remote")
canvas = await loadCanvas()
print("Main: Convert canvas data")
canvas = dataToImg(canvas, canvasShape)
print("Main: Update local canvas data and pixel cache")
with globalCanvasLocker:
globalCanvas[:] = canvas
globalDiffCache[:] = genDiffCache(globalCanvas, img, mask, writePos)
print("Main: Canvas updated")
del canvas
with cookieLock:
if(os.path.isfile("black.json")):
blacklistedCookieList[:] = json.load(open("black.json", "r"))
for cookiePath in newCookiePathList:
if(os.path.splitext(cookiePath)[-1] != ".cookie"):
continue
if((not cookiePath in cookiePathList) and (not cookiePath in blacklistedCookieList)):
cookies = loadCookie(os.path.join("./accounts", cookiePath))
cookieList.append(cookies)
cookiePathList.append(cookiePath)
futureList.append(asyncio.ensure_future(procressAccount(cookies, cookiePath)))
print("Main: Loaded new cookie %s" % (cookiePath))
elif(cookiePath in blacklistedCookieList):
print("Main: Rejected new cookie %s" % (cookiePath))
del cookiePath
except Exception as e:
print("Main: Exception when loading cookie => %s" % (str(e)))
await asyncio.sleep(10)
print("Main: About to exit.")
loop = asyncio.get_event_loop()
loop.run_until_complete(main())