V
V
Vladimir Vladimirovich2020-07-19 14:57:18
Python
Vladimir Vladimirovich, 2020-07-19 14:57:18

Heroku bot gives an error, although the bot works in the console, what should I do?

Hello everyone, I uploaded the bot to Heroku
Everything went well, there were no errors
Here is the bot code:

import logging
import asyncio
from bs4 import BeautifulSoup as BS
import requests
from aiogram import Bot, Dispatcher, executor, types
import config
import json
import re

logging.basicConfig(level=logging.INFO)
bot = Bot(token = config.token)
dp = Dispatcher(bot)


@dp.message_handler(commands=['start'])
async def send_welcome(message: types.Message):
    await bot.send_message(message.from_user.id , 'Отправь мне ссылку в формате "https://www.instagram.com", "https://www.tiktok.com" или "https://vm.tiktok.com", а я тебе отправлю фотографию :)', disable_web_page_preview='true')

@dp.message_handler()
async def echo(message: types.Message):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'}
    JS_SHARED_DATA_PATTERN = re.compile('window._sharedData = ({.+});')
    URL = message.text
    if URL.startswith('https://www.instagram.com') == True :
        r = requests.get(URL, headers=headers, allow_redirects=True)
        soup = BS(r.content, 'html.parser')
        script = soup.find('script', attrs={'type': "text/javascript"}, text=JS_SHARED_DATA_PATTERN)
        m = JS_SHARED_DATA_PATTERN.search(script.text)
        shared_data_text = m.group(1)
        data = json.loads(shared_data_text)
        url = data.get('entry_data').get('PostPage')[0].get('graphql').get('shortcode_media')
        await bot.send_video(message.from_user.id, url)

In the console, the bot works well.
But already on the server itself, it gives an error
. Here are the logs:
2020-07-19T11:42:14.766277+00:00 app[worker.1]: File "main.py", line 45, in echo
2020-07-19T11:42:14.766278+00:00 app[worker.1]: shared_data_text = m.group(1)
2020-07-19T11:42:14.766278+00:00 app[worker.1]: AttributeError: 'NoneType' object has no attribute 'group'

I can’t understand why it shows this error, I already tried to deploy it on a non-asynchronous library, the same error
What’s wrong with the json library or what, I don’t understand, it doesn’t fit into requirements.txt
What to do in this situation, I I know if there are those who understand what the problem is, please help
Thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2020-07-19
@DemchukAgry

Instagram is protected from parsing.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question