Repository navigation
Expand file tree
/
Copy pathNegative_Comment.py
More file actions
68 lines (44 loc) · 2.6 KB
/
Copy pathNegative_Comment.py
File metadata and controls
68 lines (44 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import requests
from textblob import TextBlob
#For Sentiment Analysis in Python, we use the libaray TextBlob
from textblob.sentiments import NaiveBayesAnalyzer
#NaiveBayesAnalyzer is same as Bays theorem in probability
#classifier for sentiment analysis.It uses the the data set of TextBlob library:
from common import BASE_URL,APP_ACCESS_TOKEN
from Get_Post_Id import get_post_id
from colorama import *
positive_comments=[] # list to store list of positive comments
negative_comments=[] # list to store number of negative comments
total_comments=[] # list to store total number of comments
# this Function is used for analysing The comment whether it is positive comment or negative comment
# Also deleting the comments from the user's posts if it is negative
def delete_negative_comment(insta_username):
media_id = get_post_id(insta_username)
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']):
#Here's a naive implementation of how to delete the negative comments :)
for x in range(0, len(comment_info['data'])):
comment_id = comment_info['data'][x]['id']
comment_text = comment_info['data'][x]['text']
total_comments.append(comment_text) #adding total number of comments to the list
blob = TextBlob(comment_text, analyzer=NaiveBayesAnalyzer())
if (blob.sentiment.p_neg > blob.sentiment.p_pos):
negative_comments.append(comment_text) # adding negative comments
print Fore.RED+Style.BRIGHT+'Negative comment : %s' % (comment_text)
delete_url = (BASE_URL + 'media/%s/comments/%s/?access_token=%s') % (media_id, comment_id, APP_ACCESS_TOKEN)
print 'DELETE request url : %s' % (delete_url)
delete_info = requests.delete(delete_url).json()
if delete_info['meta']['code'] == 200:
print 'Comment successfully deleted!\n'
else:
print 'Unable to delete comment!'
else:
positive_comments.append(comment_text) #adding positive comments
print Fore.BLUE+Style.BRIGHT+'Positive comment : %s\n' % (comment_text)
else:
print 'There are no existing comments on the post!'
else:
print 'Status code other than 200 received!'