TelegramBot + Instagram

Scriam in postul anterior ca am facut un mic script inutil, for fun, pentru ca se poate. Scriptul ma anunta cati followeri am pe Instagram si cati am in plus sau in minus fata de ultima data cand a fost rulat scriptul.

Pe scurt: Instagram iti pune la dispozitie un API, dar nu prea. API-ul e pentru ceva aplicatii facute prin Facebook, trebuie sa te faci Facebook developer, etc, etc, n-am prea inteles bine si nici nu mi-am batut capul. Am gasit in schimb o solutie prin care Instagram iti varsa un json pe care poti sa-l parsezi: curl 'https://www.instagram.com/{ig_username}/?__a=1'

Abuse it wisely. Bineinteles, trebuie setat un cookie in request-ul cURL si eu am lasat si UserAgent-ul. Nu cred ca e nevoie de UA, dar nu costa nimic sa fie acolo.

Json-ul respectiv poate fi parsat prin jq si numarul de followeri salvat intr-o variabila. Ceva de genul: followers=$(curl {URL} | jq .graphql.user.edge_followed_by.count).

Ce faceti pe urma cu informatia e treaba voastra, eu am ales sa compar ziua de ieri cu ziua de azi si sa trimit informatia catre Telegram. Logica din spatele if-urilor probabil nu e cea mai buna, but it works.

#!/bin/bash

follow_db=$(cat /tmp/followdb)
TOKEN=aivreatu
CHID=nope
CURL=$(curl 'https://www.instagram.com/{username}/?__a=1' -X 'GET' -H 'Cookie: haha' -H 'User-Agent: ceva' -s | jq .graphql.user.edge_followed_by.count)

if [[ $CURL == '' ]]; then
    curl -s "https://api.telegram.org/$TOKEN/sendMessage?chat_id=$CHID&text=Eroare in cURL Instagram"
    exit 1
fi

cur_followers=$CURL
yest_followers=$follow_db
amount=$(echo $(($cur_followers-$yest_followers)))


if [[ $yest_followers < $cur_followers ]]; then
    curl -s "https://api.telegram.org/$TOKEN/sendMessage?chat_id=$CHID&text=Instagram report ?:%0AYou have gained $amount new followers, you now have a total of $cur_followers"
elif [[ $yest_followers > $cur_followers ]]; then
    curl -s "https://api.telegram.org/$TOKEN/sendMessage?chat_id=$CHID&text=Instagram report ?:%0AYou have lost $amount followers, you now have a total of $cur_followers"
elif [[ $yest_followers == $cur_followers ]]; then
    curl -s "https://api.telegram.org/$TOKEN/sendMessage?chat_id=$CHID&text=Instagram report ?:%0ANo followers change, you still have $cur_followers"
fi

echo $cur_followers > /tmp/followdb

exit 0

Probabil scriptul putea fi scris mai frumos, probabil nu. Probabil putea fi scris mai eficient, probabil nu. Probabil putea sa fie un oneliner, probabil… nu. La mine atat s-a putut. If it looks stupid but it works, then it’s not stupid. Pentru cei care au citit “codul” si au vazut ceva dubios acolo, puteti sa-mi ziceti in comentarii.
PS: %0A inseamna newline(enter) pentru TelegramBot, am aflat asta cand am facut scriptul.

Ne vedem in postul urmator.

About Author:

I look 12, but I'm a 42-year-old investment banker!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.