Exemplo n.º 1
0
def insta(search_term):
	returnval=''

	api = InstagramAPI(access_token=ACCESS_TOKEN, client_secret=CLIENT_SECRET)

	try:
		username = search_term
		#print username
		if ' ' in username:
			username = username.replace(' ', '')

		if not api.user_search(username, 1):
			return "No user by that name was found."
		else:
			userid = api.user_search(username, 1)[0].id
			recent_media, next_ = api.user_recent_media(user_id=userid,count=5)
			for media in recent_media:
			   #text = media.caption.text
			   link = media.link
			   returnval += '~'+link+'\n'

			return returnval

	except InstagramAPIError as e:
	   if (e.status_code == 400):
	      return "Cannot retrieve data. User is set to private."
	   if (e.status_code == 404):
	   	  return "Content not found."
Exemplo n.º 2
0
def insta(search_term):
	returnval=''

	api = InstagramAPI(access_token=ACCESS_TOKEN, client_secret=CLIENT_SECRET)

	try:
		username = search_term
		#print username
		if ' ' in username:
			username = username.replace(' ', '')

		if not api.user_search(username, 1):
			return "No user by that name was found."
		else:
			userid = api.user_search(username, 1)[0].id
			recent_media, next_ = api.user_recent_media(user_id=userid,count=5)
			for media in recent_media:
			   #text = media.caption.text
			   link = media.link
			   returnval += '~'+link+'\n'

			return returnval

	except InstagramAPIError as e:
	   if (e.status_code == 400):
	      return "Cannot retrieve data. User is set to private."
	   if (e.status_code == 404):
	   	  return "Content not found."
Exemplo n.º 3
0
def search_user(request):
    print 'search_user'
    if request.method == 'POST':
        form = InstagramUserForm(request.POST)
        if form.is_valid():
            review_id = request.POST['username']
            account = SocialAccount.objects.get(user__username=request.user.username)
            social_app = SocialApp.objects.get(name='Instagram')
            token = SocialToken.objects.get(account=account, app=social_app)
            api = InstagramAPI(access_token=token)

            context = {'users':[], 'form': InstagramUserForm(), 'id_form': InstagramUserIDForm()}

            results = api.user_search(q=review_id)
            for result in results:
                context['users'].append({'username': result.username, 'id': result.id})

            # url = 'https://api.instagram.com/v1/users/search?q={id}&access_token={token}'.format(id=review_id, token=token.token)
            # try:
            #     response = requests.get(url)
            #     data = json.loads(response.content)
            #     if data['meta']['code'] == 200:
            #         for d in data['data']:
            #             context['users'].append({'username': d['username'], 'id': d['id']})
            # except ValueError:
            #     pass
    return render(request, 'core/user_search.html', context)            
Exemplo n.º 4
0
def auth_request():
    api = InstagramAPI(access_token=OTHER['access_token'])
    target_ids = api.user_search(OTHER['target'])

    target_id = None
    for search_hit in target_ids:
        if search_hit.username == OTHER['target']:
            target_id = search_hit.id
            break

    if target_id == None:
        logging.error('Did not find user, please check username')
        return []

    my_name   = api.user().username
    logging.debug('Starting check recent media')
    recent_media, url = api.user_recent_media(user_id=target_id, count = 20)
    liked_media = []
    for media in recent_media:
        logging.debug('Processing media %s' % media.id)
        users = api.media_likes(media.id)
        will_like = True
        for user in users:
            if user.username == my_name:
                will_like = False
                break
        if will_like:
            logging.debug('Liking media %s' % media.id)
            api.like_media(media.id)
            liked_media.append(media)
        else:
            logging.debug('Already liked media %s, aborting like' % media.id)

    return liked_media
Exemplo n.º 5
0
def auth_request():  
    api = InstagramAPI(access_token=OTHER['access_token'],client_secret=CONFIG['client_secret'])
    target_ids = api.user_search(OTHER['target'],1)
    if len(target_ids) > 1:
        logging.error('Found mutiple users, please check username')
        return

    target_id = target_ids[0].id
    my_name   = api.user().username
    logging.debug('Starting check recent media')
    recent_media, url = api.user_recent_media(user_id=target_id, count = 1)
    liked_media = []
    for media in recent_media:
        logging.debug('Processing media %s' % media.id)
        users = api.media_likes(media.id)
        will_like = True
        for user in users:
            if user.username == my_name:
                will_like = False
                break
        if will_like:
            logging.debug('Liking media %s' % media.id)
            api.like_media(media.id)
            liked_media.append(media)
        else:
            logging.debug('Already liked media %s, aborting like' % media.id)

    return liked_media
Exemplo n.º 6
0
def user(username, page=1):
    u = InstagramAPI(access_token=session['access_token'],
                     client_secret=secret.secrets['client_secret'])
    id = u.user_search(username)[0].id
    user_media, next_ = u.user_recent_media(user_id=id,count=20)

    for i in range(1, page):
        user_media, next_ = u.user_recent_media(user_id=id,
                                                count=20,
                                                with_next_url=next_)
    photos_thumbnail = []
    photos_standard = []
    title = username + " Recent Media-Page " + str(page)
    prev_page = False
    next_page = False
    if next_:
        prev_page = True
    if page != 1:
        next_page = True
    for media in user_media:
        photos_thumbnail.append("%s" % media.images['thumbnail'].url)
        photos_standard.append("%s" % media.images['standard_resolution'].url)
    return render_template("recent.html", thumb=photos_thumbnail,
                           photos=photos_standard, prev=prev_page,
                           next=next_page, page=page, title=title)
Exemplo n.º 7
0
class InstagramClient:
    """ Find muscal user's recent posts
        return a list of album and artist names.
    """
    def __init__(self):
        # Make an unauthenticated connection
        self.api = InstagramAPI(
            client_id=INSTAGRAM_AUTH['CLIENT_ID'],
            client_secret=INSTAGRAM_AUTH['CLIENT_SECRET'])

    def search_user(self):
        # Find the user based on settings
        return self.api.user_search(q=INSTAGRAM_USER)

    def get_user_id(self):
        # Convert the user name specified into a user_id
        return self.search_user()[0].id

    def recent_media(self):
        # Get a list of the most recent posts.
        user_id = self.get_user_id()
        get_media, next_page = self.api.user_recent_media(user_id=user_id,
                                                          count=30)
        media = get_media
        while next_page:  # Paginate using the query string returned
            qry_str = next_page.split("?")[-1]
            max_id = parse_qs(qry_str)['max_id'][0]
            get_media, next_page = self.api.user_recent_media(user_id=user_id,
                                                              count=30,
                                                              max_id=max_id)
            media = media + get_media

        return media

    def get_recent_album_details(self):
        # Add the album artist details to self.recent_albums
        recent_media = []
        for media in self.recent_media():
            try:
                caption_text = media.caption.text
                artist, album = self.parse_name(caption_text)
            except AttributeError:
                print "Cannot get caption text for %s" % media.link
                artist, album = (None, None)
            if artist and album:
                recent_media.append((artist, album))
        return recent_media

    def parse_name(self, comment_text):
        # Parse album and artist from the comment
        try:
            tag, artist, album = comment_text.split("-")
            if tag.strip() != TARGET_TAG:
                raise ValueError  # lazy way to skip non 3old3new posts
        except ValueError:
            # if there are too few dashes, move on
            print "Cannot parse %s " % comment_text
            return [None, None]

        return artist.strip(), album.strip()
Exemplo n.º 8
0
	def instagram(self, nameIn = None):
		if(nameIn == None):
			raise ValueError('TwitterSearch.instagram: name is necessary')
		access_token  = "2941460406.4011edb.8d81ba564aa340aca6d77b690e16b415"
		client_secret = "27f2a6304afe420fa5d12123648a65c9"
		api = InstagramAPI(access_token=access_token, client_secret=client_secret)
		result = api.user_search(nameIn)
		return result	
Exemplo n.º 9
0
def main():

    access_token = secret.ACCESS_TOKEN
    api = InstagramAPI(access_token=access_token)

    username = "******"
    me = api.user_search(username, count=1)[0].id

    # run whatever you want
    print NonmutualFollowers(api).find(me)
Exemplo n.º 10
0
def main():

    access_token = secret.ACCESS_TOKEN
    api = InstagramAPI(access_token=access_token)

    username = "******"
    me = api.user_search(username, count=1)[0].id

    # run whatever you want
    print NonmutualFollowers(api).find(me)
Exemplo n.º 11
0
def insta(account):
    """
    @account: str
    """
    log(account, "@")
    api = InstagramAPI(
        client_id=cfg.get('instagram', 'client_id'),
        client_secret=cfg.get('instagram', 'client_secret')
    )
    user = api.user_search(account)[0]
    found_user = api.user(user.id)
    return found_user.counts['followed_by']
Exemplo n.º 12
0
def instagram(key):
	api = InstagramAPI(client_id='b64f54dc4fb3486f87b55d92381e2625', client_secret='b5fa80c366b94cc980c882855630fe92')
	for item in api.user_search(q=key, count=10):
		username = item.username
		dp = item.profile_picture
		did = item.id
		web = item.website

		bio = item.bio
		print dp, username, did,web, bio
	lol = "text"
	return lol
Exemplo n.º 13
0
def instagram1(key):
	api = InstagramAPI(client_id='b64f54dc4fb3486f87b55d92381e2625', client_secret='b5fa80c366b94cc980c882855630fe92')
	for item in api.user_search(q=key, count=10):
		username = item.username
		dp = item.profile_picture
		did = item.id
		web = item.website

		bio = item.bio
		print dp, username, did,web, bio
	lol = "text"
	return lol
Exemplo n.º 14
0
    def clean(self):
        """ При сохранении проверяем существование аккаунта и получаем инфу о нем из Instagram"""

        api = InstagramAPI(client_id=settings.INSTAGRAM_CLIENT_ID, client_secret=settings.INSTAGRAM_CLIENT_SECRET)
        user = api.user_search(self.username, 1)

        # Поиск ведется по четкому совпадению введенного имени
        if len(user) == 0 or self.username != user[0].username:
            raise ValidationError(u'Пользователя с таким именем не существует')

        self.instagram_id = user[0].id
        self.full_name = user[0].full_name
        self.profile_picture = user[0].profile_picture
Exemplo n.º 15
0
class Instagram:
    def __init__(self):
        access_token = "1c7300cd9baa4ebb875cf970450ea49a"
        client_secret = "773cba503f6a4056bf2610d01077dfcb"
        self.api = InstagramAPI(client_id=access_token, client_secret=client_secret)

    def search(self, account):
        return self.api.user_search(q=account, count=1)

    def get_user(self, name):
        user = self.search(name)

        return user[0]

    def get_media(self, user_id, count = 10):
        return self.api.user_recent_media(user_id=user_id, count=count)
Exemplo n.º 16
0
class Instagram(Plugin):
    def load(self):
        access_token = self.config.get(c.ACCESS_TOKEN_KEY)
        client_secret = self.config.get(c.CLIENT_SECRET_KEY)
        self.api = InstagramAPI(access_token=access_token,
                                client_secret=client_secret)

    @command(p.string('!instagram') + p.bind(p.word, 'channel'), master_only)
    async def last_pic(self, message, channel):
        users = self.api.user_search(channel)
        if users:
            user = users[0]
            media, _ = self.api.user_recent_media(user_id=user.id, count=10)
            medium = random.choice(media)
            await self.send_message(
                message.channel,
                'A media from `{}`\'s instagram\n{}'.format(
                    user.full_name, medium.images['standard_resolution'].url),
                delete_after=30)
Exemplo n.º 17
0
def select_user(request):
	
	if not request.session.get('token'):
		return redirect('totem.views.auth')
	token = request.session.get('token')
	output = {}

	api = InstagramAPI(access_token=token)

	if request.GET.get('username'):
		
		user = request.GET.get('username')
		users = api.user_search(q=user)		

		if len(users) == 1:
			user = users[0]
			return redirect(reverse('totem.views.show_photos', kwargs={'user': user.id }))

	return render_to_response('totem/select-user.html', output, context_instance=RequestContext(request))
Exemplo n.º 18
0
def find_identifier():
# if instagram info is in session variables, then display user photos
	if 'instagram_access_token' in session and 'instagram_user' in session:
		api = InstagramAPI(access_token=session['instagram_access_token'])

		identifier = re.compile("We've teamed up with our friends")
		users_to_follow = 'knowlita, acme_nyc'
		user_list = users_to_follow.split(',')
		user_list = [x.strip(' ') for x in user_list]  
		
		uids = []
		for key_user in user_list:
			user_search = api.user_search(q=key_user)
			uids.append(user_search[0].id)

		media_ids = []
		for uid in uids:
			recent_media, next = api.user_recent_media( user_id=uid , count=30)
			for media in recent_media:
				if media.caption != None:
					if identifier.search(media.caption.text):
						media_ids.append((media.id, media.images['standard_resolution'].url))
					else:
						recent_media, next = api.user_recent_media( with_next_url = next)
						for media in recent_media:
							if media.caption != None:
								if identifier.search(media.caption.text):
									media_ids.append((media.id, media.images['standard_resolution'].url))

		
		templateData = {
			'media_ids' : media_ids,
			'uids' : uids
		}

		return render_template('find_identifier.html', **templateData)
	else:
		return redirect('/connect')
Exemplo n.º 19
0
class Instagram(Plugin):

    def load(self):
        access_token = self.config.get(c.ACCESS_TOKEN_KEY)
        client_secret = self.config.get(c.CLIENT_SECRET_KEY)
        self.api = InstagramAPI(
            access_token  = access_token,
            client_secret = client_secret
        )

    @command(p.string('!instagram') + p.bind(p.word, 'channel'), master_only)
    async def last_pic(self, message, channel):
        users = self.api.user_search(channel)
        if users:
            user = users[0]
            media, _ = self.api.user_recent_media(user_id=user.id, count=10)
            medium = random.choice(media)
            await self.send_message(
                message.channel,
                'A media from `{}`\'s instagram\n{}'
                    .format(user.full_name, medium.images['standard_resolution'].url),
                delete_after = 30
            )
Exemplo n.º 20
0
def User_Search():
    u = InstagramAPI(access_token=session['access_token'],
                     client_secret=secrets['client_secret'])
    if request.method == "POST":
        query = request.form['query']
        if query is None:
            return "Please Enter something."
        else:
            user_search_result = u.user_search(query)
            users = []
            user_profile_picture = []
            for user in user_search_result:
                users.append("{}".format(user.username))
                user_profile_picture.append("{}".format(user.profile_picture))
            if len(users) is 0:
                e = "No Users Found!"
                return render_template("search.html", error=e,
                                       title="User Search")

            return render_template("search.html", title="User Search",
                                   users=users,
                                   user_profile_picture=user_profile_picture,
                                   )
    return render_template("search.html", title="User Search")
# Like all media by a user
# by Christopher Su
# May 3, 2014

from instagram.client import InstagramAPI
import sys
import secret

USERNAME = str(sys.argv[1])

access_token = secret.ACCESS_TOKEN
api = InstagramAPI(access_token=access_token)
USER_ID = api.user_search(USERNAME, count=1)[0].id
recent, next = api.user_recent_media(user_id=USER_ID, count=-1)
recent = recent[200:250]
for cur in recent[:-1]:
    api.like_media(cur.id)
    print "Liked %s" % cur.link
Exemplo n.º 22
0
# this function takes the usernames friends and his/her friends of friends and creates a networkx graph
def networkx_graph(follow_list, friends_follow, username):
    nxg = nx.Graph()
    [nxg.add_edge(username, follow.username) for follow in follow_list]
    \
    
    for name in friends_follows:
        [nxg.add_edge(name, follower) for follower in friends_follows[name][0]]
    return nxg

# main arguments
if __name__ == '__main__':
    access_token = ["XXXXX",
                    "XXXXX"]
    api = InstagramAPI(access_token=access_token[0])
    user_name = sys.argv[1]
    if user_name:
        user_id = api.user_search(user_name)[0].id
    else:
        user_id = "337135" # default to Itamar's account
    print user_id
    try: #error handling for private users
        friends = get_follow_list(user_id)
        friends_follows = get_friends_follow(friends[:10])[0] # to avoid rate limiting, taking the first 10 friends
        network_graph = networkx_graph(friends, friends_follows, user_name)
        nx.draw(network_graph)
        plt.show()
    except:
        print "Private User"
from instagram.client import InstagramAPI
from urlparse import urlparse, parse_qs
from progressbar import ProgressBar, SimpleProgress, Bar
import sys, string

max_photos_per_request = 100
total_likes = num_returned = 0

access_token = string.strip(open("token.txt").read())
api = InstagramAPI(access_token=access_token)

if len(sys.argv) == 2:
    user = api.user_search(sys.argv[1], 1)[0]  # optional argument: user id (default: self)
else:
    user = api.user("self")

media_count = api.user(user.id).counts["media"]

pbar = ProgressBar(widgets=[SimpleProgress(), " photos ", Bar()], maxval=media_count).start()

next_max_id = ""

while True:
    recent_media, next = api.user_recent_media(user_id=user.id, count=max_photos_per_request, max_id=next_max_id)

    num_returned += len(recent_media)
    if num_returned > pbar.maxval:  # Instagram media count isn't always accurate :(
        pbar.maxval = num_returned
    pbar.update(num_returned)

    for media in recent_media:
Exemplo n.º 24
0
class InstagramCrawler(object):
    """
    Crawl data in mentioned instagram accounts.
    """
    def __init__(self, access_token, client_secret, db_interface):
        """
        Init instagram crawler, and the api object.

        Args:
            access_token (str): instagram api access token
            client_secret (str): instagram api secret client
            db_interface (MongoInterface)

        """
        self._api = InstagramAPI(access_token=access_token,
                                 client_secret=client_secret)

        self._db_interface = db_interface

    def fetch_ids(self, accounts):
        """
        Given instagram accounts, get their ids.

        accounts (list): instagram usernames
        """
        logger.info('fetching instagram IDs')

        users = {}

        accounts = list(set(accounts))

        for account in accounts:
            logger.debug('fetching id for %s', account)
            user_list = self._api.user_search(account)

            for user in user_list:
                if user.username == account:
                    users[account] = {}
                    users[account]['id'] = user.id
                    users[account]['image'] = user.profile_picture

        return users

    def run(self, users):
        """
        Get all fetched images.

        Args:
            users (dict): instagram user dicts in the following format:
            {
            username:
                {
                id:
                image
                }
            }
        Returns:
            list: fetched images
        """
        self._user_dicts = users

        for user in self._user_dicts:
            self._fetch_for_user(user)

    def _fetch_for_user(self, username):
        """
        Given a user, get recent data on his profile.

        Args:
            username (str)

        Returns:
            list: fetched images
        """
        logger.info('retrieving data for %s', str(username))

        recent_media, next_ = self._api.user_recent_media(
            user_id=self._user_dicts[username]['id'])

        for media_item in recent_media:
            self._parse_media_item(media_item, username)

    def _parse_media_item(self, media_item, username):
        """
        Parse media object.

        Args:
            media_item (obj): instagram media object
            username (str)

        Returns:
            dict: parsed media_item item
        """
        parsed_media_item = {}

        parsed_media_item['account'] = username
        parsed_media_item['account_image'] = self._user_dicts[username][
            'image']

        if media_item.caption:
            parsed_media_item['caption'] = media_item.caption.text

        parsed_media_item['lang'] = 'en'
        parsed_media_item['date'] = media_item.created_time

        parsed_media_item['tags'] = []
        for tag in media_item.tags:
            parsed_media_item['tags'].append(str(tag).split(' ')[1])

        parsed_media_item['url'] = media_item.link
        parsed_media_item['media_type'] = media_item.type

        if media_item.type == 'image':
            parsed_media_item['img_vid_src'] = media_item.images[
                'standard_resolution'].url
        else:
            parsed_media_item['img_vid_src'] = media_item.videos[
                'standard_resolution'].url

        parsed_media_item['likes'] = media_item.like_count
        parsed_media_item['media_id'] = media_item.id

        parsed_media_item['src'] = 'instagram'
        parsed_media_item['type'] = 'social_media'

        self._db_interface.insert_one('timeline_items', parsed_media_item)
        self._db_interface.insert_one('instagram_post', parsed_media_item)
Exemplo n.º 25
0
class RecolectorUsuario(Recolector):
	"""
		Recolecta la información del usuario dado un nombre de usuario,
		Es muy importante porque todas las queries en Instagram usan id y no nombre.
		Por lo que es necesario hacer un patrón proxy
	"""
	def __init__(self, escritor):
		super(RecolectorUsuario, self).__init__(escritor)
		self.authorizator = GetAuthorizations(4999)
		self.apoyo = ApoyoInstagram()
		self.api = None
		self.inicializa()

	def inicializa(self):
		self.authorizator.load_token()
		client_id, client_secret = self.authorizator.get_secret()
		self.api = InstagramAPI(client_id=client_id, client_secret=client_secret)


	def recolecta(self, query, forceDownload = False):
		"""
			Implementado proxy
			por ahora solo admite un usuario por nombre de usuario
		"""
		id = self.apoyo.isUserInDBByUsername(query)
		if id == False:
			usuariosArray = self.privateRealizaConsulta(query)
			self.guarda(usuariosArray)
		else:
			if self.apoyo.isUserCompleteInDBByUsername(query) == True:
				if forceDownload == True:
					usuariosArray = self.privateRealizaConsulta_id(id)
					self.guarda(usuariosArray)
			else:
				usuariosArray = self.privateRealizaConsulta_id(id)
				self.guarda(usuariosArray)

	def guarda(self, arrayDatos):
		#if len(self.escritores) == 0:
		#	print arrayDatos

		for escritor in self.escritores:
			escritor.escribe(arrayDatos)

	def privateRealizaConsulta(self, query):
		if self.authorizator.is_limit_api():
			raise Exception('LIMITE')

		try:
			users = self.api.user_search(q=query, count="1")
			if len(users) <= 0:
				return []
			user = users[0]
			identificador = user.id
			self.authorizator.add_query_to_key()
			usuarioFinal = self.api.user(identificador)
			self.authorizator.add_query_to_key()
			return [usuarioFinal]

		except Exception, e:
			self.authorizator.add_query_to_key()
			print e
			if "429" in str(e):
				raise Exception('LIMITE')
			return []
Exemplo n.º 26
0
def index(request, access_token_error=None, username_error=None):

    # Verify that the request is a POST
    if request.method == 'POST':

        # If the access_token argument is empty, use the access token from setting.py; otherwise use the access_token argument
        if request.POST['access_token']:
            access_token = request.POST['access_token']
        else:
            access_token = settings.INSTAGRAM_ACCESS_TOKEN

        # Initialize Instagram library with access_token
        api = InstagramAPI(access_token=access_token)

        if request.POST['username']:
            # Find users matching username (should only be one)
            try:
                users = api.user_search(request.POST['username'])

            # Rescue exception if access_token is incorrect
            except:
                if request.POST['access_token']:
                    access_token_error = "The Access Token entered is invalid"
                else:
                    access_token_error = "The Access Token in settings.py is invalid"

        try:
            # Map attributes of the first and only user in the users list
            user = users[0]
            attributes = {
                'id': user.id,
                'bio': user.bio,
                'full_name': user.full_name,
                'profile_picture': user.profile_picture,
                'username': user.username,
                'website': user.website,
            }

            # If any value in the attributes dict is blank, then remove the key value pair from the dict
            attributes = dict((k, v) for k, v in attributes.items() if v)

        # If users array is empty, then no matching users were found
        except:
            username_error = "The username entered is not valid"

        if request.is_ajax():

            # If an exception occured, determine which error message should be displayed
            if (access_token_error or username_error):
                if access_token_error:
                    error = access_token_error
                elif username_error:
                    error = username_error

                # Return error message to template
                return HttpResponse(json.dumps({"error": (error)}),
                                    mimetype="application/json")

            # If lookup request is successful, return attributes to template
            else:
                return HttpResponse(json.dumps(attributes),
                                    mimetype="application/json")
        # If POST request isn't AJAX, manually assign view context
        else:
            context = {}

            # If an exception occured, assign an error to the context, otherwise assign the attributes dict to the context
            if access_token_error:
                context.update({'error': access_token_error})
            elif username_error:
                context.update({'error': username_error})
            else:
                context = attributes

            # Return view context to template
            return render(request, 'index.html', context)

    # If request isn't POST, then display the template
    else:
        return render(request, 'index.html')
Exemplo n.º 27
0
def index(request, access_token_error=None, username_error=None): 

	# Verify that the request is a POST
	if request.method == 'POST':
		
		# If the access_token argument is empty, use the access token from setting.py; otherwise use the access_token argument
		if request.POST['access_token']:
			access_token = request.POST['access_token']
		else:
			access_token = settings.INSTAGRAM_ACCESS_TOKEN
		
		# Initialize Instagram library with access_token
		api = InstagramAPI(access_token=access_token)

		if request.POST['username']:
			# Find users matching username (should only be one)
			try:
				users = api.user_search(request.POST['username'])

			# Rescue exception if access_token is incorrect
			except:
				if request.POST['access_token']:
					access_token_error = "The Access Token entered is invalid"
				else:
					access_token_error = "The Access Token in settings.py is invalid"

		try:
			# Map attributes of the first and only user in the users list
			user = users[0]
			attributes = {
				'id': user.id,
				'bio': user.bio,
				'full_name': user.full_name,
				'profile_picture': user.profile_picture,
				'username': user.username,
				'website': user.website,
			}

			# If any value in the attributes dict is blank, then remove the key value pair from the dict
			attributes = dict((k,v) for k,v in attributes.items() if v)
		
		# If users array is empty, then no matching users were found
		except:
			username_error = "The username entered is not valid"

		if request.is_ajax():
			
			# If an exception occured, determine which error message should be displayed
			if (access_token_error or username_error):
				if access_token_error:
					error = access_token_error
				elif username_error:
					error = username_error

				# Return error message to template
				return HttpResponse(json.dumps({"error":(error)}), mimetype="application/json")
			
			# If lookup request is successful, return attributes to template
			else:
				return HttpResponse(json.dumps(attributes), mimetype="application/json")
		# If POST request isn't AJAX, manually assign view context
		else:
			context = {}
			
			# If an exception occured, assign an error to the context, otherwise assign the attributes dict to the context
			if access_token_error:
				context.update({'error':access_token_error})
			elif username_error:
				context.update({'error':username_error})
			else:
				context = attributes

			# Return view context to template
			return render(request, 'index.html', context)
	
	# If request isn't POST, then display the template
	else:
		return render(request, 'index.html')
        relations[str(person_id)] = []
    try:
        for user in api.user_follows(person_id)[0]:
            max = max + 1
            if max >=40:
                return  0
            f.write(str(person_id)+" -neighbor- "+str(user.id)+"\n")
            # relations[str(person_id)].append(str(user.id))
            if depth<=depth_max:
                user_follows(user.id, depth+1)
    except:
        return 0


influencers = ['ChiaraFerragni','songofstyle','juliahengel','tuulavintage','susiebubble','peaceloveshea','zoella','laurenconrad','marianodivaio','audrinapatridge','whowhatwear']
for inf in influencers:
    try:
        user_follows(api.user_search(inf)[0].id,0)
    except:
        continue


# original test
# user_follows(1725181484,0)
# print api.user_search('ChiaraFerragni')[0].id


f.close()


Exemplo n.º 29
0
def run_contest():
# if instagram info is in session variables, then display user photos
	if 'instagram_access_token' in session and 'instagram_user' in session:
		api = InstagramAPI(access_token=session['instagram_access_token'])


		identifier = re.compile("We've teamed up with our friends")
		users_to_follow = 'knowlita, acme_nyc'
		user_list = users_to_follow.split(',')
		user_list = [x.strip(' ') for x in user_list]  
		
		uids = []
		for key_user in user_list:
			user_search = api.user_search(q=key_user)
			uids.append(user_search[0].id)

		media_ids = []
		for uid in uids:
			recent_media, next = api.user_recent_media( user_id=uid , count=30)
			for media in recent_media:
				if media.caption != None:
					if identifier.search(media.caption.text):
						media_ids.append(media.id)
					else:
						recent_media, next = api.user_recent_media( with_next_url = next)
						for media in recent_media:
							if media.caption != None:
								if identifier.search(media.caption.text):
									media_ids.append(media.id)

		

		media_ids = [media_ids[0],media_ids[2]]

		def find_insta_handles(text):
			p = re.compile('(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([_A-Za-z]+[_A-Za-z0-9]+)')
			return p.findall(text)

		def tagged_users(comment):
			handles = find_insta_handles(comment)
			handles = [str(i) for i in handles]
			handles = [y for y in handles if y not in user_list]
			return handles


		valid_participants = []
		tagged = []
		ntp = []
		tp = []


		for img_id in media_ids:
			contest_comments = api.media_comments(media_id = img_id)
			for comment in contest_comments:
				if comment.user.username not in user_list:
					for user in tagged_users(comment.text):
						tagged.append(user)
						if len(tagged_users(comment.text)) >= 3 and comment.user.username not in user_list and comment.user.username not in valid_participants:
							valid_participants.append(comment.user.username)
							if comment.user.username in tagged:
								tp.append(comment.user.username)
							else:
								ntp.append(comment.user.username)

		
		## note that this measure is technically wrong, as the users could have overlapping followers
		tot_num_followers = 0			
		for key in user_list:
			key_search = api.user_search(q=key)
			key_basics = api.user(key_search[0].id)
			tot_num_followers += key_basics.counts['followed_by']



		num_tagged_participants = len(tp)
		num_untagged_participants = len(ntp)
		num_tagged_users = len(tagged)
		num_valid_participants = len(valid_participants)

		virality_coef = float(num_tagged_participants)/num_tagged_users
		contest_engagement = float(num_untagged_participants)/tot_num_followers




		templateData = {
			'media_ids' : media_ids,
			#'comments' : contest_comments,
			'valid_participants' : valid_participants,
			'tp' : tp,
			'ntp' : ntp,
			'tagged' : tagged,
			'num_untagged_participants' : str(num_untagged_participants),
			'num_tagged_participants' : str(num_tagged_participants),
			'num_valid_participants' : str(num_valid_participants),
			'num_tagged_users' : str(num_tagged_users),
			'num_followers' : str(tot_num_followers),
			'virality_coef' : str(round(virality_coef,4)),
			'contest_engagement' : str(round(contest_engagement,4)),

		}

		return render_template('run_contest.html', **templateData)
	else:
		return redirect('/connect')
Exemplo n.º 30
0
class InstagramExplorer(SocialExplorer):
    def __init__(self, access_token, username, debug=False):
        super(InstagramExplorer, self).__init__(access_token, None, username,
                                                debug)
        self._api = InstagramAPI(access_token=self._token)
        self._load_data()

    def _load_data(self):
        if None:
            # TODO: load local data, if it exists
            raise NotImplementedError("TODO: load local data, if it exists")
            pass
        else:
            self._load_api_data()

    def _load_api_data(self):
        user = self._match_username()
        self.user_id = int(user.id)

        if user:
            print 'Matched Instagram username %s to user id %s' % (
                self.username, user.id) if self._debug else None
            posts = self._load_content(user.id)
            self._standardize_data(posts)

            labels, core_samples_mask, clusters, X = dbscan_cluster(
                self.locations)
            self._add_labels(labels)
            self._collect_label_meta()
            print 'Location Clusters: %s' % self._label_meta if self._label_meta else None
        else:
            raise InvalidUserException()

    def _match_username(self):
        users = self._api.user_search(self._username)
        match = [user for user in users if user.username == self._username]
        return match[0] if match else None

    def _load_content(self, user_id):
        post_results = []
        visited = {}
        posts, next_ = self._api.user_recent_media(user_id=user_id)
        post_results.extend(posts)
        next_url, max_id = self._next_url(next_, posts)

        while not visited.get(max_id):
            posts, next_ = self._api.user_recent_media(user_id=user_id,
                                                       max_id=max_id)
            visited[max_id] = True
            post_results.extend(posts)
            next_url, max_id = self._next_url(next_, posts)

        return post_results

    @staticmethod
    def _process_instagram_datum(datum):
        url = datum.link
        id = datum.id
        text = datum.caption.text if datum.caption is not None else None
        location = Location(datum.location.point.latitude,
                            datum.location.point.longitude)
        media_data = datum.images if datum.type == 'image' else datum.videos
        media = {
            key: Media(url=val.url, height=val.height, width=val.width)
            for (key, val) in media_data.iteritems()
        }
        username = datum.user.username
        user_id = datum.user.id

        return SocialData(url, id, media, text, username, user_id, location)

    def _standardize_data(self, response_data):
        # Filter or limit results to location-tagged updates
        self._data = [
            InstagramExplorer._process_instagram_datum(media)
            for media in response_data
            if hasattr(media, 'location') and hasattr(media.location, 'point')
            and hasattr(media.location.point, 'latitude')
            and hasattr(media.location.point, 'longitude')
        ]

    def _add_labels(self, labels):
        if len(labels) != len(self._data):
            raise MalformedSocialExplorerException(
                "The number of labels must match the number of posts")
        for i in range(len(labels)):
            self._data[i].location.label = labels[i]

    @staticmethod
    def _next_url(url, media_list):
        if media_list and url:
            max_media_id = media_list[-1].id
            parsed = urlparse(url)
            query = parse_qs(parsed.query)
            query_str = "access_token=%s&max_id=%s" % (
                query.get('access_token')[-1], max_media_id)
            new_url = urlunparse([
                parsed.scheme, parsed.netloc, parsed.path, '', query_str, None
            ])
            return new_url, max_media_id
        else:
            return None, None
Exemplo n.º 31
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
Exemplo n.º 32
0
from hashlib import sha256

ips = ''
secret = ''

signature = hmac.new(secret, ips, sha256).hexdigest()
header = '|'.join([ips, signature])

access_token = ""
api = InstagramAPI(access_token=access_token)
count = 0
while True:
    session = Session()
    profile = ScrapeRequest.get_unliked_request(session)
    profile.liked = True
    try:
        user = api.user_search(q=profile.url)[0]
        recent_media, next_ = api.user_recent_media(user_id=user.id, count=20)
        for photo in recent_media[:3]:
            response = requests.post("https://api.instagram.com/v1/media/{0}/likes".format(photo.id),
                params={"access_token": access_token},
                headers={"X-Insta-Forwarded-For":header})
            count += 1
            print photo
    except Exception, e:
        print e
        pass
    session.commit()
    if count > 50:
        break
Exemplo n.º 33
0
def email_helper(user):
	error_code = 'OK'

	user_profile = get_object_or_404(UserProfile, user=user)	
	user_twitter= UserSocialAuth.objects.filter(user=user, provider='twitter')
	user_instagram = UserSocialAuth.objects.filter(user=user, provider='instagram')
	
	# initialize those variables
	tweets = {}
	instagrams = {}
	instagram_links = {}
	mashups = None
	mashup_links = {}
	haikus_attributed = []
	haiku_links = {}
	audio_links = {}	
	random_photos = []
	num_haikus = 8
	num_mashups = 9
	
	# TWITTER CONTENT (HAIKUS, PODCAST)
	
	# connect to twitter api
	if user_twitter:
		oauth_access_token=(user_twitter.get().tokens).get('oauth_token')
		oauth_access_secret=(user_twitter.get().tokens).get('oauth_token_secret')
		auth = tweepy.OAuthHandler(SOCIAL_AUTH_TWITTER_KEY, SOCIAL_AUTH_TWITTER_SECRET)
		auth.set_access_token(oauth_access_token, oauth_access_secret)
		api_twitter = tweepy.API(auth)  	
	
	# haiku input txt file
	f_haikus = open(os.path.join(BASE_DIR, 'tweets_haikus.txt'), 'w')
	# twitter audio input txt file
	f_audio = open(os.path.join(BASE_DIR, 'tweets_audio.txt'), 'w')
	
	# get tweets and write new tweets to haiku and twitter audio input files
	for person in user_profile.twitters.all():		
		person_tweets = []
		try:
			timeline = api_twitter.user_timeline(screen_name=person.username, include_rts=False, exclude_replies=True, count=TWITTER_COUNT)
			f_audio.write('\nSTARTING TWEETS FROM USERNAME, @' + person.username.encode('ascii', 'xmlcharrefreplace') + ':\n,')
			for t in timeline:
				if t.created_at.replace(tzinfo=utc) >= user_profile.last_email_time:
					person_tweets.append(t)			
					
					filtered_tweet = ' '.join([word for word in t.text.split(' ') if not word.startswith('http') and not has_number(word) and not is_only_punc(word)])
					f_haikus.write(filtered_tweet.encode('ascii', 'xmlcharrefreplace') + '{')
					
					no_links = ' '.join([word for word in t.text.split(' ') if not word.startswith('http')])
					f_audio.write(no_links.encode('ascii', 'xmlcharrefreplace') + ' ,STOP, \n')
					
			if len(person_tweets) > 0:
				tweets[person.username] = person_tweets
		except Exception:
			print >> sys.stderr, user_profile.user.username +': Sending email, Twitter API error, couldn\'t fetch timeline'
	f_audio.write('\n END OF ALL TWEETS')
	f_audio.close()
	f_haikus.close()
	
	# run the haiku script
	p_haikus = subprocess.Popen(['python2.7', '/home/bshepherd/webapps/slowdown_django/slowdown/haiku.py', '/home/bshepherd/webapps/slowdown_django/slowdown/tweets_haikus.txt'], stdout=subprocess.PIPE)
	haikus = p_haikus.communicate()[0].split('\n')
	
	# attribute the haikus (this code is the worst code)
	haikus_split = split(haikus, 4)	
	haiku_pool = ['\n'.join(h[:-1]) for h in haikus_split]
	if len(haiku_pool) < num_haikus:
		num_haikus = len(haiku_pool)	
	haikus_result = random.sample(haiku_pool, num_haikus)
	sub_results = []
	
	for h in haikus_result:
		substrings = h.split('{')
		if len(substrings) == 1:	
			for username, t_list in tweets.items():			
				for t in t_list:
					filtered_tweet = ' '.join([word for word in t.text.split(' ') if not word.startswith('http') and not has_number(word) and not is_only_punc(word)])
					
					if h.replace('\n', ' ').strip().encode('ascii', 'xmlcharrefreplace') in filtered_tweet.encode('ascii', 'xmlcharrefreplace'):
						link = '<sup><a href="http://twitter.com/' + username + '/status/' + t.id_str + '" target="_blank">1</a></sup> '
						if h:
							haikus_attributed.append(h + link)
		else:
			counter = 1
			sub_result = []
			for s in substrings:			
				if len(s.replace('\n', ' ').strip()) > 5:
					for username, t_list in tweets.items():
						for t in t_list:
							filtered_tweet = ' '.join([word for word in t.text.split(' ') if not word.startswith('http') and not has_number(word) and not is_only_punc(word)])
							if s.replace('\n', ' ').strip().encode('ascii', 'xmlcharrefreplace') in filtered_tweet.encode('ascii', 'xmlcharrefreplace'):

								link = '<sup><a href="http://twitter.com/' + username + '/status/' + t.id_str + '" target="_blank">' + str(counter) + '</a></sup> '
								if s:
									sub_result.append(s + link)
								counter = counter + 1
			sub_results.append(sub_result)
								
	for result in sub_results:
		haikus_attributed.append(''.join(r for r in result))
 	
 	# save a permalink item for each haiku
 	for h in haikus_attributed:
		i = Item(user=user, item_type=Item.HAIKU, text=h)
		i.save()
		i.hash_code = hash_from_id(i.id)
		i.save()
		haiku_links[h] = i.hash_code
	
	# run the audio script for the tweets
 	p_audio = subprocess.Popen(['python2.7', '/home/bshepherd/webapps/slowdown_django/slowdown/audiotweets.py', '-o', '/home/bshepherd/webapps/slowdown_static/audio/tweets_audio.mp3', '-f', '/home/bshepherd/webapps/slowdown_django/slowdown/tweets_audio.txt'], stdout=subprocess.PIPE)
 	p_audio.communicate()

	# save a permalink item for the twitter podcast
 	i = Item(user=user, item_type=Item.PODCAST)
	i.save()
	i.hash_code = hash_from_id(i.id)
	try:
		os.rename('/home/bshepherd/webapps/slowdown_static/audio/tweets_audio.mp3', '/home/bshepherd/webapps/slowdown_static/audio/tweets_' + i.hash_code + '.mp3')
		i.audio = '/static/audio/tweets_' + i.hash_code + '.mp3'
		i.save()
	except Exception:
		error_code = 'TWEET AUDIO FAILED'
		print >> sys.stderr, user_profile.user.username +': Sending email, couldn\'t save Twitter audio'
		
	audio_links['twitter'] = i.hash_code
 	
 	# INSTAGRAM CONTENT (MASHUPS, PODCAST, RANDOM PHOTOS)
	
	if user_instagram:
		# connect to instagram api
		token_instagram = user_instagram.get().tokens
		api_instagram = InstagramAPI(access_token=token_instagram)

		# instagram audio input txt file
		f_audio_insta = open(os.path.join(BASE_DIR, 'insta_audio.txt'), 'w')
	
		# get photos and write captions to instagram audio input txt file
		for person in user_profile.instagrams.all():
			person_instagrams = []	
			try:
				uid = api_instagram.user_search(person.username)[0].id
				recent_media, next = api_instagram.user_recent_media(user_id=uid, count=INSTAGRAM_COUNT)
				f_audio_insta.write('\nSTARTING INSTAGRAM CAPTIONS FROM USERNAME, @' + person.username.encode('ascii', 'xmlcharrefreplace') + ':\n,')
				for media in recent_media:
					if media.created_time.replace(tzinfo=utc) >= user_profile.last_email_time:
						instagram_links[media.images['standard_resolution'].url] = media.link
						person_instagrams.append(media.images['standard_resolution'].url)
						if media.caption:
							f_audio_insta.write(media.caption.text.encode('ascii', 'xmlcharrefreplace') + ' ,STOP, \n')
				if len(person_instagrams) > 0:
					instagrams[person.username] = person_instagrams
			except Exception:
				print >> sys.stderr, user_profile.user.username +': Sending email, Instagram API error, couldn\'t fetch recent media'

		f_audio_insta.close()

		# get all tweets and photos, select randomly to create mashups
		if tweets.values() and instagrams.values():
			tweet_pool = [item for sublist in tweets.values() for item in sublist]
			instagram_pool = [item for sublist in instagrams.values() for item in sublist]
			if len(tweet_pool) < num_mashups:
				num_mashups = len(tweet_pool)
			if len(instagram_pool) < num_mashups:
				num_mashups = len(instagram_pool)
			tweets_random = random.sample(tweet_pool, num_mashups)
			instagrams_random = random.sample(instagram_pool, num_mashups)
			mashups = zip(tweets_random, instagrams_random)
	
		if mashups:
			# create permalink items for all mashups
			for mashup in mashups:
				image_link = instagram_links[mashup[1]]
				text_link = 'http://www.twitter.com/' + mashup[0].author.screen_name + '/status/' + mashup[0].id_str
				i = Item(user=user, item_type=Item.REMIX, image_url=mashup[1], image_link=image_link, text=mashup[0].text, text_link=text_link)
				i.save()
				i.hash_code = hash_from_id(i.id)
				i.save()
				mashup_links[mashup[1]] = i.hash_code
		
		# get random photos using user's lat and lon
		try:	
			media = api_instagram.media_search(count=9, lat=user_profile.lat, lng=user_profile.lon)
			for m in media:
				random_photos.append((m.images['standard_resolution'].url, m.link))
		except Exception:
			error_code = 'LAT LON SEARCH FAILED'
			print >> sys.stderr, user_profile.user.username +': Sending email, Instagram API error, lat lon search failed'
	 	
	 	# run the audio script for the instagram captions
		p_audio_insta = subprocess.Popen(['python2.7', '/home/bshepherd/webapps/slowdown_django/slowdown/audiotweets.py', '-o', '/home/bshepherd/webapps/slowdown_static/audio/instagramcaptions_audio.mp3', '-f', '/home/bshepherd/webapps/slowdown_django/slowdown/insta_audio.txt'], stdout=subprocess.PIPE)
		p_audio_insta.communicate()

		# create a permalink item for the instagram podcast
		i = Item(user=user, item_type=Item.PODCAST)
		i.save()
		i.hash_code = hash_from_id(i.id)
		try:
			os.rename('/home/bshepherd/webapps/slowdown_static/audio/instagramcaptions_audio.mp3', '/home/bshepherd/webapps/slowdown_static/audio/instagramcaptions_' + i.hash_code + '.mp3')
			i.audio = '/static/audio/instagramcaptions_' + i.hash_code + '.mp3'
			i.save()
		except Exception:
			error_code = 'INSTAGRAM AUDIO FAILED'
			print >> sys.stderr, user_profile.user.username +': Sending email, couldn\'t save Instagram audio'
		audio_links['instagram'] = i.hash_code
		
	# construct email content	
	html_content = render_to_string('email.html', {
		'user_profile' : user_profile,
		'tweets' : tweets,
		'instagrams' : instagrams,
		'instagram_links' : instagram_links,
		'mashups' : mashups,
		'mashup_links' : mashup_links,
		'haikus' : haikus_attributed,
		'haiku_links' : haiku_links,
		'random_photos' : random_photos,
		'audio_links' : audio_links,
	})
	
	all_tweets = [item for sublist in tweets.values() for item in sublist]
	all_photos = [item for sublist in instagrams.values() for item in sublist]

	# hash prevents emails from threading
	email_hash = hash_from_id(random.randint(0,100000))
	
	subject = user.first_name + ', It\'s Time to Slow Down!'
	
	msg = EmailMessage(subject, html_content, 'Slow Down Digest<no-reply+'+email_hash+'@slowdown.io>', [user.email])
	msg.content_subtype = "html"
	
	# attach twitter mp3 if it exists and has content
	if user_twitter and user_profile.podcasts and (len(all_tweets) > 0):
		audio_t = '/home/bshepherd/webapps/slowdown_static/audio/tweets_' + audio_links['twitter'] + '.mp3'
		try:
			msg.attach_file(audio_t)
		except Exception:
			print >> sys.stderr, user_profile.user.username +': Sending email, couldn\'t attach Twitter audio'
		
	# attach instagram mp3 if it exists and has content
	if user_instagram and user_profile.podcasts and (len(all_photos) > 0):
		audio_i = '/home/bshepherd/webapps/slowdown_static/audio/instagramcaptions_' + audio_links['instagram'] + '.mp3'
		try:
			msg.attach_file(audio_i)
		except Exception:
			print >> sys.stderr, user_profile.user.username +': Sending email, couldn\'t attach Instagram audio'
			
	msg.send()
	return error_code
Exemplo n.º 34
0
Arquivo: insta.py Projeto: egunn/IDV2
from instagram.client import InstagramAPI
import json
# import os
# import re

with open('.instagram_config.json') as data_file:
    config = json.load(data_file)

api = InstagramAPI(client_id=config['client_id'],
                   client_secret=config['client_secret'],
                   access_token=config['access_token'])
print api.access_token
users = api.user_search(q='knowuh', count=10)
for user in users:
    print user.id
recent_media, next_ = api.user_recent_media(user_id="3741139")
for media in recent_media:
    print media.images
Exemplo n.º 35
0
from instagram.client import InstagramAPI

access_token = "1532802561.870ffda.2cb6124d70d242c5a7ec54264bfb6688"
client_secret = "348c5332c8134906a99b951421eb30b6"

api = InstagramAPI(
    access_token=access_token,
    client_secret=client_secret,
)
user_id = api.user_search('dessert1112')[0].id
recent_media, next_ = api.user_recent_media(user_id=user_id, count=5)

for media in recent_media:
    print(media.caption.text)
    print('<img src="%s"/>' % media.images['thumbnail'].url)
Exemplo n.º 36
0
def acme_test():
# if instagram info is in session variables, then display user photos
	if 'instagram_access_token' in session and 'instagram_user' in session:
		api = InstagramAPI(access_token=session['instagram_access_token'])

		# recent_media, next = api.user_recent_media(user_id=922345846,count=30)
		# check = recent_media[0].location.point.latitude

		users_to_follow = 'knowlita, acme_nyc'
		user_list = users_to_follow.split(',')
		user_list = [x.strip(' ') for x in user_list]  
		
		uids = []
		for key_user in user_list:
			user_search = api.user_search(q=key_user)
			uids.append(user_search[0].id)

		contest_followers = []
		for uid in uids:
			followers, next = api.user_followed_by(user_id= uid)
			for follower in followers:
				contest_followers.append(follower.username)
			while next:
				followers, next = api.user_follows(with_next_url=next)
				for follower in followers:
					contest_followers.append(follower.username)


		contest_followers = list(set(contest_followers))

		## old hack way
		# acme_id = '922345846'
		# acme_followers, next = api.user_followed_by(user_id= acme_id)
		# acme_users = []
		# for user in acme_followers:
		# 	acme_users.append(user.username)

		# i = 0
		# while len(acme_users) < acme_basics.counts['followed_by']-50:
		# 	i += 1
		# 	response = urllib.urlopen(next)
		# 	data = json.loads(response.read())
		# 	for user in data['data']:
		# 		acme_users.append(user['username'])
		# 	next = data['pagination']['next_url']
		
		


		def is_follower(username):
			return (username in contest_followers)


		def find_insta_handles(text):
			p = re.compile('(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([_A-Za-z]+[_A-Za-z0-9]+)')
			return p.findall(text)

		def tagged_users(comment):
			handles = find_insta_handles(comment)
			handles = [str(i) for i in handles]
			handles = [y for y in handles if y not in user_list]
			return handles



		knowlita_contest_img_id = '937178239574293509_922345846'
		acme_contest_img_id = '937173533246931090_28306327'

		contest_imgs = [knowlita_contest_img_id, acme_contest_img_id]
		valid_participants = []

		for img_id in contest_imgs:
			contest_comments = api.media_comments(media_id = img_id )
			for comment in contest_comments:
				if comment.user.username not in valid_participants and comment.user.username not in user_list:
					if len(tagged_users(comment.text)) >= 3 and is_follower(comment.user.username):
						valid_participants.append(comment.user.username)



		templateData = {

			'comments' : contest_comments,
			'participants' : valid_participants,
		}

		return render_template('test.html', **templateData)
	else:
		return redirect('/connect')
Exemplo n.º 37
0
class instagramService:
    def __init__(self, config):
        self.config = config
        self.api = InstagramAPI(access_token=config['access_token'])
        self.version = 0
        self.batch = []
        self.batch_users = []
        self.feed_count = 0
        self.feed_count_total = 0
        self.instagram_username = config['instagram_username']
        self.instagram_user_id = config['instagram_user_id']

    def stop_and_exit_service(self):
        logging.debug('instagram Service - HARD QUIT - instagram SERVICE')
        sys.exit()

    def run_main_services(self):

        main_services_d = Deferred()
        try:
            print "running instagram main services"

            def find_followers_cb(res):
                def find_friends_cb(res):
                    def get_authen_user_feed_cb(res):
                        def get_popular_media_cb(res):

                            main_services_d.callback(True)

                        self.get_popular_media().addCallbacks(
                            get_popular_media_cb, lambda failure: logging.
                            error("Update Error {0}".format(failure)))

                    self.get_authenticated_user_feed().addCallbacks(
                        get_authen_user_feed_cb, lambda failure: logging.error(
                            "Update Error {0}".format(failure)))

                self.find_friends(
                    self.config['instagram_user_id']).addCallbacks(
                        find_friends_cb, lambda failure: logging.error(
                            "Update Error{0}".format(failure)))

            self.find_followers(self.config['instagram_user_id']).addCallbacks(
                find_followers_cb, lambda failure: logging.error(
                    "Update Error{0}".format(failure)))
            #self.subscribe_to_objects_by_tag(self.config['instagram_search_words'])
        except:
            logging.debug(
                'Service Instagram - Could not run main service due to error: {0}'
                .format(sys.exc_info()))

        return main_services_d

    def get_indx(self):

        return_d = Deferred()

        def authed_cb():
            logging.debug("in service_tweets - Authed Callback")
            logging.debug(
                "in service_tweets - get_indx authclient Auth status: {0}".
                format(authclient.is_authed))

            def token_cb(token):
                self.indx_con = IndxClient(self.config['address'],
                                           self.config['box'],
                                           appid,
                                           token=token,
                                           client=authclient.client)
                return_d.callback(True)

            authclient.get_token(self.config['box']).addCallbacks(
                token_cb, return_d.errback)

        def authed_cb_fail(re):
            logging.debug(
                "in service tweets - get_indx/authed_cb failed for reason {0}".
                format(re))
            return_d.errback

        logging.debug("in service_instagram - get_indx")
        authclient = IndxClientAuth(self.config['address'], appid)
        authclient.auth_plain(self.config['user'],
                              self.config['password']).addCallbacks(
                                  lambda response: authed_cb(), authed_cb_fail)

        return return_d

    def get_authenticated_user_feed(self):
        auth_d = Deferred()

        def found_cb(results):
            friends_number = 0
            followers_number = 0
            since_id = ""
            #let's see if the object has some nice things in it.
            try:
                config_returned = results['data']['service_instagram_config']
                friends_number = int(
                    config_returned['friends_list_size'][0]['@value'])
                followers_number = int(
                    config_returned['followers_list_size'][0]['@value'])
                since_id = int(config_returned['since_id'][0]['@value'])
                logging.info('Found the instagram Config Object.')
            except:
                #print sys.exc_info()
                pass

            #print "Getting Auth User's feed"
            user_feed = self.api.user_media_feed()[0]
            try:
                latest_id = user_feed[0].id
            except:
                latest_id = "Null"
            ##find the highest id...

            if (latest_id != since_id):

                logging.info("Found some new media, will insert it to INDX")
                since_id = latest_id
                objects_to_insert = []
                current_timestamp = str(
                    time.time()).split(".")[0]  #str(datetime.now())
                timestamp = str(datetime.now().isoformat('T')).split(".")[0]
                current_timestamp = str(datetime.now())

                #the user responsible for this

                #now create some nice user objects...
                for media in user_feed:

                    media_user_id = media.user.id
                    media_username = media.user.username

                    uniq_id = "instagram_media_" + media.id
                    user_feed_obj = {
                        "@id": uniq_id,
                        "app_object": appid,
                        "timestamp": timestamp,
                        "type": "post",
                        "instagram_user_id_indx": "instagram_user_me",
                        "instagram_user_id": media_user_id,
                        "instagram_username": media_username,
                        "media_id": media.id,
                        "media_caption": media.caption,
                        "media_url": media.get_standard_resolution_url(),
                        "media_like_count": media.like_count
                    }

                    #need to add INDX objects for tags and locations and the indx user of this

                    #create location if available
                    try:
                        location = media.location
                        uniq_id = "instagram_location_" + location.id
                        location_obj = {
                            "@id": uniq_id,
                            "app_object": appid,
                            "timestamp": timestamp,
                            "type": "location",
                            "instagram_location_id": location.id,
                            "location_name": location.name,
                            "latitude": location.point.latitude,
                            "longitude": location.point.longitude
                        }

                        #now add this location to the user_feed_obj
                        user_feed_obj['media_location_id'] = location_obj
                        #thhen add it to a list of ojects to insert.
                        objects_to_insert.append(location_obj)
                    except:
                        pass

                    try:
                        tag_ids = []
                        for tag in media.tags:
                            uniq_id = "instagram_tag_" + tag.name
                            tag_obj = {
                                "@id": uniq_id,
                                "app_object": appid,
                                "timestamp": timestamp,
                                "type": "tag",
                                "instagram_tag_name": tag.name
                            }
                            tag_ids.append(uniq_id)
                            objects_to_insert.append(tag_obj)

                        #now add this location to the user_feed_obj
                        user_feed_obj['tags'] = tag_ids
                    except:
                        pass

                    objects_to_insert.append(user_feed_obj)

                #now create the instagram_user_me object
                uniq_id = "instagram_user_me"
                instagram_me_obj = {
                    "@id": uniq_id,
                    "app_object": appid,
                    "timestamp": timestamp,
                    "type": "user",
                    "instagram_user_id": media_user_id,
                    "instagram_username": media_username
                }
                objects_to_insert.append(instagram_me_obj)

                #and create the instagram config
                instagram_config_obj = {
                    "@id": "service_instagram_config",
                    "app_object": appid,
                    "type": "config",
                    "config_last_updated_at": timestamp,
                    "config_for_instagram_user": self.instagram_username,
                    "friends_list_generated_at": timestamp,
                    "follower_list_generated_at": timestamp,
                    "friends_list_size": friends_number,
                    "followers_list_size": followers_number,
                    "since_id": since_id
                }
                objects_to_insert.append(instagram_config_obj)

                def update_cb(re):
                    logging.debug(
                        "network harvest async worked {0}".format(re))
                    auth_d.callback(True)

                def update_cb_fail(re):
                    logging.error(
                        "network harvest async failed {0}".format(re))
                    auth_d.errback

                self.insert_object_to_indx(objects_to_insert).addCallbacks(
                    update_cb, update_cb_fail)
            else:
                logging.info(
                    "already have the latest version of your instagram timeline"
                )
                auth_d.callback(True)

        def error_cb(re):
            found_cb()

        def_search = Deferred()
        find_instagram_config = {"@id": "service_instagram_config"}
        logging.info(
            "Searching for instagram_config to check if Popular feed already harvested... "
        )
        def_search = self.indx_con.query(json.dumps(find_instagram_config))
        def_search.addCallbacks(found_cb, error_cb)

        return auth_d
        # for feed in user_feed:
        #     print feed

    def get_searched_media(self, search_terms):
        print "getting searched media for terms: " + str(search_terms)
        returned_results, page_num = self.api.tag_search(search_terms, 20)
        return returned_results
        # for result in returned_results:
        #     print result

    def get_popular_media(self):
        pop_d = Deferred()

        def found_cb(results):
            friends_number = 0
            followers_number = 0
            since_id = ""
            #let's see if the object has some nice things in it.
            try:
                config_returned = results['data']['service_instagram_config']
                friends_number = int(
                    config_returned['friends_list_size'][0]['@value'])
                followers_number = int(
                    config_returned['followers_list_size'][0]['@value'])
                since_id = int(config_returned['since_id'][0]['@value'])
                logging.info('Found the instagram Config Object.')
            except:
                #print sys.exc_info()
                pass

        #if(since_id > 0):
            logging.info("getting popular media")

            objects_to_insert = []
            current_timestamp = str(
                time.time()).split(".")[0]  #str(datetime.now())
            timestamp = str(datetime.now().isoformat('T')).split(".")[0]
            current_timestamp = str(datetime.now())

            popular_media = self.api.media_popular(count=20)

            for media in popular_media:

                media_user_id = media.user.id
                media_username = media.user.username

                #now create the instagram_user object
                uniq_user_id = "instagram_user_" + media.user.id
                instagram_media_obj = {
                    "@id": uniq_user_id,
                    "app_object": appid,
                    "timestamp": timestamp,
                    "type": "user",
                    "instagram_user_id": media_user_id,
                    "instagram_username": media_username
                }
                objects_to_insert.append(instagram_media_obj)

                uniq_id = "instagram_media_" + media.id
                media_obj = {
                    "@id": uniq_id,
                    "app_object": appid,
                    "timestamp": timestamp,
                    "type": "post",
                    "instagram_user_id_indx": uniq_user_id,
                    "instagram_user_id": media_user_id,
                    "instagram_username": media_username,
                    "media_id": media.id,
                    "media_caption": media.caption,
                    "media_url": media.get_standard_resolution_url(),
                    "media_like_count": media.like_count
                }

                #need to add INDX objects for tags and locations and the indx user of this

                #create location if available
                try:
                    location = media.location
                    uniq_id = "instagram_location_" + location.id
                    location_obj = {
                        "@id": uniq_id,
                        "app_object": appid,
                        "timestamp": timestamp,
                        "type": "location",
                        "instagram_location_id": location.id,
                        "location_name": location.name,
                        "latitude": location.point.latitude,
                        "longitude": location.point.longitude
                    }

                    #now add this location to the user_feed_obj
                    media_obj['media_location_id'] = location_obj
                    #thhen add it to a list of ojects to insert.
                    objects_to_insert.append(location_obj)
                except:
                    pass

                try:
                    tag_ids = []
                    for tag in media.tags:
                        uniq_id = "instagram_tag_" + tag.name
                        tag_obj = {
                            "@id": uniq_id,
                            "app_object": appid,
                            "type": "tag",
                            "timestamp": timestamp,
                            "instagram_tag_name": tag.name
                        }
                        tag_ids.append(uniq_id)
                        objects_to_insert.append(tag_obj)

                    #now add this location to the user_feed_obj
                    media_obj['tags'] = tag_ids
                except:
                    pass

                objects_to_insert.append(media_obj)

            def update_cb(re):
                logging.debug("network harvest async worked {0}".format(re))
                pop_d.callback(True)

            def update_cb_fail(re):
                logging.error("network harvest async failed {0}".format(re))
                pop_d.errback

            #and create the instagram config
            instagram_config_obj = {
                "@id": "service_instagram_config",
                "app_object": appid,
                "type": "config",
                "config_last_updated_at": timestamp,
                "config_for_instagram_user": self.instagram_username,
                "friends_list_generated_at": timestamp,
                "follower_list_generated_at": timestamp,
                "friends_list_size": friends_number,
                "followers_list_size": followers_number,
                "since_id": since_id
            }
            objects_to_insert.append(instagram_config_obj)

            self.insert_object_to_indx(objects_to_insert).addCallbacks(
                update_cb, update_cb_fail)

        def error_cb(re):
            found_cb()

        def_search = Deferred()
        find_instagram_config = {"@id": "service_instagram_config"}
        logging.info(
            "Searching for instagram_config to check if Popular feed already harvested... "
        )
        def_search = self.indx_con.query(json.dumps(find_instagram_config))
        def_search.addCallbacks(found_cb, error_cb)

        return pop_d

    def find_user(self, username):
        data = self.api.user_search(username, count=20)
        print data

    def find_followers(self, userid):

        #first do a lookup and see if the latest set if followers has allready been found
        follower_d = Deferred()

        def found_cb(results):
            friends_number = 0
            followers_number = 0
            since_id = 0
            #let's see if the object has some nice things in it.
            try:
                config_returned = results['data']['service_instagram_config']
                friends_number = int(
                    config_returned['friends_list_size'][0]['@value'])
                followers_number = int(
                    config_returned['followers_list_size'][0]['@value'])
                since_id = int(config_returned['since_id'][0]['@value'])
                logging.info('Found the instagram Config Object.')
            except:
                #print sys.exc_info()
                pass
            followed_by = self.api.user_followed_by(userid)[0]
            #print "followed_by length: "+str(len(followed_by))
            #print str(followed_by)
            #see if the number is different, if it is, then update.. could be nicer than this, but for now, it will work (ish)
            if (len(followed_by) != followers_number) or (followers_number
                                                          == 0):

                followers_number = len(followed_by)
                objects_to_insert = []
                current_timestamp = str(
                    time.time()).split(".")[0]  #str(datetime.now())
                timestamp = str(datetime.now().isoformat('T')).split(".")[0]
                uniq_id = "instagram_follower_network_for_user_me"  #+self.instagram_username
                followed_by_obj = {
                    "@id": uniq_id,
                    "app_object": appid,
                    "instagram_username": self.instagram_username,
                    "instagram_user_id": self.instagram_user_id,
                    "timestamp": timestamp,
                    "followed_by_count": len(followed_by)
                }

                followers_ids = []
                for follower in followed_by:
                    #print follower.username
                    uniq_id = "instagram_user_" + str(follower.username)
                    follower_obj = {
                        "@id": uniq_id,
                        "app_object": appid,
                        "type": "user",
                        "instagram_username": str(follower.username),
                        "timestamp": timestamp
                    }
                    objects_to_insert.append(follower_obj)
                    #we can add this to the followed_by_obj later
                    followers_ids.append(uniq_id)

                #link the followers for me
                followed_by_obj['follower_ids'] = followers_ids

                # print followed_by_objs
                #for friend in friends_list:
                #print friend
                #now append the results
                def update_cb(re):
                    logging.debug(
                        "network harvest async worked {0}".format(re))
                    follower_d.callback(True)

                def update_cb_fail(re):
                    logging.error(
                        "network harvest async failed {0}".format(re))
                    follower_d.errback

                instagram_config_obj = {
                    "@id": "service_instagram_config",
                    "app_object": appid,
                    "type": "config",
                    "config_last_updated_at": timestamp,
                    "config_for_instagram_user": self.instagram_username,
                    "friends_list_generated_at": timestamp,
                    "follower_list_generated_at": timestamp,
                    "friends_list_size": friends_number,
                    "followers_list_size": followers_number,
                    "since_id": since_id
                }

                objects_to_insert.append(followed_by_obj)
                #objects_to_insert.append(followers)
                objects_to_insert.append(instagram_config_obj)

                self.insert_object_to_indx(objects_to_insert).addCallbacks(
                    update_cb, update_cb_fail)
            else:
                follower_d.callback(True)

        def error_cb(re):
            found_cb()

        def_search = Deferred()
        find_instagram_config = {"@id": "service_instagram_config"}
        logging.info(
            "Searching for instagram_config to check if network already harvested... "
        )
        def_search = self.indx_con.query(json.dumps(find_instagram_config))
        def_search.addCallbacks(found_cb, error_cb)

        return follower_d
        # print followed_by

    def find_friends(self, userid):
        friends_d = Deferred()

        def found_cb(results):
            friends_number = 0
            followers_number = 0
            since_id = 0
            #let's see if the object has some nice things in it.
            try:
                config_returned = results['data']['service_instagram_config']
                friends_number = int(
                    config_returned['friends_list_size'][0]['@value'])
                followers_number = int(
                    config_returned['followers_list_size'][0]['@value'])
                since_id = int(config_returned['since_id'][0]['@value'])
                logging.info('Found the instagram Config Object.')
            except:
                #print sys.exc_info()
                pass

            friends_by_list = self.api.user_follows(userid)[0]

            if (len(friends_by_list) != friends_number) or (friends_number
                                                            == 0):

                friends_number = len(friends_by_list)
                objects_to_insert = []
                current_timestamp = str(
                    time.time()).split(".")[0]  #str(datetime.now())
                timestamp = str(datetime.now().isoformat('T')).split(".")[0]
                uniq_id = "instagram_friends_network_for_user_me"  #+self.instagram_username
                friends_by_obj = {
                    "@id": uniq_id,
                    "app_object": appid,
                    "instagram_username": self.instagram_username,
                    "instagram_user_id": self.instagram_user_id,
                    "timestamp": timestamp,
                    "followed_by_count": len(friends_by_list)
                }

                friends_ids = []
                for friend in friends_by_list:
                    #print follower.username
                    uniq_id = "instagram_user_" + str(friend.username)
                    friend_obj = {
                        "@id": uniq_id,
                        "app_object": appid,
                        "type": "user",
                        "instagram_username": str(friend.username),
                        "timestamp": timestamp
                    }
                    objects_to_insert.append(friend_obj)
                    #we can add this to the followed_by_obj later
                    friends_ids.append(uniq_id)

                friends_by_obj['friends_ids'] = friends_ids

                # print friends_by_objs
                #for friend in friends_list:
                #print friend
                #now append the results
                def update_cb(re):
                    logging.debug(
                        "network harvest async worked {0}".format(re))
                    friends_d.callback(True)

                def update_cb_fail(re):
                    logging.error(
                        "network harvest async failed {0}".format(re))
                    friends_d.errback

                instagram_config_obj = {
                    "@id": "service_instagram_config",
                    "app_object": appid,
                    "type": "config",
                    "config_last_updated_at": timestamp,
                    "config_for_instagram_user": self.instagram_username,
                    "friends_list_generated_at": timestamp,
                    "follower_list_generated_at": timestamp,
                    "friends_list_size": friends_number,
                    "followers_list_size": followers_number,
                    "since_id": since_id
                }

                objects_to_insert.append(friends_by_obj)
                #objects_to_insert.append(followers)
                objects_to_insert.append(instagram_config_obj)

                self.insert_object_to_indx(objects_to_insert).addCallbacks(
                    update_cb, update_cb_fail)
            else:
                friends_d.callback(True)

        def error_cb(re):
            found_cb()

        def_search = Deferred()
        find_instagram_config = {"@id": "service_instagram_config"}
        logging.info(
            "Searching for instagram_config to check if network already harvested... "
        )
        def_search = self.indx_con.query(json.dumps(find_instagram_config))
        def_search.addCallbacks(found_cb, error_cb)

        return friends_d
        # print followed_by

    def subscribe_to_objects_by_tag(self, search_terms):
        def process_tag_update(update):
            print update

        reactor = subscriptions.SubscriptionsReactor()
        reactor.register_callback(subscriptions.SubscriptionType.TAG,
                                  process_tag_update)
        self.api.create_subscription(object='tag',
                                     object_id=search_terms,
                                     aspect='media',
                                     callback_url='http://*****:*****@version']
            logging.debug(
                "Succesfully Updated INDX with Objects in update_cb, new diff version of {0}"
                .format(self.version))
            #logging.debug("Inserted Object into INDX: ".format(resp))
            update_d.callback(True)
            #return update_d
            #time.sleep(2)

        def exception_cb(e, service=self, obj=obj):
            logging.debug(
                "Exception Inserting into INDX, probably wrong version given")
            if isinstance(
                    e.value, urllib2.HTTPError
            ):  # handle a version incorrect error, and update the version
                if e.value.code == 409:  # 409 Obsolete
                    response = e.value.read()
                    json_response = json.loads(response)
                    service.version = json_response['@version']
                    logging.debug(
                        'INDX insert error in Instagram Service Object: ' +
                        str(response))
                    try:
                        service.indx_con.update(service.version,
                                                obj).addCallbacks(
                                                    update_cb, exception_cb)
                        #logging.debug('Twitter Service - Successfully added Objects into Box')
                    except:
                        logging.error(
                            'Instagram Service, error on insert {0}'.format(
                                response))
                        update_d.errback(e.value)
                else:
                    logging.error('Instagram Service Unknow error: {0}'.format(
                        e.value.read()))
                    update_d.errback(e.value)
            else:
                logging.error("Error updating INDX: {0}".format(e.value))
                update_d.errback(e.value)

        logging.debug(
            "in Instagram - Trying to insert into indx...Current diff version: {0} and objects (len) given {1}"
            .format(self.version, len(obj)))
        self.indx_con.update(self.version,
                             obj).addCallbacks(update_cb, exception_cb)

        return update_d
Exemplo n.º 38
0
    def InstagramProcces(self):
        
        self.aceptar.setCursor(QCursor(Qt.ForbiddenCursor))
        self.update_progressbar(10)
        
        access_token = self.lnToken.text()
        client_secret = self.lnAcces.text()
        user_id=self.lnId.text()
 
        if not access_token or not client_secret:
            QMessageBox.information(self, "Empty values", "Complete mandatory items <access_token> and <client_secret>", QMessageBox.AcceptRole)
            return  
        try:
            api = InstagramAPI(access_token=access_token, client_secret=client_secret)
 
            #Search recent media with Tag       
            if self.TypeSearch=="hashtags":
                count=self.sp_count.value()  
                tag=self.ln_tags.text()
                if tag=="":
                    QMessageBox.information(self, "Empty values", "Tag value is empty", QMessageBox.AcceptRole)
                    self.aceptar.setCursor(QCursor(Qt.PointingHandCursor))
                    self.update_progressbar(0)
                    return  
            
                tag_search, next_tag = api.tag_search(tag)
                tag_recent_media, next = api.tag_recent_media(count,tag_name=tag_search[0].name)
                if len(tag_recent_media)==0:return self.Checklength()
                categorized,layer=self.CreateShape()
                for tag_media in tag_recent_media: 
                    self.AddFeatures(tag_media,layer,categorized)
                    
            #Search recent media with Location              
            elif self.TypeSearch=="coords":
                lat=self.ln_lat.text()
                lng=self.ln_lng.text()
                distance=self.sp_distance.value()                    
                location_search =api.media_search(lat=str(lat),lng=str(lng), distance=int(distance))  

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

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

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

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

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

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

            self.aceptar.setCursor(QCursor(Qt.PointingHandCursor))
 
            self.reject()
            
        except Exception, e:
            self.iface.messageBar().pushMessage("Error: ", "fail to load photos: "+str(e),level=QgsMessageBar.CRITICAL, duration=20)             
            self.aceptar.setCursor(QCursor(Qt.PointingHandCursor))
Exemplo n.º 39
0
#mport sys
#import json
#import simplejson
#import _json
#print (sys.version) #parentheses necessary in python 3.
access_token = '5041771937.79a3467.7673f7a4abe049c5bbe229a4ad826316'

from instagram.client import InstagramAPI
access_token = '5041771937.79a3467.7673f7a4abe049c5bbe229a4ad826316'
client_secret = "c3b09255b52847f3adeb9f823413ffe6"
api = InstagramAPI(client_secret=client_secret, access_token=access_token)
usr = api.user_search('jogryn')
#usr = api.user_relationship('jogryn')

print usr
recent_media, next_ = api.user_recent_media(user_id="jogryn", count=10)
for media in recent_media:
    print media.caption.text
'''
api = InstagramAPI(access_token=access_token, client_secret=client_secret)
recent_media, next_ = api.user_recent_media(user_id="79a3467592714815b2ab038172b2d2bf", count=10)
for media in recent_media:
    print(media.caption.text)
'''

#api = InstagramAPI(client_id='79a3467592714815b2ab038172b2d2bf', client_secret='c3b09255b52847f3adeb9f823413ffe6', access_token=access_token)
#print(api.list_subscriptions())
#popular_media = api.media_popular(count=20)

#for media in popular_media:
#x    print media.images['standard_resolution'].url
Exemplo n.º 40
0

def get_max_id(next_url):
    if next_url == None:
        return None
    import re

    pattern = re.compile("max_id=([a-zA-Z_0-9]*)")
    return re.findall(pattern, next_url)[0]


if len(sys.argv) <= 1:
    raise Error("No user specified")
username = sys.argv[1]
api = InstagramAPI(access_token=ACCESS_TOKEN)
search_result = api.user_search(username)
if search_result <= 0:
    raise Error("User not found!")
user = search_result[0]

max_id = ""
while max_id != None:
    max_id = None
    media, next = api.user_recent_media(user_id=user.id, count=70, max_id=max_id)
    for m in media:
        api.like_media(m.id)
        print "You liked %s" % m.link
        time.sleep(60)
    max_id = get_max_id(next)

print "Done. Go check at http://instagram.com/%s" % (username)
Exemplo n.º 41
0
import csv
from instagram.client import InstagramAPI

api = InstagramAPI(client_id='16f7ef0d35624ad3a3b07e18c9e45ef8', client_secret='22fdaddd6282489aab74528e16a06cb9')
f = open("unames.csv")
reader = csv.reader(f)
#f1 = open("testID.csv",'w')
#writer = csv.writer(f1)
userid = []
x = 0
for i in reader:
	if x>5:
		break
	try:
		user = api.user_search(q = i[1], count = 1)
		print str(user[0])[6:]+","+ user[0].id
	except:
		continue
	x+=1
Exemplo n.º 42
0
from instagram.client import InstagramAPI
import json
# import os
# import re

with open('instagram_config.json') as data_file:
    config = json.load(data_file)

api = InstagramAPI(client_id=config['client_id'],
                   client_secret=config['client_secret'],
                   access_token=config['access_token']
                   )
print api.access_token
users = api.user_search(q='knowuh', count=10)
for user in users:
   print user.id
recent_media, next_ = api.user_recent_media(user_id="3741139")
for media in recent_media:
    print media.images
Exemplo n.º 43
0
from instagram.client import InstagramAPI

access_token = "1308465562.eb196eb.a577b22057674f2b89126ccf34b040fd"
client_secret = "4142fd81a5c44b5d8ba8aaabbaa6e9c4"

api = InstagramAPI(
    access_token=access_token,
    client_secret=client_secret,
)
user_id = api.user_search('ying_789')[0].id

recent_media, next_ = api.user_recent_media(user_id=user_id, count=5)

for media in recent_media:
    print(media.caption.text)
    print('<img src="%s"/>' % media.images['thumbnail'].url)

#if "data" in entry["comments"]:
#	for comment in entry['comments']['data']:
#		new_media.comments.append(Comment.object_from_dictionary(comment))
Exemplo n.º 44
0
        ids_file = open(dir_data, 'r')
        data = json.load(ids_file)
    ids_file = open(dir_data, 'w')
    data[username] = int(userid)
    json.dump(data, ids_file)
    return data[username]


api = InstagramAPI(
                    client_id=authInfoProvider.get_client_id(),
                    client_secret=authInfoProvider.get_client_secret()
                   )

target_name = sys.argv[-1]

result = api.user_search(target_name)
user = [p for p in result if p.username == target_name]
if user:
    user = user[0]
else:
    print "User not found."

print "Found user:"******"-s" == sys.argv[-2]:
        need_to_update = False

if need_to_update:
Exemplo n.º 45
0
def success_metrics():
# if instagram info is in session variables, then display user photos
	if 'instagram_access_token' in session and 'instagram_user' in session:
		api = InstagramAPI(access_token=session['instagram_access_token'])

		users_to_follow = 'knowlita, acme_nyc'
		user_list = users_to_follow.split(',')
		user_list = [x.strip(' ') for x in user_list]  
		
		


		def find_insta_handles(text):
			p = re.compile('(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([_A-Za-z]+[_A-Za-z0-9]+)')
			return p.findall(text)

		def tagged_users(comment):
			handles = find_insta_handles(comment)
			handles = [str(i) for i in handles]
			handles = [y for y in handles if y not in user_list]
			return handles



		knowlita_contest_img_id = '937178239574293509_922345846'
		acme_contest_img_id = '937173533246931090_28306327'
		contest_imgs = [knowlita_contest_img_id, acme_contest_img_id]

		

		valid_participants = []
		tagged = []
		ntp = []
		tp = []

		for img_id in contest_imgs:
			contest_comments = api.media_comments(media_id = img_id)
			for comment in contest_comments:
				for user in tagged_users(comment.text):
					tagged.append(user)
				if len(tagged_users(comment.text)) >= 3:
					valid_participants.append(comment.user.username)
					if comment.user.username in tagged:
						tp.append(comment.user.username)
					else:
						ntp.append(comment.user.username)

		user_search = api.user_search(q='knowlita')
		knowlita = api.user(user_search[0].id)


		num_tagged_participants = len(tp)
		num_untagged_participants = len(ntp)
		num_tagged_users = len(tagged)
		num_valid_participants = len(valid_participants)
		num_followers = knowlita.counts['followed_by']

		virality_coef = float(num_tagged_participants)/num_tagged_users
		contest_engagement = float(num_untagged_participants)/num_followers




		templateData = {
			'valid_participants' : valid_participants,
			'tp' : tp,
			'ntp' : ntp,
			'num_untagged_participants' : str(num_untagged_participants),
			'num_tagged_participants' : str(num_tagged_participants),
			'num_valid_participants' : str(num_valid_participants),
			'num_tagged_users' : str(num_tagged_users),
			'num_followers' : str(num_followers),
			'virality_coef' : str(round(virality_coef,4)),
			'contest_engagement' : str(round(contest_engagement,4)),

		}

		return render_template('success_metrics.html', **templateData)
	else:
		return redirect('/connect')
Exemplo n.º 46
0
class Instagram:
    def __init__(self, api_index):
        self.api = InstagramAPI(client_id=credentials.instagram_credentials[api_index]["client_id"],
                                client_secret=credentials.instagram_credentials[api_index]["client_secret"])
        self.access_token = credentials.instagram_credentials[api_index]["access_token"][0]
        return None

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

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

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

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

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

        
    @staticmethod
    def tkey(key):
        tk = {"full_name": "name", "media": "num_posts", 
                "follows": "num_follows", "followed_by": "num_followers",
                "bio": "description", "id": "user_id"}
        ret = tk[key] if key in tk else key
        ret = ret.encode("utf8")
        return ret
Exemplo n.º 47
0
#T o install pip install python-instagram

from instagram.client import InstagramAPI
client_id = '837cc2720af049858962aee32cf48a93'
client_secret = '83eded60cf1643f1a307fd05dceac6a3'
access_token = '3256131004.837cc27.9a492f4c5dbe4fb6a242de6fce347e26'

api = InstagramAPI(access_token=access_token, client_secret=client_secret)
#usr = api.user_search('katyperry')
user_info = api.user_search('devinmui')
print(user_info)
#print (usr)

#api = InstagramAPI(client_id=client_id, client_secret=client_secret)
#popular_media = api.media_popular(count=20)
#for media in popular_media:
#    print (media.images['standard_resolution'].url)
Exemplo n.º 48
0
from instagram.client import InstagramAPI
import sys

with open("client_info.txt", "r") as f:
    s = f.readlines()
    access_token = s[0].rstrip()
    client_secret = s[1].rstrip()

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

user_search = api.user_search(q=sys.argv[1], count=5)

for i in user_search:
    print type(i)
    print "*" * 20
    print i.username
    print i.profile_picture
    print i.id
Exemplo n.º 49
0
def loop_printer(ine):
    for i in ine:
        if type(i) == list:
            loop_printer(i)
        elif type(i) == dict:
            print(i)
            loop_printer(i)
        else:
            print('OLOLOLO', i)


loop_printer(line['data'])
#line = ' '.join(line['data'])
#line = simplejson.loads(line)
#print(len(line['data']), type(line['data']), line['data'])

#print(api.user_follows(user_id2))
#print(api.user_followed_by(user_id2))
print(api.user(user_id))
#print(api.user_media_feed())
#print(api.user_liked_media())
print(api.user_search('jogryn'))  #works
print(api.user_relationship(user_id2))  #works

print(line['data'][0]['images'])

recent_media, next_ = api.user_recent_media()
photos = []
for media in recent_media:
    photos.append('<img src="%s"/>' % media.images['thumbnail'].url)
Exemplo n.º 50
0
from time import sleep
from instagram.client import InstagramAPI
import logging

access_token = ""
api = InstagramAPI(access_token="", client_secret="")
username = "******"

target_ids = api.user_search(username, 1)
print target_ids
target_id = target_ids[0].id
print target_id
Exemplo n.º 51
0
class Instagram:
    def __init__(self):
        self.api = InstagramAPI(client_id=CLIENT_ID, client_secret=CLIENT_SECRET, redirect_uri=REDIRECT_URI)

    def set_access_token(self, access_token):
        self.api = InstagramAPI(access_token=access_token)

    def media_popular(self, **params):
        popular = memcache.get("popular_feed")
        if not popular:
            popular = self.api.media_popular(count=params["count"], max_id=params["max_id"])
            memcache.add("popular_feed", popular, 300)
        return popular

    def user_media_feed(self, **params):
        user_id = params["user_id"]
        max_id = params["max_id"]
        count = params["count"]

        feed = memcache.get("user_media_feed_%s_%s" % (user_id, max_id))
        if not feed:
            feed = self.api.user_media_feed(count=count, max_id=max_id)
            memcache.add("user_media_feed_%s_%s" % (user_id, max_id), feed, 300)
        return feed

    def user_liked_feed(self, **params):
        user_id = params["user_id"]
        max_id = params["max_id"]
        count = params["count"]
        feed = memcache.get("user_liked_feed_%s_%s" % (user_id, max_id))
        if not feed:
            feed = self.api.user_liked_feed(count=count, max_like_id=max_id)
            memcache.add("user_liked_feed_%s_%s" % (user_id, max_id), feed, 300)
        return feed

    def user(self, user_id):
        user = memcache.get("user_%s" % (user_id))
        if not user:
            user = self.api.user(user_id)
            user["full_name"] = escape(user["full_name"].encode("utf-8"))
            user["full_name"] = self._convert_emoji(user["full_name"])
            memcache.add("user_%s" % (user_id), user, 300)
        return user

    def media(self, media_id):
        media = memcache.get("media_%s" % media_id)
        if not media:
            media = self.api.media(media_id)
            media.user["full_name"] = escape(media.user["full_name"].encode("utf-8"))
            media.user["full_name"] = self._convert_emoji(media.user["full_name"])
            if media.caption:
                media.caption["text_original"] = media.caption["text"]
                media.caption["text"] = escape(media.caption["text"].encode("utf-8"))
                media.caption["text"] = self._convert_emoji(media.caption["text"])
                media.caption["text"] = self._convert_tag_to_link(media.caption["text"])
            memcache.add("media_%s" % media_id, media, 300)
        return media

    def media_comments(self, media_id):
        comments = memcache.get("media_comments_%s" % (media_id))
        if not comments:
            converter = emoji.factory("softbank", "utf-8")
            converter.prefix = '<span class="emoji emoji_'
            converter.suffix = '"></span>'
            comments = self.api.media_comments(media_id)
            for comment in comments:
                comment["text"] = escape(comment["text"].encode("utf-8"))
                comment["text"] = self._convert_emoji(comment["text"])
                comment["text"] = self._convert_tag_to_link(comment["text"])
            memcache.add("media_comments_%s" % (media_id), comments, 300)
        return comments

    def media_likes(self, media_id):
        likes = memcache.get("media_likes_%s" % (media_id))
        if not likes:
            likes = self.api.media_likes(media_id)
            memcache.add("media_likes_%s" % (media_id), likes, 300)
        return likes

    def user_recent_media(self, **params):
        user_id = params["user_id"]
        max_id = params["max_id"]
        feed = memcache.get("user_recent_media_%s_%s" % (user_id, max_id))
        if not feed:
            feed = self.api.user_recent_media(user_id=user_id, max_id=max_id)
            memcache.add("user_recent_media_%s_%s" % (user_id, max_id), feed, 300)
        return feed

    def user_follows(self, **params):
        user_id = params["user_id"]
        count = params["count"]
        cursor = params["cursor"]
        follows = memcache.get("user_follows_%s_%s_%s" % (user_id, count, cursor))
        if not follows:
            follows = self.api.user_follows(user_id=user_id, count=count, cursor=cursor)
            for user in follows[0]:
                user["full_name"] = escape(user["full_name"].encode("utf-8"))
                user["full_name"] = self._convert_emoji(user["full_name"])
            memcache.add("user_follows_%s_%s_%s" % (user_id, count, cursor), follows, 300)
        return follows

    def user_followed_by(self, **params):
        user_id = params["user_id"]
        count = params["count"]
        cursor = params["cursor"]
        follows = memcache.get("user_followed_by_%s_%s_%s" % (user_id, count, cursor))
        if not follows:
            follows = self.api.user_followed_by(user_id=user_id, count=count, cursor=cursor)
            for user in follows[0]:
                user["full_name"] = escape(user["full_name"].encode("utf-8"))
                user["full_name"] = self._convert_emoji(user["full_name"])
            memcache.add("user_followed_by_%s_%s_%s" % (user_id, count, cursor), follows, 300)
        return follows

    def like_media(self, **params):
        user_id = params["user_id"]
        media_id = params["media_id"]
        max_id = params["max_id"]

        self.api.like_media(media_id)
        memcache.add("user_has_liked_%s_%s" % (user_id, media_id), True, 300)
        memcache.delete("media_likes_%s" % (media_id))

    def unlike_media(self, **params):
        user_id = params["user_id"]
        media_id = params["media_id"]
        max_id = params["max_id"]

        self.api.unlike_media(media_id)
        memcache.delete("user_has_liked_%s_%s" % (user_id, media_id))
        memcache.delete("media_likes_%s" % (media_id))

    def create_media_comment(self, **params):
        media_id = params["media_id"]
        text = params["text"]
        self.api.create_media_comment(media_id=media_id, text=text)
        memcache.delete("media_%s" % media_id)
        memcache.delete("media_comments_%s" % media_id)

    def user_find_by_username(self, username):
        user = memcache.get("user_find_by_username_%s" % (username))

        if not user:
            users = self.api.user_search(q=username, count=None)
            for u in users:
                if username == u["username"]:
                    user = u
                    memcache.add("user_find_by_username_%s" % (username), user)

        return user

    def tag_recent_media(self, **params):
        tag_name = params["tag_name"]
        count = params["count"]
        max_id = params["max_id"]

        feed = memcache.get("tag_recent_media_%s_%s" % (tag_name, max_id))

        if not feed:
            feed = self.api.tag_recent_media(tag_name=tag_name, count=count, max_id=max_id)
            memcache.add("tag_recent_media_%s_%s" % (tag_name, max_id), feed, 300)

        return feed

    def get_authorize_login_url(self, **params):
        uri = memcache.get("authorize_login_uri")
        if not uri:
            uri = self.api.get_authorize_login_url(scope=params["scope"])
            memcache.add("authorize_login_uri", uri, 300)
        return uri

    def _convert_emoji(self, text):
        converter = emoji.factory("softbank", "utf-8")
        converter.prefix = '<span class="emoji emoji_'
        converter.suffix = '"></span>'
        text = converter.convert(text)
        return text

    def _convert_tag_to_link(self, text):
        text = re.sub(r"#([a-zA-Z0-9\-]+)", '<a href="/tag/\g<1>">#\g<1></a>', text)
        return text

    def relationship(self, **params):
        owner_id = params["owner_id"]
        my_id = params["my_id"]

        relationship = memcache.get("relationship_%s_%s" % (my_id, owner_id))
        if not relationship:
            relationship = self.api.relationship(owner_id)
            memcache.add("relationship_%s_%s" % (my_id, owner_id), relationship, 300)

        return relationship

    def follow_user(self, **params):
        owner_id = params["owner_id"]
        my_id = params["my_id"]

        relationship = self.api.follow_user(user_id=owner_id)
        memcache.delete("relationship_%s_%s" % (my_id, owner_id))
        return relationship

    def unfollow_user(self, **params):
        owner_id = params["owner_id"]
        my_id = params["my_id"]

        relationship = self.api.unfollow_user(user_id=owner_id)
        memcache.delete("relationship_%s_%s" % (my_id, owner_id))
        return relationship
