Created
August 19, 2014 01:51
-
-
Save pzp1997/b54424087c57f66852f1 to your computer and use it in GitHub Desktop.
Python script for discovering who unfollowed you on Instagram (and unfollowing those people).
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 python2.7 | |
"""Discovers who unfollowed you on Instagram (and unfollows those people).""" | |
import json | |
from webbrowser import open_new_tab | |
from urllib2 import urlopen, URLError | |
from urlparse import urlparse | |
from time import time | |
from os import rename | |
__author__ = 'Palmer Paul' | |
__version__ = "1.0.0-alpha" | |
__email__ = "[email protected]" | |
class Application(object): | |
def __init__(self): | |
self.client_id = "fa6cf0f95ce4430f90b6ec52b89786c4" | |
self.rURI = "http://localhost:8515/" | |
self.scope = ["relationships"] | |
self.data = self.load_data() | |
self.followers = [] | |
def load_data(self): | |
try: | |
fp_read = open("data.txt", "r") | |
try: | |
data = json.load(fp_read) | |
fp_read.close() | |
except ValueError: | |
fp_read.close() | |
data = self.load_error() | |
except IOError: | |
data = self.load_error() | |
return data | |
def load_error(self): | |
token = self.authenticate() | |
data = (token, {time(): self.update(token)}) | |
write = open("temp.txt", "w") | |
json.dump(data, write) | |
write.close() | |
rename("temp.txt", "data.txt") | |
return data | |
def authenticate(self): | |
if len(self.scope) == 0: | |
self.scope.append("basic") | |
open_new_tab("https://instagram.com/oauth/authorize/?client_id=" + self.client_id + "&redirect_uri=" + self.rURI + "&scope=" + "+".join(self.scope) + "&response_type=token") | |
url = raw_input("After authorizing this app, copy the entire URL from the address bar and paste it here (it doesn't matter if the page cannot be found): ") | |
while not "http://localhost:8515/#" in url: | |
url = raw_input("Please copy and paste the ENTIRE URL: ") | |
url = urlparse(url)[5] | |
token = url[url.find("access_token=")+13:] | |
return token | |
def update(self, token): | |
next_url = "https://api.instagram.com/v1/users/3/followed-by?access_token=" + token | |
while True: | |
try: | |
request = json.loads(urlopen(next_url).read()) | |
except URLError: | |
print "Error: Could not access the Instagram API" | |
raise SystemExit | |
for user in range(len(request["data"])): | |
self.followers.append(request["data"][user]["username"]) | |
try: | |
next_url = request["pagination"]["next_url"] | |
except KeyError: | |
break | |
print len(self.followers) | |
self.save() | |
return self.followers | |
def save(self): | |
self.data[1][time()] = self.followers | |
write = open("temp.txt", "w") | |
json.dump(self.data, write) | |
write.close() | |
rename("temp.txt", "data.txt") | |
def compare(self): | |
follow_hist = self.data[1] | |
last = follow_hist[max(follow_hist.keys())] | |
current = self.update(self.data[0]) | |
lost = [] | |
for follower in range(len(last)): | |
if not last[follower] in current: | |
lost.append(last[follower]) | |
gained = [] | |
for follower in range(len(current)): | |
if not current[follower] in last: | |
gained.append(current[follower]) | |
return lost, gained | |
def main(self): | |
pass | |
myApp = Application() | |
print myApp.compare() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment