-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmini_T.py
More file actions
318 lines (225 loc) · 10.5 KB
/
Copy pathmini_T.py
File metadata and controls
318 lines (225 loc) · 10.5 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common.keys import Keys
from time import sleep,time,ctime
from random import randint
from fake_useragent import UserAgent
ua = UserAgent()
from pyvirtualdisplay import Display
import random
import os
import subprocess
import re
import requests
class YouViewer:
def __init__(self):
#log file title everytime script starts
self.log_info(f'.......event started.....local TIME : {ctime()}')
# CONFIGURABLE variables
self.MAX_WAIT = 30
self.MAX_ERROR = 200
self.NUM_OF_VIEWS = 300 #increase this
self.WATCH_TIME_LIMIT = 300
self.BROWSER = 'chrome'
self.PROXY = False
self.PROXY_LIST = []
self.CONSUMED_PROXIES = 200
self.PROXY_TIMEOUT = 3
####replace these with your channel information####
#the title of targetted video
self.VIDEO_TITLE = 'Joker song 2019'
#give your channel name here
self.SEARCH_BY = "Muhammad fahim joker"
#first two to three words of your channel description used for finding your channel from search list
self.CHANNEL_DESCRIPTION='hi.'
#stopwatch
self.time_counter = time()
if self.PROXY:
self.get_proxy()
####################################
def begin_loop(self):
self.NUM_OF_ERRORS = 0
for i in range(self.NUM_OF_VIEWS):
try:
self.starting_preparation()
self.main_func()
self.end_operation()
except Exception as e:
try:
self.end_operation()
except:
pass
self.NUM_OF_ERRORS += 1
print(str(e))
self.log_info(str(e))
print(f'Time passed :{self.count_time()}')
if self.NUM_OF_ERRORS > self.MAX_ERROR:
self.log_info('maximum error limit exceded')
raise SystemExit
############## __init__ method ends here ###############
def main_func(self):
if True:
roaming = self.roam_randomly_to(what_to_search = self.SEARCH_BY)
#customize this sector for best results
if roaming:
element2 = self.view_xpath(f'//div[text()="{self.CHANNEL_DESCRIPTION}"]/preceding-sibling::h3/a')
element2.click()
sleep(1)
element3= self.view_xpath(f'//*[text()="{self.VIDEO_TITLE}"]')
element3.click()
sleep(1)
print('sleeping for watchtime')
element6 = self.view_xpath('//div[@class="watch-view-count"]')
views= element6.text
print('successfully loaded page with '+ views)
self.log_info(f'SUCCESS-> TIME:{self.count_time()} counting: '+ views)
sleep(randint(70,self.WATCH_TIME()))
else:
print('View did not complete')
self.log_info(f'ERROR-> TIME:{self.count_time()}')
###############loging info##########
print(f'time passed : {self.count_time()}')
if roaming:
self.roam_randomly_to()
#################### methods start here ################################
#this method is used in starting_preparation
def get_proxy(self):
try:
self.driver = webdriver.Chrome()
self.driver.get('https://www.proxy-list.download/HTTPS')
elem = self.driver.find_element_by_xpath('//*[@id="txta1"]')
unfiltered = elem.get_attribute('value')
filtered = re.findall(r'(\d+\.\d+\.\d+\.\d+:\d+)\n', unfiltered)
self.PROXY_LIST = filtered
self.end_operation()
return self.PROXY_LIST
except Exception as e:
self.end_operation()
print('ERROR AT GET PROXY')
print(str(e))
raise e
def random_proxy(self):
for i in range(self.CONSUMED_PROXIES + 1):
try:
x = random.choice(self.PROXY_LIST)
print('TRYING PROXY: '+x)
proxies= {
'http':'http://'+ x,
'https':'https://'+ x,
}
r = requests.get('https://www.google.com/humans.txt',
timeout = self.PROXY_TIMEOUT,
proxies = proxies
)
if r.status_code == 200:
return x
except Exception as e:
#removing invalid proxy
self.PROXY_LIST.remove(x)
if not i < self.CONSUMED_PROXIES:
print('NO VALID PROXY ERROR')
raise e
print(f'TRYING ANOTHER PROXY....PROXY NO.{i+1}')
######other functions######
def WATCH_TIME(self):
return randint(self.WATCH_TIME_LIMIT, self.WATCH_TIME_LIMIT+5)
def count_time(self):
return time()-self.time_counter
#configure the chrome
def starting_preparation(self):
#applies when running chrome
if self.BROWSER == 'chrome':
self.chrome_options = webdriver.ChromeOptions()
self.chrome_options.add_argument('--no-sandbox')
self.chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
self.chrome_options.add_argument("disable-infobars")
self.chrome_options.add_argument("--start-maximized")
self.chrome_options.add_argument("--allow-running-insecure-content")
self.chrome_options.add_experimental_option('useAutomationExtension', False)
self.chrome_options.add_argument(f'--user-agent={ua.random}')
if self.PROXY:
#feed proxy to the chrome
rando_prox = self.random_proxy()
self.chrome_options.add_argument(f'--proxy-server={rando_prox}')
self.driver = webdriver.Chrome(chrome_options = self.chrome_options)
self.driver.get('https://www.youtube.com')
#log_info for later use
def log_info(self,comment):
with open('YTlog.txt',mode='a') as writer:
writer.write(comment+'\n')
def end_operation(self):
self.driver.quit()
#customised for setting timeout
def view_xpath(self,xpath_pattern):
start_time = time()
while True:
try:
self.element = self.driver.find_element_by_xpath(xpath_pattern)
return self.element
except WebDriverException as e:
if (time() - start_time) > self.MAX_WAIT:
print('ERROR AT VIEW_XPATH')
raise e
sleep(0.5)
#randomly browses youtube
def roam_randomly_to(self,roam_permit=1,what_to_search = None, each_watch_len=3):
current_url = self.driver.execute_script('return window.location.href')
#check if its a mobile site
if current_url.startswith('https://www.'):
try:
sleep(randint(2,3))
return_to_home= self.driver.find_element_by_xpath('//a[text()="Remind me later"]')
return_to_home.click()
except Exception:
print('did not tell me to update my browser!')
try:
sleep(randint(2,3))
prompt = self.driver.find_element_by_xpath('//*[@id="text" and text()="Ok"]')
prompt.click()
print('prompt clicked')
except Exception:
print('prompt not found')
for i in range(randint(roam_permit, roam_permit+3)):
sleep(randint(2,6))
#this l is used for getting out of a loophole in youtube
l=0
while True:
l+=1
sleep(.5)
inp = '//span[@class="video-time"]/preceding-sibling::img[contains(@src,"https://i.ytimg.com")]'
x = self.driver.find_elements_by_xpath(inp)
print(f"site roam {i}")
try:
x = random.choice(x)
x.click()
break
except Exception as e:
print('cannot click, moving to another object')
print(str(e))
if l>10:
raise ValueError('went to a loophole')
elif l>6:
continue
elif l>5:
self.driver.execute_script("window.history.go(-1)")
wait=randint(0,3)
if wait:
sleep(randint(each_watch_len,each_watch_len+30))
#now we search
if what_to_search != None:
srch_elem = self.view_xpath('//input[@placeholder="Search"]')
srch_elem.send_keys(what_to_search)
srch_elem.send_keys(Keys.ENTER)
print('searched.......')
sleep(randint(1,3))
print('everything completed')
return True
else:
print('went to a mobile version of youtube')
return False
if __name__ == '__main__':
display = Display(visible=0, size=(1024, 768))
display.start()
runner = YouViewer()
runner.begin_loop()
display.stop()