Skip to content

Instantly share code, notes, and snippets.

@chanramen
Created July 3, 2021 08:02
Show Gist options
  • Save chanramen/374402a9954e8b791db8b17719120af4 to your computer and use it in GitHub Desktop.
Save chanramen/374402a9954e8b791db8b17719120af4 to your computer and use it in GitHub Desktop.
Turbo gif downloader
"""Script for downloading gif images from vk.com group gif бумеры 2004
Actually, you can download any gifs from any group, you just need to insert ID.
Requirements are:
* python 3.9+ (should work on python 3.6+, but haven't checked)
* aiohttp, asyncio-pool packages
ATTENTION: to poeceed, you have to acquire your own service token and insert it in TOKEN variable
"""
import asyncio
import os
import aiohttp
from asyncio_pool import AioPool
ID = '-117149296'
COUNT = '100'
PAGE_SIZE = 100
TOKEN = "<token>"
START_OFFSET = 0
OUT_DIR = os.path.join(os.getcwd(), "gifs")
download_offset = 0
async def main():
url_list = []
session = aiohttp.ClientSession()
offset = 0 + START_OFFSET
total_items = PAGE_SIZE + START_OFFSET + 1
while offset < total_items:
async with session.get(
f'https://api.vk.com/method/wall.get?owner_id={ID}&count={COUNT}&access_token={TOKEN}&v=5.131&offset={offset}') as response:
data = await response.json()
items = data['response']['items']
total_items = data['response']["count"]
offset += len(items)
attachment_list = [item.get("attachments") for item in items if item.get("attachments") is not None]
url_list += [attch["doc"]["url"] for item in attachment_list for attch in item if
attch["type"] == "doc" and attch["doc"]["ext"] == "gif"]
print(f"offset is {offset}")
with open("items.txt", 'w') as file:
file.write("\n".join(url_list))
pool = AioPool()
async def downloader(url):
async with session.get(url) as response:
data = await response.read()
global download_offset
filename = os.path.join(OUT_DIR, f"{download_offset}.gif")
with open(filename, 'wb') as f:
f.write(data)
print(f"downloaded {filename}")
download_offset += 1
await pool.map(downloader, url_list)
await session.close()
if __name__ == '__main__':
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment