def tag_search(): api = InstagramAPI(client_id='b64f54dc4fb3486f87b55d92381e2625', client_secret='b5fa80c366b94cc980c882855630fe92') tag_search, next_tag = api.tag_search(q="thefuturepark") tag_recent_media, next = api.tag_recent_media(count=1000,tag_name="thefuturepark") photos = [] content = "<h2>Media Search</h2>" for tag_media in reversed(tag_recent_media): # print tag_media res = tag_media.caption.text retweet = 0 favorites = tag_media.like_count name = tag_media.user.username real = tag_media.user.full_name pic = tag_media.user.profile_picture followers = 0 # date = unicode(tag_media.created_time.replace(microsecond=0)) date = tag_media.created_time print date embed = tag_media.get_standard_resolution_url() enable = True photos.append('<img src="%s"/>' % tag_media.get_standard_resolution_url()) photos.append(tag_media.caption.text) data = models.Trace.query.filter(models.Trace.key==res, models.Trace.name ==name) if data.count() > 0: print "kaparehas" else: print "wala" t = models.Trace(tweet='thefuturepark', key=res, retweet=retweet, favorites=favorites, name=name, real=real, pic=pic, followers=followers, date=date,embed=embed,enable=enable) db.session.add(t) db.session.commit() content += ''.join(photos) return content
def Tag_Search(page=1): if request.method == "POST": query = request.form["query"] if not query: e = "Please Enter something." return render_template("search.html", error=e, title="Tag Search") u = InstagramAPI(access_token=session['access_token'], client_secret=secrets['client_secret']) tag_search, next_tag = u.tag_search(q=query) tag_recent_media, next_ = u.tag_recent_media(tag_name=tag_search[0] .name) for i in range(1, page): tag_recent_media, next_ = u.tag_recent_media(tag_name=tag_search[0] .name, with_next_url=next_) tags = [] imgs = [] title = "Tag Search-Page " + str(page) prev_page = False next_page = False if next_: prev_page = True if page != 1: next_page = True # for media in tag_recent_media: # tags.append("{}".format(media.get_thumbnail_url())) # tags.append("{}".format(media.get_standard_url())) return render_template("search.html", tags=tags, imgs=imgs, prev=prev_page, next=next_page, page=page, title=title) return render_template("search.html")
def get_latest_photos(hashtag, num): api = InstagramAPI(access_token=token, client_secret=CONFIG['client_secret']) result, next_tag = api.tag_search(q=hashtag) tag_recent_media, next = api.tag_recent_media(count=num, tag_name=(result[0].name if len(result) > 0 else hashtag)) photos = [] for tag_media in tag_recent_media: instaphoto = instagram_photo(tag_media) photos.append(instaphoto) return photos
def _instagram(tag): access_token = "" api = InstagramAPI(access_token=access_token) tag_search, next_tag = api.tag_search(q=tag) if tag_search: tag_recent_media, next = api.tag_recent_media( tag_name=tag_search[0].name) return tag_recent_media return []
def add_activity_page(self): access_token = "[INSERT ACCESS TOKEN]" client_secret = "[INSERT CLIENT SECRET]" api = InstagramAPI(access_token=access_token, client_secret=client_secret) recent_media, url = api.user_recent_media() photos_travel=[] tag_search, next_tag = api.tag_search(q="travel") tag_recent_media, next = api.tag_recent_media(tag_name=tag_search[0].name) for tag_media in tag_recent_media: photos_travel.append("%s" % tag_media.get_standard_resolution_url()) photos_outdoor=[] tag_search, next_tag = api.tag_search(q="outdoor") tag_recent_media, next = api.tag_recent_media(tag_name=tag_search[0].name) for tag_media in tag_recent_media: photos_outdoor.append("%s" % tag_media.get_standard_resolution_url()) photos_indoor=[] tag_search, next_tag = api.tag_search(q="indoor") tag_recent_media, next = api.tag_recent_media(tag_name=tag_search[0].name) for tag_media in tag_recent_media: photos_indoor.append("%s" % tag_media.get_standard_resolution_url()) info = { "user_id": session["user_id"]} activities = self.models["User"].get_all_activities(info) return self.load_view("/users/add_activity.html", activities=activities, recent_media=recent_media, url = url, photos_travel = photos_travel, photos_indoor=photos_indoor, photos_outdoor=photos_outdoor)
class InstagramSearch: """ this class wraps the instagram api functionality """ def __init__(self, access_token=None): if not access_token: access_token=ACCESS_TOKEN self.api = InstagramAPI(access_token=access_token) self.api_calls = 0 def tag_recent_media(self, tag_name, pages=1, count=25): """ Searches instagram api for tag_name and returns serialized dictionary of media """ duplicates = 0 tag_recent_media = {} max_id = None # could try except this to prevent timeouts for pg in range(pages): new_media = 0 print "searching for {0} with id {1} on loop {2}".format(tag_name, max_id, pg) media, callback = self.api.tag_recent_media(count, max_id, tag_name=tag_name) self.api_calls += 1 for each in media: if each.id in tag_recent_media: duplicates += 1 else: new_media += 1 tag_recent_media[each.id] = serialize_media(each) print "pg {0} --> found {1} new media".format(pg, new_media) print callback max_id = int(callback.split("&")[-1].split("=")[1]) print "query: {0} --> retrieved {1} items --> {2} duplicates".format(tag_name, len(tag_recent_media), duplicates) return tag_recent_media def tag_search(self, tag): return self.api.tag_search(tag)
def insta_tag(search_tag): returnval='' api = InstagramAPI(access_token=ACCESS_TOKEN, client_secret=CLIENT_SECRET) try: #if ' ' in search_term: # search_term = search_term.replace(' ', '') search_term = search_tag if not api.tag_search(search_term, 1): print "No result found. :(" else: recent_media, next_ = api.tag_recent_media(1, 20, search_term) for media in recent_media: link = media.link returnval += ' '+link+' \n' return returnval except InstagramAPIError as e: if (e.status_code == 400): return "Cannot retrieve data. User is set to private." if (e.status_code == 404): return "Content not found."
def insta_tag(): returnval='' api = InstagramAPI(access_token=ACCESS_TOKEN, client_secret=CLIENT_SECRET) try: #if ' ' in search_term: # search_term = search_term.replace(' ', '') search_term = 'selfie' if not api.tag_search(search_term, 1): print "No result found. :(" else: recent_media, next_ = api.tag_recent_media(1, 20, search_term) for media in recent_media: link = media.link returnval += ' '+link+' \n' return returnval except InstagramAPIError as e: if (e.status_code == 400): return "Cannot retrieve data. User is set to private." if (e.status_code == 404): return "Content not found."
from instagram.client import InstagramAPI from nltk.stem.porter import * import operator Instagram_CLIENT_ID = "[you client ID]" Instagram_CLIENT_SECRET = "[your client secret]" Instagram_ACCESS_TOKEN = "[your access token]" Instagram_api = InstagramAPI(access_token=Instagram_ACCESS_TOKEN, client_id=Instagram_CLIENT_ID, client_secret=Instagram_CLIENT_SECRET) q = 'pokemongo' stemmer = PorterStemmer() count=100 related_tags = [stemmer.stem(t.name) for t in Instagram_api.tag_search(q, count)[0]] tags_count = dict((i, related_tags.count(i)) for i in related_tags) sorted_tags = sorted(tags_count.items(), key=operator.itemgetter(1), reverse=True)[:5] print sorted_tags tags = '' for tg in sorted_tags: tags += tg[0]+',' print tags ## cell 2 import flickrapi import json
'redirect_uri': 'http://localhost:8080', #http://localhost: 'response_type': 'code' } baseurl = 'https://api.instagram.com/oauth/authorize/?' url = baseurl + urllib.urlencode(params) webbrowser.open(url) tokenListener = Listener() while not tokenListener.hasPin(): ## waits until we have a pin pass ## now that we have the auth code, go get the token params = { 'client_id': 'FILL THIS IN', 'client_secret': 'FILL THIS IN', 'grant_type':'authorization_code', 'code':tokenListener.code, 'redirect_uri': 'http://localhost:8080', #http://localhost: } url = 'https://api.instagram.com/oauth/access_token' access_token = json.load(urllib2.urlopen(url,urllib.urlencode(params)))['access_token'] return access_token access_token = getAccess() api = InstagramAPI(access_token=access_token) media = api.tag_search("snowy",2) print media
#print ('%s', parts[1]); str1 = ''.join(words) hashtag = str1 filepreamble = "/export/sc/hamed/qtrhealth/instagram/foodimages/Mar2015week4/hashtag_" filepostamble = ".txt" outputfilename = filepreamble + hashtag + filepostamble outputfile = open(outputfilename, 'w') string1 = "curl https://api.instagram.com/v1/tags/" string2 = "/media/recent?access_token=XXXXXXX" print hashtag cmd = string1 + hashtag + string2 #os.system('curl "https://api.instagram.com/v1/locations/search?access_token=XXXXXXX&foursquare_v2_id="+location_id ') time.sleep(1) try: tag_search, next_tag = api.tag_search(q=hashtag) tag_recent_media, next = api.tag_recent_media( tag_name=tag_search[0].name) except bind.InstagramAPIError, ie: if ie.status_code == 400: print "protected account (400) APINotAllowedError-you cannot view this resource" continue elif ie.status_code == 503: # this should not happen, but Instagram returns this message no matter what x_ratelimit_remaining is. print "rate limit (503) Rate limited-Your client is making too many request per second" time.sleep(10) continue else: print ie time.sleep(30)
from instagram.client import InstagramAPI api = InstagramAPI(client_id='pablocoellopulido') recent_media, next_ = api.tag_search('caminodesantiago', 10) photos = [] for media in recent_media: photos.append('<img src="%s"/>' % media.images['thumbnail'].url)
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))
if t.name not in tags_list: tags_list.append(t.name) return tags_list ''' users = {} for line in movies: if 'title' not in line: continue title_original = line.split('title:')[1].strip() titles = movie_tag(line.split('title:')[1].strip()) #search movie title tags that have the most media max_count = -1 for title in titles: tags = api.tag_search(q=title, count=10) if not tags[0]: continue tags = tags[0] count = tags[0].media_count if count > max_count: max_count = count max_tag = tags[0] count = 0 while True: try: media = api.tag_recent_media(tag_name=max_tag.name) if media[1] is None or max_count == -1: continue max_tag_id = media[1].split('max_tag_id=')[1] #get ids of users who we want
continue for t in tags[0]: if t.name not in tags_list: tags_list.append(t.name) return tags_list ''' users = {} for line in movies: if 'title' not in line: continue title_original = line.split('title:')[1].strip() titles = movie_tag(line.split('title:')[1].strip()) #search movie title tags that have the most media max_count = -1 for title in titles: tags = api.tag_search(q=title,count = 10) if not tags[0]: continue tags = tags[0] count = tags[0].media_count if count > max_count: max_count = count max_tag = tags[0] count = 0 while True: try: media = api.tag_recent_media(tag_name = max_tag.name) if media[1] is None or max_count == -1: continue max_tag_id = media[1].split('max_tag_id=')[1] #get ids of users who we want
str1 = ''.join(words) hashtag=str1 filepreamble="/export/sc/hamed/qtrhealth/instagram/foodimages/Mar2015week4/hashtag_" filepostamble=".txt" outputfilename=filepreamble+hashtag+filepostamble outputfile= open(outputfilename, 'w') string1="curl https://api.instagram.com/v1/tags/" string2="/media/recent?access_token=XXXXXXX" print hashtag cmd=string1+hashtag+string2 #os.system('curl "https://api.instagram.com/v1/locations/search?access_token=XXXXXXX&foursquare_v2_id="+location_id ') time.sleep(1) try: tag_search, next_tag = api.tag_search(q=hashtag) tag_recent_media, next = api.tag_recent_media(tag_name=tag_search[0].name) except bind.InstagramAPIError, ie: if ie.status_code == 400: print "protected account (400) APINotAllowedError-you cannot view this resource" continue elif ie.status_code == 503: # this should not happen, but Instagram returns this message no matter what x_ratelimit_remaining is. print "rate limit (503) Rate limited-Your client is making too many request per second" time.sleep(10) continue else: print ie time.sleep(30) continue
class instagramService: def __init__(self, config): self.config = config self.api = InstagramAPI(access_token=config["access_token"]) self.version = 0 self.batch = [] self.batch_users = [] self.feed_count = 0 self.feed_count_total = 0 self.instagram_username = config["instagram_username"] self.instagram_user_id = config["instagram_user_id"] def stop_and_exit_service(self): logging.debug("instagram Service - HARD QUIT - instagram SERVICE") sys.exit() def run_main_services(self): main_services_d = Deferred() try: print "running instagram main services" def find_followers_cb(res): def find_friends_cb(res): def get_authen_user_feed_cb(res): def get_popular_media_cb(res): main_services_d.callback(True) self.get_popular_media().addCallbacks( get_popular_media_cb, lambda failure: logging.error("Update Error {0}".format(failure)) ) self.get_authenticated_user_feed().addCallbacks( get_authen_user_feed_cb, lambda failure: logging.error("Update Error {0}".format(failure)) ) self.find_friends(self.config["instagram_user_id"]).addCallbacks( find_friends_cb, lambda failure: logging.error("Update Error{0}".format(failure)) ) self.find_followers(self.config["instagram_user_id"]).addCallbacks( find_followers_cb, lambda failure: logging.error("Update Error{0}".format(failure)) ) # self.subscribe_to_objects_by_tag(self.config['instagram_search_words']) except: logging.debug("Service Instagram - Could not run main service due to error: {0}".format(sys.exc_info())) return main_services_d def get_indx(self): return_d = Deferred() def authed_cb(): logging.debug("in service_tweets - Authed Callback") logging.debug("in service_tweets - get_indx authclient Auth status: {0}".format(authclient.is_authed)) def token_cb(token): self.indx_con = IndxClient( self.config["address"], self.config["box"], appid, token=token, client=authclient.client ) return_d.callback(True) authclient.get_token(self.config["box"]).addCallbacks(token_cb, return_d.errback) def authed_cb_fail(re): logging.debug("in service tweets - get_indx/authed_cb failed for reason {0}".format(re)) return_d.errback logging.debug("in service_instagram - get_indx") authclient = IndxClientAuth(self.config["address"], appid) authclient.auth_plain(self.config["user"], self.config["password"]).addCallbacks( lambda response: authed_cb(), authed_cb_fail ) return return_d def get_authenticated_user_feed(self): auth_d = Deferred() def found_cb(results): friends_number = 0 followers_number = 0 since_id = "" # let's see if the object has some nice things in it. try: config_returned = results["data"]["service_instagram_config"] friends_number = int(config_returned["friends_list_size"][0]["@value"]) followers_number = int(config_returned["followers_list_size"][0]["@value"]) since_id = int(config_returned["since_id"][0]["@value"]) logging.info("Found the instagram Config Object.") except: # print sys.exc_info() pass # print "Getting Auth User's feed" user_feed = self.api.user_media_feed()[0] try: latest_id = user_feed[0].id except: latest_id = "Null" ##find the highest id... if latest_id != since_id: logging.info("Found some new media, will insert it to INDX") since_id = latest_id objects_to_insert = [] current_timestamp = str(time.time()).split(".")[0] # str(datetime.now()) timestamp = str(datetime.now().isoformat("T")).split(".")[0] current_timestamp = str(datetime.now()) # the user responsible for this # now create some nice user objects... for media in user_feed: media_user_id = media.user.id media_username = media.user.username uniq_id = "instagram_media_" + media.id user_feed_obj = { "@id": uniq_id, "app_object": appid, "timestamp": timestamp, "type": "post", "instagram_user_id_indx": "instagram_user_me", "instagram_user_id": media_user_id, "instagram_username": media_username, "media_id": media.id, "media_caption": media.caption, "media_url": media.get_standard_resolution_url(), "media_like_count": media.like_count, } # need to add INDX objects for tags and locations and the indx user of this # create location if available try: location = media.location uniq_id = "instagram_location_" + location.id location_obj = { "@id": uniq_id, "app_object": appid, "timestamp": timestamp, "type": "location", "instagram_location_id": location.id, "location_name": location.name, "latitude": location.point.latitude, "longitude": location.point.longitude, } # now add this location to the user_feed_obj user_feed_obj["media_location_id"] = location_obj # thhen add it to a list of ojects to insert. objects_to_insert.append(location_obj) except: pass try: tag_ids = [] for tag in media.tags: uniq_id = "instagram_tag_" + tag.name tag_obj = { "@id": uniq_id, "app_object": appid, "timestamp": timestamp, "type": "tag", "instagram_tag_name": tag.name, } tag_ids.append(uniq_id) objects_to_insert.append(tag_obj) # now add this location to the user_feed_obj user_feed_obj["tags"] = tag_ids except: pass objects_to_insert.append(user_feed_obj) # now create the instagram_user_me object uniq_id = "instagram_user_me" instagram_me_obj = { "@id": uniq_id, "app_object": appid, "timestamp": timestamp, "type": "user", "instagram_user_id": media_user_id, "instagram_username": media_username, } objects_to_insert.append(instagram_me_obj) # and create the instagram config instagram_config_obj = { "@id": "service_instagram_config", "app_object": appid, "type": "config", "config_last_updated_at": timestamp, "config_for_instagram_user": self.instagram_username, "friends_list_generated_at": timestamp, "follower_list_generated_at": timestamp, "friends_list_size": friends_number, "followers_list_size": followers_number, "since_id": since_id, } objects_to_insert.append(instagram_config_obj) def update_cb(re): logging.debug("network harvest async worked {0}".format(re)) auth_d.callback(True) def update_cb_fail(re): logging.error("network harvest async failed {0}".format(re)) auth_d.errback self.insert_object_to_indx(objects_to_insert).addCallbacks(update_cb, update_cb_fail) else: logging.info("already have the latest version of your instagram timeline") auth_d.callback(True) def error_cb(re): found_cb() def_search = Deferred() find_instagram_config = {"@id": "service_instagram_config"} logging.info("Searching for instagram_config to check if Popular feed already harvested... ") def_search = self.indx_con.query(json.dumps(find_instagram_config)) def_search.addCallbacks(found_cb, error_cb) return auth_d # for feed in user_feed: # print feed def get_searched_media(self, search_terms): print "getting searched media for terms: " + str(search_terms) returned_results, page_num = self.api.tag_search(search_terms, 20) return returned_results # for result in returned_results: # print result def get_popular_media(self): pop_d = Deferred() def found_cb(results): friends_number = 0 followers_number = 0 since_id = "" # let's see if the object has some nice things in it. try: config_returned = results["data"]["service_instagram_config"] friends_number = int(config_returned["friends_list_size"][0]["@value"]) followers_number = int(config_returned["followers_list_size"][0]["@value"]) since_id = int(config_returned["since_id"][0]["@value"]) logging.info("Found the instagram Config Object.") except: # print sys.exc_info() pass # if(since_id > 0): logging.info("getting popular media") objects_to_insert = [] current_timestamp = str(time.time()).split(".")[0] # str(datetime.now()) timestamp = str(datetime.now().isoformat("T")).split(".")[0] current_timestamp = str(datetime.now()) popular_media = self.api.media_popular(count=20) for media in popular_media: media_user_id = media.user.id media_username = media.user.username # now create the instagram_user object uniq_user_id = "instagram_user_" + media.user.id instagram_media_obj = { "@id": uniq_user_id, "app_object": appid, "timestamp": timestamp, "type": "user", "instagram_user_id": media_user_id, "instagram_username": media_username, } objects_to_insert.append(instagram_media_obj) uniq_id = "instagram_media_" + media.id media_obj = { "@id": uniq_id, "app_object": appid, "timestamp": timestamp, "type": "post", "instagram_user_id_indx": uniq_user_id, "instagram_user_id": media_user_id, "instagram_username": media_username, "media_id": media.id, "media_caption": media.caption, "media_url": media.get_standard_resolution_url(), "media_like_count": media.like_count, } # need to add INDX objects for tags and locations and the indx user of this # create location if available try: location = media.location uniq_id = "instagram_location_" + location.id location_obj = { "@id": uniq_id, "app_object": appid, "timestamp": timestamp, "type": "location", "instagram_location_id": location.id, "location_name": location.name, "latitude": location.point.latitude, "longitude": location.point.longitude, } # now add this location to the user_feed_obj media_obj["media_location_id"] = location_obj # thhen add it to a list of ojects to insert. objects_to_insert.append(location_obj) except: pass try: tag_ids = [] for tag in media.tags: uniq_id = "instagram_tag_" + tag.name tag_obj = { "@id": uniq_id, "app_object": appid, "type": "tag", "timestamp": timestamp, "instagram_tag_name": tag.name, } tag_ids.append(uniq_id) objects_to_insert.append(tag_obj) # now add this location to the user_feed_obj media_obj["tags"] = tag_ids except: pass objects_to_insert.append(media_obj) def update_cb(re): logging.debug("network harvest async worked {0}".format(re)) pop_d.callback(True) def update_cb_fail(re): logging.error("network harvest async failed {0}".format(re)) pop_d.errback # and create the instagram config instagram_config_obj = { "@id": "service_instagram_config", "app_object": appid, "type": "config", "config_last_updated_at": timestamp, "config_for_instagram_user": self.instagram_username, "friends_list_generated_at": timestamp, "follower_list_generated_at": timestamp, "friends_list_size": friends_number, "followers_list_size": followers_number, "since_id": since_id, } objects_to_insert.append(instagram_config_obj) self.insert_object_to_indx(objects_to_insert).addCallbacks(update_cb, update_cb_fail) def error_cb(re): found_cb() def_search = Deferred() find_instagram_config = {"@id": "service_instagram_config"} logging.info("Searching for instagram_config to check if Popular feed already harvested... ") def_search = self.indx_con.query(json.dumps(find_instagram_config)) def_search.addCallbacks(found_cb, error_cb) return pop_d def find_user(self, username): data = self.api.user_search(username, count=20) print data def find_followers(self, userid): # first do a lookup and see if the latest set if followers has allready been found follower_d = Deferred() def found_cb(results): friends_number = 0 followers_number = 0 since_id = 0 # let's see if the object has some nice things in it. try: config_returned = results["data"]["service_instagram_config"] friends_number = int(config_returned["friends_list_size"][0]["@value"]) followers_number = int(config_returned["followers_list_size"][0]["@value"]) since_id = int(config_returned["since_id"][0]["@value"]) logging.info("Found the instagram Config Object.") except: # print sys.exc_info() pass followed_by = self.api.user_followed_by(userid)[0] # print "followed_by length: "+str(len(followed_by)) # print str(followed_by) # see if the number is different, if it is, then update.. could be nicer than this, but for now, it will work (ish) if (len(followed_by) != followers_number) or (followers_number == 0): followers_number = len(followed_by) objects_to_insert = [] current_timestamp = str(time.time()).split(".")[0] # str(datetime.now()) timestamp = str(datetime.now().isoformat("T")).split(".")[0] uniq_id = "instagram_follower_network_for_user_me" # +self.instagram_username followed_by_obj = { "@id": uniq_id, "app_object": appid, "instagram_username": self.instagram_username, "instagram_user_id": self.instagram_user_id, "timestamp": timestamp, "followed_by_count": len(followed_by), } followers_ids = [] for follower in followed_by: # print follower.username uniq_id = "instagram_user_" + str(follower.username) follower_obj = { "@id": uniq_id, "app_object": appid, "type": "user", "instagram_username": str(follower.username), "timestamp": timestamp, } objects_to_insert.append(follower_obj) # we can add this to the followed_by_obj later followers_ids.append(uniq_id) # link the followers for me followed_by_obj["follower_ids"] = followers_ids # print followed_by_objs # for friend in friends_list: # print friend # now append the results def update_cb(re): logging.debug("network harvest async worked {0}".format(re)) follower_d.callback(True) def update_cb_fail(re): logging.error("network harvest async failed {0}".format(re)) follower_d.errback instagram_config_obj = { "@id": "service_instagram_config", "app_object": appid, "type": "config", "config_last_updated_at": timestamp, "config_for_instagram_user": self.instagram_username, "friends_list_generated_at": timestamp, "follower_list_generated_at": timestamp, "friends_list_size": friends_number, "followers_list_size": followers_number, "since_id": since_id, } objects_to_insert.append(followed_by_obj) # objects_to_insert.append(followers) objects_to_insert.append(instagram_config_obj) self.insert_object_to_indx(objects_to_insert).addCallbacks(update_cb, update_cb_fail) else: follower_d.callback(True) def error_cb(re): found_cb() def_search = Deferred() find_instagram_config = {"@id": "service_instagram_config"} logging.info("Searching for instagram_config to check if network already harvested... ") def_search = self.indx_con.query(json.dumps(find_instagram_config)) def_search.addCallbacks(found_cb, error_cb) return follower_d # print followed_by def find_friends(self, userid): friends_d = Deferred() def found_cb(results): friends_number = 0 followers_number = 0 since_id = 0 # let's see if the object has some nice things in it. try: config_returned = results["data"]["service_instagram_config"] friends_number = int(config_returned["friends_list_size"][0]["@value"]) followers_number = int(config_returned["followers_list_size"][0]["@value"]) since_id = int(config_returned["since_id"][0]["@value"]) logging.info("Found the instagram Config Object.") except: # print sys.exc_info() pass friends_by_list = self.api.user_follows(userid)[0] if (len(friends_by_list) != friends_number) or (friends_number == 0): friends_number = len(friends_by_list) objects_to_insert = [] current_timestamp = str(time.time()).split(".")[0] # str(datetime.now()) timestamp = str(datetime.now().isoformat("T")).split(".")[0] uniq_id = "instagram_friends_network_for_user_me" # +self.instagram_username friends_by_obj = { "@id": uniq_id, "app_object": appid, "instagram_username": self.instagram_username, "instagram_user_id": self.instagram_user_id, "timestamp": timestamp, "followed_by_count": len(friends_by_list), } friends_ids = [] for friend in friends_by_list: # print follower.username uniq_id = "instagram_user_" + str(friend.username) friend_obj = { "@id": uniq_id, "app_object": appid, "type": "user", "instagram_username": str(friend.username), "timestamp": timestamp, } objects_to_insert.append(friend_obj) # we can add this to the followed_by_obj later friends_ids.append(uniq_id) friends_by_obj["friends_ids"] = friends_ids # print friends_by_objs # for friend in friends_list: # print friend # now append the results def update_cb(re): logging.debug("network harvest async worked {0}".format(re)) friends_d.callback(True) def update_cb_fail(re): logging.error("network harvest async failed {0}".format(re)) friends_d.errback instagram_config_obj = { "@id": "service_instagram_config", "app_object": appid, "type": "config", "config_last_updated_at": timestamp, "config_for_instagram_user": self.instagram_username, "friends_list_generated_at": timestamp, "follower_list_generated_at": timestamp, "friends_list_size": friends_number, "followers_list_size": followers_number, "since_id": since_id, } objects_to_insert.append(friends_by_obj) # objects_to_insert.append(followers) objects_to_insert.append(instagram_config_obj) self.insert_object_to_indx(objects_to_insert).addCallbacks(update_cb, update_cb_fail) else: friends_d.callback(True) def error_cb(re): found_cb() def_search = Deferred() find_instagram_config = {"@id": "service_instagram_config"} logging.info("Searching for instagram_config to check if network already harvested... ") def_search = self.indx_con.query(json.dumps(find_instagram_config)) def_search.addCallbacks(found_cb, error_cb) return friends_d # print followed_by def subscribe_to_objects_by_tag(self, search_terms): def process_tag_update(update): print update reactor = subscriptions.SubscriptionsReactor() reactor.register_callback(subscriptions.SubscriptionType.TAG, process_tag_update) self.api.create_subscription( object="tag", object_id=search_terms, aspect="media", callback_url="http://*****:*****@version"] logging.debug( "Succesfully Updated INDX with Objects in update_cb, new diff version of {0}".format(self.version) ) # logging.debug("Inserted Object into INDX: ".format(resp)) update_d.callback(True) # return update_d # time.sleep(2) def exception_cb(e, service=self, obj=obj): logging.debug("Exception Inserting into INDX, probably wrong version given") if isinstance(e.value, urllib2.HTTPError): # handle a version incorrect error, and update the version if e.value.code == 409: # 409 Obsolete response = e.value.read() json_response = json.loads(response) service.version = json_response["@version"] logging.debug("INDX insert error in Instagram Service Object: " + str(response)) try: service.indx_con.update(service.version, obj).addCallbacks(update_cb, exception_cb) # logging.debug('Twitter Service - Successfully added Objects into Box') except: logging.error("Instagram Service, error on insert {0}".format(response)) update_d.errback(e.value) else: logging.error("Instagram Service Unknow error: {0}".format(e.value.read())) update_d.errback(e.value) else: logging.error("Error updating INDX: {0}".format(e.value)) update_d.errback(e.value) logging.debug( "in Instagram - Trying to insert into indx...Current diff version: {0} and objects (len) given {1}".format( self.version, len(obj) ) ) self.indx_con.update(self.version, obj).addCallbacks(update_cb, exception_cb) return update_d
class 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
from nltk.stem.porter import * from sets import Set import operator Instagram_CLIENT_ID = "[your Instagram client ID]" Instagram_CLIENT_SECRET = "[your Instagram client secret]" Instagram_ACCESS_TOKEN = "[your Instagram access token]" Instagram_api = InstagramAPI(access_token=Instagram_ACCESS_TOKEN, client_id=Instagram_CLIENT_ID, client_secret=Instagram_CLIENT_SECRET) q = 'travel' stemmer = PorterStemmer() count=100 related_tags = [stemmer.stem(t.name) for t in Instagram_api.tag_search(q, count)[0]] tags_count = dict((i, related_tags.count(i)) for i in related_tags) sorted_tags = sorted(tags_count.items(), key=operator.itemgetter(1), reverse=True)[:5] print sorted_tags tags = '' for tg in sorted_tags: tags += tg[0]+',' print tags # cell 2 import flickrapi import json from datetime import date
class TagSearch: """ This class is a controller for the api searching for TagSearch """ def __init__(self, access_token, tag): assert isinstance(tag, str) self.api = InstagramAPI(access_token=ACCESS_TOKEN) self.api_calls = 0 self.tag = tag self.created = datetime.datetime.now() self.save_path = self.get_save_path() self.saved_media = self.load_saved_media() self.num_runs = 0 def get_save_path(self, save_dir='tag_pickles'): """ returns path to save files """ save_dir = os.path.join(save_dir, self.tag) date_str = "{0}_{1}.pkl".format( self.tag, self.created.strftime("%d-%m-%Y")) outfile = os.path.join(save_dir, date_str) if not os.path.exists(save_dir): os.mkdir(save_dir) return outfile def set_tag(self): self.saved_media = self.get_save_path() self.tag = tag def load_saved_media(self): """ loads media from save path """ if os.path.exists(self.save_path): with open(self.save_path, 'rb') as save_file: saved_media = cPickle.load(save_file) else: saved_media = {} return saved_media def __str__(self): s = 'Tags: {0}\nAPICalls: {1}'.format(self.tag_list, self.api_calls) return s def tag_search(self, count=10): """ Search InstagramAPI tags for similar tags to self.tag (input) count = number of search results """ search_results = self.api.tag_search(self.tag, count=count) self.api_calls += 1 return search_results def tag_recent_media(self, count=50): """ returns tag_recent_media for tag_name = <tag> """ sresults = self.api.tag_recent_media(count=count, tag_name=self.tag) self.api_calls += 1 return sresults[0] def save_recent_media(self): """ saves recent media fo self.save_path """ num_duplicates = 0 num_saved = 0 for each in self.tag_recent_media(): mid = each.id if mid in self.saved_media: num_duplicates += 1 else: self.saved_media[mid] = each num_saved += 1 print "Saving {0} to {1}\nduplicates={2}".format(num_saved, self.save_path, num_duplicates) with open(self.save_path, 'wb') as save_file: cPickle.dump(self.saved_media, save_file) def run_search(self): self.save_path = self.get_save_path() self.saved_media = self.load_saved_media() print "searching for ({0})\tnum_runs={1}\nnum_saved={2}\tAPICalls={3}".format(self.tag, self.num_runs, len(self.saved_media), self.api_calls) self.save_recent_media() self.num_runs += 1
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