Created
September 9, 2019 22:01
-
-
Save saevarb/7ba9a22fd38e8f14d0751374f208ed7c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import requests | |
import time | |
headers = { | |
'Host': 'api.tradono.com', | |
'Connection': 'keep-alive', | |
'Sec-Fetch-Mode': 'cors', | |
'tradono-app-version': '1.2.139-53-g59d7aa5', | |
'authorization': 'e5a64398-cc4c-4839-850e-c597326f67aa', | |
'tradono-platform': 'web', | |
'tradono-language': 'en', | |
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36', | |
'Accept': '*/*', | |
'Origin': 'https://tradono.com', | |
'Sec-Fetch-Site': 'same-site', | |
'Referer': 'https://tradono.com/p/486620', | |
'Accept-Encoding': 'gzip, deflate, br', | |
'Accept-Language': 'en-GB,en;q=0.9,da-DK;q=0.8,da;q=0.7,de-DE;q=0.6,de;q=0.5,en-US;q=0.4', | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'Content-Length': '0', | |
} | |
# Run this many "threads" at a time | |
MAX_CONCURRENT = 8 | |
class Printer: | |
"""Simple utility class for printing messages in-place in the console | |
""" | |
# Store the lengths of the messages we have printed | |
last_lengths = [] | |
def print(self, *args): | |
last = None | |
if self.last_lengths: | |
last = self.last_lengths.pop() | |
length = sum([len(a) for a in args]) | |
self.last_lengths.insert(0, length) | |
# print as many backspaces as the length of the last message we printed | |
# before printing a new message | |
if last: | |
print("\b" * last, end="") | |
print(*args, end="", flush=True) | |
def generate_urls(): | |
for i in range(400,500): | |
yield f"https://api.tradono.com/users/486630/follows/{i}" | |
async def process_url(url): | |
res = requests.post(url,headers=headers) | |
# await asyncio.sleep(1) | |
return url | |
async def main(): | |
urls = generate_urls() | |
printer = Printer() | |
count = 0 | |
aws = [] | |
completed = 0 | |
start_time = time.time() | |
while True: | |
if count < MAX_CONCURRENT: | |
try: | |
url = next(urls) | |
aws.append(asyncio.create_task(process_url(url))) | |
count += 1 | |
except: | |
break | |
else: | |
done, _ = await asyncio.wait(aws, return_when=asyncio.FIRST_COMPLETED) | |
for d in done: | |
completed += 1 | |
current_time = time.time() | |
since = current_time - start_time | |
avg = completed / since | |
printer.print(f"Completed: {completed}, ~{avg} per second") | |
aws.remove(d) | |
count -= len(done) | |
end_time = time.time() | |
print(f"\nDone, duration: {end_time - start_time}s") | |
asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment