diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/README.md b/README.md deleted file mode 100644 index 668c22b..0000000 --- a/README.md +++ /dev/null @@ -1,17 +0,0 @@ -# VK-Photos-Downloader -![](https://github.com/Victoria1807/VK-Photos-Downloader/blob/master/Victoria1807.jpg) -## Version 1.0 | 03.08.17 -A Python script, which allow to download the photos from any album of VK. - -Realized by **Python 3.5** - -## Additional modules: - 1. **[VK](https://github.com/dimka665/vk)** - -## My profiles: - - [VK](https://vk.com/victoria_1807) - - [Telegram](https://telegram.me/Victoria1807) - - [GitHub](https://github.com/Victoria1807) - - E-mail - flamaster1807@gmail.com - - diff --git a/Victoria1807.jpg b/Victoria1807.jpg deleted file mode 100644 index 3587fed..0000000 Binary files a/Victoria1807.jpg and /dev/null differ diff --git a/requirements.txt b/requirements.txt index fd1ebe1..5326bcb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -vk==2.0.2 +vk==3.0 diff --git a/vk-photos-downloader.py b/vk-photos-downloader.py old mode 100644 new mode 100755 index 70ef1d7..c8e8667 --- a/vk-photos-downloader.py +++ b/vk-photos-downloader.py @@ -1,96 +1,117 @@ -#!/usr/bin/python3.5 -#-*- coding: UTF-8 -*- - -import vk, os, time -from urllib.request import urlretrieve - -token = input("Enter API token (access_token): ") # vk token - - -#Authorization -session = vk.Session(access_token=str(token)) -vk_api = vk.API(session,v='5.102') - -count = 0 # count of down. photos -perc = 0 # percent of down. photos -skipped = 0 # unsuccessful down. -time_now = time.time() # current time - -url = "'" + input("Enter a URL of an album: ") + "'" # url of album -folder_name = input("Enter a folder name for the downloaded photos: ") # fold. for photo -print("-------------------------------------------") - -owner_id = url.split('album')[1].split('_')[0] # id of owner -album_id = url.split('album')[1].split('_')[1][0:-1] # id of album - -get_album = vk_api.photos.getAlbums(owner_id=owner_id, album_ids=album_id) - -photos_count = get_album['items'][0]['size'] # count of ph. in albums -album_title = get_album['items'][0]['title'] # albums title - -collect = True -collected = 0 - -print("Album title: {}".format(album_title)) -print("Photos in album: {}".format(photos_count)) -print("---------------------------") - -links = [] - -while collect is True: - data = vk_api.photos.get(owner_id=owner_id, album_id=album_id, count=200, offset=collected)['items'] - collected = (len(data) + collected) - print("Collecting data " + str(collected) + "/" + str(photos_count) + " ..") - - for item in data: - largest = 0 - url = '' - for i in item['sizes']: - if i['width'] > largest: - url = i['url'] - largest = i['width'] - if url: - links.append(url) - - - if collected >= photos_count or len(data) == 0: - collect = False - else: - time.sleep(1) - -if not os.path.exists(folder_name): - os.makedirs(folder_name + '/' + album_title) # creating a folder for download photos - -print("Saving to: " + (folder_name + '/' + album_title)) -print("---------------------------") - -download_count = 0 - -for url in links: - download_count += 1 - try: - try: - filename = url.split("/")[-1] - except: - filename = (download_count + ".jpg") - - destination = (folder_name + '/' + album_title + '/' + filename) - - if not os.path.isfile(destination): - urlretrieve(url, destination) # download photo - else: - skipped += 1 - - perc = (100 * download_count) / photos_count - print("Downloaded {} of {} photos. ({}%)".format(download_count, photos_count, round(perc, 2))) - except: - print("An error occurred, file skipped.") - skipped += 1 - -minutes = int((time.time() - time_now) // 60) -seconds = int((time.time() - time_now) % 60) - -print("---------------------------") -print("Successful downloaded {} photos.".format(count)) -print("Skipped {} photos.".format(skipped)) -print("Time spent: {}.{} minutes.".format(minutes, seconds)) \ No newline at end of file +#!/usr/bin/env python3 +# -*- coding: UTF-8 -*- +import argparse +import os +import time +import traceback +from pathlib import Path +from urllib.request import urlretrieve + +import vk + + +def main(token: str, album_url: str, folder_name: Path): + vk_api = vk.API(access_token=token, v='5.102') + + count = 0 # count of down. photos + perc = 0 # percent of down. photos + skipped = 0 # unsuccessful down. + time_now = time.time() # current time + + url = "'" + album_url + "'" # url of album + print("-------------------------------------------") + + owner_id = url.split('album')[1].split('_')[0] # id of owner + album_id = url.split('album')[1].split('_')[1][0:-1] # id of album + + get_album = vk_api.photos.getAlbums(owner_id=owner_id, album_ids=album_id) + + photos_count = get_album['items'][0]['size'] # count of ph. in albums + album_title = get_album['items'][0]['title'] # albums title + + collect = True + collected = 0 + + print("Album title: {}".format(album_title)) + print("Photos in album: {}".format(photos_count)) + print("---------------------------") + + links = [] + + while collect is True: + data = vk_api.photos.get(owner_id=owner_id, album_id=album_id, count=200, offset=collected)['items'] + collected = (len(data) + collected) + print("Collecting data " + str(collected) + "/" + str(photos_count) + " ..") + + for item in data: + largest = 0 + url = '' + for i in item['sizes']: + if i['width'] > largest: + url = i['url'] + largest = i['width'] + if url: + links.append(url) + + if collected >= photos_count or len(data) == 0: + collect = False + + else: + time.sleep(1) + + target_folder = (folder_name / album_title) + target_folder.mkdir(exist_ok=True, parents=True) + + print("Saving to: " + str(target_folder)) + print("---------------------------") + + download_count = 0 + + for url in links: + download_count += 1 + try: + try: + filename = url.split("/")[-1] + + except Exception: + print("Failed to convert filename, falling back to sequential naming") + print(traceback.format_exc()) + + filename = f'{download_count}.jpg' + + destination = (target_folder / filename) + + if not os.path.isfile(destination): + urlretrieve(url, destination) # download photo + else: + skipped += 1 + + perc = (100 * download_count) / photos_count + print("Downloaded {} of {} photos. ({}%)".format(download_count, photos_count, round(perc, 2))) + + except Exception: + print("An error occurred, file skipped.") + print(traceback.format_exc()) + skipped += 1 + + minutes = int((time.time() - time_now) // 60) + seconds = int((time.time() - time_now) % 60) + + print("---------------------------") + print("Successful downloaded {} photos.".format(count)) + print("Skipped {} photos.".format(skipped)) + print("Time spent: {}.{} minutes.".format(minutes, seconds)) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + prog='vk-photos-downloader.py', + description='A tool to download photos from albums from vk.com' + ) + parser.add_argument('url', help='Url to target album', type=str) + parser.add_argument('folder_name', help='Folder to put downloaded photos', type=Path) + args = parser.parse_args() + + TOKEN: str = os.environ['VK_TOKEN'] + + main(token=TOKEN, album_url=args.url, folder_name=args.folder_name)