diff --git a/examples/bot_longpoll.py b/examples/bot_longpoll.py index 63ae410b..7b50bd95 100644 --- a/examples/bot_longpoll.py +++ b/examples/bot_longpoll.py @@ -23,8 +23,6 @@ def main(): print(event.obj.from_id) print('Текст:', event.obj.text) - print() - elif event.type == VkBotEventType.MESSAGE_REPLY: print('Новое сообщение:') @@ -33,8 +31,6 @@ def main(): print(event.obj.peer_id) print('Текст:', event.obj.text) - print() - elif event.type == VkBotEventType.MESSAGE_TYPING_STATE: print('Печатает ', end='') @@ -43,23 +39,18 @@ def main(): print('для ', end='') print(event.obj.to_id) - print() - elif event.type == VkBotEventType.GROUP_JOIN: print(event.obj.user_id, end=' ') print('Вступил в группу!') - print() - elif event.type == VkBotEventType.GROUP_LEAVE: print(event.obj.user_id, end=' ') print('Покинул группу!') - print() - else: print(event.type) - print() + + print() if __name__ == '__main__': diff --git a/examples/get_album_audio.py b/examples/get_album_audio.py index 0b11a9e8..5409ad35 100644 --- a/examples/get_album_audio.py +++ b/examples/get_album_audio.py @@ -29,7 +29,7 @@ def main(): tracks = vkaudio.get(album_id=albums[0]['id']) for n, track in enumerate(tracks, 1): - print('{}. {} {}'.format(n, track['title'], track['url'])) + print(f"{n}. {track['title']} {track['url']}") if __name__ == '__main__': diff --git a/examples/get_all_audio.py b/examples/get_all_audio.py index 9f661030..4ed42c2b 100644 --- a/examples/get_all_audio.py +++ b/examples/get_all_audio.py @@ -26,17 +26,17 @@ def main(): # Составляем рейтинг первых 15 print('Top 15:') for artist, tracks in artists.most_common(15): - print('{} - {} tracks'.format(artist, tracks)) + print(f'{artist} - {tracks} tracks') # Ищем треки самого популярного most_common_artist = artists.most_common(1)[0][0] - print('\nSearching for {}:'.format(most_common_artist)) + print(f'\nSearching for {most_common_artist}:') tracks = vkaudio.search(q=most_common_artist, count=10) for n, track in enumerate(tracks, 1): - print('{}. {} {}'.format(n, track['title'], track['url'])) + print(f"{n}. {track['title']} {track['url']}") if __name__ == '__main__': diff --git a/examples/messages_bot/user_messages_bot.py b/examples/messages_bot/user_messages_bot.py index 0493367d..804cde9d 100644 --- a/examples/messages_bot/user_messages_bot.py +++ b/examples/messages_bot/user_messages_bot.py @@ -36,7 +36,7 @@ def main(): for event in longpoll.listen(): if event.type == VkEventType.MESSAGE_NEW and event.to_me and event.text: - print('id{}: "{}"'.format(event.user_id, event.text), end=' ') + print(f'id{event.user_id}: "{event.text}"', end=' ') response = session.get( 'http://api.duckduckgo.com/', @@ -64,9 +64,7 @@ def main(): image = session.get(image_url, stream=True) photo = upload.photo_messages(photos=image.raw)[0] - attachments.append( - 'photo{}_{}'.format(photo['owner_id'], photo['id']) - ) + attachments.append(f"photo{photo['owner_id']}_{photo['id']}") vk.messages.send( user_id=event.user_id, diff --git a/examples/requests_pool.py b/examples/requests_pool.py index 7a4edd77..3c6d7f94 100644 --- a/examples/requests_pool.py +++ b/examples/requests_pool.py @@ -106,4 +106,4 @@ def main(): if __name__ == '__main__': - main() + main() \ No newline at end of file diff --git a/examples/upload_photo.py b/examples/upload_photo.py index b3cc15a0..ee1c8c45 100644 --- a/examples/upload_photo.py +++ b/examples/upload_photo.py @@ -25,9 +25,7 @@ def main(): group_id=74030368 ) - vk_photo_url = 'https://vk.com/photo{}_{}'.format( - photo[0]['owner_id'], photo[0]['id'] - ) + vk_photo_url = f"https://vk.com/photo{photo[0]['owner_id']}_{photo[0]['id']}" print(photo, '\nLink: ', vk_photo_url) diff --git a/setup.py b/setup.py index 80e4f200..90770b64 100644 --- a/setup.py +++ b/setup.py @@ -19,31 +19,23 @@ setup( name='vk_api', version=version, - author='python273', author_email='vk_api@python273.pw', - description=( 'Python модуль для создания скриптов для социальной сети ' 'Вконтакте (vk.com API wrapper)' ), long_description=long_description, long_description_content_type='text/markdown', - url='https://github.com/python273/vk_api', - download_url='https://github.com/python273/vk_api/archive/v{}.zip'.format( - version - ), - + download_url=f'https://github.com/python273/vk_api/archive/v{version}.zip', license='Apache License, Version 2.0, see LICENSE file', - packages=['vk_api', 'jconfig'], install_requires=['requests'], extras_require={ 'vkstreaming': ['websocket-client'], 'vkaudio': ['beautifulsoup4'], }, - classifiers=[ 'License :: OSI Approved :: Apache Software License', 'Operating System :: OS Independent', @@ -54,5 +46,5 @@ 'Programming Language :: Python :: 3.8', 'Programming Language :: Python :: 3.9', 'Programming Language :: Python :: 3.10', - ] + ], ) diff --git a/vk_api/audio.py b/vk_api/audio.py index d278edac..058335a1 100644 --- a/vk_api/audio.py +++ b/vk_api/audio.py @@ -123,9 +123,7 @@ def get_iter(self, owner_id=None, album_id=None, access_hash=None): if not response['data'][0]: raise AccessDenied( - 'You don\'t have permissions to browse {}\'s albums'.format( - owner_id - ) + f"You don\'t have permissions to browse {owner_id}\'s albums" ) ids = scrap_ids( @@ -134,16 +132,12 @@ def get_iter(self, owner_id=None, album_id=None, access_hash=None): if not ids: break - tracks = scrap_tracks( + yield from scrap_tracks( ids, self.user_id, self._vk.http, - convert_m3u8_links=self.convert_m3u8_links + convert_m3u8_links=self.convert_m3u8_links, ) - - for i in tracks: - yield i - if response['data'][0]['hasMore']: offset += offset_diff else: @@ -172,20 +166,14 @@ def get_albums_iter(self, owner_id=None): while True: response = self._vk.http.get( - 'https://m.vk.com/audio?act=audio_playlists{}'.format( - owner_id - ), - params={ - 'offset': offset - }, - allow_redirects=False + f'https://m.vk.com/audio?act=audio_playlists{owner_id}', + params={'offset': offset}, + allow_redirects=False, ) if not response.text: raise AccessDenied( - 'You don\'t have permissions to browse {}\'s albums'.format( - owner_id - ) + f"You don\'t have permissions to browse {owner_id}\'s albums" ) albums = scrap_albums(response.text) @@ -193,9 +181,7 @@ def get_albums_iter(self, owner_id=None): if not albums: break - for i in albums: - yield i - + yield from albums offset += ALBUMS_PER_USER_PAGE def get_albums(self, owner_id=None): @@ -232,9 +218,7 @@ def search_user(self, owner_id=None, q=''): if not json_response['payload'][1]: raise AccessDenied( - 'You don\'t have permissions to browse {}\'s audio'.format( - owner_id - ) + f"You don\'t have permissions to browse {owner_id}\'s audio" ) if json_response['payload'][1][1]['playlists']: @@ -356,16 +340,12 @@ def search_iter(self, q, offset=0): if offset_left < offset: ids = ids[offset - offset_left:] - tracks = scrap_tracks( + yield from scrap_tracks( ids, self.user_id, convert_m3u8_links=self.convert_m3u8_links, - http=self._vk.http + http=self._vk.http, ) - - for track in tracks: - yield track - offset_left += len(ids) response = self._vk.http.post( @@ -404,16 +384,12 @@ def get_updates_iter(self): if not ids: break - tracks = scrap_tracks( + yield from scrap_tracks( ids, self.user_id, convert_m3u8_links=self.convert_m3u8_links, - http=self._vk.http + http=self._vk.http, ) - - for track in tracks: - yield track - if len(updates) < 11: break @@ -447,16 +423,13 @@ def get_popular_iter(self, offset=0): json_response['sectionData']['recoms']['playlist']['list'] ) - tracks = scrap_tracks( + yield from scrap_tracks( ids[offset:] if offset else ids, self.user_id, convert_m3u8_links=self.convert_m3u8_links, - http=self._vk.http + http=self._vk.http, ) - for track in tracks: - yield track - def get_news_iter(self, offset=0): """ Искать популярные аудиозаписи (генератор) @@ -479,16 +452,12 @@ def get_news_iter(self, offset=0): ) if offset_left + len(ids) >= offset: - tracks = scrap_tracks( - ids if offset_left >= offset else ids[offset - offset_left:], + yield from scrap_tracks( + ids if offset_left >= offset else ids[offset - offset_left :], self.user_id, convert_m3u8_links=self.convert_m3u8_links, - http=self._vk.http + http=self._vk.http, ) - - for track in tracks: - yield track - offset_left += len(ids) while True: @@ -511,16 +480,12 @@ def get_news_iter(self, offset=0): break if offset_left + len(ids) >= offset: - tracks = scrap_tracks( - ids if offset_left >= offset else ids[offset - offset_left:], + yield from scrap_tracks( + ids if offset_left >= offset else ids[offset - offset_left :], self.user_id, convert_m3u8_links=self.convert_m3u8_links, - http=self._vk.http + http=self._vk.http, ) - - for track in tracks: - yield track - offset_left += len(ids) def get_audio_by_id(self, owner_id, audio_id): @@ -530,8 +495,7 @@ def get_audio_by_id(self, owner_id, audio_id): :param audio_id: ID аудио """ response = self._vk.http.get( - 'https://m.vk.com/audio{}_{}'.format(owner_id, audio_id), - allow_redirects=False + f'https://m.vk.com/audio{owner_id}_{audio_id}', allow_redirects=False ) ids = scrap_ids_from_html( @@ -543,9 +507,8 @@ def get_audio_by_id(self, owner_id, audio_id): ids, self.user_id, http=self._vk.http, - convert_m3u8_links=self.convert_m3u8_links + convert_m3u8_links=self.convert_m3u8_links, ) - if track: return next(track) else: @@ -557,34 +520,26 @@ def get_post_audio(self, owner_id, post_id): :param owner_id: ID владельца (отрицательные значения для групп) :param post_id: ID поста """ - response = self._vk.http.get( - 'https://m.vk.com/wall{}_{}'.format(owner_id, post_id) - ) + response = self._vk.http.get(f'https://m.vk.com/wall{owner_id}_{post_id}') ids = scrap_ids_from_html( response.text, filter_root_el={'class': 'audios_list'} ) - tracks = scrap_tracks( + return scrap_tracks( ids, self.user_id, http=self._vk.http, - convert_m3u8_links=self.convert_m3u8_links + convert_m3u8_links=self.convert_m3u8_links, ) - return tracks - def follow_user(self, user_id): data = self._vk.http.get(f"https://vk.com/audios{user_id}") user_hash = RE_USER_AUDIO_HASH.search(data.text) if user_hash is None: - raise AccessDenied( - 'You don\'t have permissions to browse {}\'s audio'.format( - user_id - ) - ) + raise AccessDenied(f"You don\'t have permissions to browse {user_id}\'s audio") user_hash = user_hash.groups()[1] response = self._vk.http.post( @@ -596,20 +551,14 @@ def follow_user(self, user_id): 'hash': user_hash, } ) - json_response = json.loads(response.text.replace('