def get_instagram(key): api = InstagramAPI(client_id=key['client_id'], client_secret=key['client_secret']) popular_media = api.media_popular(count=20) for media in popular_media: img_url = media.images['standard_resolution'].url print img_url
def get_conference_pictures(self, keyword): json_data = [] # Cassandra initialization cluster = Cluster([config.get("cassandra.host1"), config.get("cassandra.host2")]) session = cluster.connect('insight') # Instagram initialization instagram_api = InstagramAPI(client_id=config.get("instagram.client_id"), client_secret=config.get("instagram.client_secret")) yymmdd = datetime.utcnow().strftime('%y') + datetime.utcnow().strftime('%m') + datetime.utcnow().strftime('%d') rows = session.execute(self.TOP_10_QUERY % (yymmdd)) for (yymmdd, count, word) in rows: img_arr = [] popular_media = instagram_api.media_popular(count=20) for media in popular_media: img_arr.append(media.images['standard_resolution'].url) json_data.append({"word": word, "count": count, "pic_url": img_arr}) return json_data
class VenuePhotoCrawlerInstagram: def __init__(self): self.client = InstagramAPI( client_id=config.instagram_client_id, client_secret=config.instagram_client_secret) def fetch_instagram_id(self, foursquare_id): print 'foursquare id is ', foursquare_id res = self.client.location_search(foursquare_v2_id=foursquare_id) return res[0].id def show_popular(self): popular_media = self.client.media_popular(20) for media in popular_media: print media.caption.text def grab_photos(self, foursquare_id, max_pages, min_timestamp): try: instagram_id = self.fetch_instagram_id(foursquare_id) gen = self.client.location_recent_media( count=200, location_id=instagram_id, as_generator=True, max_pages=max_pages, min_timestamp=min_timestamp) page_cnt = 0 except: return for page in gen: save_photo_instagram(page[0], foursquare_id, instagram_id) print 'fetching page', page_cnt page_cnt += 1 time.sleep(config.instagram_API_pause)
def test_insta(self, options=None): api = InstagramAPI(client_id=config.get("instagram.client_id"), client_secret=config.get("instagram.client_secret")) popular_media = api.media_popular(count=20) for media in popular_media: print(media.images['standard_resolution'].url)
def test_insta(self, options=None): api = InstagramAPI(client_id=config.get("instagram.client_id"), client_secret=config.get("instagram.client_secret")) popular_media = api.media_popular(count=20) for media in popular_media: print(media.images['standard_resolution'].url)
def createImageFiles(): api = InstagramAPI(client_id='7f674565db0d42ed9b4dd4f366db65be', client_secret='9a5f5c38eec74eff9f479e3659d4824f') popular_media = api.media_popular(count=20) i = 0 #Stores the pictures into .jpg format, in an easy to access order for media in popular_media: url = media.images['standard_resolution'].url urllib.urlretrieve(url, "file"+ str(i) +".jpg") i += 1
def getPopularImages(self, imageType, count): images = [] api = InstagramAPI(client_id='e33a8515b0cc4bc8aef4e7b086b736b1', client_secret='b50e5753efaa40ff8953e24fb8126616') popular_media = api.media_popular(count=count) for media in popular_media: images.append(media.images[imageType].url) return images
def getPopularImages(self, imageType, count): images = [] api = InstagramAPI(client_id='e33a8515b0cc4bc8aef4e7b086b736b1', client_secret='b50e5753efaa40ff8953e24fb8126616') popular_media = api.media_popular(count=count) for media in popular_media: images.append(media.images[imageType].url) return images
def setup(): api = InstagramAPI(client_id='360cc24cb8534f7cbbfb06d7ee1e26a3', client_secret='4e1026ba847841eab8797101ceb9680a') popular_media = api.media_popular(count=20) returnThis = list() for media in popular_media: returnThis.append(media.images['standard_resolution'].url) print (returnThis) return returnThis #just for the push
def do_job(self): api = InstagramAPI(client_id='1a748d8ab1ad48ab8e97b63c5d962355', client_secret='c9388c7acb064b779a7e5a3b4c5a7c12') photos = api.media_popular(count=20) photo = random.choice(photos) self._request.shout('"{caption}" by {user}: {url}'.format( url=photo.link, user=photo.user.full_name, caption=photo.caption.text, ))
def Popular_Media(): u = InstagramAPI(access_token=session['access_token'], client_secret=secrets['client_secret']) popular_media = u.media_popular() photos_thumbnail = [] photos_standard = [] title = "Popular Media-Page " for media in popular_media: photos_thumbnail.append("%s" % media.images['thumbnail'].url) photos_standard.append("%s" % media.images['standard_resolution'].url) return render_template("recent.html", thumb=photos_thumbnail, photos=photos_standard, title=title)
def do_job(self): api = InstagramAPI(client_id='1a748d8ab1ad48ab8e97b63c5d962355', client_secret='c9388c7acb064b779a7e5a3b4c5a7c12') photos = api.media_popular(count=20) photo = random.choice(photos) self._request.shout( '"{caption}" by {user}: {url}'.format( url=photo.link, user=photo.user.full_name, caption=photo.caption.text, ) )
def main(): client = client_packet(client_id='923490f7659a44fb8a83db1a4134992f', client_secret='58999a8bbb0c42e78213585de310cafe', redirect_uri='https://192.168.0.82', begin_user_id='190353205') # access_token = client.Access_token(True) # api = InstagramAPI(client_id=client.client_id, client_secret=client.client_secret, access_token=access_token[0]) api = InstagramAPI(client_id=client.client_id, client_secret=client.client_secret) print('start searching') popular_media = api.media_popular(count=20) print('finish searching') for media in popular_media: print(media.images['standard_resolution'].url) user = api.user('190353205')
def get_most_popular(): count = request.args.get('count', MAX_COUNT) max_id = request.args.get('max_id', MAX_ID) api = InstagramAPI(access_token=session['access_token']) recent_media = api.media_popular(count, max_id) templateData = { 'size': request.args.get('size', 'thumb'), 'media': recent_media } return render_template('media.html', **templateData)
def instagrams(request): api = InstagramAPI(client_id=INSTAGRAM_CLIENT_ID, client_secret=INSTAGRAM_CLIENT_SECRET) #get popular images popular_media = api.media_popular(count=20) #add popular images p = [] for media in popular_media: p.append(media.images['standard_resolution'].url) #pass to template return render_to_response("pages/index.html", {'popular': p, 'page_title': 'Top photos from Instagram'})
class InstagramPopularMediaNode(template.Node): """ Template node for displaying the popular media of Instagram. The API doesn't need authentication, just basic access. """ def __init__(self, count): self.count = count self.api = InstagramAPI(client_id=DJANGO_INSTAGRAM_CLIENT_ID) def render(self, context): popular_media = self.api.media_popular(self.count) context['popular_media'] = popular_media return ''
def instagram(request): api = InstagramAPI(client_id='CLIENT_ID', client_secret='CLIENT_SECRET_KEY') #get popular images popular_media = api.media_popular(count=20) #add popular images p = [] for media in popular_media: print media.images['standard_resolution'].url p.append(media.images['standard_resolution'].url) #pass to template return render_to_response("example/index.html", {'popular': p})
class VenuePhotoCrawlerInstagram: def __init__(self): self.client = InstagramAPI(client_id = config.instagram_client_id, client_secret = config.instagram_client_secret) def fetch_instagram_id(self, foursquare_id): print 'foursquare id is ',foursquare_id res = self.client.location_search(foursquare_v2_id=foursquare_id) return res[0].id def show_popular(self): popular_media = self.client.media_popular(20) for media in popular_media: print media.caption.text def grab_photos(self, foursquare_id, max_pages): try: instagram_id = self.fetch_instagram_id(foursquare_id) gen = self.client.location_recent_media(count=200, location_id = instagram_id, as_generator=True, max_pages=max_pages)#, return_json=True) page_cnt = 0 except: return for page in gen: save_photo_instagram(page[0], foursquare_id, instagram_id) print 'fetching page',page_cnt page_cnt+=1 time.sleep(config.instagram_API_pause)
def get_conference_pictures(self, keyword): json_data = [] # Cassandra initialization cluster = Cluster( [config.get("cassandra.host1"), config.get("cassandra.host2")]) session = cluster.connect('insight') # Instagram initialization instagram_api = InstagramAPI( client_id=config.get("instagram.client_id"), client_secret=config.get("instagram.client_secret")) yymmdd = datetime.utcnow().strftime('%y') + datetime.utcnow().strftime( '%m') + datetime.utcnow().strftime('%d') rows = session.execute(self.TOP_10_QUERY % (yymmdd)) for (yymmdd, count, word) in rows: img_arr = [] popular_media = instagram_api.media_popular(count=20) for media in popular_media: img_arr.append(media.images['standard_resolution'].url) json_data.append({ "word": word, "count": count, "pic_url": img_arr }) return json_data
def real_time(self, keyword): start_time = time.time() json_data = [] # Cassandra initialization cluster = Cluster( [config.get("cassandra.host1"), config.get("cassandra.host2")]) session = cluster.connect('insight') # Instagram initialization instagram_api = InstagramAPI( client_id=config.get("instagram.client_id"), client_secret=config.get("instagram.client_secret")) lat_l, lng_l = 36.8, -122.75 lat_h, lng_h = 37.8, -121.75 word_data = {} timestamp = datetime.utcnow().strftime('%y') + datetime.utcnow( ).strftime('%m') + datetime.utcnow().strftime( '%d') + datetime.utcnow().strftime('%H') tweeted_words = session.execute(self.QUERY_TWEETS % timestamp) for (yymmddhh, lat, lng, timestamp, data) in tweeted_words: word_count_pairs = [pair for pair in data.split(":")] # if lat_l < lat and lat_h > lat and lng_l < lng and lng_h > lng: # >>> for pair in word_count_pairs: data = pair.split(",") word = data[0].encode("utf-8") count = int(data[1].encode("utf-8")) if word == "OVERALL_CNT": continue if word not in word_data: word_data[word] = count else: word_data[word] += count # In place sort by number of words in desc order sorted_by_loc = sorted(word_data.items(), key=operator.itemgetter(1)) sorted_by_loc.reverse() # Get data from Instagram img_arr = [] popular_media = instagram_api.media_popular(count=80) for media in popular_media: img_arr.append(media.images['standard_resolution'].url) for i in range(10): word = sorted_by_loc[i][0] count = sorted_by_loc[i][1] json_data.append({ "word": word, "count": count, "pic_url": img_arr[(i * 4):((i + 1) * 4)] }) print("Realtime API exec time for Instance is " + str(time.time() - start_time)) return json_data
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
#not working from instagram.client import InstagramAPI num_top = input("View top how many photos?") api = InstagramAPI(client_id="97437151d4c9456bb6c2b36a500e93b4", client_secret="11fb88b434d94820a23e2d32dca85a43") popular_media = api.media_popular(count=num_top) for media in popular_media: print media.images['standard_resolution'].url
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 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))
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
# Script built while exploring the Instagram API from instagram.client import InstagramAPI # Initialize our API client_id = "" access_token = "" client_secret = "" api = InstagramAPI(access_token=access_token, client_id=client_id, client_secret=client_secret) # Retrieve a post from popular media media = api.media_popular(count=1)[0] for user in media.likes: # Grab a sample user from the post like uname = user.username 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
class InstagramFeed(object): _logger = inject.attr(Logger) _settingsController = inject.attr(SettingsController) _databaseController = inject.attr(DatabaseController) _tableName = 'InstagramFeed' def __init__(self): self._logger.debug('InstagramFeed::__init__') self._initApi() self._initTable() def getNext(self): self._logger.debug('InstagramFeed::getNext') if not self._hasNext(): self._fetch() return self._getNextMediaFromDatabase() def like(self, id): self._logger.debug('InstagramFeed::like') self._api.like_media(id) def _initApi(self): self._logger.debug('InstagramFeed::_initApi') clientId = self._settingsController.get('CLIENT_ID') clientSecret = self._settingsController.get('SECRET_ID') accessToken = self._settingsController.get('ACCESS_TOKEN') self._api = InstagramAPI(client_id=clientId, client_secret=clientSecret, access_token=accessToken) def _initTable(self): self._logger.debug('InstagramFeed::_initTable') sql = 'create table if not exists {0} (id text, url text)'.format(self._tableName) self._databaseController.executeCommand(sql) def _hasNext(self): self._logger.debug('InstagramFeed::_hasNext') sql = 'select count(*) from {0}'.format(self._tableName) return self._databaseController.executeQuery(sql)[0][0] > 0 def _getNextMediaFromDatabase(self): self._logger.debug('InstagramFeed::_getNextMediaFromDatabase') sql = "select id, url from {0} order by id desc limit 0, 1".format(self._tableName) result = self._databaseController.executeQuery(sql) media = Media(result[0][0], result[0][1]) media.delete() return media def _fetch(self): self._logger.debug('InstagramFeed::_fetch') currentFeed = self._settingsController.get('CURRENT_FEED') medias = None if currentFeed == 'popular': medias = self._fetchPopularFeed() elif currentFeed == 'personal': medias = self._fetchPersonalFeed() if medias: self._saveMedias(medias) def _fetchPopularFeed(self): self._logger.debug('InstagramFeed::_fetchPopularFeed') return self._api.media_popular(count=40) def _fetchPersonalFeed(self): self._logger.debug('InstagramFeed::_fetchPersonalFeed') return self._api.user_media_feed(count=40)[0] def _saveMedias(self, medias): self._logger.debug('InstagramFeed::_saveMedias') for media in medias: Media(media.id, media.images['standard_resolution'].url).create()
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
# Script built while exploring the Instagram API from instagram.client import InstagramAPI # Initialize our API client_id = '' access_token = '' client_secret = '' api = InstagramAPI(access_token=access_token, client_id=client_id, client_secret=client_secret) # Retrieve a post from popular media media = api.media_popular(count=1)[0] for user in media.likes: # Grab a sample user from the post like uname = user.username 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])
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
from time import sleep from instagram.client import InstagramAPI import json with open('config.json') as data_file: data = json.load(data_file) access_token = data["access_token"] client_id = data["client_id"] client_secret = data["client_secret"] api = InstagramAPI(access_token=access_token, client_id=client_id, client_secret=client_secret) #tags = api.tag("food") #print tags.media_count filtered_media = api.media_popular(count=10) #print "hello %s" % (filtered_media) for media in filtered_media: print "hello %s" % (media)
def real_time(self, keyword): start_time = time.time() json_data = [] # Cassandra initialization cluster = Cluster([config.get("cassandra.host1"), config.get("cassandra.host2")]) session = cluster.connect('insight') # Instagram initialization instagram_api = InstagramAPI(client_id=config.get("instagram.client_id"), client_secret=config.get("instagram.client_secret")) lat_l, lng_l = 36.8, -122.75 lat_h, lng_h = 37.8, -121.75 word_data = {} timestamp = datetime.utcnow().strftime('%y') + datetime.utcnow().strftime('%m') + datetime.utcnow().strftime('%d') + datetime.utcnow().strftime('%H') tweeted_words = session.execute(self.QUERY_TWEETS % timestamp) for (yymmddhh, lat, lng, timestamp, data) in tweeted_words: word_count_pairs = [pair for pair in data.split(":")] # if lat_l < lat and lat_h > lat and lng_l < lng and lng_h > lng: # >>> for pair in word_count_pairs: data = pair.split(",") word = data[0].encode("utf-8") count = int(data[1].encode("utf-8")) if word == "OVERALL_CNT": continue if word not in word_data: word_data[word] = count else: word_data[word] += count # In place sort by number of words in desc order sorted_by_loc = sorted(word_data.items(), key=operator.itemgetter(1)) sorted_by_loc.reverse() # Get data from Instagram img_arr = [] popular_media = instagram_api.media_popular(count=80) for media in popular_media: img_arr.append(media.images['standard_resolution'].url) for i in range(10): word = sorted_by_loc[i][0] count = sorted_by_loc[i][1] json_data.append({"word": word, "count": count, "pic_url": img_arr[(i * 4):((i + 1) * 4)]}) print("Realtime API exec time for Instance is " + str(time.time() - start_time)) return json_data
from instagram.client import InstagramAPI client_id = "c1c440a28cc945228d0d70a3f4b51a9e" client_secret = "68a904567236aa03144ac0919643c8a2" api = InstagramAPI(client_id=client_id, client_secret=client_secret) popular_media = api.media_popular(count=20) for media in popular_media: print(media.images['standard_resolution'].url)
from instagram.client import InstagramAPI api = InstagramAPI(client_id="YOUR_CLIENT_ID", client_secret="YOUR_CLIENT_SECRET") popular_media = api.media_popular(count=5) for media in popular_media: print media.images["standard_resolution"].url
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