Advertisement
Guest User

abcdefg

a guest
Sep 30th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. import pdb
  2. import pprint
  3. import praw
  4. import os
  5.  
  6. from datetime import datetime
  7.  
  8. # TODO
  9. # 1. People who posted demonetization related articles or participants - how many of them were new?
  10. # 2. Average age of an account that participated in these discussions
  11. # 3. Sentiment of posts - positive or negative - over a period of time
  12. # 4. Sentiment of posts/comments and the average age of authors
  13. # 5. Sentiment of authors towards BJP/Modi before demonetization
  14. # 6. Top influencers - authors whose comments/submissions were upvoted the most
  15.  
  16. epoch = datetime(1970,1,1)
  17.  
  18. def count_items(reddit_list):
  19. x = 0
  20. earliest_time = (datetime.utcnow()-epoch).total_seconds()
  21. for c in reddit_list:
  22. if c.created_utc < earliest_time:
  23. earliest_time = c.created_utc
  24. x += 1
  25. return x, earliest_time
  26.  
  27. reddit = praw.Reddit(client_id='cZ5kksi-eSfL7A', client_secret='6pTEpAxxxZV_6_7XOUYTkh4LyOg',
  28. username='S1r1usBl4ck', password='spidyweb', user_agent='script')
  29.  
  30. submission_details={}
  31. author_details={}
  32. india = reddit.subreddit('india')
  33.  
  34. # Limits
  35. max_posts_to_download = 1000
  36. max_submissions_to_download = 1000
  37. max_comments_to_download = 1000
  38.  
  39. def add_author_details(author):
  40. author_name = author.name
  41. if not author_details.has_key(author_name):
  42. print 'Getting details about', author_name
  43. try:
  44. author_details[author_name] = {}
  45. author_details[author_name]['link_karma'] = author.link_karma
  46. author_details[author_name]['comment_karma'] = author.comment_karma
  47. author_details[author_name]['upvotes'] = 0
  48. author_details[author_name]['downvotes'] = 0
  49. author_details[author_name]['score'] = 0
  50. author_details[author_name]['comment_upvotes'] = 0
  51. author_details[author_name]['comment_downvotes'] = 0
  52. author_details[author_name]['comment_score'] = 0
  53. user_since = datetime.utcfromtimestamp(author.created_utc)
  54. author_details[author_name]['user_since'] = user_since
  55. except:
  56. print '\tFailed to get information about author', author_name
  57.  
  58. for submission in india.hot(limit=max_posts_to_download):
  59. try:
  60. if submission.link_flair_text=='Demonetization':
  61. print 'Found a demonetization related article'
  62. author_name = submission.author.name
  63. if not submission_details.has_key(author_name):
  64. print '\tFound new author', author_name
  65. submission_details[author_name] = []
  66. add_author_details(submission.author)
  67. submission_details[author_name].append({
  68. 'title': submission.title,
  69. 'score': submission.score,
  70. 'ups': submission.ups,
  71. 'downs': submission.downs,
  72. 'created': submission.created_utc
  73. })
  74.  
  75. print '\tRetrieving all the comments from the submission'
  76. try:
  77. for comment in submission.comments.list():
  78. if hasattr(comment, 'author') and comment.author != None:
  79. add_author_details(comment.author)
  80. author_details[author_name]['comment_upvotes'] += comment.ups
  81. author_details[author_name]['comment_downvotes'] += comment.downs
  82. author_details[author_name]['comment_score'] += comment.score
  83. except:
  84. print '\tFailed to get comments for submission'
  85. author_details[author_name]['upvotes'] += submission.ups
  86. author_details[author_name]['downvotes'] += submission.downs
  87. author_details[author_name]['score'] += submission.score
  88. except:
  89. print 'Failed to get information about submission'
  90.  
  91. for a in author_details:
  92. pprint.pprint(a)
  93. pprint.pprint(author_details[a], indent=4)
  94. #for s in submission_details[a]:
  95. # print '\t', s
  96. pdb.set_trace()
  97.  
  98. # end-of-script
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement