Last active
July 9, 2019 20:00
-
-
Save nunq/797f73cd44dd91a3facabcbfef1f8651 to your computer and use it in GitHub Desktop.
some instagram bot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import glob | |
import os | |
import random | |
import shutil | |
from google_images_download import google_images_download | |
from InstagramAPI import InstagramAPI | |
keyw = ["turtles", "cats", "dogs", "birds", "landscape", "skyline", "satellite images", "exoplanets", "lakes", "hippos", "exotic animals"] | |
path2img = "" | |
username = "" | |
password = "" | |
tofetch = 50 | |
# login | |
api = InstagramAPI(username, password) | |
api.login() | |
def fetch(keyw): | |
# google images download | |
response = google_images_download.googleimagesdownload() | |
arguments = {"keywords": keyw, "limit": tofetch, "print_urls": True, "type": "photo", "size": ">800*600", "output_directory": "./images", "safe_search": True} | |
response.download(arguments) | |
# move ./images/*keyword* to ./images/ | |
try: | |
for i in glob.glob("./images/*/*"): | |
shutil.move(i, "./images") | |
except: | |
print("error while moving downloaded images a dir up") | |
# some other file operation? | |
try: | |
for i in glob.glob("./images/*"): | |
shutil.rmtree("./images/"+keyw, ignore_errors=True) | |
except: | |
pass | |
firstFilePath = path2img+os.listdir(path2img)[0] | |
print("image to be uploaded: "+firstFilePath) | |
try: | |
API.uploadPhoto(firstFilePath, caption="1 bild für dich lol :)") | |
except: | |
print("upload failed") | |
exit() | |
# remove image to avoid dups uploads | |
os.remove(firstFilePath) | |
# if <2 images in imagedir, fetch new ones | |
if len(os.listdir(path2img)) < 2: | |
chosen_keyw = keyw[random.randint(0, len(keyw))] | |
# keyw.pop(chosen_keyw) | |
fetch(chosen_keyw) | |
print("uploaded: "+firstFilePath) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment