예제 #1
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")
예제 #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')
예제 #3
0
def myFollowing(): #written by Tim
    content = "<h2>Accounts I Follow</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'])



        follow_list, next_ = api.user_follows()
        counter =0
        content+="<ul>"

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

        content+="</ul>"

        content+="</h2>Total following 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)
예제 #4
0
 def handle(self, username, *args, **options):
     user = AggieUser.objects.get(username=username)
     account = BasicAccount.objects.get(user=user, type=BasicAccount.INSTAGRAM)
     api = InstagramAPI(access_token=account.oauth_token, client_secret=settings.INSTAGRAM_SECRET)
     feed, next_ = api.user_media_feed()
     follows, _ = api.user_follows(user.accounts.filter(type=BasicAccount.INSTAGRAM).values_list('account_id', flat=True)[0])
     while next_:
         for item in feed:
             if item.user.username in AggieUser.get_handles(user.get_friends()):
                 print item.caption, item.user, dir(item)
예제 #5
0
def User_Follows(page=1):
    u = InstagramAPI(access_token=session['access_token'],
                     client_secret=secrets['client_secret'])
    user_follows, next_ = u.user_follows()
    for i in range(1, page):
        user_follows, next_ = u.user_follows(with_next_url=next_)
    follows = []
    profile_images = []
    title = "User Follows-Page " + str(page)
    prev_page = False
    next_page = False
    if next_:
        prev_page = True
    if page != 1:
        next_page = True
    for link in user_follows:
        follows.append("{0}".format(link.username))
        profile_images.append("{0}".format(link.profile_picture))
    return render_template("recent.html", follows=follows,
                           prev=prev_page, next=next_page,
                           page=page, title=title)
예제 #6
0
class CleanFollows:
    def __init__(self, config_file):
        # Loading the configuration file, it has the access_token, user_id and others configs
        self.config = json.load(config_file)

        # Initializing the Instagram API with our access token
        self.api = InstagramAPI(access_token=self.config["access_token"],
                                client_secret=self.config['client_secret'])

    def run(self):
        while True:
            self.follows()

    def follows(self, next_page=None):
        print "next_page: ", next_page

        try:
            if next_page is None:
                follows, next_ = self.api.user_follows(
                    user_id=self.config['my_user_id'])
            else:
                follows, next_ = self.api.user_follows(
                    user_id=self.config['my_user_id'], with_next_url=next_page)

            for follow in follows:
                var = self.api.user_relationship(user_id=follow.id)
                print var.incoming_status, follow.id

                if var.incoming_status == 'none':
                    self.api.unfollow_user(user_id=follow.id)

                var = self.api.user_relationship(user_id=follow.id)
                print var.outgoing_status, follow.id

            return self.follows(next_)
        except Exception, e:
            print e
            time.sleep(1800)
            return self.follows(next_page)
예제 #7
0
class CleanFollows:
    def __init__(self, config_file):
        # Loading the configuration file, it has the access_token, user_id and others configs
        self.config = json.load(config_file)

        # Initializing the Instagram API with our access token
        self.api = InstagramAPI(access_token=self.config["access_token"], client_secret=self.config['client_secret'])

    def run(self):
        while True:
            self.follows()

    def follows(self, next_page=None):
        print "next_page: ", next_page

        try:
            if next_page is None:
                follows, next_ = self.api.user_follows(user_id=self.config['my_user_id'])
            else:
                follows, next_ = self.api.user_follows(user_id=self.config['my_user_id'], with_next_url=next_page)

            for follow in follows:
                var = self.api.user_relationship(user_id=follow.id)
                print var.incoming_status, follow.id

                if var.incoming_status == 'none':
                    self.api.unfollow_user(user_id=follow.id)

                var = self.api.user_relationship(user_id=follow.id)
                print var.outgoing_status, follow.id

            return self.follows(next_)
        except Exception, e:
            print e
            time.sleep(1800)
            return self.follows(next_page)
예제 #8
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
예제 #9
0
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
예제 #10
0
파일: views.py 프로젝트: brynn/slowdown
def edit_settings(request):
	user = get_object_or_404(User, username=request.user.username)
	user_profile = get_object_or_404(UserProfile, user=user)
	user_twitter = UserSocialAuth.objects.filter(user=user, provider='twitter')
	user_instagram = UserSocialAuth.objects.filter(user=user, provider='instagram')

	twitter_connected = False
	instagram_connected = False
	t_selected = ''
	i_selected = ''
	my_tweets = False
	my_photos = False
	local_time = datetime.datetime.utcnow().replace(tzinfo=utc)
	
	if user_twitter:
		twitter_connected = True
		if 'twitter_list' not in request.session:
			# connect to twitter api
			oauth_access_token=(user_twitter.get().tokens).get('oauth_token')
			oauth_access_secret=(user_twitter.get().tokens).get('oauth_token_secret')
			auth = tweepy.OAuthHandler(SOCIAL_AUTH_TWITTER_KEY, SOCIAL_AUTH_TWITTER_SECRET)
			auth.set_access_token(oauth_access_token, oauth_access_secret)
			api_twitter = tweepy.API(auth)	
			
			# get list of twitter friends
			request.session['twitter_list'] = []
			friends_ids = []
			try:
				friends_cursor = tweepy.Cursor(api_twitter.friends_ids,id=user_twitter.get().uid)
				for id in friends_cursor.items():
					friends_ids.append(id)
				for friends_page in paginate(friends_ids, 100):
					friends_objects = api_twitter.lookup_users(user_ids=friends_page)
					for friend in friends_objects:
						request.session['twitter_list'].append(friend.screen_name)
			except Exception:
				print >> sys.stderr, user_profile.user.username +': Editing settings, Twitter API error, tried to get friend list'
			
	if user_instagram:
		instagram_connected = True
		if 'instagram_list' not in request.session:
			# connect to instagram api
			token_instagram = user_instagram.get().tokens
			api_instagram = InstagramAPI(access_token=token_instagram)
			
			# get list of instagram friends
			request.session['instagram_list'] = []
			try:
				for page in api_instagram.user_follows(user_id=user_instagram.get().uid, as_generator=True, max_pages=15):
					for friend in page[0]:
						request.session['instagram_list'].append(friend.username)
			except Exception:
				print >> sys.stderr, user_profile.user.username +': Editing settings, Instagram API error, tried to get friend list'
	
	# construct string of currently subscribed to twitter users
	for t in user_profile.twitters.all():
		if t.uid and int(t.uid) == int(user_twitter.get().uid):
			my_tweets = True
		else:
			t_selected += t.username + ', '
	
	# construct string of currently subscribed to instagram users	
	for i in user_profile.instagrams.all():
		if i.uid and int(i.uid) == int(user_instagram.get().uid):
			my_photos = True
		else:
			i_selected += i.username + ', '

	# convert utc time to local time for rendering
	if user_profile.next_email_time:
		local_time = user_profile.next_email_time - timedelta(hours=user_profile.tz_offset)
		day_of_week = local_time.weekday()
		time_of_day = local_time.hour
	else:
		day_of_week = 0
		time_of_day = 9

	# render edit settings form
	return render_to_response('settings.html', {
   	 'user_profile' : user_profile,
   	 'instagram_connected' : instagram_connected,
   	 'twitter_connected' : twitter_connected,
	 'session' : request.session,
	 't_selected' : str(t_selected),
	 'i_selected' : str(i_selected),
	 'num_twitters_left' : 11 - len(t_selected.split(',')),
	 'num_instagrams_left' : 11 - len(i_selected.split(',')),
	 'my_tweets' : my_tweets,
	 'my_photos' : my_photos,
	 'day_of_week' : day_of_week,
	 'time_of_day' : time_of_day,
	 'local_time' : local_time

	}, RequestContext(request))

# script to go through all twitter and instagram database objects and get their profile pics, saving in case I need it again 	
# def add_urls(request):
# 	user_twitter = UserSocialAuth.objects.filter(user=request.user, provider='twitter')
# 	user_instagram = UserSocialAuth.objects.filter(user=request.user, provider='instagram')
# 	
# 	oauth_access_token=(user_twitter.get().tokens).get('oauth_token')
# 	oauth_access_secret=(user_twitter.get().tokens).get('oauth_token_secret')
# 	auth = tweepy.OAuthHandler(SOCIAL_AUTH_TWITTER_KEY, SOCIAL_AUTH_TWITTER_SECRET)
# 	auth.set_access_token(oauth_access_token, oauth_access_secret)
# 	api_twitter = tweepy.API(auth)
# 	
# 	token_instagram = user_instagram.get().tokens
# 	api_instagram = InstagramAPI(access_token=token_instagram)
# 	
# 	twitters = Twitter.objects.all()
# 	for t in twitters:
# 		t_user = api_twitter.get_user(screen_name=t.username)
# 		t.url = t_user.profile_image_url
# 		t.save()
# 	
# 	instagrams = Instagram.objects.all()
# 	for i in instagrams:
# 		i_user = api_instagram.user_search(i.username)[0]
# 		i.url = i_user.profile_picture
# 		i.save()
# 	return HttpResponseRedirect('/')
예제 #11
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
예제 #12
0
파일: account.py 프로젝트: jedestep/fwbots
class InstagramAccount(Account):
    def __init__(self,
                 client_id=None,
                 client_secret=None,
                 redirect_url=None,
                 access_token=None,
                 name=None,
                 ty=None,
                 **kwargs):
        self.client_id = client_id
        self.client_secret = client_secret
        self.redirect = redirect_url
        self.name = name
        self.ty = ty
        if access_token:
            self.access_token = access_token
            self.api = InstagramAPI(access_token=access_token)
        else:
            self.api = InstagramAPI(client_id=client_id,
                                    client_secret=client_secret,
                                    redirect_uri=redirect_url)
            url = self.api.get_authorize_login_url(
                scope=['basic', 'likes', 'comments', 'relationships'])
            print 'This account needs to be authenticated. Visit this url:'
            print url
            code = raw_input('Please enter the result code:').strip()
            self.access_token, user_info = self.api.exchange_code_for_access_token(
                code)
            db.instagram.update({'name': self.name},
                                {'$set': {
                                    'access_token': self.access_token
                                }})
            self.api = InstagramAPI(access_token=access_token)
        self.uid = self._get_uid(self.name)

    def log(self, action, details):
        Account.log(self, 'instagram', action, details)

    # Pick a popular thing and like it.
    def like_popular(self):
        self.log("like-popular", str(datetime.today()))
        popular = self.api.media_popular(count='20')
        for i in xrange(8):
            p = random.choice(popular)
            self.api.like_media(p.id)

    # Follow someone.
    def follow(self, un):
        uid = self._get_uid(un)
        # Bug in the official API call for this one. Needs direct HTTP
        payload = {'ACCESS_TOKEN': self.access_token, 'action': 'follow'}
        r = requests.post('https://api.instagram.com/v1/users/' + uid +
                          '/relationship?access_token=' + self.access_token,
                          data=payload)
        return r

    # Follow a friend of a friend.
    def follow_branch(self):
        friends = self.api.user_follows(self.uid)
        f = random.choice(friends[0])
        other_friends = self.api.user_follows(f.id)
        f2 = random.choice(other_friends[0])
        self.log("follow-branch", str(datetime.today()) + ',' + f2.username)
        self.follow(f2.username)
        return f2

    # make a generic comment
    # for now these comments are just positive
    def generic_comment_friend(self):
        #1. pick a friend
        friends = self.api.user_follows(self.uid)[0]
        f = random.choice(friends)

        #2. pick a dumb comment
        comment = random.choice(POSITIVE)

        #3. pick something they posted
        recent = self.api.user_recent_media(f.id)
        print recent
        post = random.choice(recent)
        self.log("generic-comment-friend",
                 str(datetime.today()) + ',' + str(post) + ',' + str(comment))

        #4. make a dumb comment on their dumb post
        self.api.create_media_comment(post.id, comment)

        return (post, comment)

    def generic_comment_feed(self):
        comment = random.choice(POSITIVE)
        post = random.choice(self.api.user_media_feed()[0])
        self.log("generic-comment-friend",
                 str(datetime.today()) + ',' + str(post) + ',' + str(comment))
        self.api.create_media_comment(post.id, comment)

    # like something a friend posted recently
    def like_friend(self):
        friends = self.api.user_follows(self.uid)[0]
        f = random.choice(friends)

        recent = self.api.user_recent_media(user_id=f.id, count=20)
        self.log("like-friends-post", str(datetime.today()) + ',' + f.username)
        post = random.choice(recent[0])
        self.api.like_media(post.id)
        return post

    # Helper to turn a username into a user id
    def _get_uid(self, un):
        uid = self.api.user_search(q=un)
        uid = uid[0]
        uid = uid.id
        return uid
예제 #13
0
access_token = "211585314.e1b377f.d45639f8eb394f2fa0ee3a8176311562"
api = InstagramAPI(access_token=access_token)

totalcount=api.user(211585314).counts['follows']

us=0
max_tag_id=0

users=set([])

while us<totalcount:


	if (max_tag_id == 0):
		page,next=api.user_follows( user_id = 211585314 )
	else:
		try:
			page,next=api.user_follows( user_id = 211585314, cursor=max_tag_id )

		except:
			break
	for user in page:
		if not(user.username in users):
			users.add(user.username)
			us=us+1

	try:
		m_obj=re.search(r".+&cursor=(.+)", next)
		max_tag_id=m_obj.group(1)
	except:
예제 #14
0
파일: follow.py 프로젝트: gdobler/cussac
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
from instagram.client import InstagramAPI
import indicoio
from collections import Counter

client_id = "da4a3124488840f582305f64b3557548"
client_secret = "e7dce52052b842fb909ac0c24b467afb"
access_token = "1147536024.bec82b4.fb48b565d9ad4fe09f64f63d64d4f664"
INDICO_API_KEY = '61cdd30af4bbdfe5a21b92689a872234'

api = InstagramAPI(access_token=access_token, client_secret=client_secret)
indicoio.config.api_key = INDICO_API_KEY

following, next = api.user_follows(user_id="self", count=10)
#print next
#temp, max_tag=next.split('max_tag_id=')
#max_tag = str(max_tag)

usernames = []
for f in following:
	isPrivate = api.user_relationship(user_id=f.id).target_user_is_private
	if isPrivate == False:
		usernames.append(f.id)
print len(usernames)

new_usernames = usernames[:]
for user in usernames:
	length = len(usernames)
	if length > 100000:
		break
	else:
		isPrivate = api.user_relationship(user_id=user).target_user_is_private
예제 #16
0
import random

filename="/home/bgp/DispatchDaemon/ins/"+"added.log"

fo = open(filename, "r+")
line = fo.read();
#print "Read String is : ", line
fo.close()

i=int(line)

if i>100 :         
	access_token = "211585314.e1b377f.d45639f8eb394f2fa0ee3a8176311562"
	api = InstagramAPI(access_token=access_token)

	for page,next in api.user_follows( user_id = 211585314, as_generator = True ):
		for follower in page:
			try:
				rel = api.user_relationship(user_id=follower.id)
				print follower
				if rel.incoming_status =='none' : 
	            			api.unfollow_user(user_id=follower.id)
	            			print 'removed'
		                	
			except:
				print 'api error'
				api.unfollow_user(user_id=follower.id)
			time.sleep(4*random.random())	
	os.remove(filename)
#	i=0
#	fo = open(filename, "wb")
예제 #17
0
"""
# This script written by Talha Havadar
"""
from instagram.client import InstagramAPI
from instagram.bind import InstagramAPIError
import sys

access_token = "XXXXXXX" # Your access token generated from 'get_access_token.py' script from python-instagram github repository
user_id = "XXXXXXX" # Your userid given by 'get_access_token.py' script
client_id = "XXXXXXX" # Your client id from instagram developer site
client_secret = "XXXXXXX" # Your client secret from instagram developer site
my_username = "******" # Your instagram username
api = InstagramAPI(access_token = access_token, client_secret = client_secret)
content, next_ = api.user_follows(user_id=user_id)


# Add your buddy's instagram username here to 'bros' list!!
bros = [
	"XXXXXXX",
	"XXXXXXX",
	"XXXXXXX",
	"XXXXXXX"
]

def amILikeThisPost(media_id = None):
	if media_id == None:
		print("Error: no parameter at amILikeThisPost please send media id.")
		sys.exit(1)
		pass
	likes = api.media_likes(media_id = media_id)
	amILike = False
예제 #18
0
    def InstagramProcces(self):

        self.aceptar.setCursor(QCursor(Qt.ForbiddenCursor))
        self.update_progressbar(10)

        access_token = self.lnToken.text()
        client_secret = self.lnAcces.text()
        user_id = self.lnId.text()

        if not access_token or not client_secret:
            QMessageBox.information(
                self, "Empty values",
                "Complete mandatory items <access_token> and <client_secret>",
                QMessageBox.AcceptRole)
            return
        try:
            api = InstagramAPI(access_token=access_token,
                               client_secret=client_secret)

            #Search recent media with Tag
            if self.TypeSearch == "hashtags":
                count = self.sp_count.value()
                tag = self.ln_tags.text()
                if tag == "":
                    QMessageBox.information(self, "Empty values",
                                            "Tag value is empty",
                                            QMessageBox.AcceptRole)
                    self.aceptar.setCursor(QCursor(Qt.PointingHandCursor))
                    self.update_progressbar(0)
                    return

                tag_search, next_tag = api.tag_search(tag)
                tag_recent_media, next = api.tag_recent_media(
                    count, tag_name=tag_search[0].name)
                if len(tag_recent_media) == 0: return self.Checklength()
                categorized, layer = self.CreateShape()
                for tag_media in tag_recent_media:
                    self.AddFeatures(tag_media, layer, categorized)

            #Search recent media with Location
            elif self.TypeSearch == "coords":
                lat = self.ln_lat.text()
                lng = self.ln_lng.text()
                distance = self.sp_distance.value()
                location_search = api.media_search(lat=str(lat),
                                                   lng=str(lng),
                                                   distance=int(distance))

                if len(location_search) == 0: return self.Checklength()
                categorized, layer = self.CreateShape()
                for location in location_search:
                    self.AddFeatures(location, layer, categorized)

            #Search recent media with user
            elif self.TypeSearch == "user":
                if self.lnId.text() == "":
                    QMessageBox.information(self, "Empty values",
                                            "User name value is empty",
                                            QMessageBox.AcceptRole)
                    self.aceptar.setCursor(QCursor(Qt.PointingHandCursor))
                    self.update_progressbar(0)
                    return

                user_name = self.lnId.text()
                user_search = api.user_search(user_name)

                if len(user_search) == 0: return self.Checklength()
                layer = self.CreateShapeMin()
                for user in user_search:
                    self.AddFeaturesMin(user, layer)

            #Search user recent
            elif self.TypeSearch == "user_recent":
                recent_media, next = api.user_recent_media()

                if len(recent_media) == 0: return self.Checklength()
                categorized, layer = self.CreateShape()
                for media in recent_media:
                    self.AddFeatures(media, layer, categorized)

            #Search User Media Feed
            elif self.TypeSearch == "user_media":
                media_feed, next = api.user_media_feed()

                if len(media_feed) == 0: return self.Checklength()
                categorized, layer = self.CreateShape()
                for media in media_feed:
                    self.AddFeatures(media, layer, categorized)

            #Search User follow
            elif self.TypeSearch == "user_follow":

                if self.lnId.text() == "":
                    QMessageBox.information(self, "Empty values",
                                            "User ID value is empty",
                                            QMessageBox.AcceptRole)
                    self.aceptar.setCursor(QCursor(Qt.PointingHandCursor))
                    self.update_progressbar(0)
                    return

                user_follows, next = api.user_follows(user_id)

                if len(user_follows) == 0: return self.Checklength()
                layer = self.CreateShapeMin()
                for user in user_follows:
                    self.AddFeaturesMin(user, layer)

            #Search Location recent
            elif self.TypeSearch == "location_recent":

                if self.ln_loc_id.text() == "":
                    QMessageBox.information(self, "Empty values",
                                            "Location ID value is empty",
                                            QMessageBox.AcceptRole)
                    self.aceptar.setCursor(QCursor(Qt.PointingHandCursor))
                    self.update_progressbar(0)
                    return

                location_id = int(self.ln_loc_id.text())
                recent_media, next = api.location_recent_media(
                    location_id=location_id)

                if len(recent_media) == 0: return self.Checklength()
                categorized, layer = self.CreateShape()
                for media in recent_media:
                    self.AddFeatures(media, layer, categorized)

            #Search recent popular
            elif self.TypeSearch == "popular":
                media_search = api.media_popular()

                if len(media_search) == 0: return self.Checklength()
                categorized, layer = self.CreateShape()
                for media in media_search:
                    self.AddFeatures(media, layer, categorized)

            #Save layer in output path
            QgsVectorFileWriter.writeAsVectorFormat(
                layer, self.settings.value("instagram2qgis/outpath"), "CP1250",
                None, "ESRI Shapefile")

            self.update_progressbar(100)

            self.aceptar.setCursor(QCursor(Qt.PointingHandCursor))

            self.reject()

        except Exception as e:
            self.iface.messageBar().pushMessage("Error: ",
                                                "fail to load photos: " +
                                                str(e),
                                                level=QgsMessageBar.CRITICAL,
                                                duration=20)
            self.aceptar.setCursor(QCursor(Qt.PointingHandCursor))

        return
예제 #19
0
# get user info
u = api.user()
# print user id, username, full name
print u.id, u.username, u.full_name
# print activity counts
print u.counts

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

# get followees
followees, next_ = api.user_follows()
while next_:
    more_followees, next_ = api.user_follows(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.user_media_feed(count=20)
for media in media_feed:
	# print user 
    print media.user
    # print caption
    if media.caption:
        print media.caption.text
예제 #20
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
예제 #21
0
class Instagram:
    def __init__(self, api_index):
        self.api = InstagramAPI(client_id=credentials.instagram_credentials[api_index]["client_id"],
                                client_secret=credentials.instagram_credentials[api_index]["client_secret"])
        self.access_token = credentials.instagram_credentials[api_index]["access_token"][0]
        return None

    def get_user(self, username):
        users = self.api.user_search(username)
        user_list = [u for u in users if u.username.lower() == username.lower()]
        if not user_list:
            print("Couldn't find Instagram information for " + username)
            return None
        else:
            return user_list[0]

    def check_rate_limit(self):
        while True:
            rl = self.api.x_ratelimit_remaining
            rl = int(rl) if rl else None
            if rl and rl < configs.min_instagram_rate_limit_remaining:
                print("RATE LIMIT: "+str(rl))
                time.sleep(configs.instagram_seconds_sleep_after_rate_limit)
                break
            else:
                print("rate limit: " + str(rl))
                break
        return True

    def get_user_by_id(self, user_id):
        self.check_rate_limit()
        try:
            return self.api.user(user_id)
        except:
            return None

    def get_user_data_dict(self, user):
        try:
            user_id = user.id
            response = urllib2.urlopen("https://api.instagram.com/v1/users/"+str(user_id)+"/?access_token="+self.access_token)
        except:
            return dict()
        data = json.load(response)
        info = {self.tkey(k): data["data"][k].encode("utf8") for k in ["username", "bio", "website", "full_name", "id"]}
        info["link"] = "https://instagram.com/"+info["username"]+"/"
        counts = {self.tkey(k): data["data"]["counts"][k] for k in ["media", "follows", "followed_by"]}
        info.update(counts)
        return info

    def get_follows(self, user_id):
        # rate limit issue happens here
        self.check_rate_limit()
        follows, next_ = self.api.user_follows(user_id)
        while next_:
            self.check_rate_limit()
            more_follows, next_ = self.api.user_follows(user_id, with_next_url=next_)
            follows.extend(more_follows)
        ret = [int(f.id) for f in follows if f.id]
        return ret

        
    @staticmethod
    def tkey(key):
        tk = {"full_name": "name", "media": "num_posts", 
                "follows": "num_follows", "followed_by": "num_followers",
                "bio": "description", "id": "user_id"}
        ret = tk[key] if key in tk else key
        ret = ret.encode("utf8")
        return ret
예제 #22
0
def myRecentLikes(): #written by Tim
    content = "<h2>User's Recent Likes</h2>"
    access_token = request.session['access_token']
    if not access_token:
        print "Missing Access Token"
        return 'Missing Access Token'
    try:

        api = InstagramAPI(access_token=access_token, client_secret=CONFIG['client_secret'])
        _user_id =(api.user()).id

        liked_media, next = api.user_liked_media(count=9)

        print "Webpage is loading...."


        counter = 0;
        photos = []
        filters = []
        usersThatLiked = []

        content += "<div id='liked_media'>"
        content +="<style>figure{   width:33.3%;   float:left;   margin:0px;   text-align:center;  padding:0px;} </style>"
        for media in liked_media:
            content += "<figure>"
            filters.append(media.filter)
            usersThatLiked.extend(api.media_likes(media_id = media.id))
            counter = counter +1

            #photos.append('<div style="float:left;">')
            if(media.type == 'video'):
                content += ('<video controls width height="150"><source type="video/mp4" src="%s"/></video>' % (media.get_standard_resolution_url()))
                #photos.append('<video controls width height="150"><source type="video/mp4" src="%s"/></video>' % (media.get_standard_resolution_url()))
            else:
                content+= ("<img src=%s/>" % (media.get_low_resolution_url()))
                content+= ("<figcaption>@%s" % (media.user.username))
                content+= "</figcaption>"
                #photos.append('<div class="floated_img"><img src="%s"/></div>' % (media.get_thumbnail_url()))

            content+="</figure>"


        content+= "</div><br>"


        filterCounter = Counter(filters) #makes a counter object based on the list of filters
        usersThatLikedCounter = Counter(usersThatLiked) #counts instances of any person liking the same pictures that the user did

        #outputs a ranked list of the filters used in the liked posts above
        content += "<h2> Filters used (count): </h2><ol>"
        for filterWithCount in filterCounter.most_common():
            content += "<li>" + filterWithCount[0] +"  ("+str(filterWithCount[1])+")</li>"
        content += "</ol>"

        #gets a list of people that our user follows (used to make accurate suggestions of people to follow)
        following_list, next_ = api.user_follows()

        #make a set of user id numbers
        following_ids = set()
        for user in following_list:
            following_ids.add(user.id)


        #outputs the most common users that liked the same media
        content += "<h2> Top users that also liked these posts: </h2><p>Below is a list of users who also liked the posts above, if you are not already following them, there will be a link.<ol>"
        for userWithCount in usersThatLikedCounter.most_common(11):
            if (userWithCount[0].id != _user_id): #makes sure that the current user is not displayed
                content += "<li>" + userWithCount[0].username +"  ("+str(userWithCount[1])+" similar likes)"
                if(userWithCount[0].id not in following_ids):
                    content += ("    <a href='/user_follow/%s'>Follow</a>" % (userWithCount[0].id))
                content +=    (" <p>Here's a link to their Instagram Profile:" )
                content += ("    <a href='https://www.instagram.com/%s'>instagram.com/%s</a></p></li>" % (userWithCount[0].username, userWithCount[0].username))
        content += "</ol>"

    except Exception as e:
        print "in exception ..."
        print(e)
    return "%s %s <br/>Remaining API Calls = %s/%s" % (get_nav(),content,api.x_ratelimit_remaining,api.x_ratelimit)
# ID do Eduardo? 224127472

from instagram.client import InstagramAPI

access_token = "315156082.25f5d28.2633198fd77246e28e73855d7a020a9a"
client_secret = "1befb051b1c146b28be2f58588172033"
api = InstagramAPI(access_token=access_token, client_secret=client_secret)

api.user_follows(user_id='315156082')
예제 #24
0
class Instagram:
    def __init__(self, api_index):
        self.api = InstagramAPI(
            client_id=credentials.instagram_credentials[api_index]
            ["client_id"],
            client_secret=credentials.instagram_credentials[api_index]
            ["client_secret"])
        self.access_token = credentials.instagram_credentials[api_index][
            "access_token"][0]
        return None

    def get_user(self, username):
        users = self.api.user_search(username)
        user_list = [
            u for u in users if u.username.lower() == username.lower()
        ]
        if not user_list:
            print("Couldn't find Instagram information for " + username)
            return None
        else:
            return user_list[0]

    def check_rate_limit(self):
        while True:
            rl = self.api.x_ratelimit_remaining
            rl = int(rl) if rl else None
            if rl and rl < configs.min_instagram_rate_limit_remaining:
                print("RATE LIMIT: " + str(rl))
                time.sleep(configs.instagram_seconds_sleep_after_rate_limit)
                break
            else:
                print("rate limit: " + str(rl))
                break
        return True

    def get_user_by_id(self, user_id):
        self.check_rate_limit()
        try:
            return self.api.user(user_id)
        except:
            return None

    def get_user_data_dict(self, user):
        try:
            user_id = user.id
            response = urllib2.urlopen("https://api.instagram.com/v1/users/" +
                                       str(user_id) + "/?access_token=" +
                                       self.access_token)
        except:
            return dict()
        data = json.load(response)
        info = {
            self.tkey(k): data["data"][k].encode("utf8")
            for k in ["username", "bio", "website", "full_name", "id"]
        }
        info["link"] = "https://instagram.com/" + info["username"] + "/"
        counts = {
            self.tkey(k): data["data"]["counts"][k]
            for k in ["media", "follows", "followed_by"]
        }
        info.update(counts)
        return info

    def get_follows(self, user_id):
        # rate limit issue happens here
        self.check_rate_limit()
        follows, next_ = self.api.user_follows(user_id)
        while next_:
            self.check_rate_limit()
            more_follows, next_ = self.api.user_follows(user_id,
                                                        with_next_url=next_)
            follows.extend(more_follows)
        ret = [int(f.id) for f in follows if f.id]
        return ret

    @staticmethod
    def tkey(key):
        tk = {
            "full_name": "name",
            "media": "num_posts",
            "follows": "num_follows",
            "followed_by": "num_followers",
            "bio": "description",
            "id": "user_id"
        }
        ret = tk[key] if key in tk else key
        ret = ret.encode("utf8")
        return ret
예제 #25
0
    def InstagramProcces(self):
        
        self.aceptar.setCursor(QCursor(Qt.ForbiddenCursor))
        self.update_progressbar(10)
        
        access_token = self.lnToken.text()
        client_secret = self.lnAcces.text()
        user_id=self.lnId.text()
 
        if not access_token or not client_secret:
            QMessageBox.information(self, "Empty values", "Complete mandatory items <access_token> and <client_secret>", QMessageBox.AcceptRole)
            return  
        try:
            api = InstagramAPI(access_token=access_token, client_secret=client_secret)
 
            #Search recent media with Tag       
            if self.TypeSearch=="hashtags":
                count=self.sp_count.value()  
                tag=self.ln_tags.text()
                if tag=="":
                    QMessageBox.information(self, "Empty values", "Tag value is empty", QMessageBox.AcceptRole)
                    self.aceptar.setCursor(QCursor(Qt.PointingHandCursor))
                    self.update_progressbar(0)
                    return  
            
                tag_search, next_tag = api.tag_search(tag)
                tag_recent_media, next = api.tag_recent_media(count,tag_name=tag_search[0].name)
                if len(tag_recent_media)==0:return self.Checklength()
                categorized,layer=self.CreateShape()
                for tag_media in tag_recent_media: 
                    self.AddFeatures(tag_media,layer,categorized)
                    
            #Search recent media with Location              
            elif self.TypeSearch=="coords":
                lat=self.ln_lat.text()
                lng=self.ln_lng.text()
                distance=self.sp_distance.value()                    
                location_search =api.media_search(lat=str(lat),lng=str(lng), distance=int(distance))  

                if len(location_search)==0:return self.Checklength()
                categorized,layer=self.CreateShape()
                for location in location_search:
                    self.AddFeatures(location,layer,categorized)          
  
            #Search recent media with user 
            elif self.TypeSearch=="user":                
                if self.lnId.text()=="":
                    QMessageBox.information(self, "Empty values", "User name value is empty", QMessageBox.AcceptRole)
                    self.aceptar.setCursor(QCursor(Qt.PointingHandCursor))
                    self.update_progressbar(0)
                    return
                
                user_name=self.lnId.text()
                user_search = api.user_search(user_name)

                if len(user_search)==0:return self.Checklength()
                layer=self.CreateShapeMin()
                for user in user_search:
                    self.AddFeaturesMin(user,layer)   

            #Search user recent 
            elif self.TypeSearch=="user_recent": 
                recent_media, next = api.user_recent_media()

                if len(recent_media)==0:return self.Checklength() 
                categorized,layer=self.CreateShape()
                for media in recent_media:
                    self.AddFeatures(media,layer,categorized) 
                          
            #Search User Media Feed    
            elif self.TypeSearch=="user_media":            
                media_feed, next = api.user_media_feed()

                if len(media_feed)==0:return self.Checklength() 
                categorized,layer=self.CreateShape()
                for media in media_feed:
                    self.AddFeatures(media,layer,categorized) 
            
            #Search User follow
            elif self.TypeSearch=="user_follow":
                
                if self.lnId.text()=="":
                    QMessageBox.information(self, "Empty values", "User ID value is empty", QMessageBox.AcceptRole)
                    self.aceptar.setCursor(QCursor(Qt.PointingHandCursor))
                    self.update_progressbar(0)
                    return 
                
                user_follows, next = api.user_follows(user_id)
                
                if len(user_follows)==0:return self.Checklength()
                layer=self.CreateShapeMin()
                for user in user_follows:
                    self.AddFeaturesMin(user,layer) 
             
            #Search Location recent
            elif self.TypeSearch=="location_recent":
                
                if self.ln_loc_id.text()=="":
                    QMessageBox.information(self, "Empty values", "Location ID value is empty", QMessageBox.AcceptRole)
                    self.aceptar.setCursor(QCursor(Qt.PointingHandCursor))
                    self.update_progressbar(0)
                    return  

                location_id=int(self.ln_loc_id.text())
                recent_media, next = api.location_recent_media(location_id=location_id)
                
                if len(recent_media)==0:return self.Checklength()
                categorized,layer=self.CreateShape()
                for media in recent_media:
                    self.AddFeatures(media,layer,categorized)
 
            #Search recent popular 
            elif self.TypeSearch=="popular": 
                media_search = api.media_popular()
                
                if len(media_search)==0:return self.Checklength() 
                categorized,layer=self.CreateShape()
                for media in media_search:
                    self.AddFeatures(media,layer,categorized)  
  
            #Save layer in output path
            QgsVectorFileWriter.writeAsVectorFormat(layer,self.settings.value("instagram2qgis/outpath"), "CP1250", None, "ESRI Shapefile")
 
            self.update_progressbar(100)

            self.aceptar.setCursor(QCursor(Qt.PointingHandCursor))
 
            self.reject()
            
        except Exception, e:
            self.iface.messageBar().pushMessage("Error: ", "fail to load photos: "+str(e),level=QgsMessageBar.CRITICAL, duration=20)             
            self.aceptar.setCursor(QCursor(Qt.PointingHandCursor))
예제 #26
0
파일: views.py 프로젝트: brynn/slowdown
def signup(request):
	
	user = get_object_or_404(User, username=request.user.username)
	user_profile = get_object_or_404(UserProfile, user=user)
	user_twitter = UserSocialAuth.objects.filter(user=user, provider='twitter')
	user_instagram = UserSocialAuth.objects.filter(user=user, provider='instagram') 
	twitter_connected = False  
	instagram_connected = False
	
	# connect to twitter api
	if user_twitter:
		twitter_connected = True
		oauth_access_token=(user_twitter.get().tokens).get('oauth_token')
		oauth_access_secret=(user_twitter.get().tokens).get('oauth_token_secret')
		auth = tweepy.OAuthHandler(SOCIAL_AUTH_TWITTER_KEY, SOCIAL_AUTH_TWITTER_SECRET)
		auth.set_access_token(oauth_access_token, oauth_access_secret)
		api_twitter = tweepy.API(auth)
	
	# connect to instagram api
	if user_instagram:
		instagram_connected = True
		token_instagram = user_instagram.get().tokens
		api_instagram = InstagramAPI(access_token=token_instagram)
	
	# if the user has already signed up, get profile pics and redirect to logged in home page
	if user.email:
	
		# if the user has signed up and just connected twitter via edit settings, stay on edit settings
		if user_twitter and len(user_profile.twitters.all()) == 0:
			return HttpResponseRedirect('/edit-settings/')
		
		# if the user has signed up and just connected instagram via edit settings, stay on edit settings
		if user_instagram and len(user_profile.instagrams.all()) == 0:
			return HttpResponseRedirect('/edit-settings/')
			
		# get twitter profile pics (and check that they are valid)
		if user_twitter and 'twitter_faces' not in request.session:
			request.session['twitter_faces'] = []
			for t in user_profile.twitters.all():
				if is_valid_image(t.url) and t.uid:
					request.session['twitter_faces'].append([t.username, t.url])
				else:
					try:
						t_user = api_twitter.get_user(screen_name=t.username)
						request.session['twitter_faces'].append([t.username, t_user.profile_image_url])
						t.url = t_user.profile_image_url
						t.uid = t_user.id
						t.save()
					except Exception:
						print >> sys.stderr, user_profile.user.username +': Logging in, Twitter API error, couldn\'t get Twitter object profile pic for ' + t.username
		
		# get instagram profile pics (and check that they are valid)		
		if user_instagram and 'insta_faces' not in request.session:
			request.session['insta_faces'] = []
			for i in user_profile.instagrams.all():
				if is_valid_image(i.url) and i.uid:
					request.session['insta_faces'].append([i.username, i.url])
				else:
					try:
						i_user = api_instagram.user_search(i_username)[0]
						request.session['insta_faces'].append([i.username, i_user.profile_picture])
						i.url = i_user.profile_picture
						i.uid = i_user.id
						i.save()
					except Exception:
						print >> sys.stderr, user_profile.user.username +': Logging, Instagram API error, couldn\'t get Instagram object profile pic for ' + i.username
		
		# convert UTC time to local time for rendering on logged in home page
		local_time = user_profile.next_email_time - timedelta(hours=user_profile.tz_offset)
		
		# redirect to logged in home page
		return render_to_response('done.html', {
			'user_profile' : user_profile,
			'session' : request.session,
			'local_time' : local_time,
		}, RequestContext(request))
    	
	else:
		# get list of twitter friends
		if user_twitter and 'twitter_list' not in request.session:
			request.session['twitter_list'] = [] 
# 			request.session['t_pickforme'] = ''
# 			t_counter = 0
			friends_ids = []
			try:
				friends_cursor = tweepy.Cursor(api_twitter.friends_ids,id=user_twitter.get().uid)
				for id in friends_cursor.items():
					friends_ids.append(id)
				for friends_page in paginate(friends_ids, 100):
					friends_objects = api_twitter.lookup_users(user_ids=friends_page)
					for friend in friends_objects:    				
						request.session['twitter_list'].append(friend.screen_name)
						#TODO: make this async yo
# 						if int(friend.followers_count) > 300 and int(friend.statuses_count) > 1000 and t_counter < 10:
# 							request.session['t_pickforme'] += friend.screen_name + ', '
# 							t_counter += 1
			except Exception:
				print >> sys.stderr, user_profile.user.username +': Signing up, Twitter API error, tried to get friend list'
		
		# get list of instagram friends
		if user_instagram and 'instagram_list' not in request.session:
			request.session['instagram_list'] = []
# 			request.session['i_pickforme'] = ''
# 			i_counter = 0
			try:
				for page in api_instagram.user_follows(user_id=user_instagram.get().uid, as_generator=True, max_pages=15):
					for friend in page[0]:
						request.session['instagram_list'].append(friend.username)
						#TODO: make this async yo
# 						if i_counter < 10:
# 							user_object = api_instagram.user(friend.id)
# 							if int(user_object.counts['followed_by']) > 300 and int(user_object.counts['media']) > 100:
# 								request.session['i_pickforme'] += friend.username + ', '
# 								i_counter += 1
			except Exception:
				print >> sys.stderr, user_profile.user.username +': Signing up, Instagram API error, tried to get friend list'
		
		# render sign up form
		return render_to_response('signup.html', {
		 'user_profile' : user_profile,
		 'instagram_connected' : instagram_connected,
		 'twitter_connected' : twitter_connected,
		 'session' : request.session,
		}, RequestContext(request))
    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
예제 #28
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')
예제 #29
0
파일: account.py 프로젝트: jedestep/fwbots
class InstagramAccount(Account):
    def __init__(self, client_id=None,client_secret=None,redirect_url=None,access_token=None,name=None,ty=None,**kwargs):
        self.client_id = client_id
        self.client_secret = client_secret
        self.redirect = redirect_url
        self.name = name
        self.ty = ty
        if access_token:
            self.access_token = access_token
            self.api = InstagramAPI(access_token=access_token)
        else:
            self.api = InstagramAPI(client_id=client_id,client_secret=client_secret,redirect_uri=redirect_url)
            url = self.api.get_authorize_login_url(scope=['basic','likes','comments','relationships'])
            print 'This account needs to be authenticated. Visit this url:'
            print url
            code = raw_input('Please enter the result code:').strip()
            self.access_token, user_info = self.api.exchange_code_for_access_token(code)
            db.instagram.update({'name':self.name},{'$set':{'access_token':self.access_token}})
            self.api = InstagramAPI(access_token = access_token)
        self.uid = self._get_uid(self.name)

    def log(self,action,details):
        Account.log(self,'instagram',action,details)

    # Pick a popular thing and like it.
    def like_popular(self):
        self.log("like-popular",str(datetime.today()))
        popular = self.api.media_popular(count='20')
        for i in xrange(8):
            p = random.choice(popular)
            self.api.like_media(p.id)

    # Follow someone.
    def follow(self,un):
        uid = self._get_uid(un)
        # Bug in the official API call for this one. Needs direct HTTP
        payload = {'ACCESS_TOKEN':self.access_token,'action':'follow'}
        r = requests.post('https://api.instagram.com/v1/users/'+uid+'/relationship?access_token='+self.access_token,data=payload)
        return r

    # Follow a friend of a friend.
    def follow_branch(self):
        friends = self.api.user_follows(self.uid)
        f = random.choice(friends[0])
        other_friends = self.api.user_follows(f.id)
        f2 = random.choice(other_friends[0])
        self.log("follow-branch",str(datetime.today())+','+f2.username)
        self.follow(f2.username)
        return f2

    # make a generic comment
    # for now these comments are just positive
    def generic_comment_friend(self):
        #1. pick a friend
        friends = self.api.user_follows(self.uid)[0]
        f = random.choice(friends)

        #2. pick a dumb comment
        comment = random.choice(POSITIVE)

        #3. pick something they posted
        recent = self.api.user_recent_media(f.id)
        print recent
        post = random.choice(recent)
        self.log("generic-comment-friend",str(datetime.today())+','+str(post)+','+str(comment))

        #4. make a dumb comment on their dumb post
        self.api.create_media_comment(post.id,comment)

        return (post,comment)

    def generic_comment_feed(self):
        comment = random.choice(POSITIVE)
        post = random.choice(self.api.user_media_feed()[0])
        self.log("generic-comment-friend",str(datetime.today())+','+str(post)+','+str(comment))
        self.api.create_media_comment(post.id,comment)

    # like something a friend posted recently
    def like_friend(self):
        friends = self.api.user_follows(self.uid)[0]
        f = random.choice(friends)

        recent = self.api.user_recent_media(user_id=f.id,count=20)
        self.log("like-friends-post",str(datetime.today())+','+f.username)
        post = random.choice(recent[0])
        self.api.like_media(post.id)
        return post

    # Helper to turn a username into a user id
    def _get_uid(self,un):
        uid = self.api.user_search(q=un)
        uid = uid[0]
        uid = uid.id
        return uid
def user_follow(id,name):
    global k
    global useridparent
    global usersparent
    global username_uni
    global l
    k=k+1
    d=0
    api = InstagramAPI(access_token=access_token[k%4], client_secret=client_secret[k%4])
    #print k
    try:

        content=[]
        user_follows, next = api.user_follows(id)
        match= (x for x in username_uni)
        if(not (name==match)):
            username_uni.append(name)
            l=l+1
            print l
            for user in user_follows:



                ufname=user.username
                ufid=user.id

                unamel=[name,ufname]
                uidl=[id,ufid]
                content = "," .join(map(str,unamel))
            # write  username into csv document
                w.writerow(unamel)
                fp_txt.writelines(content)
                fp_txt.write('\n')
                useridparent.append(uidl)
                usersparent.append(unamel)


                d=d+1

            #print len(users)
            while next:
            #print next
            #print usersparent
                user_follows, next = api.user_follows(with_next_url=next)
            #print user.id
                for user in user_follows:
                    ufname=user.username
                    ufid=user.id
                    unamel=[name,ufname]
                    uidl=[id,ufid]
                    content = "," .join(map(str,unamel))
                # write  username into csv document
                    w.writerow(unamel)
                    fp_txt.writelines(content)
                    fp_txt.write('\n')

                    usersparent.append(unamel)
                    useridparent.append(uidl)

                    d=d+1


            degree.append(d)
            fp_degree.write(str(d))
            fp_degree.write('\n')




            #print usersparent
        #for user in usersparent:
            #fp_init.write(user)
           # fp_init.write('\n')

            #print useridparent
            #print content
            #print len(usersparent)
            #print 'length of userid',len(useridparent)

    except Exception as e:
        print(e)