Skip to content

vkpymusic.ServiceAsync

A class that provides methods for working with VK API.

Attributes:

Name Type Description
user_agent str

The user agent string.

__token str

The access token.

logger Logger

The logger for class.

Example usage:

>>> import asyncio
>>>
>>> service = ServiceAsync.parse_config()
>>> songs = asyncio.run(service.search_songs_by_text("Imagine Dragons"))
>>> song = songs[0]
>>> asyncio.run(ServiceAsync.save_music(song))

Functions

__init__(user_agent, token)

Initializes a ServiceAsync object.

Parameters:

Name Type Description Default
user_agent str

The user agent string.

required
token str

The access token.

required

parse_config(filename='config_vk.ini') classmethod

Create an instance of ServiceAsync from config.

Parameters:

Name Type Description Default
filename str

Filename of config (default = "config_vk.ini").

'config_vk.ini'

del_config(filename='config_vk.ini') classmethod

Delete config created by 'TokenReceiver'.

Parameters:

Name Type Description Default
filename str

Filename of config (default = "config_vk.ini").

'config_vk.ini'

check_token(token) async classmethod

Check token for VK API.

Returns:

Name Type Description
bool bool

True if token is valid, False otherwise.

is_token_valid() async

Check token for VK API.

Returns:

Name Type Description
bool bool

True if token is valid, False otherwise.

get_user_info() async

Get user info by token.

Returns:

Name Type Description
UserInfo Optional[UserInfo]

Instance of 'UserInfo'.

get_count_by_user_id(user_id) async

Get count of all user's songs.

Parameters:

Name Type Description Default
user_id str | int

VK user id. (NOT USERNAME! vk.com/id*).

required

Returns:

Name Type Description
int int

count of all user's songs.

get_songs_by_userid(user_id, count=100, offset=0) async

Search songs by owner/user id.

Parameters:

Name Type Description Default
user_id str | int

VK user id. (NOT USERNAME! vk.com/id*).

required
count int

Count of resulting songs (for VK API: default/max = 100).

100
offset int

Set offset for result. For example, count = 100, offset = 100 -> 101-200.

0

Returns:

Type Description
List[Song]

list[Song]: List of songs.

get_songs_by_playlist_id(user_id, playlist_id, access_key, count=100, offset=0) async

Get songs by playlist id.

Parameters:

Name Type Description Default
user_id str | int

VK user id. (NOT USERNAME! vk.com/id*).

required
playlist_id int

VK playlist id. (Take it from methods for playlist).

required
access_key str

VK access key. (Take it from methods for playlist).

required
count int

Count of resulting songs (for VK API: default/max = 100).

100
offset int

Set offset for result. For example, count = 100, offset = 100 -> 101-200.

0

Returns:

Type Description
List[Song]

list[Song]: List of songs.

get_songs_by_playlist(playlist, count=10, offset=0) async

Get songs by instance of 'Playlist'.

Parameters:

Name Type Description Default
playlist Playlist

Instance of 'Playlist' (take from methods for receiving Playlist).

required
count int

Count of resulting songs (for VK API: default/max = 100).

10
offset int

Set offset for result. For example, count = 100, offset = 100 -> 101-200.

0

Returns:

Type Description
List[Song]

list[Song]: List of songs.

search_songs_by_text(text, count=3, offset=0) async

Search songs by text/query.

Parameters:

Name Type Description Default
text str

Text of query. Can be title of song, author, etc.

required
count int

Count of resulting songs (for VK API: default/max = 100).

3
offset int

Set offset for result. For example, count = 100, offset = 100 -> 101-200.

0

Returns:

Type Description
List[Song]

list[Song]: List of songs.

get_playlists_by_userid(user_id, count=5, offset=0) async

Get playlist by owner/user id.

Parameters:

Name Type Description Default
user_id str | int

VK user id. (NOT USERNAME! vk.com/id*).

required
count int

Count of resulting playlists (for VK API: default = 50, max = 100).

5
offset int

Set offset for result. For example, count = 100, offset = 100 -> 101-200.

0

Returns:

Type Description
List[Playlist]

list[Playlist]: List of playlists.

search_playlists_by_text(text, count=5, offset=0) async

Search playlists by text/query. Playlist - it user's collection of songs.

Parameters:

Name Type Description Default
text str

Text of query. Can be title of playlist, genre, etc.

required
count int

Count of resulting playlists (for VK API: default = 50, max = 100).

5
offset int

Set offset for result. For example, count = 100, offset = 100 -> 101-200.

0

Returns:

Type Description
List[Playlist]

list[Playlist]: List of playlists.

search_albums_by_text(text, count=5, offset=0) async

Search albums by text/query. Album - artists' album/collection of songs. In obj context - same as 'Playlist'.

Parameters:

Name Type Description Default
text str

Text of query. Can be title of album, name of artist, etc.

required
count int

Count of resulting playlists (for VK API: default = 50, max = 100).

5
offset int

Set offset for result. For example, count = 100, offset = 100 -> 101-200.

0

Returns:

Type Description
List[Playlist]

list[Playlist]: List of albums.

Get popular songs. (Be careful, it always returns less than count)

Parameters:

Name Type Description Default
count int

Count of resulting songs (for VK API: default = 50, max = 300).

50
offset int

Set offset for result. For example, count = 100, offset = 100 -> 101-200.

0

Returns:

Type Description
List[Song]

list[Song]: List of songs.

get_recommendations(user_id=None, song_id=None, count=50, offset=0) async

Get recommendations by user id or song id. (Be careful, it always returns less than count)

Parameters:

Name Type Description Default
user_id str | int

VK user id. (NOT USERNAME! vk.com/id*).

None
song_id str | int

VK song id. (track_id from Song).

None
count int

Count of resulting songs (for VK API: default = 50, max = 300).

50
offset int

Set offset for result. For example, count = 100, offset = 100 -> 101-200.

0

Returns:

Type Description
List[Song]

list[Song]: List of songs.

save_music(song, overwrite=False) async classmethod

Save song to '{workDirectory}/Music/{song name}.mp3'.

Parameters:

Name Type Description Default
song Song

'Song' instance obtained from 'ServiceAsync' methods.

required
overwrite bool

Overwrite file if it exists

False

Returns:

Name Type Description
str Optional[str]

relative path of downloaded music.