fix vk audio

This commit is contained in:
bakatrouble 2019-10-27 02:03:51 +03:00
parent 199cbb487a
commit fc08abd83e
3 changed files with 42 additions and 43 deletions

38
feeds/modules/utils.py Normal file
View File

@ -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

View File

@ -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']}",

View File

@ -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']