Example #1
0
def channelback(request):
    if request.method == 'POST':
        metadata = request.POST.get('metadata', '')
        newState = request.POST.get('state', '')
        message = request.POST.get('message', '')
        parentId = request.POST.get('parent_id', '')
        recipientId = request.POST.get('recipient_id', '')

        metaJson = json.loads(metadata)
        client_id = metaJson['client_id']
        client_secret = metaJson['client_secret']
        access_token = metaJson['token']
        name = metaJson['name']

        mediaIdSplit = parentId.split('-')
        media_id = mediaIdSplit[2]

        api = InstagramAPI(access_token=access_token,
                           client_secret=client_secret)
        comment = api.create_media_comment(media_id, message)
        mediaComments = api.media_comments(media_id)
        commentId = ''
        for mediaComment in mediaComments:
            if mediaComment.text == message:
                commentId = mediaComment.id
                newCommentId = "cmnt-" + commentId + '-' + media_id
                print(newCommentId)

    response_data = {}
    response_data['external_id'] = newCommentId
    response_data['allow_channelback'] = True
    return HttpResponse(json.dumps(response_data, ensure_ascii=False),
                        content_type="application/json;charset=UTF-8")
def comment_to_photos():
    content = request.get_json()
    comment = content['comment']
    hashtag = content['tag']
    number = 5   # content['number']
    api = InstagramAPI(access_token=token, client_secret=CONFIG['client_secret'])
    try:
        photos = get_latest_photos(hashtag, number)
    except Exception as e:
        print("Oops! Something went wrong!")
        print(e)
        return redirect("http://46.101.29.114:7000/", code=302)
        
    for photo in photos:
        api.create_media_comment(photo.id, comment)
    return jsonify(success=True, commented=number)
 def post_comment(self, media_id,text,token):
 	print token
 	api = InstagramAPI(access_token=token)
 	comment = api.create_media_comment(media_id, text)
 	return comment
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
Example #5
0
class InstagramAccount(Account):
    def __init__(self, client_id=None,client_secret=None,redirect_url=None,access_token=None,name=None,ty=None,**kwargs):
        self.client_id = client_id
        self.client_secret = client_secret
        self.redirect = redirect_url
        self.name = name
        self.ty = ty
        if access_token:
            self.access_token = access_token
            self.api = InstagramAPI(access_token=access_token)
        else:
            self.api = InstagramAPI(client_id=client_id,client_secret=client_secret,redirect_uri=redirect_url)
            url = self.api.get_authorize_login_url(scope=['basic','likes','comments','relationships'])
            print 'This account needs to be authenticated. Visit this url:'
            print url
            code = raw_input('Please enter the result code:').strip()
            self.access_token, user_info = self.api.exchange_code_for_access_token(code)
            db.instagram.update({'name':self.name},{'$set':{'access_token':self.access_token}})
            self.api = InstagramAPI(access_token = access_token)
        self.uid = self._get_uid(self.name)

    def log(self,action,details):
        Account.log(self,'instagram',action,details)

    # Pick a popular thing and like it.
    def like_popular(self):
        self.log("like-popular",str(datetime.today()))
        popular = self.api.media_popular(count='20')
        for i in xrange(8):
            p = random.choice(popular)
            self.api.like_media(p.id)

    # Follow someone.
    def follow(self,un):
        uid = self._get_uid(un)
        # Bug in the official API call for this one. Needs direct HTTP
        payload = {'ACCESS_TOKEN':self.access_token,'action':'follow'}
        r = requests.post('https://api.instagram.com/v1/users/'+uid+'/relationship?access_token='+self.access_token,data=payload)
        return r

    # Follow a friend of a friend.
    def follow_branch(self):
        friends = self.api.user_follows(self.uid)
        f = random.choice(friends[0])
        other_friends = self.api.user_follows(f.id)
        f2 = random.choice(other_friends[0])
        self.log("follow-branch",str(datetime.today())+','+f2.username)
        self.follow(f2.username)
        return f2

    # make a generic comment
    # for now these comments are just positive
    def generic_comment_friend(self):
        #1. pick a friend
        friends = self.api.user_follows(self.uid)[0]
        f = random.choice(friends)

        #2. pick a dumb comment
        comment = random.choice(POSITIVE)

        #3. pick something they posted
        recent = self.api.user_recent_media(f.id)
        print recent
        post = random.choice(recent)
        self.log("generic-comment-friend",str(datetime.today())+','+str(post)+','+str(comment))

        #4. make a dumb comment on their dumb post
        self.api.create_media_comment(post.id,comment)

        return (post,comment)

    def generic_comment_feed(self):
        comment = random.choice(POSITIVE)
        post = random.choice(self.api.user_media_feed()[0])
        self.log("generic-comment-friend",str(datetime.today())+','+str(post)+','+str(comment))
        self.api.create_media_comment(post.id,comment)

    # like something a friend posted recently
    def like_friend(self):
        friends = self.api.user_follows(self.uid)[0]
        f = random.choice(friends)

        recent = self.api.user_recent_media(user_id=f.id,count=20)
        self.log("like-friends-post",str(datetime.today())+','+f.username)
        post = random.choice(recent[0])
        self.api.like_media(post.id)
        return post

    # Helper to turn a username into a user id
    def _get_uid(self,un):
        uid = self.api.user_search(q=un)
        uid = uid[0]
        uid = uid.id
        return uid
Example #6
0
class InstagramAccount(Account):
    def __init__(self,
                 client_id=None,
                 client_secret=None,
                 redirect_url=None,
                 access_token=None,
                 name=None,
                 ty=None,
                 **kwargs):
        self.client_id = client_id
        self.client_secret = client_secret
        self.redirect = redirect_url
        self.name = name
        self.ty = ty
        if access_token:
            self.access_token = access_token
            self.api = InstagramAPI(access_token=access_token)
        else:
            self.api = InstagramAPI(client_id=client_id,
                                    client_secret=client_secret,
                                    redirect_uri=redirect_url)
            url = self.api.get_authorize_login_url(
                scope=['basic', 'likes', 'comments', 'relationships'])
            print 'This account needs to be authenticated. Visit this url:'
            print url
            code = raw_input('Please enter the result code:').strip()
            self.access_token, user_info = self.api.exchange_code_for_access_token(
                code)
            db.instagram.update({'name': self.name},
                                {'$set': {
                                    'access_token': self.access_token
                                }})
            self.api = InstagramAPI(access_token=access_token)
        self.uid = self._get_uid(self.name)

    def log(self, action, details):
        Account.log(self, 'instagram', action, details)

    # Pick a popular thing and like it.
    def like_popular(self):
        self.log("like-popular", str(datetime.today()))
        popular = self.api.media_popular(count='20')
        for i in xrange(8):
            p = random.choice(popular)
            self.api.like_media(p.id)

    # Follow someone.
    def follow(self, un):
        uid = self._get_uid(un)
        # Bug in the official API call for this one. Needs direct HTTP
        payload = {'ACCESS_TOKEN': self.access_token, 'action': 'follow'}
        r = requests.post('https://api.instagram.com/v1/users/' + uid +
                          '/relationship?access_token=' + self.access_token,
                          data=payload)
        return r

    # Follow a friend of a friend.
    def follow_branch(self):
        friends = self.api.user_follows(self.uid)
        f = random.choice(friends[0])
        other_friends = self.api.user_follows(f.id)
        f2 = random.choice(other_friends[0])
        self.log("follow-branch", str(datetime.today()) + ',' + f2.username)
        self.follow(f2.username)
        return f2

    # make a generic comment
    # for now these comments are just positive
    def generic_comment_friend(self):
        #1. pick a friend
        friends = self.api.user_follows(self.uid)[0]
        f = random.choice(friends)

        #2. pick a dumb comment
        comment = random.choice(POSITIVE)

        #3. pick something they posted
        recent = self.api.user_recent_media(f.id)
        print recent
        post = random.choice(recent)
        self.log("generic-comment-friend",
                 str(datetime.today()) + ',' + str(post) + ',' + str(comment))

        #4. make a dumb comment on their dumb post
        self.api.create_media_comment(post.id, comment)

        return (post, comment)

    def generic_comment_feed(self):
        comment = random.choice(POSITIVE)
        post = random.choice(self.api.user_media_feed()[0])
        self.log("generic-comment-friend",
                 str(datetime.today()) + ',' + str(post) + ',' + str(comment))
        self.api.create_media_comment(post.id, comment)

    # like something a friend posted recently
    def like_friend(self):
        friends = self.api.user_follows(self.uid)[0]
        f = random.choice(friends)

        recent = self.api.user_recent_media(user_id=f.id, count=20)
        self.log("like-friends-post", str(datetime.today()) + ',' + f.username)
        post = random.choice(recent[0])
        self.api.like_media(post.id)
        return post

    # Helper to turn a username into a user id
    def _get_uid(self, un):
        uid = self.api.user_search(q=un)
        uid = uid[0]
        uid = uid.id
        return uid