Exemplo n.º 52
0
def search(username, access_token):
    #user_q = User.query.filter_by(nickname = username).first()
    #if user_q:
    #    api = InstagramAPI(access_token=user_q.access_token)
    #else:
    #    file_with_token = open('app/access_token', "r")
    #    access_token = file_with_token.readline().split()[0]
    api = InstagramAPI(access_token=access_token)
    user_id = []
    for user in api.user_search(q=username):
        if user.username == username:
            user_id = [user]
    #print user_id
    #print type(user_id)
    #print 'OK_TEST_1'
    if user_id:
        recent_media, next_ = api.user_recent_media(user_id=str(user_id[0].id), count=11)

        liked = api.user_liked_media(user_id=str(user_id[0].id))
        liked = liked[:-1]
        user_liked = {}
        for media in liked[0]:
            if 'user' in dir(media):
                if media.user.username in user_liked:
                    user_liked[media.user.username] += 1
                else:
                    user_liked[media.user.username] = 1
        user_liked_sorted = []
        for pair in reversed(sorted(user_liked.items(), key=lambda item: item[1])):
            user_liked_sorted.append([pair[0], pair[1]])

        locations = {}
        for feed in recent_media:
            if 'location' in dir(feed):
                loc = feed.location.name.lower()
                if loc:
                    if loc in locations:
                        locations[loc] += 1
                    else:
                        locations[loc] = 1

        locations_sorted = []
        for pair in reversed(sorted(locations.items(), key=lambda item: item[1])):
            locations_sorted.append([pair[0], pair[1]])

        tags = {}

        for feed in recent_media:
            if 'tags' in dir(feed):
                for tag in feed.tags:
                    key = tag.name.lower()
                    if key in tags:
                        tags[key] +=1
                    else:
                        tags[key] = 1

        tags_sorted = []
        for pair in reversed(sorted(tags.items(), key=lambda item: item[1])):
            tags_sorted.append([pair[0], pair[1]])

        captions = {}

        for feed in recent_media:
            if 'caption' in dir(feed):
                if 'text' in dir(feed.caption):
                    caption = feed.caption.text.lower() + ' '
                    word = ""
                    for symb in caption:
                        if (ord(symb) == ord('#')) or (ord(u'a') <= ord(symb) <= ord(u'z')) or (ord(u'а') <= ord(symb) <= ord(u'я')):
                            word+=symb
                        else:
                            if word:
                                if not (word[0] == '#'):
                                    if (ord(u'а') <= ord(word[0]) <= ord(u'я')):
                                        word = morph.parse(word)[0].normal_form
                                        type = morph.parse(word)[0].tag.POS
                                        if type == 'PREP' or type == 'CONJ' or type == 'PRCL' or type == 'INTJ' or type == 'NUMR' or type == 'COMP':
                                            continue
                                    elif (ord(u'a') <= ord(word[0]) <= ord(u'z')):
                                        word = stemmer.stem(word)
                                        type = nltk.pos_tag(nltk.tokenize.word_tokenize(word))[0][1]
                                        if type == 'IN' or type == 'CC' or type == 'RB' or type == 'JJR':
                                            continue
                                    if word not in stopwords:
                                        if word in captions:
                                            captions[word]+=1
                                        else:
                                            captions[word]=1
                            word = ""
        comments = []
        comment_rating = {}
        for media in recent_media:
            for obj in media.comments:
                user_commented = unicode(obj.user.username)
                if user_commented in comment_rating:
                    comment_rating[user_commented] += 1
                else:
                    comment_rating[user_commented] = 1
                comment = obj.text.lower() + ' '
                comments.append(obj.text)
                word = ""
                for symb in comment:
                    if (ord(symb) == ord('@')) or (ord(symb) == ord('#')) or (ord(u'a') <= ord(symb) <= ord(u'z')) or (ord(u'а') <= ord(symb) <= ord(u'я')):
                        word+=symb
                    else:
                        if word:
                            if not ((word[0] == '#') or (word[0] == '@')):
                                if (ord(u'а') <= ord(word[0]) <= ord(u'я')):
                                    word = morph.parse(word)[0].normal_form
                                    type = morph.parse(word)[0].tag.POS
                                    if type == 'PREP' or type == 'CONJ' or type == 'PRCL' or type == 'INTJ' or type == 'NUMR' or type == 'COMP':
                                        continue
                                elif (ord(u'a') <= ord(word[0]) <= ord(u'z')):
                                    word = stemmer.stem(word)
                                    type = nltk.pos_tag(nltk.tokenize.word_tokenize(word))[0][1]
                                    if type == 'IN' or type == 'CC' or type == 'RB' or type == 'JJR':
                                        continue
                                if word not in stopwords:
                                    if word in captions:
                                        captions[word]+=1
                                    else:
                                        captions[word]=1
                            word = ""

        captions_sorted = []
        for pair in reversed(sorted(captions.items(), key=lambda item: item[1])):
            captions_sorted.append([pair[0], pair[1]])
        comment_rating_sorted = []
        for pair in reversed(sorted(comment_rating.items(), key=lambda item: item[1])):
            comment_rating_sorted.append([pair[0], pair[1]])

        images = {}
        videos = []
        filters = {}
        likes_of_users = {}
        count_of_all_likes = 0
        for media in recent_media:
            if 'videos' not in dir(media) and 'images' in dir(media):
                if (media.images['standard_resolution'].url) in images:
                    images[media.images['standard_resolution'].url] += media.like_count
                else:
                    images[media.images['standard_resolution'].url] = media.like_count
                filter = unicode(media.filter)
                if filter in filters:
                    filters[filter] += 1
                else:
                    filters[filter] = 1

            if 'videos' in dir(media):
                videos.append(media.videos['standard_resolution'].url)
            for usr in media.likes:
                if usr.username in likes_of_users:
                    likes_of_users[usr.username] += 1
                else:
                    likes_of_users[usr.username] = 1
            count_of_all_likes+=media.like_count
        likes_of_users_sorted = []
        for pair in reversed(sorted(likes_of_users.items(), key=lambda item: item[1])):
            likes_of_users_sorted.append([pair[0], pair[1]])
        images_sorted = []
        for pair in reversed(sorted(images.items(), key=lambda item: item[1])):
            images_sorted.append([pair[0], pair[1]])
        filters_sorted = []
        for pair in reversed(sorted(filters.items(), key=lambda item: item[1])):
            filters_sorted.append([pair[0], pair[1]])

        user_bsc = api.user(user_id[0].id)
        basic_information = [user_bsc.id,
        user_bsc.full_name, user_bsc.website, user_bsc.counts["media"], count_of_all_likes, user_bsc.counts["follows"],
        user_bsc.counts["followed_by"], user_bsc.profile_picture, user_bsc.bio]
        #print "OK_TEST_2"
        return [basic_information, images_sorted, videos, captions_sorted, tags_sorted,
                locations_sorted, comments, user_liked_sorted, likes_of_users_sorted, comment_rating_sorted, filters_sorted]
    else:
        return []
# print "IMAGES"
# for obj in images:
#     print obj
#
# print "VIDEOS"
# for obj in videos:
#     print obj
#
# print "CAPTIONS:"
# for obj in captions_sorted:
#     print obj[0], obj[1]
#
# print "HASHTAGS:"
# for obj in tags_sorted:
#     print obj[0], obj[1]
#
# print "PLACES:"
# for obj in locations_sorted:
#     print obj[0], obj[1]
#
# print "MOST POPULAR PLACES:"
# mstpopular = locations_sorted[:3]
# for obj in mstpopular:
#     print obj[0], obj[1]
Exemplo n.º 53
0
class InstagramSpider(scrapy.Spider):

	name = 'instagram'

	allowed_domains = ["instagram.com"]
	start_urls = ["http://www.instagram.com"]

	def __init__(self):
		access_token ="182023789.04c8e57.9e150e8ccd49440d96b7e3b0cf53179f"
		self.api = InstagramAPI(access_token=access_token,client_secret="85ecfb407c34423c8efcf82af4b38695")


		self.user_ids = {}
		self.user_imgs = {}

					# players
		accounts = ['cristiano', 'leomessi', 'waynerooney', 'zidane', 'iamzlatanibrahimovic', 'toni.kr8s', 'davidluiz_4', 'hazardeden_10', 'davidbeckham', 'andreapirlo21thierryhenry', 'luissuarez9', 'garethbale11', 'andresiniesta8', 'neymarjr', 'kingarturo23oficial', 'manuelneuer', 'didierdrogba', 'mosalah22', 'ronaldolima', 'kaka', 'm10_official', 'jamesrodriguez10', 'ikercasillasoficial', 'mb459', 'marcelotwelve', 'alexis_officia1', 'stevengerrard', 'realmadrid', 'fcbarcelona', 'chelseafc', 'arsenal', 'liverpoolfc', 'manchesterunited', 'mcfcofficial', 'juventus', 'officialasroma', 'inter', 'acmilan', 'acffiorentina', 'psg', 'fcbayern', 'bvb09', 'uefachampionsleague', 'instareplays', 'robgunillustration', 'squawkafootball', 'soccermemes', 'futbolsport', 'nikefootball', 'pumafootball', 'adidasfootball', 'instareplays', '6secfootball101greatgoals', '8fact_football', 'premierleague', 'laliga', 'espnfc', 'beinsports', 'bestoffootball', 'golazoweekly', 'worldgoalz', 'futbol_evolution', 'footballtransfersdailywe_love_football', 
					'footbalita', '8factfootball', 'football.news', 'footykix', 'yallakoraofficial', 'filgoal1', 'talksport', 'kooora', 
					'cristiano','leomessi', 'waynerooney','zidane','iamzlatanibrahimovic', 'toni.kr8s','davidluiz_4',
					'hazardeden_10','davidbeckham','andreapirlo21',
					'thierryhenry','luissuarez9','garethbale11','andresiniesta8','neymarjr','kingarturo23oficial','manuelneuer','didierdrogba','mosalah22',
					'ronaldolima','kaka','m10_official','jamesrodriguez10','ikercasillasoficial','mb459','marcelotwelve','alexis_officia1','stevengerrard',
					# clubs
					'realmadrid','fcbarcelona',
					'chelseafc','arsenal','liverpoolfc','manchesterunited','mcfcofficial',
					'juventus','officialasroma','inter','acmilan','acffiorentina',
					'psg',
					'fcbayern','bvb09',
					# other accounts
					'433','visubal','officialfootballmemes', 'instatroll_football'
					'uefachampionsleague','instareplays','robgunillustration','squawkafootball','soccermemes','futbolsport','nikefootball','pumafootball','adidasfootball','instareplays','6secfootball',
					'101greatgoals','8fact_football','premierleague','laliga','espnfc','beinsports','bestoffootball','golazoweekly','worldgoalz','futbol_evolution','footballtransfersdaily',
					'we_love_football','footbalita','8factfootball','football.news','footykix','yallakoraofficial','filgoal1','talksport','kooora']


		if not ( os.path.exists('insta_user_ids.pickle') and os.path.exists('insta_user_imgs.pickle')):
			accounts = list(set(accounts))
			for account in accounts:
				print 'looking for: ', account
				user_list = self.api.user_search(account)
				print user_list
				if user_list:
					for user in user_list:
						if user.username == account:
							self.user_ids[account] = user.id
							self.user_imgs[account] = user.profile_picture
							print 'found: ', user

			with open('insta_user_ids.pickle', 'wb') as f:
				cPickle.dump(self.user_ids, f)

			with open('insta_user_imgs.pickle', 'wb') as f:
				cPickle.dump(self.user_imgs, f)

		else:
			with open('insta_user_ids.pickle', 'r') as f:
				self.user_ids = cPickle.load(f)

			with open('insta_user_imgs.pickle', 'r') as f:
				self.user_imgs = cPickle.load(f)

	def parse(self,response):
		itemCount  = 1
		for user in self.user_ids:
			self.logger.debug("Retrieving data for " + str(user))
			recent_media, next_ = self.api.user_recent_media(user_id=self.user_ids[user])
			for media in recent_media:
				item = InstagramItem()

				if media.caption:
					item['caption'] = media.caption.text
				item['lang'] = 'en'
				item['date'] = media.created_time
				item['src'] = 'instagram'
				item['account'] = user
				item['account_image'] = self.user_imgs[user]

				item['tags'] = []
				for tag in media.tags:
					item['tags'].append(str(tag).split(' ')[1])

				item['url'] = media.link
				item['type'] = media.type

				if media.type == 'image':
					item['img_vid_src'] = media.images['standard_resolution'].url
				else:
					item['img_vid_src'] = media.videos['standard_resolution'].url

				item['likes'] = media.like_count
				item['media_id'] = media.id
				item['itemIndex'] = itemCount
				itemCount = itemCount+1

				yield item