class InstagramService(object): def __init__(self): client_id, client_secret = DATASOURCES['instagram']['client_id'], DATASOURCES['instagram']['client_secret'] self.api = InstagramAPI(client_id=client_id, client_secret=client_secret) app_id, app_secret = DATASOURCES['clarifai']['app_id'], DATASOURCES['clarifai']['app_secret'] self.image_api = ClarifaiApi(app_id=app_id, app_secret=app_secret) def posts_in_new_york(self): media = [] for row in zips: lat, lng = float(row[0]), float(row[1]) posts = self.api.media_search(lat=lat, lng=lng, distance=800) for post in posts: temp_list = [post.id, post.images['standard_resolution'].url, post.created_time, post.location.point.latitude, post.location.point.longitude] media.append(temp_list) print temp_list return media def location(self, lat, lng): posts = self.api.media_search(lat=lat, lng=lng, distance=800) media = [] for post in posts: temp_list = [post.id, post.images['standard_resolution'].url, post.created_time, post.location.point.latitude, post.location.point.longitude] media.append(temp_list) print temp_list return media def hashtag(self, lat, lng): posts = self.api.media_search(lat=lat, lng=lng, distance=800) hashtags = {} for post in posts: tokens = str(post.caption).split(' ') for token in tokens: if token and len(token) > 1: if token[0] == '#': count = hashtags.setdefault(token, 0) hashtags[token] = count + 1 elif token[0] == '"' and token[1] == '#': count = hashtags.setdefault(token[1:], 0) hashtags[token[1:]] = count + 1 return hashtags def get_tags(self, place): tags = {} lat, lng = Geocode().geocode(place) posts = self.api.media_search(lat=lat, lng=lng, distance=800) for post in posts: result = self.image_api.tag_image_urls(post.images['standard_resolution'].url) image_tags = result['results'][0]['result']['tag']['classes'][:5] for tag in image_tags: count = tags.setdefault(tag, 0) tags[tag] = count + 1 return tags
def instagram_pictures(self, venue, timestamp, window, distance, retry=True): """Get Instagram pictures for a venue/location in a give timeframe""" ## Get instagram pictures taken nearby the venue in a time window around the start date instagram = InstagramAPI(client_id=instagram_client, client_secret=instagram_secret) min_timestamp = timestamp - window[0]*3600 max_timestamp = timestamp + window[1]*3600 ## Retry to intercept the random "Oops, an error occurred" answer and other API errors - retry only once try: media = instagram.media_search(lat=venue['lat'], lng=venue['lng'], min_timestamp=min_timestamp, max_timestamp=max_timestamp, distance=distance) ## Filter to get maching only pics in a location thates match the venue name media = filter(lambda x: venue['name'].lower() in x.location.name.lower(), media) ## Return pictures return map(lambda media: { 'thumbnail' : media.images['thumbnail'].url, 'standard' : media.images['standard_resolution'].url, 'caption' : media.caption.text if media.caption else '', 'user' : media.user.username, }, media) ## Instagram API errors (retry only once) except InstagramAPIError as e: if e.status_code == '400' and retry: return self.instagram_pictures(venue, timestamp, window, distance, retry=False) except InstagramClientError as e: return self.instagram_pictures(venue, timestamp, window, distance, retry=False)
def write_stream(out_file, production = False, iterations = 61): #Create a file to store output. "a" means append (add on to previous file) fhOut = open(out_file,"a") #authorize access_token = media_mapper.keys.INSTAGRAM_ACCESS_TOKEN client_secret = media_mapper.keys.INSTAGRAM_CLIENT_SECRET api = InstagramAPI(access_token=access_token, client_secret=client_secret) #set time limit for the current time time_marker = calendar.timegm(time.gmtime()) begin_time = time_marker sleep(10) #sleep ten seconds so some instagrams have some time to come in #query for a new batch of 20 photos for i in xrange(iterations): #make sure that it only loops 60 times media_search = api.media_search(lat="37.7833",lng="-122.4167",distance=5000, min_timestamp=time_marker) #get all the metadata in a list instagram_list = instagram_metadata(media_search, time_marker) [json.dump(item, fhOut, indent = 1) for item in instagram_list] time_marker = calendar.timegm(time.gmtime()) #check to make sure that this cycle has taken an hour for request limits end_time = calendar.timegm(time.gmtime()) seconds_left = 3600 -(begin_time-end_time) #find out how much time has passed if seconds_left > 0: #if an hour hasn't passed, sleep so I don't overrequest sleep(seconds_left) fhOut.close()
def read_instagram(self, lat, lng, rds): api = InstagramAPI( client_id="1a405b6f113a4476a8b08c304b964fbc", client_secret="2fe657c6a57c46e3b6edde6306336e78" ) media = api.media_search(lat=lat, lng=lng, distance=rds + "km") res = [] for m in media: # import pdb # pdb.set_trace() res.append( { "created_at": m.created_time.strftime("%a, %d %b %Y %H:%M:%S +0000"), "timestamp": time.mktime(m.created_time.timetuple()), "id": m.id, "from_user": m.user.username, "from_user_id": m.user.id, "from_user_name": m.user.full_name, "profile_image_url": m.user.profile_picture, "text": m.caption.text if m.caption else "", "entities": { "media": [ { "url": m.images["standard_resolution"].url, "type": "photo", "sizes": {"large": {"w": 612, "h": 612}}, } ] }, "source_type": "instagram", } ) return res
def instagram(): results = [] api = InstagramAPI(client_id=app.config['CLIENT_ID'], client_secret=app.config['CLIENT_SECRET']) data = json.loads(request.data.decode()) lat = data["lat"] lng = data["lng"] dist = data["dist"] min_tstmp = data["min_timestamp"] your_location = api.media_search(count=100, lat=lat, lng=lng, distance=dist, min_timestamp=min_tstmp) for media in your_location: url = media.images['standard_resolution'].url pid = media.id img_paths = detect_faces(url, pid) if not img_paths == []: for img_path in img_paths: results.append(img_path) results = json.dumps(results) print "****** RESULTS ******" print " " print results return results
def get_insta(): parameters = urlparse.parse_qs(web.ctx.query[1:]) CLIENT_ID = 'something secret' CLIENT_SECRET = 'should have been in env variables' latitude = float(parameters['lat'][0]) longitude = float(parameters['lng'][0]) distance = int(parameters['distance'][0]) count = int(parameters['count'][0]) api = InstagramAPI(client_id=CLIENT_ID, client_secret=CLIENT_SECRET) results = api.media_search( lat=latitude, lng=longitude, count=count, distance=distance) total_pre_json = [] for picture in results: pre_json = {} pre_json['created_time'] = ( datetime.today().utcnow() - picture.created_time).days pre_json['username'] = picture.user.username pre_json['location_name'] = picture.location.name pre_json['latlng'] = { 'lat': picture.location.point.latitude, 'lng': picture.location.point.longitude} pre_json['photo_url'] = picture.images['standard_resolution'].url pre_json['tags'] = [x.name for x in picture.tags] pre_json['comments'] = [ ':'.join([x.user.username, x.text]) for x in picture.comments] total_pre_json.append(pre_json) return json.dumps(total_pre_json)
def download_instagram(): inst_client_id = '638009caa4e7485f91c3c07330448ae4' inst_client_secret = 'a79a36a7b7054ff192506263f30b6f58' inst_api = InstagramAPI(client_id = inst_client_id, client_secret = inst_client_secret) media = inst_api.media_search(lat = latitude, lng = longitude, distance = 5000, count = 10000) for i in media: make_image_url_inst(i)
def searchInstagram(lat,lng): jsonArray = [] api = InstagramAPI(client_id='69ae00c802504e0599277ed72265e221', client_secret='7a8bc6124d694a71b482593078999774') #media = api.media_search(lat="40.858844",lng="80") media = api.media_search(lat=lat,lng=lng) for row in media: data = {} data['title'] = row.location.name #data['titleImage'] = profileImage try: data['description'] = row.caption.text except : print "No Description" data['description'] = "" data['type'] = 'instagram' data['link'] = row.link data['photo'] = row.images["standard_resolution"].url jsonArray.append(data) jsss = json.dumps(jsonArray, separators=(',',':')) #print jsss return jsss
def main(argv = None): if argv is None: argv = sys.argv #exit if there aren't enough arguments if len(argv) < 4: sys.exit("Please provide latitude and longitude") # Instagram-specific setup client_id = '5fdd1db38a6046d3a3a8447410a04ef0' client_secret = 'd6b81556c31b43148081c6ff0707763e' api = InstagramAPI(client_id=client_id, client_secret=client_secret) #find the latitude and longitude lat = argv[1] lng = argv[2] # get a list of posts at a particular location data = api.media_search(count=100, lat=lat, lng=lng) recent_Media = table_InstaData(data, lat, lng) # put the list and organized data into a dataframe df = pd.DataFrame(recent_Media) fname = argv[3] + "_" + str(lat) + "_" + str(lng) + "_" + str(datetime.date.today()) + ".pkl" df.to_pickle(fname)
def mappa(request): latitudine = request.GET.get('lat') longitudine = request.GET.get('lng') instance = UserSocialAuth.objects.get(user=request.user, provider='instagram') access_token = instance.tokens['access_token'] api = InstagramAPI( access_token = access_token, client_ips = MIOIP, client_secret = CLIENT_SECRET ) media_vicini = api.media_search(lat = latitudine, lng = longitudine, count = 100, distance = 5000) #for media in media_vicini: #print media.user.username # point_obj = media.location.point # latitude = point_obj.latitude # longitude = point_obj.longitude template = loader.get_template('map.html') context = RequestContext(request, { 'media_vicini': media_vicini, "centroLat" : latitudine, "centroLng" : longitudine, }) return HttpResponse(template.render(context))
class ThreadUpdate(threading.Thread): def __init__(self, queue): self.client = InstagramAPI(client_id = config.instagram_client_id, client_secret = config.instagram_client_secret) threading.Thread.__init__(self) self.queue = queue self.now = int(time.time()) self.previous_time = int(time.time() - 15*60) def run(self): while True: job = self.queue.get() #for each job, job[0] is the sw latitude, ne is the ne latitude mid_lat = (job[0][0] + job[1][0])*1.0/2 mid_lng = (job[0][1] + job[1][1])*1.0/2 time.sleep(1) radius_km = distance.distance( (mid_lat, mid_lng), (job[0][0], job[0][1])).km print radius_km logging.debug('mid is '+str(mid_lat)+","+str(mid_lng)+", r is "+str(radius_km)) max_time = self.now counter = 0 links_count = 0 links = set() while True: counter+=1 try: res = self.client.media_search(lat = mid_lat, lng = mid_lng, max_timestamp = max_time, min_timestamp = self.previous_time, return_json = True, distance = radius_km*1000, count=100) except Exception as e: logging.warning('Exception in instagram_scanner.py line 44. Error msg: %s'%e) links_count = -1 break; print counter if len(res)==0 or max_time <= self.previous_time: break else: min_time = 100000000000; for r in res: min_time = min( int(r['created_time']), min_time) links.add( r['link'] ) links_count+=1 if min_time<max_time: max_time = min_time else: # in case all the pics in this interval are the same max_time -= 60 print 'next interval is ',self.previous_time, max_time #save_instagram_photo(page[0], mid_lat, mid_lng, radius_km) #counter += len(page[0]) #save_instagram_region(counter, mid_lat, mid_lng, radius_km) real_links = -1 if links_count==-1: real_links = -1 else: real_links = len(links) region = [mid_lat, mid_lng, radius_km*1000, self.now + (self.now-self.previous_time)/2.0, real_links] save_region_instagram(region) print 'links',real_links print 'set links',len(links) print links logging.debug('for location '+str(mid_lat) + ','+str(mid_lng) +' the number is '+str(counter) +'\t'+str(len(links))+" links added") self.queue.task_done()
def search_instagram(term_to_query, count, latitude, longitude, radius, min_timestamp, max_timestamp): ''' query instagram for images ''' # create a holding container for what the api returns list_of_images = [] api = InstagramAPI(client_id=config_settings['INSTAGRAM_CLIENT_ID'], client_secret=config_settings['INSTAGRAM_CLIENT_SECRET']) # search and set to variable instagram_media = api.media_search(term_to_query, count, latitude, longitude, min_timestamp, max_timestamp) # loop through what the search returned for media in instagram_media: # lets skip anything that doesn't have an address if media.location.point.latitude is not None and media.location.point.longitude is not None: latitude = media.location.point.latitude longitude = media.location.point.longitude else: pass if media.user.username is not None: user = media.user.username else: user = None if media.user.full_name is not None: user_full_name = media.user.full_name else: user_full_name = None if media.link is not None: link = media.link else: link = None if media.images['standard_resolution'].url is not None: image_source = media.images['standard_resolution'].url else: image_source = None if media.created_time is not None: time_date = media.created_time else: time_date = None if media.caption is not None: caption = media.caption.text else: caption = None image_dict = {'result_type': 'instagram', 'user': user, 'user_full_name': user_full_name, 'link': link, 'image_source': image_source, 'caption': caption, 'latitude': latitude, 'longitude': longitude, 'time_date': time_date} list_of_images.append(image_dict) return list_of_images
def images_display(): api = InstagramAPI(access_token=access_token, client_secret=client_secret) json_locations = api.media_search(lat=55.770968, lng=38.680028, min_timestamp=None, max_timestamp=None, distance=5000) html_code = '''<html><body>''' for media in json_locations: html_code += '''<img src=''' + media.images['standard_resolution'].url + ''' alt=''' + media.id + '''>''' html_code += '''</body></html>''' return html_code
def Location_Recent_Media(): if request.method == "POST": lat = request.form['latitude'] lng = request.form['longitude'] u = InstagramAPI(access_token=session['access_token']) media_search = u.media_search(lat=lat, lng=lng, count=32) media = [] for link in media_search: media.append("{}".format(link.get_thumbnail_url())) return render_template("media_search.html", media=media) return render_template("media_search.html", title="Location Recent Media")
def instagram(key): api = InstagramAPI(client_id='b64f54dc4fb3486f87b55d92381e2625', client_secret='b5fa80c366b94cc980c882855630fe92') for item in api.media_search(q=key, count=10, lng=120.98, lat=14.62): key = key photo = item.images['low_resolution'].url username = item.user.username real_name = item.user.full_name dp = item.user.profile_picture date = item.created_time print date, photo , real_name lol = "text" return lol
def nearby_insta(latitude, longitude): #https://github.com/Instagram/python-instagram api = InstagramAPI(client_id='e5caa7d1da134dd681db8fd39389f8e7', client_secret='468c71adce01482aa17ca24090c07a1f') photos =[] try: local_media_search = api.media_search(count=30, lat=latitude, lng=longitude, distance=rdist) #other params: min_timestamp, max_timestamp, q photos = [] for media in local_media_search: photos.append({'lri': media.images['low_resolution'].url, 'sri': media.images['standard_resolution'].url, }) except: print "Could not find instagram pics" return photos
def Media_Search(): if request.method == "POST": q = request.form['query'] lat = request.form['latitude'] lng = request.form['longitude'] if not q or not lat or not lng: e = "Please Enter latitude and longitude" return render_template("media_search.html", title="Media Search", error=e) u = InstagramAPI(access_token=session['access_token']) media_search = u.media_search(query=q, lat=lat, lng=lng, count=32) media = [] for link in media_search: media.append("{}".format(link.get_thumbnail_url())) return render_template("media_search.html", media=media) return render_template("media_search.html", title="Media Search")
def get_instagram(self): """ Queries Instagram and returns images within an area based on geo parameters """ instagram_api = InstagramAPI(client_id=Insta_Client_ID, client_secret=Insta_Client_Secret) # converts user's miles radius input into meters dist_meters = str(float(self.miles) * 1609.34) local_media = instagram_api.media_search(lat=self.latitude, lng=self.longitude, distance=dist_meters) # 'low resolution' versions of images appended to list photos = [] for media in local_media: photos.append(media.images['low_resolution'].url) return photos
def images_json(): api = InstagramAPI(access_token=access_token, client_secret=client_secret) json_locations = api.media_search(lat=55.770968, lng=38.680028, min_timestamp=None, max_timestamp=None, distance=5000) images = {} images_array = [] images_id = [] for media in json_locations: images_array.append(media.images['standard_resolution'].url) images_id.append(media.id) images["images"] = images_array images["media_id"] = images_id return simplejson.dumps(images)
def instagram(): results = [] api = InstagramAPI(client_id=app.config['CLIENT_ID'], client_secret=app.config['CLIENT_SECRET']) data = json.loads(request.data.decode()) lat = data["lat"] lng = data["lng"] dist = data["dist"] min_tstmp = data["min_timestamp"] your_location = api.media_search(count=100, lat=lat, lng=lng, distance=dist, min_timestamp=min_tstmp) for media in your_location: results.append(media.images['standard_resolution'].url) results = json.dumps(results) return results
def instagram(q,count,lat,lon,min_timestamp,max_timestamp): access_token = "31620180.ff5eee2.d5c5869e313445ffa7851204f3a2a5a7" client_secret = "5773a33ea1bd483d90d0ad41e96e8fde" api = InstagramAPI(client_id='be79976fa36248de874ed129a060ae37') posts = api.media_search(q,count,lat,lon,min_timestamp,max_timestamp) listed_posts =[] for post in posts: post_dict = { "created_time":post.created_time, "thumbnail_url":post.get_thumbnail_url(), "standard_url":post.get_standard_resolution_url(), "location":post.location, "user":post.user, "type":post.type, "caption":post.caption, } listed_posts.append(post_dict) return listed_posts
class Base(): def __init__(self, access_token): self.token = access_token self.api = InstagramAPI(access_token=access_token) def get_closest_media_objects(self, lat, lon, min_timestamp): '''returns a list of media ids''' media = self.api.media_search(lat=lat, lng=lon, min_timestamp=min_timestamp) return media def make_igram(self, media_obj): o = media_obj post_id = o.id if o.caption: text = o.caption.text else: text = 'Check out my instagram!' created = o.created_time content_img_url = o.images["low_resolution"].url user_img_url = o.user.profile_picture user_id = o.user.id username = o.user.username coord = "{0},{1}".format(o.location.point.latitude, o.location.point.longitude) if o.location.name: place_name = o.location.name else: place_name = Coordinate_handler().reverse_lookup_locality(coord) up_votes = o.like_count i = Post(post_id, text, created, content_img_url, user_img_url, user_id, place_name, coord, username, 'instagram', up_votes) return i def get_staging_file(self, lat, lon): staging_file_path = settings.STAGING_RAW_FEED_FILE_PATH file_name_prefix = settings.insta_raw_feed_file_prefix self.staging_full_path = "{0}{1}{2}_{3}{4}.txt".format(staging_file_path, file_name_prefix, Date_handler().get_current_utc_date_string(settings.UTC_TIMESTAMP_FORMAT), str(lat), str(lon)) f = File_handler(self.staging_full_path) return f def write_igrams_to_file(self, lat, lon, cache_post_ids): min_timestamp = Date_handler().get_utc_x_minutes_ago(settings.insta_fetch_window_in_minutes) media_set = self.get_closest_media_objects(lat, lon, min_timestamp) igrams = [(self.make_igram(media)) for media in media_set if not cache.if_exists(cache_post_ids, media.id)] if len(igrams): f = self.get_staging_file(lat,lon) [f.append_to_file_as_json(i.get_as_dict()) for i in igrams] f.copy_file_to(self.staging_full_path, settings.RAW_FEED_FILE_PATH)
def run(self): try: api = InstagramAPI(access_token=self.access_token, client_secret=Config.CLIENT_SECRET, client_id=Config.CLIENT_ID) result = api.media_search(lat=self.lat, lng=self.lng, max_timestamp=self.stime, count=1000, distance=4990) current_time = 0 insert_count = 0 logging.info("Server return %d item from (%s,%s)"%(len(result),self.lat,self.lng)) for media in result: caption = "" tags = "" latitude = "" longitude = "" location_id = "" if media.caption: caption = media.caption.text if media.location: location_id = str(media.location.id) latitude = str(media.location.point.latitude) longitude = str(media.location.point.longitude) for tag in media.tags: tags = tags + tag.name tags = tags + " " raw_json = json.dumps(media.raw) #raw_json_compressed = zlib.compress(raw_json) current_time = calendar.timegm(media.created_time.timetuple()) image_record = {"json":raw_json, "image_id": media.id, "created_time": media.created_time, "caption":caption, "location_id":location_id, "longitude":longitude, "latitude":latitude, "tags":tags, "user_id":media.user.id, "user_name":media.user.username} user_record = {"user_id":media.user.id, "user_name":media.user.username} logging.debug("Writing Image with ID:%s %s"%(media.id, media.created_time)) upr = self.manager.image_collection.update_one({"image_id": media.id},{"$set":image_record}, upsert=True) if upr.upserted_id != None: insert_count += 1 self.manager.user_collection.update_one({"user_id":media.user.id}, {"$set":user_record} , upsert=True) if len(result) <= 5: current_time = self.stime - 86400*5 job = self.manager.job_collection.find_one({"lat":self.lat, "lng":self.lng}) job['ctime'] = current_time self.manager.job_collection.save(job) logging.info("Job done! Updating Current Time %s %s for (%s,%s) Insert:%s "%(current_time, datetime.utcfromtimestamp(float(current_time)), self.lat, self.lng, insert_count)) except: traceback.print_exc()
class Instagram: CONSUMER_KEY = '5d9cdd5e12dd4ed7862ff7d0b9867bd7' CONSUMER_SECRET = 'ba7221fefb2547f291af2a98d30b768a' def __init__(self): self.api = InstagramAPI(client_id=self.CONSUMER_KEY, client_secret=self.CONSUMER_SECRET) def get_posts(self, request): all_posts = [] try: # multiply distance value by 1000 because distance is measured in # meters media_search = self.api.media_search( count=10, lat=request.form['lat'], lng=request.form['long'], distance=request.form['value'] * 1000) if (media_search): for media in media_search: if (hasattr(media, 'images') & ("standard_resolution" in media.images)): img_urls = [media.images['standard_resolution'].url] if hasattr(media.caption, 'text'): text = Util.remove_non_ascii(media.caption.text) else: text = "" tags = Util.extract_hashtags(text) item = Item(text, img_urls, tags) all_posts.append(item) except UnicodeEncodeError: pass # NOOP return all_posts
def busca_insta(quantidade): api = InstagramAPI(client_id='46d30b6779544db0a9cc344f938ea7b8', client_secret='c29b5f93556443b9bb25fdd2f2528a4f') popular_media = api.media_search(count=quantidade, lat="-19.9026615",lng="-44.1041373", distance=50000000) r_insta = [] for media in popular_media: tags = '' if hasattr(media, 'tags'): if media.tags is not None: r_tags = [] for insta_tag in media.tags: r_tags.append(str(insta_tag).split(':')[1]) tags = ';'.join(r_tags) else: tags = ' ' vetor = {'tags': tags, 'link': str(media.link), 'url':str(media.images['low_resolution'].url)} r_insta.append(vetor) return r_insta
def crawl_locations(points): api = InstagramAPI(access_token=access_token) for point in points: print("Media search for %s,%s"%(point['lat'],point['lng'])) try: crawled_media = api.media_search(lat=point['lat'],lng=point['lng'], distance=CRAWL_RADIUS) except InstagramAPIError as ie: print("Instagram API error" + str(ie)) return except Exception as e: print("General exception" + str(e)) return for media in crawled_media: print media, media.user, media.images['thumbnail'].url print "++" print ("Got %d results\n"%len(crawled_media)) if len(crawled_media) > 0: latest_id = crawled_media[0].id print "Latest result ID:", latest_id
class InstaGrabber: MAX_SEARCH_COUNT = 100 MAX_ATTEMPTS = 3 def __init__(self, client_id, client_secret): self.__api = InstagramAPI(client_id=client_id, client_secret=client_secret) def find_tags(self, coords, distance, max_date, min_date, logger): self.all_media = set() max_stamp = max_date attempts = self.MAX_ATTEMPTS while max_stamp > min_date: try: logger.debug( "Send request {0}: lat:{1} long:{2} dist:{3} max:{4} count:{5}" .format(threading.current_thread().ident, coords[0], coords[1], distance, max_stamp, self.MAX_SEARCH_COUNT)) media = self.__api.media_search(lat=coords[0], lng=coords[1], distance=distance, max_timestamp=max_stamp, count=self.MAX_SEARCH_COUNT) except InstagramAPIError as ex: if ex.error_type == "Rate limited": raise InstaGrabberBanException(ex.error_message, ex.status_code) else: raise ex except InstagramClientError as ex: if attempts > 0: attempts -= 1 continue else: raise ex logger.debug("Answer received {0}".format( threading.current_thread().ident)) if len(media) == 0: break self.all_media |= set(media) if max_stamp <= datetime_to_timestamp(media[-1].created_time): max_stamp = max_stamp - 3600 else: max_stamp = datetime_to_timestamp(media[-1].created_time) def calc_tags(self, max_timestamp, min_timestamp): tags = {} def is_media_in_range(m): return datetime_to_timestamp(m.created_time) < max_timestamp and \ datetime_to_timestamp(m.created_time) >= min_timestamp for m in filter(is_media_in_range, self.all_media): try: for t in m.tags: utf_tag = t.name.encode('utf-8') if not utf_tag in tags: tags[utf_tag] = 0 tags[utf_tag] += 1 except AttributeError: pass return tags
from instagram.client import InstagramAPI import urllib, io from PIL import Image urls = [] count = "10" lat = "51.507606" lng = "-0.111370" min_timestamp = "1442994611" max_timestamp = "1443001813" api = InstagramAPI(client_id = "XXXX", client_secret = "XXXX", ) lat_media = api.media_search(count=100,lat=lat,lng=lng,min_timestamp=min_timestamp,max_timestamp=max_timestamp,distance=500) for media in lat_media: urls.append(media.images['standard_resolution'].url) for URL in urls: print URL fd = urllib.urlopen(URL) image_file = io.BytesIO(fd.read()) im = Image.open(image_file) im.show()
class Retriever: def __init__(self, config, db): self.config = config self.api = InstagramAPI( client_id=config.data['instagram']['client_id'], client_secret=config.data['instagram']['client_secret']) self.db = db def add_user(self, user_id, username): self.db.cur.execute("SELECT username FROM user WHERE id=%s", (user_id, )) row = self.db.cur.fetchone() if row is None: self.db.cur.execute( "INSERT INTO user (id, username) VALUES (%s, %s)", (user_id, username)) self.db.conn.commit() def media_id(self, user_id, loc_id, ts, lat, lng, ig_id): self.db.cur.execute("SELECT id FROM media WHERE ig_id=%s", (ig_id, )) row = self.db.cur.fetchone() if row is None: self.db.cur.execute( "INSERT INTO media (user, location, ts, lat, lng, ig_id) VALUES (%s, %s, %s, %s, %s, %s)", (user_id, loc_id, ts, lat, lng, ig_id)) media_id = self.db.cur.lastrowid self.db.conn.commit() else: media_id = row[0] return media_id def add_like(self, user_id, media_id): self.db.cur.execute("SELECT id FROM likes WHERE user=%s and media=%s", (user_id, media_id)) row = self.db.cur.fetchone() if row is None: self.db.cur.execute( "INSERT INTO likes (user, media) VALUES (%s, %s)", (user_id, media_id)) self.db.conn.commit() def add_comment(self, user_id, media_id): self.db.cur.execute( "SELECT id FROM comment WHERE user=%s and media=%s", (user_id, media_id)) row = self.db.cur.fetchone() if row is None: self.db.cur.execute( "INSERT INTO comment (user, media) VALUES (%s, %s)", (user_id, media_id)) self.db.conn.commit() def loc_min_ts(self, loc_id): self.db.cur.execute("SELECT min_ts FROM location WHERE id=%s", (loc_id, )) row = self.db.cur.fetchone() min_ts = row[0] return min_ts def set_loc_min_ts(self, loc_id, min_ts): self.db.cur.execute("UPDATE location SET min_ts=%s WHERE id=%s", (min_ts, loc_id)) self.db.conn.commit() def query_point_step(self, lat, lng, loc_id, min_ts, max_ts): # throttle time.sleep(0.5) min_dt = datetime.max try: medias = self.api.media_search(q="", distance=5000, count=1000, lat=lat, lng=lng, min_timestamp=min_ts, max_timestamp=max_ts) except InstagramClientError as e: print(e) if e.status_code == "500": return max_ts # give up on this location raise InstagramClientError(e.error_message, e.status_code) count = 0 for m in medias: try: lat = m.location.point.latitude lng = m.location.point.longitude ig_id = m.id ts = calendar.timegm(m.created_time.utctimetuple()) user_id = m.user.id self.add_user(user_id, m.user.username) media_id = self.media_id(user_id, loc_id, ts, lat, lng, ig_id) if m.created_time < min_dt: min_dt = m.created_time for u in m.likes: self.add_user(u.id, u.username) self.add_like(u.id, media_id) for c in m.comments: self.add_user(c.user.id, c.user.username) self.add_comment(c.user.id, media_id) count += 1 except AttributeError as e: print("attribute missing: %s" % e) print('retrived photos: %s' % count) print('date: %s' % min_dt) if len(medias) > 0: q_min_ts = calendar.timegm(min_dt.utctimetuple()) else: q_min_ts = max_ts return q_min_ts def query_point(self, lat, lng, loc_id, min_ts, max_ts): q_last_min_ts = -1 q_max_ts = self.loc_min_ts(loc_id) if q_max_ts < 0: q_max_ts = max_ts q_min_ts = self.query_point_step(lat, lng, loc_id, min_ts, q_max_ts) self.set_loc_min_ts(loc_id, q_min_ts) while q_min_ts != q_last_min_ts: q_last_min_ts = q_min_ts q_min_ts = self.query_point_step(lat, lng, loc_id, min_ts, q_min_ts) self.set_loc_min_ts(loc_id, q_min_ts) def run_once(self): self.db.cur.execute( "SELECT id, name, lat, lng FROM location WHERE done=0") rows = self.db.cur.fetchall() count = 0 min_ts = self.config.data['time_range']['min_ts'] max_ts = self.config.data['time_range']['max_ts'] for row in rows: count += 1 print(count, row[1]) self.query_point(row[2], row[3], row[0], min_ts, max_ts) self.db.cur.execute("UPDATE location SET done=1 WHERE id=%s", (row[0], )) self.db.conn.commit() return True def run(self): done = False while not done: try: done = self.run_once() except KeyboardInterrupt: print('interrupted by user.') done = True except Exception as e: print(e) print('done.')
[["40.78", "-111.97"], "Salt Lake City"], [["44.47", "-73.15"], "Burlington"], [["36.85", "-75.98"], "Virginia Beach"], [["47.45", "-122.30"], "Seattle"], [["38.37", "-81.60"], "Charleston"], [["42.95", "-87.90"], "Milwaukee"], [["41.15", "-104.82"], "Cheyenne"]] for location in locations: with open(location[1] + ".csv", 'wb') as f: writer = csv.writer(f, delimiter=',') writer.writerow([ "ID", "Created At", "Filter", "Number of Likes", "HashTags", "url" ]) for i in range(0, 5): recent_media = api.media_search( lat=location[0][0], lng=location[0][1], distance=5000, count=500, min_timestamp=time.time() - (i + 1) * 24 * 60 * 60, max_timestamp=time.time() - i * 24 * 60 * 60) for media in recent_media: tags = [] if hasattr(media, 'tags'): tags = media.tags writer.writerow([ media.id, media.created_time, media.filter, media.like_count, tags, media.images['standard_resolution'].url ])
def index(request): photos = [] if request.method == 'POST': # If the form has been submitted... form = MapForm(request.POST) # A form bound to the POST data if form.is_valid(): # All validation rules pass lat_f = form.cleaned_data['lat'] lng_f = form.cleaned_data['lng'] radius = int(float(form.cleaned_data['radius']) * 1000) date_form = form.cleaned_data['date'] if date_form == date.today(): end = datetime.now() else: end = datetime(date_form.year, date_form.month, date_form.day, 23, 59, 59) api = InstagramAPI(client_id='53ff568efcd6492eb9b88c7b92a615b4', client_secret='3649d3a0675647b1839a5aa580a10dbc') loc_photos = [] end_t = int(end.strftime('%s')) start = end - timedelta(hours=2) start_t = int(start.strftime('%s')) try: q = api.media_search(lat=lat_f, lng=lng_f, distance=radius, count=60, max_timestamp=end_t, min_timestamp=start_t) loc_photos = loc_photos + q except: return render_to_response( 'error.html', context_instance=RequestContext(request)) stop_date = end.day while(stop_date == end.day): end = start start = end - timedelta(hours=2) end_t = int(end.strftime('%s')) start_t = int(start.strftime('%s')) q = api.media_search(lat=lat_f, lng=lng_f, distance=radius, count=60, max_timestamp=end_t, min_timestamp=start_t) loc_photos = loc_photos + q photos = loc_photos top_list = [] for photo in photos: photo.created_time = photo.created_time + timedelta(hours=2) #likes = api.media_likes(photo.id) #photo.l = len(likes) #top_img = str(photo.images["low_resolution"]) #top_list.append({'top_username': photo.user.username, 'top_img': top_img, 'top_url': top_img[7:], 'top_likes': photo.l, 'top_time': photo.created_time}) #top_sort = sorted(top_list, key=lambda k: k['top_likes']) #top_list = top_sort[-3:] location_list = locations(lat_f, lng_f) return render_to_response( 'base.html', #{'photos': photos, 'locations': location_list, 'form': form, 'top_list': top_list}, {'photos': photos, 'locations': location_list, 'form': form}, context_instance=RequestContext(request)) else: form = MapForm() lat_f = 50.449443 lng_f = 30.456111 radius = 1000 end = datetime.now() api = InstagramAPI(client_id='53ff568efcd6492eb9b88c7b92a615b4', client_secret='3649d3a0675647b1839a5aa580a10dbc') loc_photos = [] end_t = int(end.strftime('%s')) start = end - timedelta(hours=2) start_t = int(start.strftime('%s')) try: q = api.media_search(lat=lat_f, lng=lng_f, distance=radius, count=60, max_timestamp=end_t, min_timestamp=start_t) loc_photos = loc_photos + q except: return render_to_response( 'error.html', context_instance=RequestContext(request)) stop_date = end.day while(stop_date == end.day): end = start start = end - timedelta(hours=2) end_t = int(end.strftime('%s')) start_t = int(start.strftime('%s')) try: q = api.media_search(lat=lat_f, lng=lng_f, distance=radius, count=60, max_timestamp=end_t, min_timestamp=start_t) loc_photos = loc_photos + q except: return render_to_response( 'error.html', context_instance=RequestContext(request)) photos = loc_photos top_list = [] for photo in photos: photo.created_time = photo.created_time + timedelta(hours=2) #likes = api.media_likes(photo.id) #photo.l = len(likes) #top_img = str(photo.images["low_resolution"]) #top_list.append({'top_username': photo.user.username, 'top_img': top_img, 'top_url': top_img[7:], 'top_likes': photo.l, 'top_time': photo.created_time}) #top_sort = sorted(top_list, key=lambda k: k['top_likes']) #top_list = top_sort[-3:] location_list = locations(lat_f, lng_f) return render_to_response( 'base.html', #{'photos': photos, 'locations': location_list, 'form': form, 'top_list': top_list}, {'photos': photos, 'locations': location_list, 'form': form}, context_instance=RequestContext(request))
for media in media_feed: # print media's user print ___19 # print caption if ___20: print ___20 print "++" # get followed people media and info crawled={} for media in media_feed: if media.user.id in crawled: ___21 crawled[media.user.id] = True # friend's recent media recent_media, next_ = api.user_recent_media(___1, count=10) # friend's info user_info = api.user(___2) # print number of media elements print ("Got %d items for user %s"%(len(recent_media), media.user)) # print user full name, id, bio, number of followers print ("This is %s, ID %s, bio %s, followed by %s"%(user_info.full_name, user_info.id, user_info.bio, user_info.counts['followed_by'])) print ("++") # search public content posted around a geo-location (cornell tech) crawled_media = api.media_search(___3=40.741, ___3=-74.002) print "Got %d results\n" % len(crawled_media)
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))
class InstagramService(object): def __init__(self): client_id, client_secret = DATASOURCES['instagram'][ 'client_id'], DATASOURCES['instagram']['client_secret'] self.api = InstagramAPI(client_id=client_id, client_secret=client_secret) app_id, app_secret = DATASOURCES['clarifai']['app_id'], DATASOURCES[ 'clarifai']['app_secret'] self.image_api = ClarifaiApi(app_id=app_id, app_secret=app_secret) def posts_in_new_york(self): media = [] for row in zips: lat, lng = float(row[0]), float(row[1]) posts = self.api.media_search(lat=lat, lng=lng, distance=800) for post in posts: temp_list = [ post.id, post.images['standard_resolution'].url, post.created_time, post.location.point.latitude, post.location.point.longitude ] media.append(temp_list) print temp_list return media def location(self, lat, lng): posts = self.api.media_search(lat=lat, lng=lng, distance=800) media = [] for post in posts: temp_list = [ post.id, post.images['standard_resolution'].url, post.created_time, post.location.point.latitude, post.location.point.longitude ] media.append(temp_list) print temp_list return media def hashtag(self, lat, lng): posts = self.api.media_search(lat=lat, lng=lng, distance=800) hashtags = {} for post in posts: tokens = str(post.caption).split(' ') for token in tokens: if token and len(token) > 1: if token[0] == '#': count = hashtags.setdefault(token, 0) hashtags[token] = count + 1 elif token[0] == '"' and token[1] == '#': count = hashtags.setdefault(token[1:], 0) hashtags[token[1:]] = count + 1 return hashtags def get_tags(self, place): tags = {} lat, lng = Geocode().geocode(place) posts = self.api.media_search(lat=lat, lng=lng, distance=800) for post in posts: result = self.image_api.tag_image_urls( post.images['standard_resolution'].url) image_tags = result['results'][0]['result']['tag']['classes'][:5] for tag in image_tags: count = tags.setdefault(tag, 0) tags[tag] = count + 1 return tags
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
#not working from instagram.client import InstagramAPI q = 100 count = 10 lat = 42.35 lng = 71.06 min_timestamp = 1388534400 max_timestamp = 1388552400 api = InstagramAPI(client_id="97437151d4c9456bb6c2b36a500e93b4", client_secret="11fb88b434d94820a23e2d32dca85a43") popular_media = api.media_search(q, count, lat, lng, min_timestamp, max_timestamp) for media in popular_media: print media.images['standard_resolution'].url
cities[row[0]] = city #temp test value to limit number of cities from csv we process counter+=1 if counter == MAX_CITIES: break print cities citiesDB.close() all_cities = [] all_media = [] api = InstagramAPI(client_id=CLIENT_ID, client_secret=CLIENT_SECRET) for cityName in cities: city = cities[cityName] media = api.media_search(count=MEDIA_COUNT, lat=city['lat'], lng=city['lng'], distance=city['distance']) city_list = [] for image in media: image_dict = {} image_dict['image-thumb'] = '' image_dict['image-large'] = '' image_dict['timestamp'] = '' image_dict['likes'] = '' image_dict['comments'] = '' image_dict['tags'] = [] image_dict['location'] = {} try: image_dict['image_thumb'] = image.images['thumbnail'].url image_dict['image_large'] = image.images['standard_resolution'].url image_dict['timestamp'] = image.created_time.isoformat() image_dict['likes'] = len(image.likes)
def index(request): date = datetime.now() context_dict = {'weather' : Weather.objects.get(date_id=date), 'popularity_list' : Popularity.objects.all() } user = request.user if request.method == 'POST': # Gather the username and password provided by the user. # This information is obtained from the login form. # We use request.POST.get('<variable>') as opposed to request.POST['<variable>'], # because the request.POST.get('<variable>') returns None, if the value does not exist, # while the request.POST['<variable>'] will raise key error exception user_id_for_follow = request.POST.get('user_id_for_follow') if user_id_for_follow: print user_id_for_follow access_token = request.POST.get('token') if access_token: print "got access token" try: api = InstagramAPI(access_token=access_token, client_secret=CONFIG['client_secret']) request.session['access_token'] = access_token api.follow_user(user_id=str(user_id_for_follow)) print "followed the poster" return render(request, 'home/index.html', {'weather' : Weather.objects.get(date_id=date), 'popularity_list' : Popularity.objects.all()}) except Exception as e: print('xxxx',e) return HttpResponse(e) else: photo_id_for_like = request.POST.get('photo_id_for_like') if photo_id_for_like: print photo_id_for_like access_token = request.POST.get('token') if access_token: print "got access token" try: api = InstagramAPI(access_token=access_token, client_secret=CONFIG['client_secret']) request.session['access_token'] = access_token print "trying to like the photo" api.like_media(media_id=photo_id_for_like) print "liked the photo" return render(request, 'home/index.html', {'weather' : Weather.objects.get(date_id=date)}) except Exception as e: print(e) return HttpResponse(e) else: lat = request.POST.get('lat') lng = request.POST.get('lng') search_value = request.POST.get('search_value') bool_clear_search = request.POST.get('clear_history') access_token = request.POST.get('token') favorite_place_name = request.POST.get('favorite_place_name') # Add the search history to the user's history. if user.is_anonymous(): print "Anonymous user detected, breaking early." else: profile = user.userprofile if search_value: profile.history = search_value + '\n' + profile.history elif bool_clear_search: profile.history = "" print "cleared history!!" profile.save() return render(request, 'home/index.html', {'weather' : Weather.objects.get(date_id=date), 'popularity_list' : Popularity.objects.all()}) if favorite_place_name != "": favorite_place_name = request.POST.get('favorite_place_name') favorite_place_content_string = request.POST.get('favorite_place_content_string') #fav = user.entry_set.create(user_profile=user, place_name=favorite_place_name, lat=lat, lng=lng, content_string=favorite_place_content_string) fav = Favorite.objects.get_or_create(user_profile=user, place_name=favorite_place_name, lat=lat, lng=lng, content_string=favorite_place_content_string) print fav print 'Favorite printed!' else: print "Not favorited!" if profile: print "There's a profile!" print user.username print profile.history profile.save() else: print "NO profile.. no login, no history." if access_token: try: api = InstagramAPI(access_token=access_token, client_secret=CONFIG['client_secret']) request.session['access_token'] = access_token print "Real ones ", lat, lng media_search = api.media_search(lat=lat, lng=lng, distance=100) photos = {} times = [] for media in media_search: photos[media] = media.get_standard_resolution_url() times.append(media.created_time) location_popularity = calculate_popularity(times) p = Popularity.objects.get_or_create(lat=lat,lng=lng,pop=location_popularity) popularity_list = Popularity.objects.all() context_dict = {'access_token': access_token, 'photos': photos, 'weather' : Weather.objects.get(date_id=date), 'popularity_list': popularity_list} except Exception as e: print(e) else: code = request.GET.get("code") if not code: return HttpResponse('missing code. \n Please ensure you are logged in instagram before searching for places!') try: access_token, user_info = unauthenticated_api.exchange_code_for_access_token(code) if not access_token: return HttpResponse('Could not get access token') api = InstagramAPI(access_token=access_token, client_secret=CONFIG['client_secret']) request.session['access_token'] = access_token print "Real ones ", lat, lng media_search = api.media_search(lat=lat, lng=lng, distance=100) photos = {} times = [] for media in media_search: photos[media] = media.get_standard_resolution_url() times.append(media.created_time) location_popularity = calculate_popularity(times) p = Popularity.objects.get_or_create(lat=lat,lng=lng,pop=location_popularity) popularity_list = Popularity.objects.all() context_dict = {'access_token': access_token, 'photos': photos, 'weather' : Weather.objects.get(date_id=date), 'popularity_list': popularity_list} except Exception as e: print(e) return render(request, 'home/index.html', context_dict) else: # No context variables to pass to the template system, hence the # blank dictionary object... return render(request, 'home/index.html', context_dict)
# Get popular media from Instagram print "Example of Getting the popular media from Instagram" popular_media = api.media_popular(count=10) for media in popular_media: print media.images['standard_resolution'].url print "Let's try something more cool with the Instagram API" print "In this example we can search media by latitude and longitude" # Madrid media_search = api.media_search(q="paradise places", count=5, lat='40.3985540', lng='-3.6216730') # Cotia, SP #media_search = api.media_search(q="cars", count=5, lat='-23.6026680', lng='-46.9194690') # Munich, Germany #media_search = api.media_search(q="#big data", count=5, lat='48.1351250', lng='11.5819810') # Moscow, Russia #media_search = api.media_search(q="#big data", count=5, lat='55.7558260', lng='37.6173000') for medias in media_search: print medias.images['standard_resolution'].url print "Now let's work with more cities - We will create a Location Matrix"