Insbotify/instab.py

97 lines
2.4 KiB
Python
Raw Normal View History

2021-11-01 22:48:15 +00:00
import info_imp
import songrand
import requests
import shutil
import os
from os.path import exists
from instabot import Bot
import glob
import bitly_api
# If cookie.json exists, deletes it (won't log in if cooke.json exists)
cookie_del = glob.glob("config/*cookie.json")
if cookie_del != []:
os.remove(cookie_del[0])
# Instagram Bot Credentials
user = info_imp.datinho['ig_user']
pw = info_imp.datinho['ig_pw']
# Lists to be storage the lists from songrand.py functions
songs = []
name = []
cover = []
cov3r = []
# Call songrand.py functions to get the lists
songrand.getCover(cover)
songrand.getSongList(songs)
songrand.getNameSong(name)
# Picks the number
number = songrand.pickNumber(songs)
# Picks the song picked randomly through songrand.py function pickNumber, stores the value in number
fate1 = songrand.pickList(number, songs)
fate2 = songrand.pickList(number, name)
# Sorts the cover links
cov3r = sorted(set(cover), key=lambda x:cover.index(x))
# Picks the cover of the song picked before, number it's multiplied by 3 because there are 3 different resolutions for each song
# This way we get the best resolution, since the best resolution gets downloaded first
fate3 = songrand.pickList(number*3, cov3r)
# Gets the name of the cover downloaded in this folder
filename = fate3.split("/")[-1]
# Bitly shortener to shorten the spotify song link
BITLY_ACCESS_TOKEN = info_imp.datinho['BITLY_TOKEN']
access = bitly_api.Connection(access_token = BITLY_ACCESS_TOKEN)
full_link = fate1
short_url = access.shorten(full_link)
furl = short_url['url']
# Caption for the post
cap = fate2 + ' ' + furl
# Download the cover pic in .JPEG format
r = requests.get(fate3, stream = True)
if r.status_code == 200:
r.raw.decode_content = True
with open(filename,'wb') as f:
shutil.copyfileobj(r.raw, f)
# InstaBot logs in the account and posts the cover (filename) with the captions (cap)
bot = Bot()
bot.login(username = user, password = pw)
bot.upload_photo(filename,
caption = cap)
# We create a new variable so we can delete the picture after we posted it
filew = filename + '.REMOVE_ME'
# When the image is uploaded, it renames to filew because of InstaBot, so we check if filename exists. If filename exists, we delete it
val1 = os.path.exists(filename)
if val1 == True:
os.remove(filename)
elif os.path.exists(filew) == True:
os.remove(filew)
# If filename doesn't exist, we do the same with filew