Guest User

VkDocs on Python

a guest
Feb 2nd, 2018
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.53 KB | None | 0 0
  1. import sys
  2. try:
  3.     import aiohttp
  4. except ModuleNotFoundError:
  5.     print("aiohttp not found. Use 'pip3 install aiohttp' in console to fix it.")
  6. import asyncio
  7. import argparse
  8.  
  9. def get_arg():
  10.     parser = argparse.ArgumentParser()
  11.     parser.add_argument('-f', '--file', nargs='?')
  12.     parser.add_argument('-i', '--id', nargs='?')
  13.     parser.add_argument('-s', '--start', default=1, nargs='?')
  14.     parser.add_argument('-e', '--end', default=1000000000, nargs='?')
  15.     parser.add_argument('-v', action='store_const', const=True)
  16.     return parser
  17.  
  18. result = []
  19. headers = {
  20.     'Accept': "text/html,application/xhtml+xm…plication/xml;q=0.9,*/*;q=0.8",
  21.     "Accept-Encoding": "gzip, deflate, br",
  22.     "Accept-Language": "ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3",
  23.     "Cache-Control": "max-age=0",
  24.     "Connection": "keep-alive",
  25.     "Host": "vk.com",
  26.     "Upgrade-Insecure-Requests": "1",
  27.     "User-Agent": "Mozilla/5.0 (Windows NT 10.0; …) Gecko/20100101 Firefox/58.0"
  28.     }
  29. async def request(i):
  30.     async with aiohttp.ClientSession() as session:
  31.         for n in i:
  32.             response = await session.get('https://vk.com/doc{id}_{i}'.format(id=id, i=str(n).zfill(9)),
  33.                     headers=headers)
  34.             if not response.headers.get('x-frame-options'):
  35.                 print(response.url)
  36.                 result.append(str(response.url))
  37.             response.close()
  38.             if namespace.v:
  39.                 print(response.url, response.status)
  40.  
  41. async def task(it):
  42.  
  43.     futures = [request(i) for i in zip(*[iter(it)]*100 )]
  44.     for i, future in enumerate(asyncio.as_completed(futures)):
  45.         res = await future
  46.  
  47. async def more_task(itt):
  48.     futures = [task(i) for i in zip(*[iter(range((itt-1)*1000, itt*1000))]*100)]
  49.     for i, future in enumerate(asyncio.as_completed(futures)):
  50.         res = await future
  51.  
  52. if __name__ == '__main__':
  53.     try:
  54.         parser = get_arg()
  55.         namespace = parser.parse_args(sys.argv[1:])
  56.         global id
  57.         if namespace.id:
  58.             id = namespace.id
  59.         else:
  60.             id = input('Input Id: ')
  61.         ioloop = asyncio.get_event_loop()
  62.         [ioloop.run_until_complete(more_task(i)) for i in range(1,10000000)]
  63.         ioloop.close()
  64.  
  65.     except KeyboardInterrupt:
  66.         for item in result:
  67.             print(item)
  68.         else:
  69.             print('\n There is nothing')
  70.     finally:
  71.         if namespace.file:
  72.             with open(str(namespace.file), 'w') as file:
  73.                 for line in result:
  74.                     file.write(line)
Advertisement
Add Comment
Please, Sign In to add comment