-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
96 lines (70 loc) · 2.6 KB
/
main.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
from fake_useragent import UserAgent
import ssl
import time
import asyncio
import aiohttp
import argparse
import re
import signal
import sys
NUMBER_OF_CONCURRENT_TASKS = 1
# in seconds (floating point)
SLEEP_TIME = 2
REQUEST_TIMEOUT = 10
start_time = None
tries = 0
successful = 0
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
sys.stderr.flush()
async def try_to_search(session):
global tries, successful
tries += 1
headers = {
'User-Agent': UserAgent().random
}
search_text = 'list comprehension'
url = 'https://www.google.com/search?client=ubuntu&q={}&oq={}'.format(search_text, search_text)
try:
async with session.get(url, headers=headers, timeout=REQUEST_TIMEOUT) as resp:
resp_text = await resp.text()
if 'foobar' in resp_text.lower():
eprint('DEBUG: found foobar keyword')
if 'find.foo' in resp_text.lower():
eprint('DEBUG: found find.foo keyword')
matches = re.search(r'(https?://(www.)?google.com/foobar/[^\"]+)', resp_text)
if matches:
successful += 1
print(matches.groups()[0])
sys.stdout.flush()
return True
except (aiohttp.client_exceptions.ServerDisconnectedError,
aiohttp.client_exceptions.ClientHttpProxyError,
aiohttp.client_exceptions.ClientProxyConnectionError,
aiohttp.client_exceptions.ClientResponseError,
aiohttp.client_exceptions.ClientPayloadError,
asyncio.TimeoutError,
aiohttp.client_exceptions.ClientOSError,
ssl.CertificateError) as ex:
eprint('Error during request. Proxy: {}. Exception: {}'.format(None, type(ex)))
return False
async def main():
global tries, successful
session_kwargs = {}
async with aiohttp.ClientSession(**session_kwargs) as session:
while True:
tasks = [try_to_search(session) for _ in range(NUMBER_OF_CONCURRENT_TASKS)]
if tasks:
await asyncio.gather(*tasks)
await asyncio.sleep(SLEEP_TIME)
def exit_handler(*_):
eprint(' tries: {}; successful: {}; took time: {} seconds'.format(tries, successful, time.time() - start_time))
sys.exit(0)
if __name__ == '__main__':
start_time = time.time()
signal.signal(signal.SIGINT, exit_handler)
parser = argparse.ArgumentParser(
description='Get your invite to Google\'s programming challenge'
)
command_line_args = parser.parse_args()
asyncio.get_event_loop().run_until_complete(main())