Skip to content

Instantly share code, notes, and snippets.

@desulaid
Created May 19, 2019 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save desulaid/8d60a7a84da0fca3840fad02370ee7d8 to your computer and use it in GitHub Desktop.
Save desulaid/8d60a7a84da0fca3840fad02370ee7d8 to your computer and use it in GitHub Desktop.
SAVE VK PHOTOS
from vk_api import api
vk = api(YOUR_ACCESS_TOKEN)
album = vk.get_photos_from_album(VK_ID, ALBUM_ID/saved/profile/wall, COUNT)
vk.save_photos(album, DIRECTORY)
from requests import get
from json import loads
import os
class api:
def __init__(self, token):
self.__token__ = token
self.__api__ = 'https://api.vk.com/method/'
def get_photos_from_album(self, owner_id, album_id, count):
method = f'{self.__api__}photos.get'
result = get(method, params = {
'owner_id': owner_id,
'album_id': album_id,
'photo_sizes': 1,
'count': count,
'access_token': self.__token__,
'version': '5.95'
})
return loads(result.text)
def save_photos(self, album, save_to):
index = 0
`
for photo in album['response']:
index += 1
max_size = len(photo['sizes']) - 1
photo_url = photo['sizes'][max_size]['src']
path = f'{save_to}/{index}.jpg'
os.makedirs(save_to, exist_ok=True)
with open(path, 'wb') as handle:
image = get(photo_url, stream=True).content
handle.write(image)
print(f'> Фото {path}.jpg сохранено')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment