Pull to refresh
12
0
Артем Коломацкий @Kolomatskiy

Python developer

Send message
def create_playlist_file(self, user_id: Union[int, str]) -> str:
        # Probably it is better to separate it all to different methods

        all_music_link = f'https://api.vk.com/method/audio.get?owner_id={user_id}&access_token={self.token}&v=5.85'
        all_music = requests.get(all_music_link).json()['response']['items']
        song_ids = []

        # Create unique song ids
        for song in all_music:
            if not song.get('access_key'):
                continue
            access_key = song['access_key']
            song_id = song['id']
            song_ids.append(f'{user_id}_{song_id}_{access_key}')

        splited_song_ids = self.group(song_ids, 200)  # Grouping ids because of VK API limits for songs in one request

        result = OrderedDict()  # OrderedDict here and below is to specify order of songs like in a real playlist

        for chunk in splited_song_ids:
            # Making one request to get data for each 200 songs
            ids_for_request = ','.join(chunk)
            data = {'audios': ids_for_request, 'access_token': self.token, 'v': '5.85'}
            songs_with_urls = requests.post('https://api.vk.com/method/audio.getById', data=data).json()['response']

            sleep(0.5)  # Excepting VK API rate limit

            # Getting only needed data for each song to create a beautiful playlist file
            for song in songs_with_urls:
                id_ = song['id']
                author = song.get('artist')
                title = song.get('title')
                src = song.get('url')
                if not src:
                    is_blocked = True
                else:
                    is_blocked = False
                needed_data = OrderedDict(author=author, title=title, src=src, is_blocked=is_blocked)
                result[id_] = needed_data
Мы вот получаем один обычный токен через авторизацию по логину-паролю под видом одного из официальных приложений и по этому токену напрямую используем старые методы аудио-апи. И никаких вытаскиваний GMS не нужно.

Information

Rating
Does not participate
Works in
Registered
Activity