dginz

downloadVkMusic.py

Apr 23rd, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.77 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. VK_APP_ID = 3763243
  4. VK_PERMISSIONS_MASK = 8
  5. REDIRECT_URL = 'https://oauth.vk.com/blank.html'
  6.  
  7. 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
  8. VK_AUDIO_GET_REQUEST = 'https://api.vk.com/method/audio.get?count=6000&v=5.27&access_token=%s'
  9.  
  10. import re
  11. import requests
  12. import json
  13. import string
  14. import urllib
  15. import os.path
  16. import sys
  17.  
  18. print ('WARNING: all files are downloaded into your current directory')
  19. print ()
  20. print ('Open %s in your browser' % VK_AUTH_URL)
  21. print ('and print the page you are redirected to after you give the permissions:')
  22. response = input('URL: ')
  23.  
  24. matcher = re.search('access_token=([^&]+)', response)
  25. access_token = matcher.group(1)
  26.  
  27. audios = json.loads(requests.get(VK_AUDIO_GET_REQUEST % access_token).content.decode('utf-8'))['response']['items']
  28.  
  29. translit_table = {
  30.     'а': 'a',
  31.     'б': 'b',
  32.     'в': 'v',
  33.     'г': 'g',
  34.     'д': 'd',
  35.     'е': 'e',
  36.     'ё': 'yo',
  37.     'ж': 'zh',
  38.     'з': 'z',
  39.     'и': 'i',
  40.     'й': 'y',
  41.     'к': 'k',
  42.     'л': 'l',
  43.     'м': 'm',
  44.     'н': 'n',
  45.     'о': 'o',
  46.     'п': 'p',
  47.     'р': 'r',
  48.     'с': 's',
  49.     'т': 't',
  50.     'у': 'u',
  51.     'ф': 'f',
  52.     'х': 'kh',
  53.     'ц': 'ts',
  54.     'ч': 'ch',
  55.     'ш': 'sh',
  56.     'щ': 'sch',
  57.     'ъ': 'y',
  58.     'ы': 'i',
  59.     'ь': 'y',
  60.     'э': 'e',
  61.     'ю': 'yu',
  62.     'я': 'ya',
  63.  
  64.     'А': 'A',
  65.     'Б': 'B',
  66.     'В': 'V',
  67.     'Г': 'G',
  68.     'Д': 'D',
  69.     'Е': 'E',
  70.     'Ё': 'Yo',
  71.     'Ж': 'Zh',
  72.     'З': 'Z',
  73.     'И': 'I',
  74.     'Й': 'Y',
  75.     'К': 'K',
  76.     'Л': 'L',
  77.     'М': 'M',
  78.     'Н': 'N',
  79.     'О': 'O',
  80.     'П': 'P',
  81.     'Р': 'R',
  82.     'С': 'S',
  83.     'Т': 'T',
  84.     'У': 'U',
  85.     'Ф': 'F',
  86.     'Х': 'Kh',
  87.     'Ц': 'Ts',
  88.     'Ч': 'Ch',
  89.     'Ш': 'Sh',
  90.     'Щ': 'Sch',
  91.     'Ъ': 'Y',
  92.     'Ы': 'I',
  93.     'Ь': 'Y',
  94.     'Э': 'E',
  95.     'Ю': 'Yu',
  96.     'Я': 'Ya',
  97. }
  98.  
  99. valid_chars = '-_.() %s%s' % (string.ascii_letters, string.digits)
  100. valid_chars = frozenset(valid_chars)
  101.  
  102.  
  103. index = 1
  104. total = len(audios)
  105. for audio in audios:
  106.     name = audio['artist'] + ' - ' + audio['title']
  107.     name = ''.join(translit_table[c] if c in translit_table else c for c in name)
  108.     name = ''.join(c for c in name if c in valid_chars)
  109.     name = (name[:72] + '...') if len(name) >= 75 else name
  110.     sys.stdout.write ('Downloading "%s" (%d/%d)... ' % (name, index, total))
  111.     index += 1
  112.     filename = name + '.mp3'
  113.     if os.path.isfile(filename):
  114.         print ('SKIP, file exists')
  115.         continue
  116.     stream = urllib.request.urlopen(audio['url'])
  117.     with open(name + '.mp3', 'b+w') as f:
  118.         f.write(stream.read())
  119.     print ('DONE')
Add Comment
Please, Sign In to add comment