-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebsiteThread.py
192 lines (162 loc) · 6.92 KB
/
WebsiteThread.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
190
191
192
import threading
from Driver import *
from random import randint, choice
import time
import os
import json
MIN_WAIT = 3
MAX_WAIT = 5
class WebsiteThread(threading.Thread):
def __init__(self, agent, user, only_dump, debug = False):
threading.Thread.__init__(self)
self.agent = agent
self.user = user
self.only_dump = only_dump
self.debug = debug
self.driver = init_driver(self.user["user"], self.agent.short, True if self.user["gmail_login"] else self.debug)
self.in_prod_page = False
self.collection = {}
def run(self):
self.display("RUN")
self.login()
if self.only_dump:
self.visit_collection()
else:
t_end = time.time() + 60*30
now = time.time()
while now < t_end:
self.find_that_flamedeer()
self.browse_website()
now = time.time()
self.driver.close()
def visit_collection(self):
self.driver.get(self.agent.collection)
try:
prizes = self.driver.find_elements_by_class_name('mc-background--primary')
all_cards = {}
for prize in prizes:
p = prize.find_elements_by_tag_name('p')
cards_dict = {}
if len(p) > 0:
#collectable cards
prize_name = p[1].text
cards = prize.find_elements_by_class_name('mc-card')
for card in cards:
card_name = card.find_element_by_class_name('mc-festiveFont').text
try:
counter = card.find_element_by_class_name('mc-card-counter-child').text
except Exception as e:
counter = 0
cards_dict[card_name] = counter
all_cards[prize_name] = cards_dict
else:
#immediat cards
card = prize.find_element_by_tag_name('img')
card_img_url = card.get_attribute('src')
card_type = card_img_url[card_img_url.rfind('level')+5:card_img_url.rfind('level')+7].strip('_')
card_name = card.get_attribute('alt')
cards_dict[card_name] = 0 if int(card_type) == 0 else 1
all_cards[card_name] = cards_dict
all_cards["time"] = time.time()
self.collection = all_cards
self.dump_collection()
except Exception as e:
print(e)
def dump_collection(self):
folder = self.agent.short
if not os.path.exists(folder):
os.makedirs(folder)
fname = "{short}/{uname}.json".format(short = self.agent.short, uname = self.user["user"])
with open(fname, 'w') as fp:
json.dump(self.collection, fp)
def display(self, other = ""):
print("{time}:{user}:{site}:{other}".format(time=time.time(), user=self.user["user"], site=self.agent.short, other=other))
def login(self):
self.display("LOGIN")
self.driver.get(self.agent.login)
to_login = False
try:
conn_btn = WebDriverWait(self.driver, randint(MIN_WAIT, MAX_WAIT)).until(
EC.presence_of_element_located((By.XPATH, '/html/body/main/div[2]/div/div/div[2]/div[3]/div[2]/div/div/ul/li/div/button')))
to_login = conn_btn.is_displayed()
except Exception as e:
pass
if(to_login):
if self.user["gmail_login"]:
try:
WebDriverWait(self.driver, 120).until(
EC.presence_of_element_located((By.XPATH, '//input[@type="email"]'))).send_keys(self.user["email"])
WebDriverWait(self.driver, 120).until(
EC.presence_of_element_located((By.XPATH, '//div[@id="identifierNext"]'))).click()
WebDriverWait(self.driver, 120).until(
EC.presence_of_element_located((By.XPATH, '//input[@type="password"]'))).send_keys(self.user["pw"])
WebDriverWait(self.driver, 120).until(
EC.presence_of_element_located((By.XPATH, '//div[@id="passwordNext"]'))).click()
except Exception as e:
pass
self.driver.get(self.agent.url)
else:
try:
user_field = self.driver.find_element(By.XPATH, '//*[@id="login_form-identity"]')
user_field.send_keys(self.user["user"])
pw_field = self.driver.find_element(By.XPATH, '//*[@id="login_form-password"]')
pw_field.send_keys(self.user["pw"])
conn_btn.click()
self.driver.get(self.agent.url)
except Exception as e:
print(e)
def browse_website(self):
something_to_do = [
self.scroll,
self.nav_page,
self.rand_deal]
t_end = time.time() + randint(1, 5)
while time.time() < t_end:
fcn = choice(something_to_do)
fcn()
self.display("BROWSE:{}".format(fcn.__name__))
self.refresh()
time.sleep(randint(1,2))
def refresh(self):
self.driver.get(self.agent.url)
def rand_deal(self):
if self.in_prod_page:
self.driver.get(self.agent.url)
self.in_prod_page = False
else:
try:
deal_btn = self.driver.find_elements_by_class_name("thread-title--list")
choice(deal_btn).click()
self.in_prod_page = True
except Exception as e:
pass
print(e)
def nav_page(self):
next_prev_btns = [
'//*[@id="pagination"]/nav/div/span[6]/a',
'//*[@id="pagination"]/nav/div/span[3]/a'
]
try:
page_btn = self.driver.find_element(By.XPATH, choice(next_prev_btns))
page_btn.click()
except Exception as e:
print(e)
def scroll(self):
self.driver.execute_script("$('html,body').animate({{ scrollTop: {} }}, 'slow');".format(randint(0, 900)))
def find_that_flamedeer(self):
try:
popup, link = None, None
_min, _max = 5*60, 15*60
popup = WebDriverWait(self.driver, randint(_min, _max)).until(
EC.presence_of_element_located((By.CLASS_NAME, "mc-notification-character--peek"))
)
link = WebDriverWait(self.driver, 5).until(
EC.presence_of_element_located((By.CLASS_NAME, "mc-btn--primary"))
)
link.click()
self.display("FOUND")
self.visit_collection()
time.sleep(2)
self.driver.get(self.agent.url)
except Exception as e:
self.display("NOTFOUND")