forked from kidig/vk-audio-sync
/
vk_audiosync.py
119 lines (83 loc) · 3.02 KB
/
vk_audiosync.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# -*- coding: utf-8 -*-
import os
import urllib2
import json
import codecs
import re
import HTMLParser
from mutagen.easyid3 import EasyID3
from mutagen.id3 import ID3NoHeaderError
import urlgrabber
import vk_api
import config
from time import time
def get_token(credentials=[]):
if not len(credentials):
credentials = ('friends',)
token, user_id = vk_api.auth(config.USERNAME,
config.PASSWORD,
config.CLIENT_ID,
",".join(credentials))
return (token, user_id)
def get_audio(token, uid):
res = vk_api.call_method('audio.getCount', {
'oid': uid}, token)
audio_cnt = res['response']
res = vk_api.call_method('audio.get', {
'count': audio_cnt}, token)
return res
def clean_audio_tag(tag):
h = HTMLParser.HTMLParser()
tag = h.unescape(tag)
tag = h.unescape(tag) # need to unescape unescaped entities
tag = re.sub(r'http://.[^\s]+', '', tag) # remove any urls
tag = tag.replace(' :)','') # remove smiles
ctag = re.compile(u'[^a-zA-Zа-яА-ЯёЁ0-9\s_\.,&#!?\-\'"`\/\|\[\]\(\)]')
tag = ctag.sub('', tag).strip() # kill most unusual symbols
tag = re.sub(r'\s+', ' ', tag) # remove long spaces
return tag
def set_id3(filename, title, artist):
try:
mp3info = EasyID3(filename)
except ID3NoHeaderError:
mp3info = EasyID3()
mp3info['title'] = title
mp3info['artist'] = artist
mp3info.save(filename)
# audio.get
def download_playlist():
token, user_id = get_token(['audio'])
user_audio = get_audio(token, user_id)['response']
with codecs.open(config.PLAYLIST, 'w', 'utf8') as fp:
for track in user_audio:
aid = str(track.get('aid'))
artist = clean_audio_tag(track.get('artist'))
title = clean_audio_tag(track.get('title'))
url = track.get('url')
fp.write('%s\n' % ('\t'.join((aid, artist, title, url))))
# download mp3 files
def download_audio_files():
if not os.path.exists(config.MUSIC_PATH):
os.makedirs(config.MUSIC_PATH)
for line in open(config.PLAYLIST):
aid, artist, title, url = line.rstrip('\n').decode('utf-8').split('\t')
filename = os.path.basename(url).split('?')[0]
filepath = os.path.join(config.MUSIC_PATH, "%s_%s" % (aid, filename))
print "Download %s - %s..." % (artist, title)
g = urlgrabber.grabber.URLGrabber(reget='simple')
try:
g.urlgrab(url, filename=filepath)
except urlgrabber.grabber.URLGrabError, e:
if e.exception[1] != 'The requested URL returned error: 416 Requested Range Not Satisfiable':
raise
set_id3(filepath, title, artist)
def main():
print 'start...'
if not os.path.isfile(config.PLAYLIST):
print 'download playlist...',
download_playlist()
print 'ok'
download_audio_files()
print 'done.'
if __name__ == '__main__':
main()