Advertisement
Guest User

Untitled

a guest
May 16th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. # The MIT License (MIT)
  2. # Copyright (c) 2016 roignac@gmail.com
  3. # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  4. # associated documentation files (the "Software"), to deal in the Software without restriction,
  5. # including without limitation the rights to use, copy, modify, merge, publish, distribute,
  6. # sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
  7. # furnished to do so, subject to the following conditions:
  8. # The above copyright notice and this permission notice shall be included in all copies or
  9. # substantial portions of the Software.
  10. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  11. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  12. # AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  13. # DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  14. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  15.  
  16. import praw
  17. import re
  18. import argparse
  19. from datetime import datetime
  20. from collections import Counter
  21.  
  22. USER_AGENT = "Vox Reddi 0.2"
  23. VOTE_REGEXP = re.compile('\s*\+(\w+)')
  24. MINIMUM_REGISTERED_TIME_IN_DAYS = 30
  25.  
  26.  
  27. class UnparsableComment(Exception):
  28. def __init__(self, comment):
  29. self.comment_id = comment.id
  30. self.author = comment.author
  31.  
  32. def __repr__(self):
  33. return u"Cannot parse comment id %s by %s" % (self.comment_id, self.author)
  34.  
  35.  
  36. class VoteException(Exception):
  37. def __init__(self, option, voter, message):
  38. self.option = option
  39. self.voter = voter
  40. self.message = message
  41.  
  42. def __repr__(self):
  43. return u"Ignoring vote by %s for '%s' - %s" % (self.voter, self.option, self.message)
  44.  
  45.  
  46. def parse_comment(comment, voters):
  47. comment_body = comment.body
  48. match = re.match(VOTE_REGEXP, comment_body)
  49.  
  50. if not match:
  51. raise UnparsableComment(comment)
  52.  
  53. option = match.group(1)
  54. voter = comment.author
  55. if voter is None:
  56. raise VoteException(voter, option, "account was removed")
  57.  
  58. voter_created_date = datetime.fromtimestamp(voter.created_utc)
  59. datediff = datetime.utcnow() - voter_created_date
  60. if datediff.days < MINIMUM_REGISTERED_TIME_IN_DAYS:
  61. raise VoteException(voter, option, "registered %s days ago" % datediff.days)
  62.  
  63. if voter in voters:
  64. raise VoteException(voter, option, "has already voted")
  65.  
  66. if comment.edited:
  67. raise VoteException(voter, option, "comment has been edited")
  68.  
  69. return (option, voter)
  70.  
  71.  
  72. def parse_votes_for_post(submission):
  73. options = []
  74. voters = []
  75.  
  76. print("Counting votes in %s" % submission.id)
  77. top_level_comments = submission.comments
  78. for comment in top_level_comments:
  79. try:
  80. (option, voter) = parse_comment(comment, voters)
  81. options.append(option)
  82. voters.append(voter)
  83. print("Recorded vote for '%s' by %s, commentid %s" % (option, voter, comment.id))
  84. except (VoteException, UnparsableComment) as e:
  85. print(repr(e))
  86.  
  87. return (voters, Counter(options))
  88.  
  89.  
  90. if __name__ == '__main__':
  91. parser = argparse.ArgumentParser()
  92. parser.add_argument("--submission", required=True)
  93. args = parser.parse_args()
  94. submission_id = args.submission
  95.  
  96. r = praw.Reddit(user_agent=(USER_AGENT))
  97. submission = r.get_submission(submission_id=submission_id)
  98. announcement_date = datetime.fromtimestamp(submission.created_utc)
  99. td = datetime.utcnow() - announcement_date
  100. print("%s days %s hours %s minutes since last edit\n" %
  101. (td.days, td.seconds//3600, (td.seconds//60) % 60))
  102.  
  103. (voters, vote_results) = parse_votes_for_post(submission)
  104. print('\n----')
  105. print("Voters:\n%s" % [v.name for v in voters])
  106. print("Total voted: %s" % len(voters))
  107. print("Vote results:\n%s" % vote_results)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement