Example #1
0
class InstagramSearcher:
    """Creates an Instagram API client and returns search results"""
    def __init__(self):
        self.client = InstagramAPI(client_id=os.environ['EDUC8_IG_CLIENT_ID'], client_secret=os.environ['EDUC8_IG_CLIENT_SECRET'])
        self.search_results = []

    def get_ig_locations(self, lat, lng):
        return self.client.location_search(lat=lat, lng=lng)

    def search_all_locations(self, locations):
        for location in locations:
            try:
                results = self.client.location_recent_media(location_id = location.id)
                for media in results[0]:
                    result = {'source': 'Instagram'}
                    result['location_name'] = location.name
                    result['created'] = media.created_time
                    result['username'] = media.user.username
                    result['avatar'] = media.user.profile_picture
                    if media.caption:
                        result['caption'] = media.caption.text
                    else:
                        result['caption'] = ""
                    result['url'] = media.images['standard_resolution'].url
                    result['ig_shortcode'] = get_ig_shortcode(media.link) # TODO: this helper method is used by both IG and TwitterSearcher objects, how to make this work?
                    self.search_results.append(result)
            except InstagramAPIError as e:
                print e
            except Exception as ex:
                print ex

    def get_photo_url_from_shortcode(self, shortcode):
        ig_result = self.client.media_shortcode(shortcode)
        return ig_result.images['standard_resolution'].url
Example #2
0
def print_pics_dict1(yelp_id):
 
    api = InstagramAPI(client_id=client_id, client_secret=client_secret)

    recent_photos = {}
    instapics = []
    instalinks = []
    instalocations = read_csv_insta(yelp_id)

    # print instalocations.values()


    for location_id in instalocations.values():
        recent_media, next = api.location_recent_media(location_id=location_id)

        for media in recent_media:
            # print media.location.name
            # if recent_photos[media.location.name]
            # recent_photos[media.location.name] = {'instalocation': infos[8]}
            # print media.link
            if len(instapics) < 5:
                pic = media.images[u'standard_resolution'].url.encode('ascii', 'ignore')
                link = media.link
                instapics.append([pic, link])
                # print media.images[u'standard_resolution'].url.encode('ascii', 'ignore')
                place_name = media.location.name.encode('ascii', 'ignore')
                recent_photos[location_id] = { place_name: instapics }
        instapics = []
        instalinks = []

    # print recent_photos.keys()
    return recent_photos
Example #3
0
class VenuePhotoCrawlerInstagram:
    def __init__(self):
        self.client = InstagramAPI(
            client_id=config.instagram_client_id,
            client_secret=config.instagram_client_secret)

    def fetch_instagram_id(self, foursquare_id):
        print 'foursquare id is ', foursquare_id
        res = self.client.location_search(foursquare_v2_id=foursquare_id)
        return res[0].id

    def show_popular(self):
        popular_media = self.client.media_popular(20)
        for media in popular_media:
            print media.caption.text

    def grab_photos(self, foursquare_id, max_pages, min_timestamp):
        try:
            instagram_id = self.fetch_instagram_id(foursquare_id)
            gen = self.client.location_recent_media(
                count=200,
                location_id=instagram_id,
                as_generator=True,
                max_pages=max_pages,
                min_timestamp=min_timestamp)
            page_cnt = 0
        except:
            return
        for page in gen:
            save_photo_instagram(page[0], foursquare_id, instagram_id)
            print 'fetching page', page_cnt
            page_cnt += 1
            time.sleep(config.instagram_API_pause)
def location_recent_media(location_id):
	try:
		api = InstagramAPI(access_token=access_token, client_secret=client_secret)
		medias, next_ = api.location_recent_media(location_id=location_id,count=5000)
		for media in medias:
			if media.type == "image":
				media_id = media.id
				media_url = media.images['standard_resolution'].url
				user_id = media.user.id 
				location_id = media.location.id 
				location_name = media.location.name
				created_time = media.created_time

				tags = media.tags
				comments = media.comments
				caption = media.caption
				likes = len(media.likes)

				num_followed_by,num_follows,num_media = user(user_id)
				

				if not os.path.exists(media_id):
					os.makedirs(media_id)
				os.chdir(media_id)
				imagename = media_id + ".jpg"
				urllib.urlretrieve(media_url, imagename)
				filename = media_id + ".txt"
				f = open(filename, 'w')
				f.write("media_id" + "\t"  + media_id + "\n")
				f.write("user_id" + "\t" + user_id + "\n" )
				f.write("location_id" + "\t" + location_id + "\n" )
				f.write("location_name" + "\t" + str(location_name) + "\n" )
				f.write("created_time" + "\t" + str(created_time) + "\n" )
				f.write("caption" + "\t" + str(caption) + "\n")
				f.write("tags" + "\t")
				for tag in tags:
					f.write(str(tag) + "\t")
				f.write("\n")

				f.write("comments" + "\t")
				for comment in comments:
					f.write(str(comment) + "\t")
				f.write("\n")
				f.write("num_followed_by" + "\t" + str(num_followed_by) + "\n")
				f.write("num_follows" + "\t" + str(num_follows) + "\n")
				f.write("num_media" + "\t" + str(num_media) + "\n")
				f.close()
				os.chdir('..')
	except Exception as e:
		print(e)
Example #5
0
def location(request, id):
    api = InstagramAPI(client_id='53ff568efcd6492eb9b88c7b92a615b4', client_secret='3649d3a0675647b1839a5aa580a10dbc')

    tag_photos = []

    tag_m, next = api.location_recent_media(location_id=str(id))
    tag_photos = tag_photos + tag_m

    photos = tag_photos

    return render_to_response(
        'location_id.html',
        {'photos': photos},
        context_instance=RequestContext(request))
Example #6
0
def fetch_instagram(client_id, client_secret, lat=None, lng=None, foursquare_id=None):
    api = InstagramAPI(client_id=client_id, client_secret=client_secret)
    try:
        l = api.location_search(lat=lat, lng=lng, foursquare_v2_id=foursquare_id)
        ids = [o.id for o in l]
        images=[]
        for lid in ids:
            i, _ = api.location_recent_media(location_id=lid)
            for img in i:
                pic = img.images['standard_resolution']
                images.append((pic.url, pic.width, pic.height))
        return images
    except InstagramAPIError as e:
        return []
Example #7
0
def salon_info_instapics(instalocation):
    api = InstagramAPI(client_id=client_id, client_secret=client_secret)

    recent_photos = {}
    recent_photos_5 = []
    try: 
        recent_media, next = api.location_recent_media(location_id=instalocation)

        for media in recent_media:
            if len(recent_photos_5) < 5:
                pic = media.images[u'standard_resolution'].url.encode('ascii', 'ignore')
                link = media.link.encode('ascii', 'ignore')
                recent_photos_5.append([pic, link])
                place_name = media.location.name.encode('ascii', 'ignore')
                recent_photos[instalocation] = { place_name: recent_photos_5 }
    except:
        print "Couldn't get Instagram photos of" + instalocation

    return recent_photos
Example #8
0
def images_geo_json():
    api = InstagramAPI(access_token=access_token, client_secret=client_secret)
    json_locations = api.location_search(q=5000, count=None, lat=55.770968, lng=38.680028,
                                         foursquare_id=None, foursquare_v2_id=None)


    html_code = '''<html><body>'''
    for location in json_locations:
        media = api.location_recent_media(5, None, location.id)
        for element in media:
            if isinstance(element, list):
                html_code += images_geo(element, html_code, location.id)
            elif isinstance(element, Media):
                if image_ids.count(element.id) == 0:
                    image_ids.append(element.id)
                    html_code += '''<img src=''' + element.images['standard_resolution'].url \
                                 + ''' alt=''' + location.id + '''>'''

    html_code += '''</body></html>'''

    print image_ids
    print html_code
    return html_code
Example #9
0
class VenuePhotoCrawlerInstagram:
    def __init__(self):
        self.client = InstagramAPI(client_id = config.instagram_client_id, client_secret = config.instagram_client_secret)
    def fetch_instagram_id(self, foursquare_id):
        print 'foursquare id is ',foursquare_id
        res = self.client.location_search(foursquare_v2_id=foursquare_id)
        return res[0].id
    def show_popular(self):
        popular_media = self.client.media_popular(20)
        for media in popular_media:
            print media.caption.text
    def grab_photos(self, foursquare_id, max_pages):
        try:
            instagram_id = self.fetch_instagram_id(foursquare_id)
            gen = self.client.location_recent_media(count=200, location_id = instagram_id, as_generator=True, max_pages=max_pages)#, return_json=True)
            page_cnt = 0
        except:
            return 
        for page in gen:
            save_photo_instagram(page[0], foursquare_id, instagram_id)    
            print 'fetching page',page_cnt
            page_cnt+=1
            time.sleep(config.instagram_API_pause)
from instagram.client import InstagramAPI

api = InstagramAPI(client_id='ade077a508f241b599aa55d924730a10', client_secret='85a2c94c85d844b79d39e86e7d8d84a7')

from itertools import product
import numpy as np

coordinates = list(product(np.arange(50.40,50.47,0.005), np.arange(30.47,30.60,0.001)))

print coordinates

users = set()

for coord in coordinates:
    try:
        locations_list = api.location_search(lat=coord[0],lng=coord[1])
        for location in locations_list:
            medias = api.location_recent_media(location_id=location.id)[0]
            for media in medias:
                print media.user
    except:
        pass
# //1526678063.915f951.bedceeb5fb524dac942a0a72035df295
# //1497158604.73e896a.041f75b9d36940c084214a5f0141fdd8
# //1497158604.915f951.9fe2738dca34431ba78a350b7d1e2dd3
# //1526678063.e3c4e2f.242f1ae2c5514c369d0cb037204429e2
# //1530119206.915f951.60a0632d13304c6c9152f15165b85385
# //first:
# //pip install python-instagram
from instagram.client import InstagramAPI

api = InstagramAPI(access_token="1526678063.915f951.bedceeb5fb524dac942a0a72035df295")
print api.location(location_id="305429").name
recent_media,next = api.location_recent_media(location_id="305429")
for media in recent_media:
    print media.tags
Example #12
0
class InstaHandler(object):
    def __init__(self):
        # Connect to Instagram API
        self.orig_client = True
        # Set parameters in params.py
        self.api = InstagramAPI(access_token=params.access_token, client_secret=params.client_secret)
        self.itercount = 0
        self.PHOTO_LIST = []
        self.LOCATION_LIST = []

    def find_locations(self, lat, lng, radius):
        try:
            locations = self.api.location_search(lat=lat, lng=lng, distance=radius, count=33)
            #print(len(locations))
            # If response hits the limit
            # Limit = 33
            if len(locations) == 33:
                print 'Response hits limit of 33. Not all locations were returned.'
            for loc in locations:
                self.LOCATION_LIST.append({
                    "name": loc.name,
                    "id": loc.id,
                    "lat": loc.point.latitude,
                    "lon": loc.point.longitude
                })
        except Exception as e:
            print e
            if e.status_code == '429':
                print 'Limit exceeded.'

    def photo_in_location(self, location_id, max_id):
        try:
            if max_id == 0:
                medias = self.api.location_recent_media(location_id=location_id, count=33)
            else:
                medias = self.api.location_recent_media(location_id=location_id, max_id=max_id, count=33)
            self.itercount += 1
        except Exception, e:
            print e
            if e.status_code == '429':
                print 'Limit exceeded.'

        # Check if there are more photos to download
        if len(medias[0]) > 0:
            cont = True
            print medias[0][0].location.name
            for media in medias[0]:
                # Discard videos now
                if media.type == 'image':
                    tags_arr = []
                    for tag in media.tags:
                        tags_arr.append(tag.name)
                    caption = media.caption
                    if caption == None:
                        text = None
                    else:
                        text = caption.text
                    try:
                       self.PHOTO_LIST.append({
                            "id": media.id.split('_')[0],
                            "username": unicode(media.user.username).encode('utf8'),
                            "user_id": media.user.id,
                            "likes": media.like_count,
                            "comments": media.comment_count,
                            "tagged_users": len(media.users_in_photo),
                            "filter": media.filter,
                            "caption": unicode(text).encode('utf8'),
                            "url": media.link,
                            "photo_url": media.images['standard_resolution'].url,
                            "location_id": media.location.id,
                            "created_at": str(media.created_time),
                            "tags": tags_arr
                        })
                    except AttributeError:
                        continue
                    if media.created_time < DATE_LIMIT or self.itercount > 5:
                        cont = False

            # Recursively continue downloading, pass last media id
            if cont:
                self.photo_in_location(location_id, media.id.split('_')[0])
Example #13
0
def get_media(object, object_id, subscription_id):
    client_id = settings.IG_CLIENT_ID
    client_secret = settings.IG_CLIENT_SECRET
    api = InstagramAPI(client_id=client_id, client_secret=client_secret)
    if object == 'tag':
        try:
            tag_instance = IGTag.objects.get(ig_id=subscription_id)
            tag = tag_instance.tag
            
            tag_recent_media, next_ = api.tag_recent_media(3, 0, tag)
            ban_list = []
            try:
                banned_people = BadPerson.objects.all() #summon ban list
                for obj in banned_people:
                    ban_list.append(obj.username)
            except:
                pass
            for media in tag_recent_media:
                obj, created = InstaPost.objects.get_or_create(insta_id=media.id)
               
                obj.username = media.user.username
                obj.link = media.link
                obj.thumbnail = media.images['thumbnail'].url
                obj.standard_resolution = media.images['standard_resolution'].url
                obj.caption_text = media.caption.text
                obj.profile_picture = media.user.profile_picture
                obj.created_time = media.created_time

                obj.tag = tag_instance
                try:
                    ig_location_id = media.location.id
                    
                    loc, created = Location.objects.get_or_create(location_id=ig_location_id)
                    if created:
                       
                        loc.name = media.location.name
                        loc.latitude = media.location.point.latitude
                        loc.longitude = media.location.point.latitude
                        loc.save()
                        obj.location = Location.objects.get(location_id=ig_location_id)
                        obj.save()
                    
                    else:
                       
                        loc.name = media.location.name
                        loc.latitude = media.location.point.latitude
                        loc.longitude = media.location.point.longitude
                        loc.save()
                        obj.location = Location.objects.get(location_id=ig_location_id)
                        obj.save()
                        continue
                except:
                    pass

                obj.save()
                
        except:
            print "get media exception"
            pass
    elif object == 'location':
        
        loc_recent_media, next_ = api.location_recent_media(5, 9999999999999999999, object_id) #wtf
        
        ban_list = []
        try:
            banned_people = BadPerson.objects.all() #summon ban list
            for obj in banned_people:
                ban_list.append(obj.username)
        except:
            pass
        for media in loc_recent_media:
            obj, created = InstaPost.objects.get_or_create(insta_id=media.id)
            
            obj.username = media.user.username
            obj.link = media.link
            obj.thumbnail = media.images['thumbnail'].url
            obj.standard_resolution = media.images['standard_resolution'].url
            if media.caption.text:
                obj.caption_text = media.caption.text
            obj.profile_picture = media.user.profile_picture
            obj.created_time = media.created_time
            ig_location_id = media.location.id
            location_instance = Location.objects.get(location_id=ig_location_id)
            obj.location = location_instance
            obj.save()

    else:
        print "unsupported object"
        pass
class InstagramCrawler:

    def __init__(self):
        self.api = InstagramAPI(
            access_token=ACCESS_TOKEN,
            client_id = CLIENT_ID,
            client_secret = CLIENT_SECRET)
        self.clean_dir_csv()

    
    def clean_dir_csv(self):
        if os.path.exists(OUTPUT_CSV):
            os.remove(OUTPUT_CSV)

        if os.path.exists(OUTPUT_IMAGE_DIR):
            files = glob.glob(OUTPUT_IMAGE_DIR + '/*.jpg')
            for path in files:
                os.remove(path)
        else:
            os.mkdir(OUTPUT_IMAGE_DIR)

        if os.path.exists(OUTPUT_IMAGE320_DIR):
            files = glob.glob(OUTPUT_IMAGE320_DIR + '/*.jpg')
            for path in files:
                os.remove(path)
        else:
            os.mkdir(OUTPUT_IMAGE320_DIR)
                


    def search_instagram(self):
        location_ids, location_lls = self.__search_location_ids()
        count = 0

        for location_id,location_ll in zip(location_ids,location_lls):

            max_id = ""
            next = True
            while(not next is None):
                media_ids,next = self.api.location_recent_media(
                    count = 30,
                    location_id = location_id,
                    max_id = max_id)
            
                with open(OUTPUT_CSV, 'w') as csvfile:
                    #writer = csv.writer(csvfile, delimiter=str(','), quoting=csv.QUOTE_MINIMAL)
                    writer = csv.writer(csvfile, delimiter=str(','), quoting=csv.QUOTE_MINIMAL)                    
                    writer.writerow(['origin_url', 'file_name', 'latitude', 'longitude', 'location_id', 'tags'])
                    for media_id in media_ids:
                        thumb_url = media_id.images['thumbnail'].url
                        thumb320_url = media_id.images['low_resolution'].url

                        print "now %d downloading" % count
                        try:
                            r = requests.get(thumb_url)
                            r2 = requests.get(thumb320_url)
                            if r.status_code == 200 and r2.status_code == 200:
                                file_name = "%04d.jpg" % count
                                path = "{0}/{1}".format(OUTPUT_IMAGE_DIR, file_name)
                                self.__save_image(path, r.content)
                                path = "{0}/{1}".format(OUTPUT_IMAGE320_DIR, file_name)
                                self.__save_image(path, r2.content)

                                tags = [tag.name for tag in media_id.tags]
                                print tags
                                tags = unicode(','.join(tags)).encode('utf_8')

                                print writer.writerow([thumb_url, file_name, location_ll[0], location_ll[1], location_id, tags])
                                print file_name
                             
                        except Exception as e:
                            print type(str(e))
                            print e.message
                            pass

                        if not next is None:
                            temp, max_location_id = next.split("max_id=")
                            max_id = str(max_location_id)
                        count += 1

                next = None
                    
    
    def __search_location_ids(self):
        media_ids = self.api.location_search(
            count = LOCATION_COUNT,
            lat = LAT,
            lng = LNG,
            distance = DISTANCE)

        return [
            [media_id.id for media_id in media_ids],
            [[media_id.point.latitude, media_id.point.longitude] for media_id in media_ids]]

    def __save_image(self, save_path, img_contents):
        f = open(save_path, "wb")
        f.write(img_contents)
        f.close()
    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))
Example #16
0

venueid_4sq_insta = pd.read_csv('dataset/'+ city + '/splitdata/' + city + 'venueid_4sq_insta'+filename, header = None)

# number, venueid_4sq, venueid_insta
venueid_4sq_insta.columns = ['venueid_number', 'venueid_4sq', 'venueid_insta'] # assignt table name


for i in range(len(venueid_4sq_insta)):
    try:
        ivenueid_number = venueid_4sq_insta.ix[i, 'venueid_number']#number
        ivenueid_4sq = venueid_4sq_insta.ix[i, 'venueid_4sq']#venueid_4sq
        ivenueid_insta = venueid_4sq_insta.ix[i, 'venueid_insta']#venueid_insta
        
        # nextpage for finish all the check-ins
        [media_ids, nextpage] = api.location_recent_media(location_id = ivenueid_insta, count = 200)
        
        
        while nextpage:
            # maxid, extract the maxid
            nextid = nextpage.split('max_id=')[1].split('&client_id')[0]
            [new_media_ids, nextpage] = api.location_recent_media(\
            location_id = ivenueid_insta, max_id = nextid)
            
            media_ids = media_ids+new_media_ids# concencate media_ids
        
        if len(media_ids)>0:
            
            for j in range(len(media_ids)):
                
                # user id
# documentation here - https://github.com/Instagram/python-instagram
access_token = 'secret'

api = InstagramAPI(client_id=client_id, client_secret=client_secret)
#api = InstagramAPI(access_token=access_token)

search_location = ''

max_id = 0
i = 0

while True:
    print('iteration {}, max_id {}'.format(i, max_id))
    i += 1
    ans = api.location_recent_media(500, max_id, search_location)
    parsed = urlparse(ans[1])
    params = {a:b for a,b in [x.split('=') for x in parsed.query.split('&')]}
    max_id = int(params['max_tag_id']) - 1
    for m in ans[0]:
        try:
            u = db_session.query(User).filter_by(uid=str(m.user.id)).one()
        except NoResultFound:
            u = User(username=m.user.username, uid=m.user.id)
            db_session.add(u)
            db_session.commit()
    
        mo = Media(mid=m.id, link=m.link, user_id=u.id, mtype=m.type, created_time=m.created_time)
        if m.type == 'image':
            mo.standard_resolution = m.images['standard_resolution'].url
        elif m.type == 'video':
Example #18
0
    def InstagramProcces(self):

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                user_follows, next = api.user_follows(user_id)

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

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

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

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

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

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

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

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

            self.update_progressbar(100)

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

            self.reject()

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

        return
Example #19
0
from instagram.client import InstagramAPI

api = InstagramAPI(client_id='ade077a508f241b599aa55d924730a10',
                   client_secret='85a2c94c85d844b79d39e86e7d8d84a7')

from itertools import product
import numpy as np

coordinates = list(
    product(np.arange(50.40, 50.47, 0.005), np.arange(30.47, 30.60, 0.001)))

print coordinates

users = set()

for coord in coordinates:
    try:
        locations_list = api.location_search(lat=coord[0], lng=coord[1])
        for location in locations_list:
            medias = api.location_recent_media(location_id=location.id)[0]
            for media in medias:
                print media.user
    except:
        pass
					places.append([name,lon,lat,stars,review_counts])

distance=2
count=1
combine=[]

for i in range(len(places)):
	lon=places[i][1]
	lat=places[i][2]
	stars=places[i][3]
	review_counts=places[i][4]
	yelp_name=places[i][0]
	location=api.location_search(distance,count,lat,lon)

	for place in location:
		if check_name(yelp_name,place.name):
			recent_media, next= api.location_recent_media(location_id=place.id)
			if len(recent_media)!=0:
				combine.append([yelp_name,stars,review_counts,len(recent_media)])			

outfile = open('finalProjectPA_WI.csv','w')
outfile.write('Name,Yelp_stars,Yelp_review,Instagram_checkin\n')

for item in combine:
	line=item[0]+','+str(item[1])+','+str(item[2])+','+str(item[3])
	outfile.write(unicode(line).encode('utf-8')+'\n')
outfile.close()



Example #21
0
    #print ('%s', parts[1]);
    str1 = ''.join(words)
    location_id = str1
    filepreamble = "output/November2014/location_"
    filepostamble = ".txt"
    outputfilename = filepreamble + location_id + filepostamble
    outputfile = open(outputfilename, 'w')
    string1 = "curl https://api.instagram.com/v1/locations/"
    string2 = "/media/recent?access_token=XXXXXXX"
    # print location_id
    cmd = string1 + location_id + string2
    #os.system('curl "https://api.instagram.com/v1/locations/search?access_token=XXXXXXX&foursquare_v2_id="+location_id ')
    time.sleep(1)

    try:
        recent_media, next = api.location_recent_media(location_id=location_id)
    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
    except bind.InstagramClientError, ice:
        if ice.status_code == 404:
# Fill in your API information
client_id = 'YOUR_ID'
client_secret = 'YOUR_SECRET'
api = InstagramAPI(client_id=client_id, client_secret=client_secret)


# Do your first location search
# See ReadMe for quick method to find Instagram location IDs
locationID=sys.argv[1]
iterations=sys.argv[2]

iterations = int(iterations)
locationID = int(locationID)
max_tag_id = ''
all_media = []
ans = api.location_recent_media(33,max_tag_id,locationID)

# Get your first media items and max_tag_id from that search
for m in ans[0]:
	all_media.append(m)
	parsed = urlparse(ans[1])
	params = {a:b for a,b in [x.split('=') for x in parsed.query.split('&')]}

# Iterate backwards through media, using max_tag_id, appending posts to the all_media array
# Increase the range() number to run more iterations for more data
for i in range(iterations):
	try:
	    max_tag_id = params['max_id']
	    ans = api.tag_recent_media(33,max_tag_id, locationID)
	    for m in ans[0]:
	        all_media.append(m)
Example #23
0
    str1 = ''.join(words)
    location_id=str1
    filepreamble="output/November2014/location_"
    filepostamble=".txt"
    outputfilename=filepreamble+location_id+filepostamble
    outputfile= open(outputfilename, 'w')
    string1="curl https://api.instagram.com/v1/locations/"
    string2="/media/recent?access_token=XXXXXXX"
    # print location_id
    cmd=string1+location_id+string2
    #os.system('curl "https://api.instagram.com/v1/locations/search?access_token=XXXXXXX&foursquare_v2_id="+location_id ')
    time.sleep(1)
    

    try:
        recent_media, next = api.location_recent_media(location_id=location_id)
    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
    except bind.InstagramClientError, ice:
        if ice.status_code == 404: