-
Notifications
You must be signed in to change notification settings - Fork 0
/
botFunc.py
189 lines (156 loc) · 7.32 KB
/
botFunc.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# import json
# import grequests
# import requests
# import _thread
# import time
from tkinter import ttk
class Bot:
threads = 0
numberOfRequests = 0
numberOfRequests *= 1
requestCount = 0
requestDoneCount = 0
voteIdx = 0
voteText = []
parsedId = ""
parsedKey = ""
done = False
json = __import__('json')
grequests = __import__('grequests')
requests = __import__('requests')
_thread = __import__('_thread')
time = __import__('time')
mttkinter = __import__('mttkinter')
tk = mttkinter.mtTkinter
tkinter = __import__('tkinter', globals(), locals(), ['ttk'], 0)
ttk = tkinter.ttk
threading = __import__('threading')
math = __import__('math')
def updateProgress(self):
self.percentage = round(100*(self.requestDoneCount/self.numberOfRequests), 1)
print(self.percentage)
oldLabel = self.logLabel.cget("text")
if oldLabel != str(self.percentage) + '%':
self.logLabel.configure(text=(str(self.percentage) + '%'))
barStr ='__________'
barStrList = list(barStr)
for idx in range(self.math.floor(self.percentage/10)):
barStrList[idx] = '#'
barStr = ''.join(barStrList)
self.logText.configure(text=barStr)
if self.percentage == 100.0:
self.stop()
def printw(self, string):
# self.addText(string)
# self.root.after(5000, lambda: self.addText(string))
pass
def addText(self, string):
old = self.logText.cget("text")
new = old+"\n"+string
self.logText.configure(text=new)
def __init__(self, threadToCall, parsedId, voteText, threads, numberOfRequests, perWord):
self.threadToCall = threadToCall
self.done = False
self.parsedId = parsedId
self.voteText = voteText
self.threads = threads
self.numberOfRequests = numberOfRequests
if perWord:
self.numberOfRequests *= len(self.voteText)
self.root = self.tk.Tk()
self.root.bind("<Destroy>", self.destroy)
self.logLabel = self.tk.LabelFrame(self.root, text="0%")
self.logText=self.tk.Label(self.logLabel, text='__________', height=1, width=10, anchor='nw', justify='left')
self.logLabel.pack(padx=10, pady=10, side='top')
self.logText.pack()
self.stopBtn = self.ttk.Button(self.root, text='Stop', command=(lambda: self.stop()))
self.stopBtn.pack(padx=10, pady=10, side='bottom')
self.startThreads()
self.tk.mainloop()
def destroy(self, event):
self.stop()
def stop(self):
self.done = True
self.stopBtn.update()
# self.time.sleep(2)
self.root.destroy()
self.callThread()
# self.exit()
def callThread(self):
self.threadToCall.start()
# def defNew(voteTextIn, threadsIn, numberOfRequestsIn, numberOfRequestsMultiIn, requestCountIn, requestDoneCountIn):
# reset()
# self.threads = threadsIn
# self.numberOfRequests = numberOfRequestsIn
# self.numberOfRequests *= numberOfRequestsMultiIn
# self.requestCount = requestCountIn
# self.requestDoneCount = requestDoneCountIn
# self.voteText = voteTextIn
# def reset():
# self.threads = 0
# self.numberOfRequests = 0
# self.requestCount = 0
# self.requestDoneCount = 0
# self.voteIdx = 0
def vote(self, voteIn):
# voteIn is the text to be voted with
newdata = {"question_type":"wordcloud","vote":voteIn}
# s = grequests.post('https://www.govote.at/core/identifier')
#print(s)
# identifier = s.json()['identifier']
# It seems the page rejects GET requests that do not identify a User-Agent.
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
content = ""
# urls = ['https://www.menti.com/core/identifier'] # old
urls = ['https://www.menti.com/core/identifiers'] # new
rs = (self.grequests.post(u, headers=headers) for u in urls)
requests = self.grequests.map(rs)
for response in requests:
content = response.content
identifier = self.json.loads(content)['identifier']
self.printw(identifier)
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36', 'accept': 'application/json','accept-encoding': 'gzip, deflate, br','accept-language': 'en-US,en;q=0.9','content-lengt': '42','content-type': 'application/json; charset=UTF-8','cookie': '_ga=GA1.2.1555705289.1573573495; _fbp=fb.1.1573573435740.1944003412; identifier1='+identifier+'; _gid=GA1.2.1316845992.1575545568; _gat=1; _gat_UA-23693781-9=1; _gat_UA-23693781-3=1','origin': 'https://www.menti.com','referer': 'https://www.menti.com/'+self.parsedId,'sec-fetch-mode': 'cors','sec-fetch-site': 'same-origin','user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36 OPR/65.0.3467.48','x-identifier': identifier}
# print(headers)
content = ""
# urls2 = ['https://www.govote.at/core/votes/'+self.parsedKey] # old
urls2 = ['https://www.menti.com/core/votes/'+self.parsedKey] # new
rs = (self.grequests.post(u, json = newdata, headers = headers) for u in urls2)
requests = self.grequests.map(rs)
for response in requests:
content = response
print(content.content)
self.printw(content)
# vote(voteText)
def threadFun(self, threadName):
# time.sleep(delay)
# global requestCount
# global voteIdx
# global voteText
# global requestDoneCount
while self.requestCount < self.numberOfRequests and self.done is False:
self.requestCount += 1
if self.voteIdx == len(self.voteText):
self.voteIdx = 0
text = self.voteText[self.voteIdx]
self.voteIdx += 1
# vote vvv
self.printw(str(threadName) + " Requests " + str(self.requestCount) + " with " + str(text))
self.vote(text)
self.requestDoneCount += 1
self.updateProgress()
def startThreads(self):
# It seems the page rejects GET requests that do not identify a User-Agent.
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
# s = self.requests.get('https://www.menti.com/core/objects/vote_keys/'+self.parsedId, headers=headers) # old
s = self.requests.get('https://www.menti.com/core/vote-keys/'+self.parsedId+'/series', headers=headers) # new
string = s.text
jsondata = self.json.loads(string)
question = jsondata['questions'][0]
parsedKey = question['public_key']
self.parsedKey = parsedKey
self.printw("Key: " + str(parsedKey))
for idx in range(self.threads):
# self._thread.start_new_thread( self.threadFun, ("Thread-" + str(idx), ) )
t = self.threading.Thread(target=(lambda: self.threadFun("Thread-" + str(idx), )))
t.start()
self.time.sleep(0.2)