import time
import asyncio
import aiohttp
async def is_name_available(s, name):
async with s.get("https://twitter.com/%s" % name) as res:
if res.raise_for_status == 404:
print('%s is available!' % name)
return name
async def check_all_names(names):
async with aiohttp.ClientSession(raise_for_status=True) as s:
tasks = []
for name in names:
task = asyncio.create_task(is_name_available(s, name))
tasks.append(task)
return await asyncio.gather(*tasks)
def main():
with open('names.txt') as in_file, open('available.txt', 'w') as out_file:
names = [name.strip() for name in in_file]
start_time = time.time()
results = asyncio.get_event_loop().run_until_complete(check_all_names(names))
results = [i for i in results if i]
out_file.write('\n'.join(results))
print(f'[ <? ] Checked {len(names)} words in {round(time.time()-start_time, 2)} second(s)')
if __name__ == '__main__':
main()
I cannot seem to figure out how to go about returning only 404'd links in is_name_available with this asyncio/aiohttp structure I'm using from another project of mine. I'm a beginner in python and any help is appreciated.
asyncio.run(check_all_names(names))
without getting the event loop explicitly – Pynchia Jun 1 '20 at 21:47