From fc08abd83e343cf5f4fa3679e67cfc7caa2b9218 Mon Sep 17 00:00:00 2001 From: bakatrouble Date: Sun, 27 Oct 2019 02:03:51 +0300 Subject: [PATCH] fix vk audio --- feeds/modules/utils.py | 38 ++++++++++++++++++++++++++++++++++++++ feeds/modules/vk_feed.py | 22 ++-------------------- feeds/modules/vk_music.py | 25 ++----------------------- 3 files changed, 42 insertions(+), 43 deletions(-) create mode 100644 feeds/modules/utils.py diff --git a/feeds/modules/utils.py b/feeds/modules/utils.py new file mode 100644 index 0000000..0fb23b9 --- /dev/null +++ b/feeds/modules/utils.py @@ -0,0 +1,38 @@ +import os +import subprocess +import tempfile +from io import BytesIO +from uuid import uuid4 + +import requests + + +def get_vk_audio(m3u8): + with tempfile.TemporaryDirectory() as d: + fname = '{}.mp3'.format(uuid4()) + path = os.path.join(d, fname) + subprocess.check_output(['ffmpeg', '-i', m3u8, path]) + with open(path, 'rb') as f: + tf = BytesIO(f.read()) + tf.name = fname + tf.seek(0) + return tf + + +def get_vk_photo(attachment): + for size in (2560, 1280, 807, 604, 130, 75): + if f'photo_{size}' in attachment: + return attachment[f'photo_{size}'] + return None + + +def get_file(url): + fname = '?'.join(url.split('?')[:-1]) + extension = os.path.basename(fname).split('.')[-1] + f = tempfile.NamedTemporaryFile(suffix=f'.{extension}' if extension else None) + r = requests.get(url, stream=True) + for chunk in r.iter_content(1024 * 1024): + if chunk: + f.write(chunk) + f.seek(0) + return f diff --git a/feeds/modules/vk_feed.py b/feeds/modules/vk_feed.py index af329e2..197f8e5 100644 --- a/feeds/modules/vk_feed.py +++ b/feeds/modules/vk_feed.py @@ -13,28 +13,10 @@ from vk_api import VkApi from vk_api.audio import VkAudio from feeds.models import FeedModuleConfig +from feeds.modules.utils import get_vk_photo, get_vk_audio from feeds.utils import DatabaseConfig, captcha_handler -def get_vk_photo(attachment): - for size in (2560, 1280, 807, 604, 130, 75): - if f'photo_{size}' in attachment: - return attachment[f'photo_{size}'] - return None - - -def get_file(url): - fname = '?'.join(url.split('?')[:-1]) - extension = os.path.basename(fname).split('.')[-1] - f = tempfile.NamedTemporaryFile(suffix=f'.{extension}' if extension else None) - r = requests.get(url, stream=True) - for chunk in r.iter_content(1024 * 1024): - if chunk: - f.write(chunk) - f.seek(0) - return f - - class VKFeedModuleConfig(FeedModuleConfig): owner_id = models.IntegerField() send_text = models.BooleanField(default=True) @@ -71,7 +53,7 @@ class VKFeedModuleConfig(FeedModuleConfig): bot.send_media_group(chat_id, [InputMediaPhoto(photo) for photo in photos]) for a in post['attachments']: if 'audio' in a: - f = get_file(vk_audio.get_audio_by_id(a['audio']['owner_id'], a['audio']['id'])) + f = get_vk_audio(vk_audio.get_audio_by_id(a['audio']['owner_id'], a['audio']['id'])) bot.send_audio(chat_id, f, None, a['audio'].get('artist'), a['audio'].get('title')) if self.send_links: bot.send_message(chat_id, f"https://vk.com/wall{post['owner_id']}_{post['id']}", diff --git a/feeds/modules/vk_music.py b/feeds/modules/vk_music.py index 4fc300f..9da5a3f 100644 --- a/feeds/modules/vk_music.py +++ b/feeds/modules/vk_music.py @@ -11,31 +11,10 @@ from vk_api import VkApi from vk_api.audio import VkAudio from feeds.models import FeedModuleConfig +from feeds.modules.utils import get_vk_audio from feeds.utils import DatabaseConfig, captcha_handler -def get_vk_photo(attachment): - for size in (2560, 1280, 807, 604, 130, 75): - if f'photo_{size}' in attachment: - return attachment[f'photo_{size}'] - return None - - -def get_file(url, session=None): - if session is None: - session = requests - - fname = '?'.join(url.split('?')[:-1]) - extension = os.path.basename(fname).split('.')[-1] - f = tempfile.NamedTemporaryFile(suffix=f'.{extension}' if extension else None) - r = session.get(url, stream=True) - for chunk in r.iter_content(1024 * 1024): - if chunk: - f.write(chunk) - f.seek(0) - return f - - class VKMusicFeedModuleConfig(FeedModuleConfig): owner_id = models.IntegerField() @@ -63,7 +42,7 @@ class VKMusicFeedModuleConfig(FeedModuleConfig): else: break for track in lst: - f = get_file(track['url'], vk_session.http) + f = get_vk_audio(track['url']) bot.send_audio(chat_id, f, duration=track['duration'], performer=track['artist'], title=track['title'], timeout=40) yield track['id']