-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvkalbu.py
executable file
·154 lines (135 loc) · 4.51 KB
/
vkalbu.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python3
import os
import sys
import argparse
import configparser
import vk_api
import albums_parser
import album
import track
import subprocess
# Parse args.
usage = "vkalbu /path/to/albums_list.json"
description="Discography creator for vk.com."
args_parser = argparse.ArgumentParser(
description=description,
usage=usage
)
args_parser.add_argument(
'albums_file',
help='path to file containing albums list in json format'
)
args_parser.add_argument(
'--timeout',
help='vk api requests timeout in seconds',
type=int,
default=3
)
args_parser.add_argument(
'--sleep',
help='sleep time in seconds between vk api requests',
type=int,
default=1
)
args = args_parser.parse_args()
"""Returns albums list json from specified file."""
def get_albums_list():
# Try to read list from file.
if args.albums_file:
with open(args.albums_file) as fp:
albums = fp.read()
return albums
return None
"""Handles captcha request."""
def captcha_handler(captcha):
print("Captcha %s" % captcha.get_url())
key = input("Enter captcha code: ").strip()
return captcha.try_again(key)
# Create vk.com albums, search for tracks and add it to albums.
if __name__ == '__main__':
# Get albums data from stdin or albums file
albparser = albums_parser.AlbumsParser()
albums = get_albums_list()
if not albums:
print('Failed to read albums list. Albums file not provided.')
if not albums:
print('Empty albums list, exit.')
sys.exit()
albums = albparser.parse_json(albums)
# Read config.
config = configparser.ConfigParser()
config.read(os.path.dirname(__file__) + '/auth.ini')
# Get vk api object.
vk = vk_api.VkApi(
login=config['user']['login'],
password=config['user']['password'],
app_id=config['app']['id'],
scope='audio',
captcha_handler=captcha_handler
)
# Authorize.
try:
vk.authorization()
except vk_api.AuthorizationError as e:
print(e)
sys.exit()
# Init APIs for albums and tracks.
album_api = album.Album(vk)
track_api = track.Track(vk)
# Process provided albums.
for album in albums:
# Create album if doesn't exist.
album['full_name'] = '%s — %s (%d)' % (
album['artist'],
album['title'],
album['year']
)
album_id = album_api.get_id_by_title(album['full_name'])
if not album_id:
print('Creating album "%s".' % album['full_name'])
m_args = {
'title': album['full_name']
}
added_album = vk.method('audio.addAlbum', m_args)
album_id = added_album['album_id']
else:
print('album "%s" exists, reusing' % album['full_name'])
# Find tracks, collect their information.
tracks_for_album = []
for track in reversed(album['tracks']):
track_query = track_api.Query(
artist=album['artist'],
album=album['title'],
title=track['title'],
year=album['year'],
length=track['duration']
)
print('Searching for track:')
print(track_query)
found_track = track_api.search(track_query)
if (found_track):
print('Found track %d "%s"' % (
found_track['id'],
found_track['title']
))
m_args = {
'audio_id': found_track['id'],
'owner_id': found_track['owner_id']
}
saved_track_id = vk.method('audio.add', m_args)
if saved_track_id:
tracks_for_album.append(saved_track_id)
# Add all the found tracks into album at once.
if len(tracks_for_album):
print('Adding %d tracks to album "%s".' % (
len(tracks_for_album),
album['full_name']
))
audio_ids = ",".join(str(i) for i in tracks_for_album)
print('Audio_ids %s.' % audio_ids)
m_args = {
'album_id': album_id,
'audio_ids': audio_ids
}
vk.method('audio.moveToAlbum', m_args)
print('Done.')