-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReddit.py
198 lines (176 loc) · 6.47 KB
/
Reddit.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import os
import praw
from praw.models import MoreComments
import pandas as pd
import datetime as dt
from textblob import TextBlob
from elasticsearch import Elasticsearch, helpers
from datetime import datetime
import tkinter as tk
from dotenv import load_dotenv
load_dotenv()
es = Elasticsearch()
reddit = praw.Reddit(client_id=os.environ.get("client_id"),
client_secret=os.environ.get("client_secret"),
user_agent="SIN-PROJECT",
username=os.environ.get("username"),
password=os.environ.get("password"))
movies_sub = reddit.subreddit('movies')
new = movies_sub.new(limit=100)
# for comments in movies_sub.stream.submissions():
# print(comments)
# # comment = TextBlob(comments.body)
# # print(comment.sentiment.polarity)
# get static review for a particular movie from all time
def staticReview(movie_name):
for submission in movies_sub.search(movie_name, limit=10):
print(submission.title)
submission_polarity = 0
comment_count = 0
comment_count_n = 0
comment_count_p = 0
comments = submission.comments.list()
for c in comments:
if isinstance(c, MoreComments):
continue
print(c.body)
comment = TextBlob(c.body)
if comment:
comment_count += 1
c_polarity = comment.sentiment.polarity
if c_polarity < 0:
comment_count_n += 1
elif c_polarity > 0:
comment_count_p += 1
print(comment.sentiment.polarity)
submission_polarity += comment.sentiment.polarity
mapping = {
"mappings": {
"properties": {
"author": {
"type": "keyword"
},
"date": {
"type": "date"
},
"message": {
"type": "keyword"
},
"polarity": {
"type": "keyword"
},
"sentiment": {
"type": "keyword"
},
"post_url": {
"type": "keyword"
},
"flairs": {
"type": "keyword"
},
"positive_comments": {
"type": "number"
},
"negative_comments": {
"type": "number"
}
}
}
}
if submission_polarity < 0:
sentiment = "negative"
elif submission_polarity == 0:
sentiment = "neutral"
else:
sentiment = "positive"
es.indices.create(index='logstash-reddit', body=mapping, ignore=400)
print(submission_polarity/comment_count)
es.index(index="logstash-reddit",
# doc_type="test-type",
body={"author": submission.author.name,
# parse the milliscond since epoch to elasticsearch and reformat into datatime stamp in Kibana later
"date": submission.created_utc,
"message": submission.title,
"polarity": submission_polarity/comment_count,
"sentiment": sentiment,
"post_url": submission.url,
"flairs": submission.link_flair_text,
"positive_comments": comment_count_p,
"negative_comments": comment_count_n
})
# get live updates on a movie review
def streamComments(movie_name):
for submission in reddit.subreddit("movies").stream.submissions():
# remove to get all movies
# if movie_name not in submission.title:
# continue
print(submission.title)
s = TextBlob(submission.title)
submission_polarity = s.sentiment.polarity
mapping = {
"mappings": {
"properties": {
"author": {
"type": "keyword"
},
"date": {
"type": "date"
},
"message": {
"type": "keyword"
},
"polarity": {
"type": "keyword"
},
"sentiment": {
"type": "keyword"
},
}
}
}
if submission_polarity < 0:
sentiment = "negative"
elif submission_polarity == 0:
sentiment = "neutral"
else:
sentiment = "positive"
es.indices.create(index='logstash-live-reddit', body=mapping, ignore=400)
print(submission_polarity)
es.index(index="logstash-live-reddit",
# doc_type="test-type",
body={"author": submission.author.name,
# parse the milliscond since epoch to elasticsearch and reformat into datatime stamp in Kibana later
"date": submission.created_utc,
"message": submission.title,
"polarity": submission_polarity,
"sentiment": sentiment,
})
def getMovieName():
movie = entry1.get();
label1 = tk.Label(root, text="Check Kibana For Visualization")
canvas1.create_window(200, 230, window=label1)
root.destroy()
while True:
try:
staticReview(movie)
# streamComments(movie)
except KeyboardInterrupt:
break
except:
continue
if __name__ == '__main__':
root = tk.Tk()
canvas1 = tk.Canvas(root, width=400, height=300)
canvas1.pack()
label1 = tk.Label(root, text='Search data for the movie')
label1.config(font=('helvetica', 14))
canvas1.create_window(200, 25, window=label1)
label2 = tk.Label(root, text='Enter movie name:')
label2.config(font=('helvetica', 10))
canvas1.create_window(200, 100, window=label2)
entry1 = tk.Entry(root)
canvas1.create_window(200, 140, window=entry1)
button1 = tk.Button(text='Add', command=getMovieName,
bg='brown', fg='white', font=('helvetica', 9, 'bold'))
canvas1.create_window(200, 180, window=button1)
root.mainloop()