#!/usr/bin/env python

import multiprocessing
import requests
from os import path, makedirs

API_URL = 'https://api.vk.com/method'


class VKException(Exception):
    pass


def request_api(method, params={}):
    req_params = {'v': '5.5'}
    req_params.update(params)
    response = requests.get('{}/{}'.format(API_URL, method), params=req_params)
    data = response.json()
    if 'error' in data:
        raise VKException('Code - {error_code}. Message - {error_msg}'.format(
            **data['error']))
    return data['response']


def create_parser():
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('owner', help='owner name or id')
    parser.add_argument('-u', help='owner is user', action='store_true',
                        dest='source_is_user')
    parser.add_argument('-a', '--album', nargs='*', type=int,
                        help='specify album ids to download. if it is empty, user albums list will be printed')
    parser.add_argument('-p', '--path',
                        help='specify path to save photos',
                        default=path.join(path.dirname(path.abspath(__file__)),
                                          'download/'))
    return parser


def get_download_dir(dir_path, subdir=None):
    abs_path = path.abspath(dir_path)
    if not subdir is None:
        abs_path = path.join(abs_path, subdir)
    if not path.exists(abs_path):
        makedirs(abs_path)
    return abs_path


def downloader(bits):
    pos, url, pos_len, download_dir = bits
    response = requests.get(url, stream=True)
    ext = url.split('.')[-1]
    pos = str(pos + 1).rjust(pos_len, '0')
    file_name = '{}/{}.{}'.format(download_dir, pos, ext)
    with open(file_name, 'wb') as f:
        for chunk in response.iter_content(1024):
            f.write(chunk)


def download_photos(**kwargs):
    req_args, req_kwargs = ('groups.getById', ), {'params': {'group_id': kwargs['owner']}}
    if kwargs['source_is_user']:
        req_args, req_kwargs = ('users.get', ), {'params': {'user_ids': kwargs['owner']}}

    try:
        owner_info = request_api(*req_args, **req_kwargs)[0]
    except VKException:
        print('Can\'t find owner with name or id {}'.format(kwargs['owner']))
    else:
        owner_id = owner_info['id']
        if not kwargs['source_is_user']:
            owner_id = '-{}'.format(owner_id)

        albums = request_api('photos.getAlbums', params={'owner_id': owner_id})

        if not kwargs['album']:
            print('Album list\n\nid\t\ttitle')
            print('-' * 80)
            for album in albums['items']:
                print(u'{id}\t{title}'.format(**album))
            return

        queue = []
        for down_album in kwargs['album']:
            valid = False
            for album in albums['items']:
                if down_album == album['id']:
                    valid = True
                    break
            if valid:
                print('Downloading {}'.format(down_album))
                download_dir = get_download_dir(kwargs['path'], str(down_album))
                print('Saving to {}...'.format(download_dir))
                photos = request_api(
                    'photos.get',
                    params={'owner_id': owner_id, 'album_id': down_album}
                )
                photos_count = photos['count']
                pos_len = len(str(photos_count))
                photo_suffixes = ['2560', '1280', '807', '604', '130', '75']

                for pos, photo in enumerate(photos['items']):
                    for suffix in photo_suffixes:
                        key = 'photo_{}'.format(suffix)
                        if key in photo:
                            queue.append(
                                (pos, photo[key], pos_len, download_dir)
                            )
                            break
            else:
                print('Wrong album id {}'.format(down_album))

        if queue:
            pool = multiprocessing.Pool()
            pool.map(downloader, queue)


if __name__ == '__main__':
    parser = create_parser()
    args = parser.parse_args()
    download_photos(**vars(args))

@@ -2754,189 +2754,70 @@
     
-valid = False%0A            for 
+if any(down_
 album 
-in
+==
  album
-s
 %5B'i
-tems'%5D:%0A                if down_
+d'%5D for 
 album 
-==
+in
  album
+s
 %5B'i
-d'%5D:%0A                    valid = True%0A                    break%0A            if valid
+tems'%5D)
 :%0A  

validate album id in more pythonic way