Instabot-py Rework

Amine Ben Asker
4 min readJun 10, 2019

--

A few months ago, I proposed a considerable rework to the Instabot-Py maintainers.
The issue is posted and archived in the main repo. below is the link https://github.com/instabot-py/instabot.py/issues/2113

Configuration management

Instead of the in the script configuration file. Instabot fetches its configuration from environment variables, local YAML file or external database. It allows running multiple instances of Instabot with different configuration file without any modification to the code or the entry script (historical exemple.py).
For advanced usage of Instabot, you could easily schedule stateless docker instances of this project.

Backend persistence class

Instabot was highly coupled with the SQLite database.
I proposed the below rework for more extensibility.

  • Creating a database class to handle different persistence backed.
  • Rework SQLite implementation to ensure compatibility and set it as default backed.
  • Write a MongoDB persistence class.
    MongoDB persistence class is not yet implemented. I think it’s not suited to include it in the main project. It could be implemented as a separate class.

Logging & Notification

The print function/Instagram.write_log are refactored to a high-level logging class to describe/debug different events like follow, unfollow, like, comment, python exception, user warnings.

  • Write notification class and set stdout as the default logging handler to ensure current behaviour.
  • Add logging handler configuration to send events to user Telegram/Slack (or/and Error Events to community Telegram/Slack channel).

New Features

A new features you may like

Loose coupling between configuration and python scripts

Many of Instabot users have a script file per Instagram account or particular configuration.

For me I remeber I had 3 files in my working directory


ls -lah
drwxr-xr-x 3 yuri yuri 4.0K Jan 12 22:53 src
drwxr-xr-x 6 yuri yuri 4.0K Jan 12 22:57 venv
-rw-r — r — 1 yuri yuri 11 Jan 12 22:09 version.txt
-rw-r — r — 1 yuri yuri 667 Nov 24 2018 yuri.py
-rw-r — r — 1 yuri yuri 689 Nov 24 2018 yuri_unfollow.py
-rw-r — r — 1 yuri yuri 148 Nov 24 2018 diving.py

Instabot YAML configuration is introduced.
By default, when running Instabot by typing :


instabot_py --

Instabot looks for a configuration file named instabot.config.yml.
Here how you can specify another configuration path to Instabot.

INSTABOT_CONFIG_FILE=my-custom-configuration.yml instabot_py — 

let’s take a real example for one of the Instabot users.
My girlfriend runs 2 instances of Instabot in her Raspberry Pi with different configuration files named follow.yml and unfollow.yml.
Here her configuration directory:

pi@raspberrypi:~/instabot.py $ tree config/
config/
├── account1
│ ├── follow.yml
│ └── unfollow.yml
└── account2
└── follow.yml
2 directories, 3 files

Inside the config directory, there are 2 others directories: one per account.
The content of one of his follow.yml file :

pi@raspberrypi:~/instabot.py $ cat follow.yml
---
login : REMOVED
password : REMOVED
database:
type: sql
connection_string: sqlite:///account1.db
debug: 0
logging.formatters.telegram.format: “%(name)s — %(levelname)s — %(message)s”
logging.handlers.telegram:
level: INFO
formatter: telegram
class: telegram_handler.TelegramHandler
token: REMOVED
chat_id: REMOVED
logging.loggers.InstaBot.handlers:
— telegram
— console
follow_time: 604800
tag_list: [‘paris’,’portraitmood’,’parisien’,’discoverportrait’,’moodygrams’,’portrait’,’igersparis’]
unfollow_per_day: 1000
follow_per_day: 1000
like_per_day: 1000
unlike_per_day: 1000

As you see, the username, password, telegram configuration and other parameters are mentioned in each configuration file.

Note: I already added a task to my backlog to allow reuse of some of the parameters to avoid their redefinition in each file.

She inserted 3 crontab lines :

pi@raspberrypi:~/instabot.py $ crontab -l 
43 13* * * cd /home/pi/instabot.py; ./runner.sh account1 follow &> /dev/null
20 10 * * * cd /home/pi/instabot.py; ./runner.sh account1 unfollow &> /dev/null
33 12,18 * * * cd /home/pi/instabot.py; ./runner.sh account2 follow &> /dev/null

She uses a helper script to activate the python virtual environment before running Instabot :
here is the code used

pi@raspberrypi:~/instabot.py $ cat runner.sh
#!/bin/sh
username=$1
action=$2
. venv/bin/activate
export INSTABOT_CONFIG_FILE=config/$username/$action.yml
python instabot_py --

Tada :) it s done. as easy as that

Bonus :

Install Instabot :

pi@raspberrypi:~ $ mkdir instabot ; cd instabot# Create a virtual env
pi@raspberrypi:~/instabot $ python3 -m venv venv --prompt instabot-py
# Activate the virtual env
pi@raspberrypi:~/instabot $ . venv/bin/activate
#Install instabot
pi@raspberrypi:~/instabot (instabot-py) $ pip install git+https://github.com/instabot-py/instabot.py
# run Instabot
pi@raspberrypi:~/instabot (instabot-py) $ instabot-py --

Configure Instabot to send logs to telegram

The Instabot community appreciated telegram log handler, but they found it not simple to get started with it.
First of all, you need to create a telegram chatbot to be used by Instabot to send logs to Telegram accounts.

please follow this link to create a chatbot https://core.telegram.org/bots#3-how-do-i-create-a-bot

As they said, you need to send a message @BotFather

/start
/newbot

Name your bot (name should finish with “bot” word like mashroom_bot).
If everything is okay, BotFather gives you the access token.
let it be your token : 12345:TOKEN_TOKEN

Now you need to initialize a conversation between your account and the bot.

  • Using your phone/telegram account, send a message to your bot.
  • Get your chat id

Access to your Virtualenv where Instabot is installed.

#Install python-telegram-handler package 
pip install python-telegram-handler
python -m telegram_handler <your token here>

This command prints the chat_id to stdout.
let it be 987643

  • Update your Instabot configuration file
logging.handlers.telegram:
level: INFO
formatter: telegram
class: telegram_handler.TelegramHandler
token: 12345:TOKEN_TOKEN
chat_id: 987643

So now you could follow Instabot activity using your phone.

--

--

Amine Ben Asker

#Software & #Security engineer and Free software & #Blockchain fan. C/C++ & Python developer. loves reading & blogging. Let's automate all the things.