add anticaptcha support for vk feeds

This commit is contained in:
bakatrouble 2019-02-08 17:04:37 +03:00
parent c1679c52a9
commit 5817496228
4 changed files with 18 additions and 4 deletions

View File

@ -32,3 +32,4 @@ class FeedsAppConfigForm(ConfigForm):
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())
anticaptcha_token = forms.CharField()

View File

@ -1,17 +1,19 @@
import os import os
import tempfile import tempfile
from io import BytesIO
import requests import requests
import sentry_sdk import sentry_sdk
from django.db import models from django.db import models
from djconfig import config from djconfig import config
from python_anticaptcha import AnticaptchaClient, ImageToTextTask
from telebot import TeleBot from telebot import TeleBot
from telebot.types import InputMediaPhoto from telebot.types import InputMediaPhoto
from vk_api import VkApi from vk_api import VkApi
from vk_api.audio import VkAudio from vk_api.audio import VkAudio
from feeds.models import FeedModuleConfig from feeds.models import FeedModuleConfig
from feeds.utils import DatabaseConfig from feeds.utils import DatabaseConfig, captcha_handler
def get_vk_photo(attachment): def get_vk_photo(attachment):
@ -47,7 +49,7 @@ class VKFeedModuleConfig(FeedModuleConfig):
last_id = 0 last_id = 0
vk_session = VkApi(login=config.vk_username, password=config.vk_password, config=DatabaseConfig, 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_session.auth()
vk_audio = VkAudio(vk_session) vk_audio = VkAudio(vk_session)
vk = vk_session.get_api() vk = vk_session.get_api()

View File

@ -11,7 +11,7 @@ from vk_api import VkApi
from vk_api.audio import VkAudio from vk_api.audio import VkAudio
from feeds.models import FeedModuleConfig from feeds.models import FeedModuleConfig
from feeds.utils import DatabaseConfig from feeds.utils import DatabaseConfig, captcha_handler
def get_vk_photo(attachment): def get_vk_photo(attachment):
@ -48,7 +48,7 @@ class VKMusicFeedModuleConfig(FeedModuleConfig):
last_id = 0 last_id = 0
vk_session = VkApi(login=config.vk_username, password=config.vk_password, config=DatabaseConfig, 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: if config.russian_proxy_string:
vk_session.http.proxies = { vk_session.http.proxies = {
'http': config.russian_proxy_string, 'http': config.russian_proxy_string,

View File

@ -1,7 +1,11 @@
from io import BytesIO
import requests
import yaml import yaml
from django.views.generic import TemplateView from django.views.generic import TemplateView
from djconfig import config from djconfig import config
from jconfig.base import BaseConfig from jconfig.base import BaseConfig
from python_anticaptcha import AnticaptchaClient, ImageToTextTask
from yaml.parser import ParserError from yaml.parser import ParserError
from cabinet.utils import CabinetViewMixin from cabinet.utils import CabinetViewMixin
@ -58,3 +62,10 @@ class DatabaseConfig(BaseConfig):
form = FeedsAppConfigForm(data={'vk_auth': yaml.dump(self._settings)}) form = FeedsAppConfigForm(data={'vk_auth': yaml.dump(self._settings)})
form.is_valid() form.is_valid()
form.save() 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()