Pirated copy of paid service in 39 lines of Python code

image
During the execution of the order for the development of the telegram bot I had the need to get a screenshot of the web page with its delivery to the user. Why think about solving a problem when you can find it? As it turned out, not to pay! More sweat cut.


So, fate pushed me to the service url2png . It seems to be cool: you register, you get an API token and make yourself requests. But no matter how.


image


No, well, seriously, VDS for a few telegrams bots is cheaper! And then it became clear to me that I would have to get out with all available means. It did not take long to wrestle, since such a thing as Selenium was found . Selenium requires the installation of a special driver in accordance with the browser used. I warn you that PhantomJS is no longer supported by Selenium, so google chrome will be used to work in headless mode (when the webdriver starts up, the browser window does not open). How to configure for this vds? First of all, you need to install the browser itself. In the console you need to enter the following commands.


sudo apt update
sudo apt install -y chromium-browser

After this link you need to find out the latest version of chromedriver (2.41 at the moment). You need to install it with the following commands.


wget https://chromedriver.storage.googleapis.com/2.41/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
sudo mv chromedriver /usr/bin/chromedriver
sudo chown root:root /usr/bin/chromedriver
sudo chmod +x /usr/bin/chromedriver

I would also like to note that to debug the telegram bot on your machine will have to install a VPN if you are in Russia. Now you can start developing the bot. Libraries will be needed:


pytelegrambotapi
selenium
validators

You can install them safely using pip. The beginning of the script looks like this.


# -*- coding: utf-8 -*-import telebot
import os
import validators
from selenium import webdriver

First, I created a bot and set up a browser to work in headless mode.


#создаём бота
token = 'token of this bot'
bot = telebot.TeleBot(token, threaded = False)
#настраиваем браузер для корректной работы в headless режиме
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--no-sandbox')

Then he released a greeting and user assistance.


#имплементация обязательных команд /start и /help@bot.message_handler(commands=['start'])defhello_user(message):
    bot.send_message(message.chat.id, 'Hello, ' + message.from_user.username + "!")
@bot.message_handler(commands=['help'])defshow_help(message):
    bot.send_message(message.chat.id, 'To get screenshot of webpage use command /getpng.\nExample: /getpng https://www.google.com')

The most important thing is to get a screenshot. Using the validators library, the validation (sorry for the tautology) of the link entered by the user is performed. Also, using the os module, the screenshot is deleted from the server after sending, in order not to take up space.


#получение скрина сайта с помощью selenium и headless chrome@bot.message_handler(commands=['getpng'])defget_screenshot(message):
    uid = message.chat.id
    url = ""try:
        url = message.text.split(' ')[1]
    except IndexError:
        bot.send_message(uid, 'You have not entered URL!')
        returnifnot validators.url(url):
        bot.send_message(uid, 'URL is invalid!')
    else:
        photo_path = str(uid) + '.png'
        driver = webdriver.Chrome(chrome_options = options)
        driver.set_window_size(1280, 720)
        driver.get(url)
        driver.save_screenshot(photo_path)
        bot.send_photo(uid, photo = open(photo_path, 'rb'))
        driver.quit()
        os.remove(photo_path)

Run the bot and check its work!


#запуск ботаif __name__ == '__main__':
    bot.infinity_polling()

image


As you can see, everything works great. Of course, all sorts of buns can be improved, but I set a goal to build a foundation and reached it. Actually, the link to the bot for those who wish and to the githab repository for those interested. In the meantime, all good, see you in the following publications!


Also popular now: