Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import praw
- from textblob import TextBlob
- from prettytable import PrettyTable
- def f_num(num):
- return '{0:.2f}'.format(num).rstrip('0').rstrip('.')
- def main():
- with open("credentials.txt", "r") as f:
- DF = f.read().splitlines()
- REDDIT = praw.Reddit(client_id=DF[0],
- client_secret=DF[1],
- user_agent="python script",
- username=DF[2],
- password=DF[3])
- # assumes DT is pinned post on the sub
- dt = [x.id for x in REDDIT.subreddit('neoliberal').hot(limit=1)].pop(0)
- submission = REDDIT.submission(id=dt)
- # structure = {Username: number of posts, overall score, score per post,
- # controversial, textbody}
- comments = dict()
- submission.comments.replace_more(limit=None)
- for comment in submission.comments.list():
- try:
- username = comment.author.name
- score = comment.score
- controversial = comment.controversiality
- body = comment.body
- if username not in comments:
- comments[username] = [1, score, controversial, [body]]
- else:
- old_count, old_score, old_controversial, body_list = comments[username]
- comments[username] = [
- old_count + 1,
- old_score + score,
- old_controversial + controversial,
- body_list + [body]]
- except Exception:
- pass
- sorted_users = sorted(
- comments.items(), key=lambda i: i[1][0], reverse=True)
- for x in comments:
- tmp = comments[x][3]
- sentiments = []
- if tmp is not None:
- for comment in tmp:
- blob = TextBlob(''.join(comment))
- sentiments.append(blob.polarity)
- pos, neutral, neg = sum(
- x < 0 for x in sentiments), sum(
- x == 0 for x in sentiments), sum(
- x > 0 for x in sentiments)
- p, n, neg = float(pos * 100) / len(tmp), float(neutral *
- 100) / len(tmp), float(neg * 100) / len(tmp)
- comments[x][3] = "{:>5}% {:>5}% {:>5}%".format(
- f_num(p), f_num(n), f_num(neg))
- t = PrettyTable(['Name', 'Postcount', 'Karma', 'Karma/Post',
- 'Controversial', 'Sentiment (Pos/Ntr/Neg)'])
- for key, value in sorted_users:
- karma_per_post = 0 if value[1] == 0 else float(value[1] / value[0])
- t.add_row([key, value[0], value[1],
- '{0:.2f}'.format(karma_per_post), value[2], value[3]])
- print(t)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement