diff --git a/feeds/admin.py b/feeds/admin.py index 9271d17..cd9e061 100644 --- a/feeds/admin.py +++ b/feeds/admin.py @@ -1,7 +1,13 @@ from django.contrib import admin 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(EchoFeedModuleConfig) +admin.site.register(DankMemesFeedModuleConfig) +admin.site.register(ShittyWatercolourFeedModuleConfig) +admin.site.register(VKFeedModuleConfig) +admin.site.register(VKMusicFeedModuleConfig) +admin.site.register(WPComicFeedModuleConfig) diff --git a/feeds/forms.py b/feeds/forms.py index b4e9389..76fb2df 100644 --- a/feeds/forms.py +++ b/feeds/forms.py @@ -28,6 +28,7 @@ class FeedsAppConfigForm(ConfigForm): title = 'Feeds' feed_bot_token = forms.CharField(required=True) + russian_proxy_string = forms.CharField() vk_username = forms.CharField(required=True) vk_password = forms.CharField(required=True) vk_auth = forms.CharField(required=False, widget=forms.Textarea()) diff --git a/feeds/migrations/0004_vkmusicfeedmoduleconfig.py b/feeds/migrations/0004_vkmusicfeedmoduleconfig.py new file mode 100644 index 0000000..8b4793c --- /dev/null +++ b/feeds/migrations/0004_vkmusicfeedmoduleconfig.py @@ -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, + }, + ), + ] diff --git a/feeds/models.py b/feeds/models.py index 0c1092d..0a1b1ff 100644 --- a/feeds/models.py +++ b/feeds/models.py @@ -32,6 +32,9 @@ class Feed(models.Model): self.save() execute_feed.delay(self.pk) + def __str__(self): + return self.title + class Meta: unique_together = ('config_type', 'config_id') diff --git a/feeds/modules/__init__.py b/feeds/modules/__init__.py index cd51d5c..1167ae2 100644 --- a/feeds/modules/__init__.py +++ b/feeds/modules/__init__.py @@ -1,8 +1,10 @@ from .echo import EchoFeedModuleConfig from .vk_feed import VKFeedModuleConfig +from .vk_music import VKMusicFeedModuleConfig from .dank_memes import DankMemesFeedModuleConfig from .shitty_watercolour import ShittyWatercolourFeedModuleConfig from .wp_comic import WPComicFeedModuleConfig FEED_MODULES = [EchoFeedModuleConfig, VKFeedModuleConfig, DankMemesFeedModuleConfig, ShittyWatercolourFeedModuleConfig, WPComicFeedModuleConfig] + diff --git a/feeds/modules/vk_music.py b/feeds/modules/vk_music.py new file mode 100644 index 0000000..bfdcf8f --- /dev/null +++ b/feeds/modules/vk_music.py @@ -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 diff --git a/requirements.txt b/requirements.txt index aa67074..7778f06 100644 --- a/requirements.txt +++ b/requirements.txt @@ -15,6 +15,7 @@ enum34==1.1.6 idna==2.8 kombu==4.2.2.post1 psycopg2-binary==2.7.6.1 +PySocks==1.6.8 pyTelegramBotAPI==3.6.6 pytz==2018.9 PyYAML==3.13