Skip to content

Instantly share code, notes, and snippets.

@xlucasdemelo
Last active January 3, 2019 23:31
Show Gist options
  • Save xlucasdemelo/3c4d251f9fc87540560a6f0274100572 to your computer and use it in GitHub Desktop.
Save xlucasdemelo/3c4d251f9fc87540560a6f0274100572 to your computer and use it in GitHub Desktop.
Scripts to search a person name by instagram followers
from InstagramAPI import InstagramAPI
import json
import datetime
import os, errno
import requests
import logging
import logging.handlers
import os
import sched, time
import sys
handler = logging.handlers.WatchedFileHandler(
os.environ.get("LOGFILE", "logs.log"))
formatter = logging.Formatter(logging.BASIC_FORMAT)
handler.setFormatter(formatter)
root = logging.getLogger()
root.setLevel(os.environ.get("LOGLEVEL", "INFO"))
root.addHandler(handler)
log = logging.getLogger(__name__)
username = "Instagram username"
password = "Instagram password"
def instagramLogin():
instagramAPI = InstagramAPI(username, password)
instagramAPI.login()
instagramRequest(instagramAPI)
def instagramRequest(instagramAPI):
userPosts = {}
followersArray = []
next_max_id =''
followers_count = 1
i = 0
while followers_count != 0:
i+=1
#id from the band Pense
#Retrieve followers from an Instagram Account
followers = instagramAPI.getUserFollowers('371528917', next_max_id)
followers_raw = instagramAPI.LastJson
followers = followers_raw['users']
followers_count = len(followers)
userPosts = {}
#Iterate followers list and save all of their posts
for user in followers:
if (user['username'] not in userPosts ):
try:
instagramAPI.getUserFeed(user['pk'])
posts = instagramAPI.LastJson['items']
userPosts[user['username']] = posts
except:
pass
#write the json with Follower name and all posts to file
with open('users' + str(i) +'.json', 'w') as outfile:
json.dump(userPosts, outfile)
log.info(followers_raw['next_max_id'])
next_max_id = followers_raw['next_max_id']
instagramLogin()
import json
from pprint import pprint
from os import listdir
def list_files(directory, extension):
return (f for f in listdir(directory) if f.endswith('.' + extension))
json_files = list_files(".", "json")
users = []
for i in json_files:
print(i)
data = json.load(open(i))
#For each user post verify if it was taken at the location 'Foz do Iguaçu'
for key, value in data.items() :
for att in value:
try:
if (att['location']['city'].count('Foz')):
users.append(key)
except:
pass
print(users)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment