vk music module
This commit is contained in:
parent
c677718c78
commit
ed91a1f5ae
@ -1,7 +1,13 @@
|
|||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
|
|
||||||
from .models import Feed
|
from .models import Feed
|
||||||
from feeds.modules.echo import EchoFeedModuleConfig
|
from feeds.modules import EchoFeedModuleConfig, DankMemesFeedModuleConfig, ShittyWatercolourFeedModuleConfig,\
|
||||||
|
VKFeedModuleConfig, VKMusicFeedModuleConfig, WPComicFeedModuleConfig
|
||||||
|
|
||||||
admin.site.register(Feed)
|
admin.site.register(Feed)
|
||||||
admin.site.register(EchoFeedModuleConfig)
|
admin.site.register(EchoFeedModuleConfig)
|
||||||
|
admin.site.register(DankMemesFeedModuleConfig)
|
||||||
|
admin.site.register(ShittyWatercolourFeedModuleConfig)
|
||||||
|
admin.site.register(VKFeedModuleConfig)
|
||||||
|
admin.site.register(VKMusicFeedModuleConfig)
|
||||||
|
admin.site.register(WPComicFeedModuleConfig)
|
||||||
|
@ -28,6 +28,7 @@ class FeedsAppConfigForm(ConfigForm):
|
|||||||
title = 'Feeds'
|
title = 'Feeds'
|
||||||
|
|
||||||
feed_bot_token = forms.CharField(required=True)
|
feed_bot_token = forms.CharField(required=True)
|
||||||
|
russian_proxy_string = forms.CharField()
|
||||||
vk_username = forms.CharField(required=True)
|
vk_username = forms.CharField(required=True)
|
||||||
vk_password = forms.CharField(required=True)
|
vk_password = forms.CharField(required=True)
|
||||||
vk_auth = forms.CharField(required=False, widget=forms.Textarea())
|
vk_auth = forms.CharField(required=False, widget=forms.Textarea())
|
||||||
|
23
feeds/migrations/0004_vkmusicfeedmoduleconfig.py
Normal file
23
feeds/migrations/0004_vkmusicfeedmoduleconfig.py
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
# Generated by Django 2.1.5 on 2019-01-26 06:36
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('feeds', '0003_dankmemesfeedmoduleconfig_shittywatercolourfeedmoduleconfig_wpcomicfeedmoduleconfig'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='VKMusicFeedModuleConfig',
|
||||||
|
fields=[
|
||||||
|
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('owner_id', models.IntegerField()),
|
||||||
|
],
|
||||||
|
options={
|
||||||
|
'abstract': False,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
]
|
@ -32,6 +32,9 @@ class Feed(models.Model):
|
|||||||
self.save()
|
self.save()
|
||||||
execute_feed.delay(self.pk)
|
execute_feed.delay(self.pk)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.title
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
unique_together = ('config_type', 'config_id')
|
unique_together = ('config_type', 'config_id')
|
||||||
|
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
from .echo import EchoFeedModuleConfig
|
from .echo import EchoFeedModuleConfig
|
||||||
from .vk_feed import VKFeedModuleConfig
|
from .vk_feed import VKFeedModuleConfig
|
||||||
|
from .vk_music import VKMusicFeedModuleConfig
|
||||||
from .dank_memes import DankMemesFeedModuleConfig
|
from .dank_memes import DankMemesFeedModuleConfig
|
||||||
from .shitty_watercolour import ShittyWatercolourFeedModuleConfig
|
from .shitty_watercolour import ShittyWatercolourFeedModuleConfig
|
||||||
from .wp_comic import WPComicFeedModuleConfig
|
from .wp_comic import WPComicFeedModuleConfig
|
||||||
|
|
||||||
FEED_MODULES = [EchoFeedModuleConfig, VKFeedModuleConfig, DankMemesFeedModuleConfig, ShittyWatercolourFeedModuleConfig,
|
FEED_MODULES = [EchoFeedModuleConfig, VKFeedModuleConfig, DankMemesFeedModuleConfig, ShittyWatercolourFeedModuleConfig,
|
||||||
WPComicFeedModuleConfig]
|
WPComicFeedModuleConfig]
|
||||||
|
|
||||||
|
70
feeds/modules/vk_music.py
Normal file
70
feeds/modules/vk_music.py
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
import os
|
||||||
|
import tempfile
|
||||||
|
|
||||||
|
import requests
|
||||||
|
import sentry_sdk
|
||||||
|
from django.db import models
|
||||||
|
from djconfig import config
|
||||||
|
from telebot import TeleBot
|
||||||
|
from telebot.types import InputMediaPhoto
|
||||||
|
from vk_api import VkApi
|
||||||
|
from vk_api.audio import VkAudio
|
||||||
|
|
||||||
|
from feeds.models import FeedModuleConfig
|
||||||
|
from feeds.utils import DatabaseConfig
|
||||||
|
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
MODULE_NAME = 'VK music'
|
||||||
|
|
||||||
|
def execute(self, bot: TeleBot, chat_id, last_id):
|
||||||
|
config._reload_maybe()
|
||||||
|
|
||||||
|
if last_id is None:
|
||||||
|
last_id = 0
|
||||||
|
|
||||||
|
vk_session = VkApi(login=config.vk_username, password=config.vk_password, config=DatabaseConfig,
|
||||||
|
api_version='5.60')
|
||||||
|
if config.russian_proxy_string:
|
||||||
|
vk_session.http.proxies = {
|
||||||
|
'http': config.russian_proxy_string,
|
||||||
|
'https': config.russian_proxy_string,
|
||||||
|
}
|
||||||
|
vk_session.auth()
|
||||||
|
vk_audio = VkAudio(vk_session)
|
||||||
|
lst = []
|
||||||
|
for audio in vk_audio.get_iter(self.owner_id):
|
||||||
|
if audio['id'] > last_id:
|
||||||
|
lst = [audio] + lst
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
for track in reversed(lst):
|
||||||
|
f = get_file(track['url'], vk_session.http)
|
||||||
|
bot.send_audio(chat_id, f, duration=track['duration'], performer=track['artist'],
|
||||||
|
title=track['title'])
|
||||||
|
last_id = track['id']
|
||||||
|
return last_id
|
@ -15,6 +15,7 @@ enum34==1.1.6
|
|||||||
idna==2.8
|
idna==2.8
|
||||||
kombu==4.2.2.post1
|
kombu==4.2.2.post1
|
||||||
psycopg2-binary==2.7.6.1
|
psycopg2-binary==2.7.6.1
|
||||||
|
PySocks==1.6.8
|
||||||
pyTelegramBotAPI==3.6.6
|
pyTelegramBotAPI==3.6.6
|
||||||
pytz==2018.9
|
pytz==2018.9
|
||||||
PyYAML==3.13
|
PyYAML==3.13
|
||||||
|
Loading…
Reference in New Issue
Block a user