コード例 #1
0
def get_user_post():
    user_id = friend_id()
    if user_id:
        request_url = (base_url + 'users/%s/media/recent/?access_token=%s') % (
            user_id, app_access_token)
        print '\nGET request url : %s' % (request_url)
        user_media = requests.get(request_url).json()
        if user_media['meta']['code'] == 200:
            if len(user_media['data']):
                #return user_media['data'][0]['id']
                image_name = user_media['data'][0]['id'] + '.jpeg'
                image_url = user_media['data'][0]['images'][
                    'standard_resolution']['url']
                urllib.urlretrieve(image_url, image_name)
                print colored(
                    'Your friend\'s recent post has been downloaded!', 'green')
            else:
                print "There is no recent post!"
                exit()
        else:
            print colored(user_media['meta']['error_message'], 'red')
            exit()
    else:
        # if user enter wrong user name which does not exist
        print colored("User not found", 'red')
        exit()
コード例 #2
0
def get_list_of_comment():
    user_id = friend_id()
    if user_id:
        media_id = get_media_id(user_id)
        request_url = (base_url + 'media/' + media_id +
                       '/comments?access_token=%s') % (app_access_token)
        comment_list = requests.get(request_url).json()
        if comment_list['meta']['code'] == 200:
            if len(comment_list['data']):
                print colored("\nSome recent comments are", 'green')
                count = 0
                for ele in comment_list['data']:
                    count += 1
                    print "%d.\n comment: " % (count) + colored(
                        ele['text'], "green")
                    print " user name: " + colored(ele['from']["username"],
                                                   "yellow")
            else:
                print colored('no comment on this post till now', 'green')
                exit()
        else:
            # if access token is wrong an error message gets printed
            print colored(comment_list['meta']['error_message'], 'red')
            exit()
    else:
        print colored("User not found", 'red')
コード例 #3
0
def like_a_post():
    user_id = friend_id()
    if user_id:
        media_id = get_media_id(user_id)
        request_url = (base_url + 'media/%s/likes') % (media_id)
        payload = {"access_token": app_access_token}
        print 'POST request url is : %s' % (request_url)
        post_a_like = requests.post(request_url, payload).json()
        if post_a_like['meta']['code'] == 200:
            print colored('Successful Done!', 'green')
        else:
            print colored('Some Error. Try again!', 'yellow')
    else:
        print colored("Incorrect Username", 'red')
コード例 #4
0
def post_a_comment():
    user_id = friend_id()
    if user_id:
        media_id = get_media_id(user_id)
        comment_text = raw_input("Your comment: ")
        payload = {"access_token": app_access_token, "text": comment_text}
        request_url = (base_url + 'media/%s/comments') % (media_id)
        print 'POST request url : %s' % (request_url)
        make_comment = requests.post(request_url, payload).json()
        if make_comment['meta']['code'] == 200:
            print colored("Successfully added a new comment!", "green")
        else:
            print colored("Unable to add comment. Try again!", "red")
    else:
        print colored("Incorrect Username")
コード例 #5
0
def delete_negative_comment():
    user_id = friend_id()
    if user_id:
        media_id = get_media_id(user_id)
        request_url = (base_url + 'media/%s/comments/?access_token=%s') % (
            media_id, app_access_token)
        print 'GET request url : %s' % (request_url)
        comment_info = requests.get(request_url).json()

        if comment_info['meta']['code'] == 200:
            if len(comment_info['data']):
                print colored("\nSome recent comments with sentiment analysis",
                              'yellow')
                count = 0
                for ele in comment_info['data']:
                    count += 1
                    print "\n%d.\ncomment: " % (count) + colored(
                        ele['text'], "green")
                    blob = TextBlob(ele['text'], analyzer=NaiveBayesAnalyzer())
                    print blob.sentiment
                    if (blob.sentiment.p_neg > blob.sentiment.p_pos):
                        print colored("Negative comment", "red")
                        delete_url = (
                            base_url + 'media/%s/comments/%s/?access_token=%s'
                        ) % (media_id, ele['id'], app_access_token)
                        print colored('DELETE request url :', 'red')
                        print("%s\n" % (delete_url))
                        delete_info = requests.delete(delete_url).json()

                        if delete_info['meta']['code'] == 200:
                            print colored('Comment successfully deleted!',
                                          'green')
                        else:
                            print colored('Unable to delete comment!',
                                          'yellow')
                    else:
                        print colored("Positive comment", 'green')
                    print "user name: " + colored(ele['from']["username"],
                                                  "green")
            else:
                print colored('no comment on this post till now', 'green')
                exit()
        else:
            # if access token is wrong an error message gets printed
            print colored(comment_info['meta']['error_message'], 'red')
            exit()
    else:
        print colored("Incorrect Username", 'red')
コード例 #6
0
def friend_info():
    user_id = friend_id(
    )  #calling friend_id function to access the id of the user
    if user_id:
        request_url = (base_url + 'users/%s/?access_token=%s') % (
            user_id, app_access_token)
        request_object = requests.get(request_url)
        info = request_object.json()
        print colored('Your friend\'s info is:', 'green'), info
        print colored(
            "Your friend\'s Follower:",
            'green') + (" %s") % (info['data']['counts']['followed_by'])
        print colored('People your friend Follows:',
                      'green') + (" %s") % (info['data']['counts']['follows'])
        print colored('No. of posts:',
                      "green") + (" %s") % (info['data']['counts']['media'])
    else:
        #if user enter wrong user name which does not exist
        print colored("User not found", 'red')
コード例 #7
0
ファイル: matplot.py プロジェクト: varuntyagi-i/Instabot
def interest():
    user_id = friend_id()
    if user_id:
        request_url = (base_url + 'users/' + user_id +
                       '/media/recent/?access_token=%s') % (app_access_token)
        hashtag = requests.get(request_url).json()
        if hashtag['meta']['code'] == 200:
            if len(hashtag['data']):
                print colored("\nSome recent post with hashtags: ", 'green')
                count = 0
                tags_list = []
                for ele in hashtag['data']:
                    count += 1
                    if ele['tags'] == []:
                        print '\n%d\n ' % (count) + colored(
                            'no hashtag', 'yellow')
                        #tags_list.append('')
                    else:
                        print "\n%d" % (count)
                        for tag in range(0, len(ele['tags'])):
                            print "tags: " + colored(ele['tags'][tag], "green")
                            tags_list.append(ele['tags'][tag])

                    print " post url: " + colored(
                        ele['images']['standard_resolution']['url'], "yellow")

                if len(tags_list) > 0:
                    # contains all values with there frequency in tags_list list and store it in counts(dictionary)
                    # note: return typr of Counter is dictionary
                    counts = Counter(tags_list)
                    # use to plot graph on the basis of hashtag analysis of user
                    wordcloud = WordCloud().generate_from_frequencies(counts)
                    plt.imshow(wordcloud, interpolation='bilinear')
                    plt.axis("off")
                    plt.show()

                    # print the frequecy of each hashtag
                    print '\n'
                    for ele in counts:
                        print "%s : %d" % (ele, counts[ele])

                else:
                    print colored("no hashtag on user's post", 'red')
                """
                caption_list_count = []
                for value in caption_list:
                    caption_list_count.append(caption_list.count(value))
                print len(caption_list_count)
                print caption_list_count
                print caption_list

                x = np.arange(len(caption_list_count))
                plt.bar(x, height = caption_list_count)
                plt.xticks(x+1,caption_list)
                plt.plot([1, 2, 3], [1, 2, 3])
                """

            else:
                print colored('no post till now: ', 'yellow')
                exit()
        else:
            # if access token is wrong an error message gets printed
            print colored(hashtag['meta']['error_message'], 'red')
            exit()
    else:
        print colored("User not found", 'red')