From 58174962284abd0065955bc0ddcc836cc7c7a60e Mon Sep 17 00:00:00 2001 From: bakatrouble Date: Fri, 8 Feb 2019 17:04:37 +0300 Subject: [PATCH] add anticaptcha support for vk feeds --- feeds/forms.py | 1 + feeds/modules/vk_feed.py | 6 ++++-- feeds/modules/vk_music.py | 4 ++-- feeds/utils.py | 11 +++++++++++ 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/feeds/forms.py b/feeds/forms.py index 76fb2df..c5b0c89 100644 --- a/feeds/forms.py +++ b/feeds/forms.py @@ -32,3 +32,4 @@ class FeedsAppConfigForm(ConfigForm): vk_username = forms.CharField(required=True) vk_password = forms.CharField(required=True) vk_auth = forms.CharField(required=False, widget=forms.Textarea()) + anticaptcha_token = forms.CharField() diff --git a/feeds/modules/vk_feed.py b/feeds/modules/vk_feed.py index 532faf4..af329e2 100644 --- a/feeds/modules/vk_feed.py +++ b/feeds/modules/vk_feed.py @@ -1,17 +1,19 @@ import os import tempfile +from io import BytesIO import requests import sentry_sdk from django.db import models from djconfig import config +from python_anticaptcha import AnticaptchaClient, ImageToTextTask 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 +from feeds.utils import DatabaseConfig, captcha_handler def get_vk_photo(attachment): @@ -47,7 +49,7 @@ class VKFeedModuleConfig(FeedModuleConfig): last_id = 0 vk_session = VkApi(login=config.vk_username, password=config.vk_password, config=DatabaseConfig, - api_version='5.60') + api_version='5.60', captcha_handler=captcha_handler) vk_session.auth() vk_audio = VkAudio(vk_session) vk = vk_session.get_api() diff --git a/feeds/modules/vk_music.py b/feeds/modules/vk_music.py index c935fd5..a2c7a84 100644 --- a/feeds/modules/vk_music.py +++ b/feeds/modules/vk_music.py @@ -11,7 +11,7 @@ from vk_api import VkApi from vk_api.audio import VkAudio from feeds.models import FeedModuleConfig -from feeds.utils import DatabaseConfig +from feeds.utils import DatabaseConfig, captcha_handler def get_vk_photo(attachment): @@ -48,7 +48,7 @@ class VKMusicFeedModuleConfig(FeedModuleConfig): last_id = 0 vk_session = VkApi(login=config.vk_username, password=config.vk_password, config=DatabaseConfig, - api_version='5.60') + api_version='5.60', captcha_handler=captcha_handler) if config.russian_proxy_string: vk_session.http.proxies = { 'http': config.russian_proxy_string, diff --git a/feeds/utils.py b/feeds/utils.py index f1ec84a..c0f9ebe 100644 --- a/feeds/utils.py +++ b/feeds/utils.py @@ -1,7 +1,11 @@ +from io import BytesIO + +import requests import yaml from django.views.generic import TemplateView from djconfig import config from jconfig.base import BaseConfig +from python_anticaptcha import AnticaptchaClient, ImageToTextTask from yaml.parser import ParserError from cabinet.utils import CabinetViewMixin @@ -58,3 +62,10 @@ class DatabaseConfig(BaseConfig): form = FeedsAppConfigForm(data={'vk_auth': yaml.dump(self._settings)}) form.is_valid() form.save() + + +def captcha_handler(captcha): + client = AnticaptchaClient(config.anticaptcha_token) + job = client.createTask(ImageToTextTask(BytesIO(requests.get(captcha.get_url()).content))) + job.join() + return job.get_captcha_text()