-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstasentiments.py
76 lines (66 loc) · 2.47 KB
/
instasentiments.py
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
69
70
71
72
73
74
75
import instaloader
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
import numpy as np
from instaloader import Profile, Post
def getPublicProfileCaptions(profile_id):
profilename = profile_id
loader = instaloader.Instaloader()
profile = Profile.from_username(loader.context,profilename)
profile_pic = profile.get_profile_pic_url()
full_name = profile.full_name
posts = profile.get_posts()
captions = []
for post in posts:
if post.caption != None:
captions.append(post.caption)
if len(captions) < 1:
loader.close()
return "No captions found! Are you sure this profile is public and has posted?", "Empty", "Empty"
else:
loader.close()
return captions, profile_pic, full_name
def getPrivateProfileCaptions(profile_id, login, password):
profilename = profile_id
loader = instaloader.Instaloader()
try:
loader.login(login,password)
except:
return "Failed to login!", "Empty", "Empty"
profile = Profile.from_username(loader.context, profilename)
posts = profile.get_posts()
profile_pic = profile.get_profile_pic_url()
full_name = profile.full_name
captions = []
for post in posts:
if post.caption != None:
captions.append(post.caption)
if len(captions) < 1:
loader.close()
return "No captions found! Are you sure this profile has posted?", "Empty", "Empty"
else:
loader.close()
return captions, profile_pic, full_name
def getSentiments(captions):
if len(captions) > 0 and type(captions) == list:
analyser = SentimentIntensityAnalyzer()
neutral = []
positive = []
negative = []
compound = []
for caption in captions:
neutral.append(analyser.polarity_scores(caption)['neu'])
positive.append(analyser.polarity_scores(caption)['pos'])
negative.append(analyser.polarity_scores(caption)['neg'])
compound.append(analyser.polarity_scores(caption)['compound'])
positive = np.array(positive)
negative = np.array(negative)
neutral = np.array(neutral)
compound = np.array(compound)
return {
'Neutral':round(neutral.mean(),2)*100.0,
'Positive':round(positive.mean(),2)*100.0,
'Negative':round(negative.mean(), 2) * 100.0,
'Overall':round(compound.mean(), 2) * 100.0
}
else:
return captions