vkpymusic.Service
A class for working with VK API.
Attributes:
Name | Type | Description |
---|---|---|
user_agent |
str
|
User agent string. |
__token |
str
|
Token for VK API. |
logger |
Logger
|
The logger for class. |
Example usage:
>>> service = Service.parse_config()
>>> songs = service.search_songs_by_text("Imagine Dragons")
>>> for song in songs:
... Service.save_music(song)
Functions
__init__(user_agent, token)
Initializes a Service object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
user_agent
|
str
|
User agent string. |
required |
token
|
str
|
Token for VK API. |
required |
set_logger(logger)
classmethod
Set logger for class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
logger
|
Logger
|
Logger. |
required |
parse_config(filename='config_vk.ini')
classmethod
Create an instance of Service 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 value = "config_vk.ini"). |
'config_vk.ini'
|
check_token(token)
classmethod
Check token for VK API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token
|
str
|
Token for VK API. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if token is valid, False otherwise. |
is_token_valid()
Check token for VK API.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if token is valid, False otherwise. |
get_user_info()
Get user info by token.
Returns:
Name | Type | Description |
---|---|---|
UserInfo |
Optional[UserInfo]
|
Instance of 'UserInfo'. |
get_count_by_user_id(user_id)
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)
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)
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)
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)
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)
Get playlist by owner/user id.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
user_id
|
str or 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)
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)
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(count=50, offset=0)
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 = 500). |
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)
Get recommendations by user id or song id. (Be careful, it always returns less than count)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
user_id
|
int
|
VK user id. (NOT USERNAME! vk.com/id*). |
None
|
song_id
|
int
|
VK song id. |
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)
classmethod
Save song to '{workDirectory}/Music/{song name}.mp3'.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
song
|
Song
|
'Song' instance obtained from 'Service' methods. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
Optional[str]
|
relative path of downloaded music. |