def get_all_followers(user, access_token, filename):
	follower_table = {'Data' : []}
	api = InstagramAPI(access_token=access_token)
	followers, next = api.user_followed_by(user_id=user)
	while next:
		more_followers, next = api.user_followed_by(with_next_url=next)
		followers.extend(more_followers)
	
	for follower in followers:
		follower_table['Data'].append({'Username' : follower.username,
					       'Profile_picture' : follower.profile_picture,
					       'ID' : follower.id,
					       'Full_name' : follower.full_name})
	followers_data = follower_table['Data']
	csv_file = open(filename, 'wb')
	csv_writer = csv.writer(csv_file)
	
	count = 0
	
	for data in followers_data:
		if count == 0:
			header = data.keys()
			csv_writer.writerow(header)
			count += 1
		csv_writer.writerow(data.values())
		
	csv_file.close()
	print "%s created successfully" % filename
	return follower_table
Example #2
0
def index():

    # if instagram info is in session variables, then display user photos
    if 'instagram_access_token' in session and 'instagram_user' in session:
        userAPI = InstagramAPI(access_token=session['instagram_access_token'])
        follows = []
        follows, next_ = userAPI.user_follows(user_id=session['instagram_user'].get('id'))
        while next_:
            more_follows, next_ = userAPI.user_follows(with_next_url=next_)
            follows.extend(more_follows)
        
        followed_by = []
        followed_by, _ = userAPI.user_followed_by(user_id=session['instagram_user'].get('id'))
        while _:
            more_followed_by, _ = api.user_followed_by(with_next_url=_)
            followed_by.extend(more_followed_by)

        followers_names=list(map(str,follows))
        followed_by_names=list(map(str,followed_by))
        unique_people=list(set(followers_names) - set(followed_by_names))
        clean_list=[i.replace("User: "******"") for i in unique_people]
        result=[i for i in follows if i.username in clean_list]
        resultattr = {}
        for i in result:
            resultattr[i.username]=i.profile_picture


        return render_template('result.html', result = resultattr)
        

    else:

        return render_template('index.html')
Example #3
0
def myFollowers(): #written by Tim
    content = "<h2>My Followers</h2>"
    access_token = request.session['access_token']
    if not access_token:
        return 'Missing Access Token'
    try:
        api = InstagramAPI(access_token=access_token, client_secret=CONFIG['client_secret'])



        follower_list, next_ = api.user_followed_by()
        counter =0
        content+="<ul>"

        for user in follower_list:
            content+="<li><em>"+user.getName()+"</em></li>"
            counter = counter +1

        content+="</ul>"

        content+="</h2>Total follower count: "+str(counter)+"</h2><p></p><p></p>"


    except Exception as e:
        print(e)
    return "%s %s <br/>Remaining API Calls = %s/%s" % (get_nav(),content,api.x_ratelimit_remaining,api.x_ratelimit)
Example #4
0
def index():

    # if instagram info is in session variables, then display user photos
    if "instagram_access_token" in session and "instagram_user" in session:
        userAPI = InstagramAPI(access_token=session["instagram_access_token"])
        follows = []
        follows, next_ = userAPI.user_follows(user_id=session["instagram_user"].get("id"))
        while next_:
            more_follows, next_ = userAPI.user_follows(with_next_url=next_)
            follows.extend(more_follows)

        followed_by = []
        followed_by, _ = userAPI.user_followed_by(user_id=session["instagram_user"].get("id"))
        while _:
            more_followed_by, _ = api.user_followed_by(with_next_url=_)
            followed_by.extend(more_followed_by)

        followers_names = list(map(str, follows))
        followed_by_names = list(map(str, followed_by))
        unique_people = list(set(followers_names) - set(followed_by_names))
        clean_list = [i.replace("User: "******"") for i in unique_people]
        result = [i for i in follows if i.username in clean_list]
        resultattr = {}
        for i in result:
            resultattr[i.username] = i.profile_picture

        return render_template("result.html", result=resultattr)

    else:

        return render_template("index.html")
Example #5
0
def User_Followed_By(page=1):
    u = InstagramAPI(access_token=session['access_token'],
                     client_secret=secrets['client_secret'])
    user_followed_by, next_ = u.user_followed_by(client_secret=secrets
                                                 ['client_secret'])
    for i in range(1, page):
        user_followed_by, next_ = u.user_followed_by(with_next_url=next_)
    follows = []
    profile_images = []
    title = "User Followed By-Page " + str(page)
    prev_page = False
    next_page = False
    if next_:
        prev_page = True
    if page != 1:
        next_page = True
    for link in user_followed_by:
        follows.append("{0}".format(link.username))
        profile_images.append("{0}".format(link.profile_picture))
    return render_template("recent.html", follows=follows,
                           profile_images=profile_images, prev=prev_page,
                           next=next_page, page=page, title=title)
Example #6
0
def home(request):
	code = str(request.GET.get('code'))
	oauth = OAuth2API(client_id=CLIENT_ID, client_secret=CLIENT_SECRET, redirect_uri=REDIRECT_URI)
	access_token, user_info = oauth.exchange_code_for_access_token(code)
	# access_token, user_info = ('1327109423.a9fd754.8a4991734ff942ac9af1065eca26b96e', {'bio': u'', 'full_name': 'Bharadwaj Srigiriraju', 'id': '1327109423', 'website': u'', 'profile_picture': 'http://images.ak.instagram.com/profiles/anonymousUser.jpg', 'username': '******'})
	request.session['access_token'] = access_token
	api = InstagramAPI(access_token=access_token)
	user_id = user_info['id']
	follower_list, page_no = api.user_followed_by(user_id)
	recent_timings = []
	feed = api.user_media_feed()
	media_feed = feed[0]
	for media in media_feed:
		recent_timings.append(media.created_time)
	hours = []
	days = {}
	for datetime_obj in recent_timings:
		hours.append(datetime_obj.hour)
		day = datetime_obj.today().weekday()

		if day not in days:
			days[day] = 0
		else:
			days[day] += 1
	hours_count = dict(Counter(hours))
	sorted_hours = sorted(hours_count.items(), key=operator.itemgetter(1))
	days_count = dict(Counter(days))
	sorted_days = sorted(days_count.items(), key=operator.itemgetter(1))
	res = []
	day_name = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
	day_number = int(sorted_days[len(sorted_days)-1][0])
	for i in xrange(3):
		astring = "The " + str(i+1) + " best time for you is: " + str(int(sorted_hours[len(sorted_hours)-i-1][0])-1) + ":30"
		res.append(astring)
	res.append("<br> The best day for you is: " + str(day_name[day_number]))
	
	# for follower in follower_list:
	# 	follower_id = follower.id
	# 	recent_timings.append(api.user_recent_media(follower_id))
	
	return HttpResponse('<br>'.join(res))
    uid = user.id
    media_feed, _next = api.user_recent_media(user_id=uid, count=10)

    print "10 Latest Posts for %s:%s" % (uid, uname)
    for i, m in enumerate(media_feed):
        print "#" * 80
        print "%d: %s" % (i, m.id)
        print "#" * 80
        print "created: %s" % m.created_time
        print "caption: %s" % m.caption
        print "location: %s" % (m.location if hasattr(m, "location") else "Unknown")
        print "tags: %s" % ", ".join([x.name for x in m.tags])
        print "%d comments, %d likes" % (m.comment_count, m.like_count)
        print "filter: %s" % m.filter
        print "link: %s" % m.link
        print "img: %s" % m.get_standard_resolution_url()
        print

    # We can also grab a list of users that follow or are followed by our user
    followers, _next = api.user_followed_by(uid)
    print "Followers of %s:%s" % (uid, uname)
    for i, u in enumerate(followers):
        print "%d. %s:%s" % (i + 1, u.id, u.username)
    print

    following, _next = api.user_follows(uid)
    print "Followees of %s:%s" % (uid, uname)
    for i, u in enumerate(following):
        print "%d. %s:%s" % (i + 1, u.id, u.username)
    print
Example #8
0
    media_feed, _next = api.user_recent_media(user_id=uid, count=10)

    print "10 Latest Posts for %s:%s" % (uid, uname)
    for i, m in enumerate(media_feed):
        print "#" * 80
        print "%d: %s" % (i, m.id)
        print "#" * 80
        print "created: %s" % m.created_time
        print "caption: %s" % m.caption
        print "location: %s" % (m.location
                                if hasattr(m, 'location') else "Unknown")
        print "tags: %s" % ", ".join([x.name for x in m.tags])
        print "%d comments, %d likes" % (m.comment_count, m.like_count)
        print "filter: %s" % m.filter
        print "link: %s" % m.link
        print "img: %s" % m.get_standard_resolution_url()
        print

    # We can also grab a list of users that follow or are followed by our user
    followers, _next = api.user_followed_by(uid)
    print "Followers of %s:%s" % (uid, uname)
    for i, u in enumerate(followers):
        print "%d. %s:%s" % (i + 1, u.id, u.username)
    print

    following, _next = api.user_follows(uid)
    print "Followees of %s:%s" % (uid, uname)
    for i, u in enumerate(following):
        print "%d. %s:%s" % (i + 1, u.id, u.username)
    print
4. Browse to docs: https://github.com/Instagram/python-instagram and https://www.instagram.com/developer/endpoints/

from instagram.client import InstagramAPI
api = InstagramAPI(access_token="INSTAGRAM_TOKEN")

# get user info
u = ___12
# print user id, username, full name
print ___13
# print activity counts
print ___14

# get followers
followers, next_ = api.___15
while next_:
    more_followers, next_ = api.user_followed_by(with_next_url=next_)
    followers.extend(more_followers)

# get followees
followees, next_ = api.___16
while next_:
    more_followees, next_ = api.___17(with_next_url=next_)
    followees.extend(more_followees)

# intersect followers and followees 
set([f.username for f in followers]) & set([f.username for f in followees])

# get user feed
media_feed, next_ = api.___18(count=20)
for media in media_feed:
	# print media's user 
Example #10
0
from instagram.client import InstagramAPI

access_token = ""
client_secret = ""
api = InstagramAPI(access_token=access_token, client_secret=client_secret)
# recent_media, next_ = api.user_recent_media(user_id="", count=10)
recent_media = api.user_recent_media(user_id="danvn", count=10)

# for media in recent_media:
#    print (media.caption.text)

user_followers, next = api.user_followed_by()

len(user_followers)  #this is the numbers of followers you have
Example #11
0
from instagram.client import InstagramAPI
import csv
api = InstagramAPI(client_id='16f7ef0d35624ad3a3b07e18c9e45ef8', client_secret='22fdaddd6282489aab74528e16a06cb9')
f = open("testID.csv")
reader = csv.reader(f)
for row in reader:
	#print row
	try:
		follows = api.user_follows(row[1])[0]
		for i in follows:
			follower = str(i.id)
			print row[1]+","+ follower
			f1 = api.user_follows(follower)[0]
			#print f1
			#print follows
			for j in f1:
				print str(i.id)+","+str(j.id)
		
		followed_by = api.user_followed_by(row[1])[0]
		for i in followed_by:
			followed = str(i.id)
			print followed+","+row[1]
			f1 = api.user_followed_by(followed)[0]
			for j in f1:
				print followed+","+str(j.id)		
	except:
		continue
Example #12
0
class instagramService:
    def __init__(self, config):
        self.config = config
        self.api = InstagramAPI(access_token=config['access_token'])
        self.version = 0
        self.batch = []
        self.batch_users = []
        self.feed_count = 0
        self.feed_count_total = 0
        self.instagram_username = config['instagram_username']
        self.instagram_user_id = config['instagram_user_id']

    def stop_and_exit_service(self):
        logging.debug('instagram Service - HARD QUIT - instagram SERVICE')
        sys.exit()

    def run_main_services(self):

        main_services_d = Deferred()
        try:
            print "running instagram main services"

            def find_followers_cb(res):
                def find_friends_cb(res):
                    def get_authen_user_feed_cb(res):
                        def get_popular_media_cb(res):

                            main_services_d.callback(True)

                        self.get_popular_media().addCallbacks(
                            get_popular_media_cb, lambda failure: logging.
                            error("Update Error {0}".format(failure)))

                    self.get_authenticated_user_feed().addCallbacks(
                        get_authen_user_feed_cb, lambda failure: logging.error(
                            "Update Error {0}".format(failure)))

                self.find_friends(
                    self.config['instagram_user_id']).addCallbacks(
                        find_friends_cb, lambda failure: logging.error(
                            "Update Error{0}".format(failure)))

            self.find_followers(self.config['instagram_user_id']).addCallbacks(
                find_followers_cb, lambda failure: logging.error(
                    "Update Error{0}".format(failure)))
            #self.subscribe_to_objects_by_tag(self.config['instagram_search_words'])
        except:
            logging.debug(
                'Service Instagram - Could not run main service due to error: {0}'
                .format(sys.exc_info()))

        return main_services_d

    def get_indx(self):

        return_d = Deferred()

        def authed_cb():
            logging.debug("in service_tweets - Authed Callback")
            logging.debug(
                "in service_tweets - get_indx authclient Auth status: {0}".
                format(authclient.is_authed))

            def token_cb(token):
                self.indx_con = IndxClient(self.config['address'],
                                           self.config['box'],
                                           appid,
                                           token=token,
                                           client=authclient.client)
                return_d.callback(True)

            authclient.get_token(self.config['box']).addCallbacks(
                token_cb, return_d.errback)

        def authed_cb_fail(re):
            logging.debug(
                "in service tweets - get_indx/authed_cb failed for reason {0}".
                format(re))
            return_d.errback

        logging.debug("in service_instagram - get_indx")
        authclient = IndxClientAuth(self.config['address'], appid)
        authclient.auth_plain(self.config['user'],
                              self.config['password']).addCallbacks(
                                  lambda response: authed_cb(), authed_cb_fail)

        return return_d

    def get_authenticated_user_feed(self):
        auth_d = Deferred()

        def found_cb(results):
            friends_number = 0
            followers_number = 0
            since_id = ""
            #let's see if the object has some nice things in it.
            try:
                config_returned = results['data']['service_instagram_config']
                friends_number = int(
                    config_returned['friends_list_size'][0]['@value'])
                followers_number = int(
                    config_returned['followers_list_size'][0]['@value'])
                since_id = int(config_returned['since_id'][0]['@value'])
                logging.info('Found the instagram Config Object.')
            except:
                #print sys.exc_info()
                pass

            #print "Getting Auth User's feed"
            user_feed = self.api.user_media_feed()[0]
            try:
                latest_id = user_feed[0].id
            except:
                latest_id = "Null"
            ##find the highest id...

            if (latest_id != since_id):

                logging.info("Found some new media, will insert it to INDX")
                since_id = latest_id
                objects_to_insert = []
                current_timestamp = str(
                    time.time()).split(".")[0]  #str(datetime.now())
                timestamp = str(datetime.now().isoformat('T')).split(".")[0]
                current_timestamp = str(datetime.now())

                #the user responsible for this

                #now create some nice user objects...
                for media in user_feed:

                    media_user_id = media.user.id
                    media_username = media.user.username

                    uniq_id = "instagram_media_" + media.id
                    user_feed_obj = {
                        "@id": uniq_id,
                        "app_object": appid,
                        "timestamp": timestamp,
                        "type": "post",
                        "instagram_user_id_indx": "instagram_user_me",
                        "instagram_user_id": media_user_id,
                        "instagram_username": media_username,
                        "media_id": media.id,
                        "media_caption": media.caption,
                        "media_url": media.get_standard_resolution_url(),
                        "media_like_count": media.like_count
                    }

                    #need to add INDX objects for tags and locations and the indx user of this

                    #create location if available
                    try:
                        location = media.location
                        uniq_id = "instagram_location_" + location.id
                        location_obj = {
                            "@id": uniq_id,
                            "app_object": appid,
                            "timestamp": timestamp,
                            "type": "location",
                            "instagram_location_id": location.id,
                            "location_name": location.name,
                            "latitude": location.point.latitude,
                            "longitude": location.point.longitude
                        }

                        #now add this location to the user_feed_obj
                        user_feed_obj['media_location_id'] = location_obj
                        #thhen add it to a list of ojects to insert.
                        objects_to_insert.append(location_obj)
                    except:
                        pass

                    try:
                        tag_ids = []
                        for tag in media.tags:
                            uniq_id = "instagram_tag_" + tag.name
                            tag_obj = {
                                "@id": uniq_id,
                                "app_object": appid,
                                "timestamp": timestamp,
                                "type": "tag",
                                "instagram_tag_name": tag.name
                            }
                            tag_ids.append(uniq_id)
                            objects_to_insert.append(tag_obj)

                        #now add this location to the user_feed_obj
                        user_feed_obj['tags'] = tag_ids
                    except:
                        pass

                    objects_to_insert.append(user_feed_obj)

                #now create the instagram_user_me object
                uniq_id = "instagram_user_me"
                instagram_me_obj = {
                    "@id": uniq_id,
                    "app_object": appid,
                    "timestamp": timestamp,
                    "type": "user",
                    "instagram_user_id": media_user_id,
                    "instagram_username": media_username
                }
                objects_to_insert.append(instagram_me_obj)

                #and create the instagram config
                instagram_config_obj = {
                    "@id": "service_instagram_config",
                    "app_object": appid,
                    "type": "config",
                    "config_last_updated_at": timestamp,
                    "config_for_instagram_user": self.instagram_username,
                    "friends_list_generated_at": timestamp,
                    "follower_list_generated_at": timestamp,
                    "friends_list_size": friends_number,
                    "followers_list_size": followers_number,
                    "since_id": since_id
                }
                objects_to_insert.append(instagram_config_obj)

                def update_cb(re):
                    logging.debug(
                        "network harvest async worked {0}".format(re))
                    auth_d.callback(True)

                def update_cb_fail(re):
                    logging.error(
                        "network harvest async failed {0}".format(re))
                    auth_d.errback

                self.insert_object_to_indx(objects_to_insert).addCallbacks(
                    update_cb, update_cb_fail)
            else:
                logging.info(
                    "already have the latest version of your instagram timeline"
                )
                auth_d.callback(True)

        def error_cb(re):
            found_cb()

        def_search = Deferred()
        find_instagram_config = {"@id": "service_instagram_config"}
        logging.info(
            "Searching for instagram_config to check if Popular feed already harvested... "
        )
        def_search = self.indx_con.query(json.dumps(find_instagram_config))
        def_search.addCallbacks(found_cb, error_cb)

        return auth_d
        # for feed in user_feed:
        #     print feed

    def get_searched_media(self, search_terms):
        print "getting searched media for terms: " + str(search_terms)
        returned_results, page_num = self.api.tag_search(search_terms, 20)
        return returned_results
        # for result in returned_results:
        #     print result

    def get_popular_media(self):
        pop_d = Deferred()

        def found_cb(results):
            friends_number = 0
            followers_number = 0
            since_id = ""
            #let's see if the object has some nice things in it.
            try:
                config_returned = results['data']['service_instagram_config']
                friends_number = int(
                    config_returned['friends_list_size'][0]['@value'])
                followers_number = int(
                    config_returned['followers_list_size'][0]['@value'])
                since_id = int(config_returned['since_id'][0]['@value'])
                logging.info('Found the instagram Config Object.')
            except:
                #print sys.exc_info()
                pass

        #if(since_id > 0):
            logging.info("getting popular media")

            objects_to_insert = []
            current_timestamp = str(
                time.time()).split(".")[0]  #str(datetime.now())
            timestamp = str(datetime.now().isoformat('T')).split(".")[0]
            current_timestamp = str(datetime.now())

            popular_media = self.api.media_popular(count=20)

            for media in popular_media:

                media_user_id = media.user.id
                media_username = media.user.username

                #now create the instagram_user object
                uniq_user_id = "instagram_user_" + media.user.id
                instagram_media_obj = {
                    "@id": uniq_user_id,
                    "app_object": appid,
                    "timestamp": timestamp,
                    "type": "user",
                    "instagram_user_id": media_user_id,
                    "instagram_username": media_username
                }
                objects_to_insert.append(instagram_media_obj)

                uniq_id = "instagram_media_" + media.id
                media_obj = {
                    "@id": uniq_id,
                    "app_object": appid,
                    "timestamp": timestamp,
                    "type": "post",
                    "instagram_user_id_indx": uniq_user_id,
                    "instagram_user_id": media_user_id,
                    "instagram_username": media_username,
                    "media_id": media.id,
                    "media_caption": media.caption,
                    "media_url": media.get_standard_resolution_url(),
                    "media_like_count": media.like_count
                }

                #need to add INDX objects for tags and locations and the indx user of this

                #create location if available
                try:
                    location = media.location
                    uniq_id = "instagram_location_" + location.id
                    location_obj = {
                        "@id": uniq_id,
                        "app_object": appid,
                        "timestamp": timestamp,
                        "type": "location",
                        "instagram_location_id": location.id,
                        "location_name": location.name,
                        "latitude": location.point.latitude,
                        "longitude": location.point.longitude
                    }

                    #now add this location to the user_feed_obj
                    media_obj['media_location_id'] = location_obj
                    #thhen add it to a list of ojects to insert.
                    objects_to_insert.append(location_obj)
                except:
                    pass

                try:
                    tag_ids = []
                    for tag in media.tags:
                        uniq_id = "instagram_tag_" + tag.name
                        tag_obj = {
                            "@id": uniq_id,
                            "app_object": appid,
                            "type": "tag",
                            "timestamp": timestamp,
                            "instagram_tag_name": tag.name
                        }
                        tag_ids.append(uniq_id)
                        objects_to_insert.append(tag_obj)

                    #now add this location to the user_feed_obj
                    media_obj['tags'] = tag_ids
                except:
                    pass

                objects_to_insert.append(media_obj)

            def update_cb(re):
                logging.debug("network harvest async worked {0}".format(re))
                pop_d.callback(True)

            def update_cb_fail(re):
                logging.error("network harvest async failed {0}".format(re))
                pop_d.errback

            #and create the instagram config
            instagram_config_obj = {
                "@id": "service_instagram_config",
                "app_object": appid,
                "type": "config",
                "config_last_updated_at": timestamp,
                "config_for_instagram_user": self.instagram_username,
                "friends_list_generated_at": timestamp,
                "follower_list_generated_at": timestamp,
                "friends_list_size": friends_number,
                "followers_list_size": followers_number,
                "since_id": since_id
            }
            objects_to_insert.append(instagram_config_obj)

            self.insert_object_to_indx(objects_to_insert).addCallbacks(
                update_cb, update_cb_fail)

        def error_cb(re):
            found_cb()

        def_search = Deferred()
        find_instagram_config = {"@id": "service_instagram_config"}
        logging.info(
            "Searching for instagram_config to check if Popular feed already harvested... "
        )
        def_search = self.indx_con.query(json.dumps(find_instagram_config))
        def_search.addCallbacks(found_cb, error_cb)

        return pop_d

    def find_user(self, username):
        data = self.api.user_search(username, count=20)
        print data

    def find_followers(self, userid):

        #first do a lookup and see if the latest set if followers has allready been found
        follower_d = Deferred()

        def found_cb(results):
            friends_number = 0
            followers_number = 0
            since_id = 0
            #let's see if the object has some nice things in it.
            try:
                config_returned = results['data']['service_instagram_config']
                friends_number = int(
                    config_returned['friends_list_size'][0]['@value'])
                followers_number = int(
                    config_returned['followers_list_size'][0]['@value'])
                since_id = int(config_returned['since_id'][0]['@value'])
                logging.info('Found the instagram Config Object.')
            except:
                #print sys.exc_info()
                pass
            followed_by = self.api.user_followed_by(userid)[0]
            #print "followed_by length: "+str(len(followed_by))
            #print str(followed_by)
            #see if the number is different, if it is, then update.. could be nicer than this, but for now, it will work (ish)
            if (len(followed_by) != followers_number) or (followers_number
                                                          == 0):

                followers_number = len(followed_by)
                objects_to_insert = []
                current_timestamp = str(
                    time.time()).split(".")[0]  #str(datetime.now())
                timestamp = str(datetime.now().isoformat('T')).split(".")[0]
                uniq_id = "instagram_follower_network_for_user_me"  #+self.instagram_username
                followed_by_obj = {
                    "@id": uniq_id,
                    "app_object": appid,
                    "instagram_username": self.instagram_username,
                    "instagram_user_id": self.instagram_user_id,
                    "timestamp": timestamp,
                    "followed_by_count": len(followed_by)
                }

                followers_ids = []
                for follower in followed_by:
                    #print follower.username
                    uniq_id = "instagram_user_" + str(follower.username)
                    follower_obj = {
                        "@id": uniq_id,
                        "app_object": appid,
                        "type": "user",
                        "instagram_username": str(follower.username),
                        "timestamp": timestamp
                    }
                    objects_to_insert.append(follower_obj)
                    #we can add this to the followed_by_obj later
                    followers_ids.append(uniq_id)

                #link the followers for me
                followed_by_obj['follower_ids'] = followers_ids

                # print followed_by_objs
                #for friend in friends_list:
                #print friend
                #now append the results
                def update_cb(re):
                    logging.debug(
                        "network harvest async worked {0}".format(re))
                    follower_d.callback(True)

                def update_cb_fail(re):
                    logging.error(
                        "network harvest async failed {0}".format(re))
                    follower_d.errback

                instagram_config_obj = {
                    "@id": "service_instagram_config",
                    "app_object": appid,
                    "type": "config",
                    "config_last_updated_at": timestamp,
                    "config_for_instagram_user": self.instagram_username,
                    "friends_list_generated_at": timestamp,
                    "follower_list_generated_at": timestamp,
                    "friends_list_size": friends_number,
                    "followers_list_size": followers_number,
                    "since_id": since_id
                }

                objects_to_insert.append(followed_by_obj)
                #objects_to_insert.append(followers)
                objects_to_insert.append(instagram_config_obj)

                self.insert_object_to_indx(objects_to_insert).addCallbacks(
                    update_cb, update_cb_fail)
            else:
                follower_d.callback(True)

        def error_cb(re):
            found_cb()

        def_search = Deferred()
        find_instagram_config = {"@id": "service_instagram_config"}
        logging.info(
            "Searching for instagram_config to check if network already harvested... "
        )
        def_search = self.indx_con.query(json.dumps(find_instagram_config))
        def_search.addCallbacks(found_cb, error_cb)

        return follower_d
        # print followed_by

    def find_friends(self, userid):
        friends_d = Deferred()

        def found_cb(results):
            friends_number = 0
            followers_number = 0
            since_id = 0
            #let's see if the object has some nice things in it.
            try:
                config_returned = results['data']['service_instagram_config']
                friends_number = int(
                    config_returned['friends_list_size'][0]['@value'])
                followers_number = int(
                    config_returned['followers_list_size'][0]['@value'])
                since_id = int(config_returned['since_id'][0]['@value'])
                logging.info('Found the instagram Config Object.')
            except:
                #print sys.exc_info()
                pass

            friends_by_list = self.api.user_follows(userid)[0]

            if (len(friends_by_list) != friends_number) or (friends_number
                                                            == 0):

                friends_number = len(friends_by_list)
                objects_to_insert = []
                current_timestamp = str(
                    time.time()).split(".")[0]  #str(datetime.now())
                timestamp = str(datetime.now().isoformat('T')).split(".")[0]
                uniq_id = "instagram_friends_network_for_user_me"  #+self.instagram_username
                friends_by_obj = {
                    "@id": uniq_id,
                    "app_object": appid,
                    "instagram_username": self.instagram_username,
                    "instagram_user_id": self.instagram_user_id,
                    "timestamp": timestamp,
                    "followed_by_count": len(friends_by_list)
                }

                friends_ids = []
                for friend in friends_by_list:
                    #print follower.username
                    uniq_id = "instagram_user_" + str(friend.username)
                    friend_obj = {
                        "@id": uniq_id,
                        "app_object": appid,
                        "type": "user",
                        "instagram_username": str(friend.username),
                        "timestamp": timestamp
                    }
                    objects_to_insert.append(friend_obj)
                    #we can add this to the followed_by_obj later
                    friends_ids.append(uniq_id)

                friends_by_obj['friends_ids'] = friends_ids

                # print friends_by_objs
                #for friend in friends_list:
                #print friend
                #now append the results
                def update_cb(re):
                    logging.debug(
                        "network harvest async worked {0}".format(re))
                    friends_d.callback(True)

                def update_cb_fail(re):
                    logging.error(
                        "network harvest async failed {0}".format(re))
                    friends_d.errback

                instagram_config_obj = {
                    "@id": "service_instagram_config",
                    "app_object": appid,
                    "type": "config",
                    "config_last_updated_at": timestamp,
                    "config_for_instagram_user": self.instagram_username,
                    "friends_list_generated_at": timestamp,
                    "follower_list_generated_at": timestamp,
                    "friends_list_size": friends_number,
                    "followers_list_size": followers_number,
                    "since_id": since_id
                }

                objects_to_insert.append(friends_by_obj)
                #objects_to_insert.append(followers)
                objects_to_insert.append(instagram_config_obj)

                self.insert_object_to_indx(objects_to_insert).addCallbacks(
                    update_cb, update_cb_fail)
            else:
                friends_d.callback(True)

        def error_cb(re):
            found_cb()

        def_search = Deferred()
        find_instagram_config = {"@id": "service_instagram_config"}
        logging.info(
            "Searching for instagram_config to check if network already harvested... "
        )
        def_search = self.indx_con.query(json.dumps(find_instagram_config))
        def_search.addCallbacks(found_cb, error_cb)

        return friends_d
        # print followed_by

    def subscribe_to_objects_by_tag(self, search_terms):
        def process_tag_update(update):
            print update

        reactor = subscriptions.SubscriptionsReactor()
        reactor.register_callback(subscriptions.SubscriptionType.TAG,
                                  process_tag_update)
        self.api.create_subscription(object='tag',
                                     object_id=search_terms,
                                     aspect='media',
                                     callback_url='http://*****:*****@version']
            logging.debug(
                "Succesfully Updated INDX with Objects in update_cb, new diff version of {0}"
                .format(self.version))
            #logging.debug("Inserted Object into INDX: ".format(resp))
            update_d.callback(True)
            #return update_d
            #time.sleep(2)

        def exception_cb(e, service=self, obj=obj):
            logging.debug(
                "Exception Inserting into INDX, probably wrong version given")
            if isinstance(
                    e.value, urllib2.HTTPError
            ):  # handle a version incorrect error, and update the version
                if e.value.code == 409:  # 409 Obsolete
                    response = e.value.read()
                    json_response = json.loads(response)
                    service.version = json_response['@version']
                    logging.debug(
                        'INDX insert error in Instagram Service Object: ' +
                        str(response))
                    try:
                        service.indx_con.update(service.version,
                                                obj).addCallbacks(
                                                    update_cb, exception_cb)
                        #logging.debug('Twitter Service - Successfully added Objects into Box')
                    except:
                        logging.error(
                            'Instagram Service, error on insert {0}'.format(
                                response))
                        update_d.errback(e.value)
                else:
                    logging.error('Instagram Service Unknow error: {0}'.format(
                        e.value.read()))
                    update_d.errback(e.value)
            else:
                logging.error("Error updating INDX: {0}".format(e.value))
                update_d.errback(e.value)

        logging.debug(
            "in Instagram - Trying to insert into indx...Current diff version: {0} and objects (len) given {1}"
            .format(self.version, len(obj)))
        self.indx_con.update(self.version,
                             obj).addCallbacks(update_cb, exception_cb)

        return update_d
class Instagram:
    def __init__(self):
        self.api = InstagramAPI(client_id=CLIENT_ID, client_secret=CLIENT_SECRET, redirect_uri=REDIRECT_URI)

    def set_access_token(self, access_token):
        self.api = InstagramAPI(access_token=access_token)

    def media_popular(self, **params):
        popular = memcache.get("popular_feed")
        if not popular:
            popular = self.api.media_popular(count=params["count"], max_id=params["max_id"])
            memcache.add("popular_feed", popular, 300)
        return popular

    def user_media_feed(self, **params):
        user_id = params["user_id"]
        max_id = params["max_id"]
        count = params["count"]

        feed = memcache.get("user_media_feed_%s_%s" % (user_id, max_id))
        if not feed:
            feed = self.api.user_media_feed(count=count, max_id=max_id)
            memcache.add("user_media_feed_%s_%s" % (user_id, max_id), feed, 300)
        return feed

    def user_liked_feed(self, **params):
        user_id = params["user_id"]
        max_id = params["max_id"]
        count = params["count"]
        feed = memcache.get("user_liked_feed_%s_%s" % (user_id, max_id))
        if not feed:
            feed = self.api.user_liked_feed(count=count, max_like_id=max_id)
            memcache.add("user_liked_feed_%s_%s" % (user_id, max_id), feed, 300)
        return feed

    def user(self, user_id):
        user = memcache.get("user_%s" % (user_id))
        if not user:
            user = self.api.user(user_id)
            user["full_name"] = escape(user["full_name"].encode("utf-8"))
            user["full_name"] = self._convert_emoji(user["full_name"])
            memcache.add("user_%s" % (user_id), user, 300)
        return user

    def media(self, media_id):
        media = memcache.get("media_%s" % media_id)
        if not media:
            media = self.api.media(media_id)
            media.user["full_name"] = escape(media.user["full_name"].encode("utf-8"))
            media.user["full_name"] = self._convert_emoji(media.user["full_name"])
            if media.caption:
                media.caption["text_original"] = media.caption["text"]
                media.caption["text"] = escape(media.caption["text"].encode("utf-8"))
                media.caption["text"] = self._convert_emoji(media.caption["text"])
                media.caption["text"] = self._convert_tag_to_link(media.caption["text"])
            memcache.add("media_%s" % media_id, media, 300)
        return media

    def media_comments(self, media_id):
        comments = memcache.get("media_comments_%s" % (media_id))
        if not comments:
            converter = emoji.factory("softbank", "utf-8")
            converter.prefix = '<span class="emoji emoji_'
            converter.suffix = '"></span>'
            comments = self.api.media_comments(media_id)
            for comment in comments:
                comment["text"] = escape(comment["text"].encode("utf-8"))
                comment["text"] = self._convert_emoji(comment["text"])
                comment["text"] = self._convert_tag_to_link(comment["text"])
            memcache.add("media_comments_%s" % (media_id), comments, 300)
        return comments

    def media_likes(self, media_id):
        likes = memcache.get("media_likes_%s" % (media_id))
        if not likes:
            likes = self.api.media_likes(media_id)
            memcache.add("media_likes_%s" % (media_id), likes, 300)
        return likes

    def user_recent_media(self, **params):
        user_id = params["user_id"]
        max_id = params["max_id"]
        feed = memcache.get("user_recent_media_%s_%s" % (user_id, max_id))
        if not feed:
            feed = self.api.user_recent_media(user_id=user_id, max_id=max_id)
            memcache.add("user_recent_media_%s_%s" % (user_id, max_id), feed, 300)
        return feed

    def user_follows(self, **params):
        user_id = params["user_id"]
        count = params["count"]
        cursor = params["cursor"]
        follows = memcache.get("user_follows_%s_%s_%s" % (user_id, count, cursor))
        if not follows:
            follows = self.api.user_follows(user_id=user_id, count=count, cursor=cursor)
            for user in follows[0]:
                user["full_name"] = escape(user["full_name"].encode("utf-8"))
                user["full_name"] = self._convert_emoji(user["full_name"])
            memcache.add("user_follows_%s_%s_%s" % (user_id, count, cursor), follows, 300)
        return follows

    def user_followed_by(self, **params):
        user_id = params["user_id"]
        count = params["count"]
        cursor = params["cursor"]
        follows = memcache.get("user_followed_by_%s_%s_%s" % (user_id, count, cursor))
        if not follows:
            follows = self.api.user_followed_by(user_id=user_id, count=count, cursor=cursor)
            for user in follows[0]:
                user["full_name"] = escape(user["full_name"].encode("utf-8"))
                user["full_name"] = self._convert_emoji(user["full_name"])
            memcache.add("user_followed_by_%s_%s_%s" % (user_id, count, cursor), follows, 300)
        return follows

    def like_media(self, **params):
        user_id = params["user_id"]
        media_id = params["media_id"]
        max_id = params["max_id"]

        self.api.like_media(media_id)
        memcache.add("user_has_liked_%s_%s" % (user_id, media_id), True, 300)
        memcache.delete("media_likes_%s" % (media_id))

    def unlike_media(self, **params):
        user_id = params["user_id"]
        media_id = params["media_id"]
        max_id = params["max_id"]

        self.api.unlike_media(media_id)
        memcache.delete("user_has_liked_%s_%s" % (user_id, media_id))
        memcache.delete("media_likes_%s" % (media_id))

    def create_media_comment(self, **params):
        media_id = params["media_id"]
        text = params["text"]
        self.api.create_media_comment(media_id=media_id, text=text)
        memcache.delete("media_%s" % media_id)
        memcache.delete("media_comments_%s" % media_id)

    def user_find_by_username(self, username):
        user = memcache.get("user_find_by_username_%s" % (username))

        if not user:
            users = self.api.user_search(q=username, count=None)
            for u in users:
                if username == u["username"]:
                    user = u
                    memcache.add("user_find_by_username_%s" % (username), user)

        return user

    def tag_recent_media(self, **params):
        tag_name = params["tag_name"]
        count = params["count"]
        max_id = params["max_id"]

        feed = memcache.get("tag_recent_media_%s_%s" % (tag_name, max_id))

        if not feed:
            feed = self.api.tag_recent_media(tag_name=tag_name, count=count, max_id=max_id)
            memcache.add("tag_recent_media_%s_%s" % (tag_name, max_id), feed, 300)

        return feed

    def get_authorize_login_url(self, **params):
        uri = memcache.get("authorize_login_uri")
        if not uri:
            uri = self.api.get_authorize_login_url(scope=params["scope"])
            memcache.add("authorize_login_uri", uri, 300)
        return uri

    def _convert_emoji(self, text):
        converter = emoji.factory("softbank", "utf-8")
        converter.prefix = '<span class="emoji emoji_'
        converter.suffix = '"></span>'
        text = converter.convert(text)
        return text

    def _convert_tag_to_link(self, text):
        text = re.sub(r"#([a-zA-Z0-9\-]+)", '<a href="/tag/\g<1>">#\g<1></a>', text)
        return text

    def relationship(self, **params):
        owner_id = params["owner_id"]
        my_id = params["my_id"]

        relationship = memcache.get("relationship_%s_%s" % (my_id, owner_id))
        if not relationship:
            relationship = self.api.relationship(owner_id)
            memcache.add("relationship_%s_%s" % (my_id, owner_id), relationship, 300)

        return relationship

    def follow_user(self, **params):
        owner_id = params["owner_id"]
        my_id = params["my_id"]

        relationship = self.api.follow_user(user_id=owner_id)
        memcache.delete("relationship_%s_%s" % (my_id, owner_id))
        return relationship

    def unfollow_user(self, **params):
        owner_id = params["owner_id"]
        my_id = params["my_id"]

        relationship = self.api.unfollow_user(user_id=owner_id)
        memcache.delete("relationship_%s_%s" % (my_id, owner_id))
        return relationship
Example #14
0
from instagram.client import InstagramAPI

access_token = "25290587.1677ed0.7131ff8507da447bb7fe54dab79fd273"
client_secret = "a8545940747f4536873f06e4a60c23a4"
api = InstagramAPI(access_token=access_token, client_secret=client_secret)
recent_media, next_ = api.user_recent_media(user_id="259220806", count=1)

for media in recent_media:
   print (media.caption.text)
   curMedia = str(media)
   curMedia = curMedia[7:]
   likesArr = api.media_likes(curMedia)
   print (likesArr)

followersList = open('followersList.txt', 'w+')
followers, next = api.user_followed_by(user_id="259220806")
count = 0
while len(followers)<100:
    more_follows, next = api.user_followed_by(with_next_url=next)
    followers.extend(more_follows)
    count += 50
#strFollowers = []
#for name in followers:
#   strFollowers.append(str(name)[6:])
for name in followers:
   curMedia = api.user_liked_media()
#user = api.user()
visited = set()

print "Waiting %d secs between requests" % wait_secs
count = 0
private_count = 0
for i, q in enumerate(queries):
    print >> sys.stderr, "%d/%d: Query: %s" % (i, len(queries), q)
    users = api.user_search(q)
    for j, u in enumerate([x for x in users if x not in visited]):
        follower_count = 0
        visited.add(u)
        print >> sys.stderr, "%d/%d|%d/%d: Found user: %s:%s" \
            % (i, len(queries), j, len(users), u.id, u.username)
        try:
            followers, next_ = api.user_followed_by(u.id)
            time.sleep(wait_secs)
            while next_ and follower_count <= follower_limit:
                print >> sys.stderr
                print >> sys.stderr, "%s:%s has %d entries" % (
                    u.id, u.username, len(followers))
                for k, uid in enumerate(followers):
                    count = count + 1
                    follower_count = follower_count + 1
                    print >> sys.stderr, "%d/%d|%d/%d|%d/%d: %s:%s" \
                        % (i, len(queries), j, len(users), follower_count, follower_limit, uid.id, uid.username)
                    doc = {"_id": uid.id, "username": uid.username}
                    try:
                        mongo.insert_one(doc)
                    except Exception as e:
                        print >> sys.stderr, e
Example #16
0
def acme_test():
# if instagram info is in session variables, then display user photos
	if 'instagram_access_token' in session and 'instagram_user' in session:
		api = InstagramAPI(access_token=session['instagram_access_token'])

		# recent_media, next = api.user_recent_media(user_id=922345846,count=30)
		# check = recent_media[0].location.point.latitude

		users_to_follow = 'knowlita, acme_nyc'
		user_list = users_to_follow.split(',')
		user_list = [x.strip(' ') for x in user_list]  
		
		uids = []
		for key_user in user_list:
			user_search = api.user_search(q=key_user)
			uids.append(user_search[0].id)

		contest_followers = []
		for uid in uids:
			followers, next = api.user_followed_by(user_id= uid)
			for follower in followers:
				contest_followers.append(follower.username)
			while next:
				followers, next = api.user_follows(with_next_url=next)
				for follower in followers:
					contest_followers.append(follower.username)


		contest_followers = list(set(contest_followers))

		## old hack way
		# acme_id = '922345846'
		# acme_followers, next = api.user_followed_by(user_id= acme_id)
		# acme_users = []
		# for user in acme_followers:
		# 	acme_users.append(user.username)

		# i = 0
		# while len(acme_users) < acme_basics.counts['followed_by']-50:
		# 	i += 1
		# 	response = urllib.urlopen(next)
		# 	data = json.loads(response.read())
		# 	for user in data['data']:
		# 		acme_users.append(user['username'])
		# 	next = data['pagination']['next_url']
		
		


		def is_follower(username):
			return (username in contest_followers)


		def find_insta_handles(text):
			p = re.compile('(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([_A-Za-z]+[_A-Za-z0-9]+)')
			return p.findall(text)

		def tagged_users(comment):
			handles = find_insta_handles(comment)
			handles = [str(i) for i in handles]
			handles = [y for y in handles if y not in user_list]
			return handles



		knowlita_contest_img_id = '937178239574293509_922345846'
		acme_contest_img_id = '937173533246931090_28306327'

		contest_imgs = [knowlita_contest_img_id, acme_contest_img_id]
		valid_participants = []

		for img_id in contest_imgs:
			contest_comments = api.media_comments(media_id = img_id )
			for comment in contest_comments:
				if comment.user.username not in valid_participants and comment.user.username not in user_list:
					if len(tagged_users(comment.text)) >= 3 and is_follower(comment.user.username):
						valid_participants.append(comment.user.username)



		templateData = {

			'comments' : contest_comments,
			'participants' : valid_participants,
		}

		return render_template('test.html', **templateData)
	else:
		return redirect('/connect')
Example #17
0
class instagramService:
    def __init__(self, config):
        self.config = config
        self.api = InstagramAPI(access_token=config["access_token"])
        self.version = 0
        self.batch = []
        self.batch_users = []
        self.feed_count = 0
        self.feed_count_total = 0
        self.instagram_username = config["instagram_username"]
        self.instagram_user_id = config["instagram_user_id"]

    def stop_and_exit_service(self):
        logging.debug("instagram Service - HARD QUIT - instagram SERVICE")
        sys.exit()

    def run_main_services(self):

        main_services_d = Deferred()
        try:
            print "running instagram main services"

            def find_followers_cb(res):
                def find_friends_cb(res):
                    def get_authen_user_feed_cb(res):
                        def get_popular_media_cb(res):

                            main_services_d.callback(True)

                        self.get_popular_media().addCallbacks(
                            get_popular_media_cb, lambda failure: logging.error("Update Error {0}".format(failure))
                        )

                    self.get_authenticated_user_feed().addCallbacks(
                        get_authen_user_feed_cb, lambda failure: logging.error("Update Error {0}".format(failure))
                    )

                self.find_friends(self.config["instagram_user_id"]).addCallbacks(
                    find_friends_cb, lambda failure: logging.error("Update Error{0}".format(failure))
                )

            self.find_followers(self.config["instagram_user_id"]).addCallbacks(
                find_followers_cb, lambda failure: logging.error("Update Error{0}".format(failure))
            )
            # self.subscribe_to_objects_by_tag(self.config['instagram_search_words'])
        except:
            logging.debug("Service Instagram - Could not run main service due to error: {0}".format(sys.exc_info()))

        return main_services_d

    def get_indx(self):

        return_d = Deferred()

        def authed_cb():
            logging.debug("in service_tweets - Authed Callback")
            logging.debug("in service_tweets - get_indx authclient Auth status: {0}".format(authclient.is_authed))

            def token_cb(token):
                self.indx_con = IndxClient(
                    self.config["address"], self.config["box"], appid, token=token, client=authclient.client
                )
                return_d.callback(True)

            authclient.get_token(self.config["box"]).addCallbacks(token_cb, return_d.errback)

        def authed_cb_fail(re):
            logging.debug("in service tweets - get_indx/authed_cb failed for reason {0}".format(re))
            return_d.errback

        logging.debug("in service_instagram - get_indx")
        authclient = IndxClientAuth(self.config["address"], appid)
        authclient.auth_plain(self.config["user"], self.config["password"]).addCallbacks(
            lambda response: authed_cb(), authed_cb_fail
        )

        return return_d

    def get_authenticated_user_feed(self):
        auth_d = Deferred()

        def found_cb(results):
            friends_number = 0
            followers_number = 0
            since_id = ""
            # let's see if the object has some nice things in it.
            try:
                config_returned = results["data"]["service_instagram_config"]
                friends_number = int(config_returned["friends_list_size"][0]["@value"])
                followers_number = int(config_returned["followers_list_size"][0]["@value"])
                since_id = int(config_returned["since_id"][0]["@value"])
                logging.info("Found the instagram Config Object.")
            except:
                # print sys.exc_info()
                pass

            # print "Getting Auth User's feed"
            user_feed = self.api.user_media_feed()[0]
            try:
                latest_id = user_feed[0].id
            except:
                latest_id = "Null"
            ##find the highest id...

            if latest_id != since_id:

                logging.info("Found some new media, will insert it to INDX")
                since_id = latest_id
                objects_to_insert = []
                current_timestamp = str(time.time()).split(".")[0]  # str(datetime.now())
                timestamp = str(datetime.now().isoformat("T")).split(".")[0]
                current_timestamp = str(datetime.now())

                # the user responsible for this

                # now create some nice user objects...
                for media in user_feed:

                    media_user_id = media.user.id
                    media_username = media.user.username

                    uniq_id = "instagram_media_" + media.id
                    user_feed_obj = {
                        "@id": uniq_id,
                        "app_object": appid,
                        "timestamp": timestamp,
                        "type": "post",
                        "instagram_user_id_indx": "instagram_user_me",
                        "instagram_user_id": media_user_id,
                        "instagram_username": media_username,
                        "media_id": media.id,
                        "media_caption": media.caption,
                        "media_url": media.get_standard_resolution_url(),
                        "media_like_count": media.like_count,
                    }

                    # need to add INDX objects for tags and locations and the indx user of this

                    # create location if available
                    try:
                        location = media.location
                        uniq_id = "instagram_location_" + location.id
                        location_obj = {
                            "@id": uniq_id,
                            "app_object": appid,
                            "timestamp": timestamp,
                            "type": "location",
                            "instagram_location_id": location.id,
                            "location_name": location.name,
                            "latitude": location.point.latitude,
                            "longitude": location.point.longitude,
                        }

                        # now add this location to the user_feed_obj
                        user_feed_obj["media_location_id"] = location_obj
                        # thhen add it to a list of ojects to insert.
                        objects_to_insert.append(location_obj)
                    except:
                        pass

                    try:
                        tag_ids = []
                        for tag in media.tags:
                            uniq_id = "instagram_tag_" + tag.name
                            tag_obj = {
                                "@id": uniq_id,
                                "app_object": appid,
                                "timestamp": timestamp,
                                "type": "tag",
                                "instagram_tag_name": tag.name,
                            }
                            tag_ids.append(uniq_id)
                            objects_to_insert.append(tag_obj)

                        # now add this location to the user_feed_obj
                        user_feed_obj["tags"] = tag_ids
                    except:
                        pass

                    objects_to_insert.append(user_feed_obj)

                # now create the instagram_user_me object
                uniq_id = "instagram_user_me"
                instagram_me_obj = {
                    "@id": uniq_id,
                    "app_object": appid,
                    "timestamp": timestamp,
                    "type": "user",
                    "instagram_user_id": media_user_id,
                    "instagram_username": media_username,
                }
                objects_to_insert.append(instagram_me_obj)

                # and create the instagram config
                instagram_config_obj = {
                    "@id": "service_instagram_config",
                    "app_object": appid,
                    "type": "config",
                    "config_last_updated_at": timestamp,
                    "config_for_instagram_user": self.instagram_username,
                    "friends_list_generated_at": timestamp,
                    "follower_list_generated_at": timestamp,
                    "friends_list_size": friends_number,
                    "followers_list_size": followers_number,
                    "since_id": since_id,
                }
                objects_to_insert.append(instagram_config_obj)

                def update_cb(re):
                    logging.debug("network harvest async worked {0}".format(re))
                    auth_d.callback(True)

                def update_cb_fail(re):
                    logging.error("network harvest async failed {0}".format(re))
                    auth_d.errback

                self.insert_object_to_indx(objects_to_insert).addCallbacks(update_cb, update_cb_fail)
            else:
                logging.info("already have the latest version of your instagram timeline")
                auth_d.callback(True)

        def error_cb(re):
            found_cb()

        def_search = Deferred()
        find_instagram_config = {"@id": "service_instagram_config"}
        logging.info("Searching for instagram_config to check if Popular feed already harvested... ")
        def_search = self.indx_con.query(json.dumps(find_instagram_config))
        def_search.addCallbacks(found_cb, error_cb)

        return auth_d
        # for feed in user_feed:
        #     print feed

    def get_searched_media(self, search_terms):
        print "getting searched media for terms: " + str(search_terms)
        returned_results, page_num = self.api.tag_search(search_terms, 20)
        return returned_results
        # for result in returned_results:
        #     print result

    def get_popular_media(self):
        pop_d = Deferred()

        def found_cb(results):
            friends_number = 0
            followers_number = 0
            since_id = ""
            # let's see if the object has some nice things in it.
            try:
                config_returned = results["data"]["service_instagram_config"]
                friends_number = int(config_returned["friends_list_size"][0]["@value"])
                followers_number = int(config_returned["followers_list_size"][0]["@value"])
                since_id = int(config_returned["since_id"][0]["@value"])
                logging.info("Found the instagram Config Object.")
            except:
                # print sys.exc_info()
                pass

            # if(since_id > 0):
            logging.info("getting popular media")

            objects_to_insert = []
            current_timestamp = str(time.time()).split(".")[0]  # str(datetime.now())
            timestamp = str(datetime.now().isoformat("T")).split(".")[0]
            current_timestamp = str(datetime.now())

            popular_media = self.api.media_popular(count=20)

            for media in popular_media:

                media_user_id = media.user.id
                media_username = media.user.username

                # now create the instagram_user object
                uniq_user_id = "instagram_user_" + media.user.id
                instagram_media_obj = {
                    "@id": uniq_user_id,
                    "app_object": appid,
                    "timestamp": timestamp,
                    "type": "user",
                    "instagram_user_id": media_user_id,
                    "instagram_username": media_username,
                }
                objects_to_insert.append(instagram_media_obj)

                uniq_id = "instagram_media_" + media.id
                media_obj = {
                    "@id": uniq_id,
                    "app_object": appid,
                    "timestamp": timestamp,
                    "type": "post",
                    "instagram_user_id_indx": uniq_user_id,
                    "instagram_user_id": media_user_id,
                    "instagram_username": media_username,
                    "media_id": media.id,
                    "media_caption": media.caption,
                    "media_url": media.get_standard_resolution_url(),
                    "media_like_count": media.like_count,
                }

                # need to add INDX objects for tags and locations and the indx user of this

                # create location if available
                try:
                    location = media.location
                    uniq_id = "instagram_location_" + location.id
                    location_obj = {
                        "@id": uniq_id,
                        "app_object": appid,
                        "timestamp": timestamp,
                        "type": "location",
                        "instagram_location_id": location.id,
                        "location_name": location.name,
                        "latitude": location.point.latitude,
                        "longitude": location.point.longitude,
                    }

                    # now add this location to the user_feed_obj
                    media_obj["media_location_id"] = location_obj
                    # thhen add it to a list of ojects to insert.
                    objects_to_insert.append(location_obj)
                except:
                    pass

                try:
                    tag_ids = []
                    for tag in media.tags:
                        uniq_id = "instagram_tag_" + tag.name
                        tag_obj = {
                            "@id": uniq_id,
                            "app_object": appid,
                            "type": "tag",
                            "timestamp": timestamp,
                            "instagram_tag_name": tag.name,
                        }
                        tag_ids.append(uniq_id)
                        objects_to_insert.append(tag_obj)

                    # now add this location to the user_feed_obj
                    media_obj["tags"] = tag_ids
                except:
                    pass

                objects_to_insert.append(media_obj)

            def update_cb(re):
                logging.debug("network harvest async worked {0}".format(re))
                pop_d.callback(True)

            def update_cb_fail(re):
                logging.error("network harvest async failed {0}".format(re))
                pop_d.errback

            # and create the instagram config
            instagram_config_obj = {
                "@id": "service_instagram_config",
                "app_object": appid,
                "type": "config",
                "config_last_updated_at": timestamp,
                "config_for_instagram_user": self.instagram_username,
                "friends_list_generated_at": timestamp,
                "follower_list_generated_at": timestamp,
                "friends_list_size": friends_number,
                "followers_list_size": followers_number,
                "since_id": since_id,
            }
            objects_to_insert.append(instagram_config_obj)

            self.insert_object_to_indx(objects_to_insert).addCallbacks(update_cb, update_cb_fail)

        def error_cb(re):
            found_cb()

        def_search = Deferred()
        find_instagram_config = {"@id": "service_instagram_config"}
        logging.info("Searching for instagram_config to check if Popular feed already harvested... ")
        def_search = self.indx_con.query(json.dumps(find_instagram_config))
        def_search.addCallbacks(found_cb, error_cb)

        return pop_d

    def find_user(self, username):
        data = self.api.user_search(username, count=20)
        print data

    def find_followers(self, userid):

        # first do a lookup and see if the latest set if followers has allready been found
        follower_d = Deferred()

        def found_cb(results):
            friends_number = 0
            followers_number = 0
            since_id = 0
            # let's see if the object has some nice things in it.
            try:
                config_returned = results["data"]["service_instagram_config"]
                friends_number = int(config_returned["friends_list_size"][0]["@value"])
                followers_number = int(config_returned["followers_list_size"][0]["@value"])
                since_id = int(config_returned["since_id"][0]["@value"])
                logging.info("Found the instagram Config Object.")
            except:
                # print sys.exc_info()
                pass
            followed_by = self.api.user_followed_by(userid)[0]
            # print "followed_by length: "+str(len(followed_by))
            # print str(followed_by)
            # see if the number is different, if it is, then update.. could be nicer than this, but for now, it will work (ish)
            if (len(followed_by) != followers_number) or (followers_number == 0):

                followers_number = len(followed_by)
                objects_to_insert = []
                current_timestamp = str(time.time()).split(".")[0]  # str(datetime.now())
                timestamp = str(datetime.now().isoformat("T")).split(".")[0]
                uniq_id = "instagram_follower_network_for_user_me"  # +self.instagram_username
                followed_by_obj = {
                    "@id": uniq_id,
                    "app_object": appid,
                    "instagram_username": self.instagram_username,
                    "instagram_user_id": self.instagram_user_id,
                    "timestamp": timestamp,
                    "followed_by_count": len(followed_by),
                }

                followers_ids = []
                for follower in followed_by:
                    # print follower.username
                    uniq_id = "instagram_user_" + str(follower.username)
                    follower_obj = {
                        "@id": uniq_id,
                        "app_object": appid,
                        "type": "user",
                        "instagram_username": str(follower.username),
                        "timestamp": timestamp,
                    }
                    objects_to_insert.append(follower_obj)
                    # we can add this to the followed_by_obj later
                    followers_ids.append(uniq_id)

                # link the followers for me
                followed_by_obj["follower_ids"] = followers_ids
                # print followed_by_objs
                # for friend in friends_list:
                # print friend
                # now append the results
                def update_cb(re):
                    logging.debug("network harvest async worked {0}".format(re))
                    follower_d.callback(True)

                def update_cb_fail(re):
                    logging.error("network harvest async failed {0}".format(re))
                    follower_d.errback

                instagram_config_obj = {
                    "@id": "service_instagram_config",
                    "app_object": appid,
                    "type": "config",
                    "config_last_updated_at": timestamp,
                    "config_for_instagram_user": self.instagram_username,
                    "friends_list_generated_at": timestamp,
                    "follower_list_generated_at": timestamp,
                    "friends_list_size": friends_number,
                    "followers_list_size": followers_number,
                    "since_id": since_id,
                }

                objects_to_insert.append(followed_by_obj)
                # objects_to_insert.append(followers)
                objects_to_insert.append(instagram_config_obj)

                self.insert_object_to_indx(objects_to_insert).addCallbacks(update_cb, update_cb_fail)
            else:
                follower_d.callback(True)

        def error_cb(re):
            found_cb()

        def_search = Deferred()
        find_instagram_config = {"@id": "service_instagram_config"}
        logging.info("Searching for instagram_config to check if network already harvested... ")
        def_search = self.indx_con.query(json.dumps(find_instagram_config))
        def_search.addCallbacks(found_cb, error_cb)

        return follower_d
        # print followed_by

    def find_friends(self, userid):
        friends_d = Deferred()

        def found_cb(results):
            friends_number = 0
            followers_number = 0
            since_id = 0
            # let's see if the object has some nice things in it.
            try:
                config_returned = results["data"]["service_instagram_config"]
                friends_number = int(config_returned["friends_list_size"][0]["@value"])
                followers_number = int(config_returned["followers_list_size"][0]["@value"])
                since_id = int(config_returned["since_id"][0]["@value"])
                logging.info("Found the instagram Config Object.")
            except:
                # print sys.exc_info()
                pass

            friends_by_list = self.api.user_follows(userid)[0]

            if (len(friends_by_list) != friends_number) or (friends_number == 0):

                friends_number = len(friends_by_list)
                objects_to_insert = []
                current_timestamp = str(time.time()).split(".")[0]  # str(datetime.now())
                timestamp = str(datetime.now().isoformat("T")).split(".")[0]
                uniq_id = "instagram_friends_network_for_user_me"  # +self.instagram_username
                friends_by_obj = {
                    "@id": uniq_id,
                    "app_object": appid,
                    "instagram_username": self.instagram_username,
                    "instagram_user_id": self.instagram_user_id,
                    "timestamp": timestamp,
                    "followed_by_count": len(friends_by_list),
                }

                friends_ids = []
                for friend in friends_by_list:
                    # print follower.username
                    uniq_id = "instagram_user_" + str(friend.username)
                    friend_obj = {
                        "@id": uniq_id,
                        "app_object": appid,
                        "type": "user",
                        "instagram_username": str(friend.username),
                        "timestamp": timestamp,
                    }
                    objects_to_insert.append(friend_obj)
                    # we can add this to the followed_by_obj later
                    friends_ids.append(uniq_id)

                friends_by_obj["friends_ids"] = friends_ids
                # print friends_by_objs
                # for friend in friends_list:
                # print friend
                # now append the results
                def update_cb(re):
                    logging.debug("network harvest async worked {0}".format(re))
                    friends_d.callback(True)

                def update_cb_fail(re):
                    logging.error("network harvest async failed {0}".format(re))
                    friends_d.errback

                instagram_config_obj = {
                    "@id": "service_instagram_config",
                    "app_object": appid,
                    "type": "config",
                    "config_last_updated_at": timestamp,
                    "config_for_instagram_user": self.instagram_username,
                    "friends_list_generated_at": timestamp,
                    "follower_list_generated_at": timestamp,
                    "friends_list_size": friends_number,
                    "followers_list_size": followers_number,
                    "since_id": since_id,
                }

                objects_to_insert.append(friends_by_obj)
                # objects_to_insert.append(followers)
                objects_to_insert.append(instagram_config_obj)

                self.insert_object_to_indx(objects_to_insert).addCallbacks(update_cb, update_cb_fail)
            else:
                friends_d.callback(True)

        def error_cb(re):
            found_cb()

        def_search = Deferred()
        find_instagram_config = {"@id": "service_instagram_config"}
        logging.info("Searching for instagram_config to check if network already harvested... ")
        def_search = self.indx_con.query(json.dumps(find_instagram_config))
        def_search.addCallbacks(found_cb, error_cb)

        return friends_d
        # print followed_by

    def subscribe_to_objects_by_tag(self, search_terms):
        def process_tag_update(update):
            print update

        reactor = subscriptions.SubscriptionsReactor()
        reactor.register_callback(subscriptions.SubscriptionType.TAG, process_tag_update)
        self.api.create_subscription(
            object="tag", object_id=search_terms, aspect="media", callback_url="http://*****:*****@version"]
            logging.debug(
                "Succesfully Updated INDX with Objects in update_cb, new diff version of {0}".format(self.version)
            )
            # logging.debug("Inserted Object into INDX: ".format(resp))
            update_d.callback(True)
            # return update_d
            # time.sleep(2)

        def exception_cb(e, service=self, obj=obj):
            logging.debug("Exception Inserting into INDX, probably wrong version given")
            if isinstance(e.value, urllib2.HTTPError):  # handle a version incorrect error, and update the version
                if e.value.code == 409:  # 409 Obsolete
                    response = e.value.read()
                    json_response = json.loads(response)
                    service.version = json_response["@version"]
                    logging.debug("INDX insert error in Instagram Service Object: " + str(response))
                    try:
                        service.indx_con.update(service.version, obj).addCallbacks(update_cb, exception_cb)
                        # logging.debug('Twitter Service - Successfully added Objects into Box')
                    except:
                        logging.error("Instagram Service, error on insert {0}".format(response))
                        update_d.errback(e.value)
                else:
                    logging.error("Instagram Service Unknow error: {0}".format(e.value.read()))
                    update_d.errback(e.value)
            else:
                logging.error("Error updating INDX: {0}".format(e.value))
                update_d.errback(e.value)

        logging.debug(
            "in Instagram - Trying to insert into indx...Current diff version: {0} and objects (len) given {1}".format(
                self.version, len(obj)
            )
        )
        self.indx_con.update(self.version, obj).addCallbacks(update_cb, exception_cb)

        return update_d