-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
42 lines (29 loc) · 928 Bytes
/
utils.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
from random import choice
with open('user-agents.txt') as f:
USER_AGENTS = [line.strip() for line in f]
class BColors:
OKGREEN = '\033[92m'
WARNING = '\033[93m'
ENDC = '\033[0m'
def print_warn(txt):
print(f'{BColors.WARNING}{txt}{BColors.ENDC}')
def print_green(txt):
print(f'{BColors.OKGREEN}{txt}{BColors.ENDC}')
def batch(iterable, size):
"""Returns elements from iterator in batches of size"""
iterator = iter(iterable)
while True:
res = []
for _ in range(size):
try:
res.append(next(iterator))
except StopIteration:
break
if not res:
return
yield res
async def fetch(session, url):
headers = {'User-Agent': choice(USER_AGENTS)}
async with session.get(url, headers=headers) as response:
content = await response.read()
return response.status, content