Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- VK_APP_ID = 3763243
- VK_PERMISSIONS_MASK = 8
- REDIRECT_URL = 'https://oauth.vk.com/blank.html'
- VK_AUTH_URL = 'https://oauth.vk.com/authorize?client_id=' + str(VK_APP_ID) + '&display=page&response_type=token&scope=' + str(VK_PERMISSIONS_MASK) + '&redirect_url=' + REDIRECT_URL
- VK_AUDIO_GET_REQUEST = 'https://api.vk.com/method/audio.get?count=6000&v=5.27&access_token=%s'
- import re
- import requests
- import json
- import string
- import urllib
- import os.path
- import sys
- print ('WARNING: all files are downloaded into your current directory')
- print ()
- print ('Open %s in your browser' % VK_AUTH_URL)
- print ('and print the page you are redirected to after you give the permissions:')
- response = input('URL: ')
- matcher = re.search('access_token=([^&]+)', response)
- access_token = matcher.group(1)
- audios = json.loads(requests.get(VK_AUDIO_GET_REQUEST % access_token).content.decode('utf-8'))['response']['items']
- translit_table = {
- 'а': 'a',
- 'б': 'b',
- 'в': 'v',
- 'г': 'g',
- 'д': 'd',
- 'е': 'e',
- 'ё': 'yo',
- 'ж': 'zh',
- 'з': 'z',
- 'и': 'i',
- 'й': 'y',
- 'к': 'k',
- 'л': 'l',
- 'м': 'm',
- 'н': 'n',
- 'о': 'o',
- 'п': 'p',
- 'р': 'r',
- 'с': 's',
- 'т': 't',
- 'у': 'u',
- 'ф': 'f',
- 'х': 'kh',
- 'ц': 'ts',
- 'ч': 'ch',
- 'ш': 'sh',
- 'щ': 'sch',
- 'ъ': 'y',
- 'ы': 'i',
- 'ь': 'y',
- 'э': 'e',
- 'ю': 'yu',
- 'я': 'ya',
- 'А': 'A',
- 'Б': 'B',
- 'В': 'V',
- 'Г': 'G',
- 'Д': 'D',
- 'Е': 'E',
- 'Ё': 'Yo',
- 'Ж': 'Zh',
- 'З': 'Z',
- 'И': 'I',
- 'Й': 'Y',
- 'К': 'K',
- 'Л': 'L',
- 'М': 'M',
- 'Н': 'N',
- 'О': 'O',
- 'П': 'P',
- 'Р': 'R',
- 'С': 'S',
- 'Т': 'T',
- 'У': 'U',
- 'Ф': 'F',
- 'Х': 'Kh',
- 'Ц': 'Ts',
- 'Ч': 'Ch',
- 'Ш': 'Sh',
- 'Щ': 'Sch',
- 'Ъ': 'Y',
- 'Ы': 'I',
- 'Ь': 'Y',
- 'Э': 'E',
- 'Ю': 'Yu',
- 'Я': 'Ya',
- }
- valid_chars = '-_.() %s%s' % (string.ascii_letters, string.digits)
- valid_chars = frozenset(valid_chars)
- index = 1
- total = len(audios)
- for audio in audios:
- name = audio['artist'] + ' - ' + audio['title']
- name = ''.join(translit_table[c] if c in translit_table else c for c in name)
- name = ''.join(c for c in name if c in valid_chars)
- name = (name[:72] + '...') if len(name) >= 75 else name
- sys.stdout.write ('Downloading "%s" (%d/%d)... ' % (name, index, total))
- index += 1
- filename = name + '.mp3'
- if os.path.isfile(filename):
- print ('SKIP, file exists')
- continue
- stream = urllib.request.urlopen(audio['url'])
- with open(name + '.mp3', 'b+w') as f:
- f.write(stream.read())
- print ('DONE')
Add Comment
Please, Sign In to add comment