Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. import praw
  2. import re
  3. from collections import defaultdict
  4.  
  5. def main():
  6.  
  7. reddit = praw.Reddit(client_id= '',
  8. client_secret= '',
  9. password= '',
  10. username= '',
  11. user_agent= 'TowerOfTrial_bot')
  12.  
  13. subreddit = reddit.subreddit('endlessfrontier')
  14. for submission in subreddit.stream.submissions():
  15. if "tower of trial" in submission.title.lower():
  16. if submission.author.name.lower() == 'darthreborn':
  17. solutions = solution_check(submission.comments)
  18. old_solutions = old_solution_check(submission.selftext)
  19. post = merge_solutions(solutions, old_solutions)
  20. submission.edit(post)
  21.  
  22.  
  23. def merge_solutions(solutions, old_solutions):
  24. body = '''Hello All! I will be hosting Server 1 ToT.
  25. Please post below and lets have a great ToT! I will update this post at least every 8 hours, maybe more often as i have time so long as new solutions are posted. I will stop updating the list after each entry has at least one solution. Thank you all for your support!
  26. New to the tower? Click here.
  27. Not sure about an abbreviation? Check here!
  28. Have ideas on how to make this more efficient / useful? Msg /u/DarthReborn
  29. Early bird? No Solutions yet? Check here for a list of premade solutions to known problems.
  30. Still don't see anything useful? Ask someone from the official discord in channel #s1_toweroftrial for help. You must assign yourself to server 1 in #role_commands to see the channel. Contact @RebornsNavy for assistance.
  31. Want to help streamline this process and can code? Msg /u/DarthReborn so we can collaborate on a useful solution!\n\n'''
  32.  
  33. for k, v in solutions.items():
  34. try:
  35. old_line = old_solutions[k]
  36. if v not in old_line:
  37. string = '\n\n '.join(v)
  38. temp = str(k) + ":" + str(string) +'\n\n'
  39. body += temp
  40.  
  41. except KeyError:
  42. print key + " is not valid"
  43.  
  44. return body
  45.  
  46. def old_solution_check(body):
  47. lst = []
  48. solution = re.split(':|\n\n', body)
  49. for n in range(0, len(solution), 2):
  50. if len(solution)%2 is 1:
  51. continue
  52. level = num(solution[n].strip())
  53. if level > 0:
  54. line = solution[n+1]
  55. lst.append((level,line))
  56.  
  57. old_solutions = defaultdict(list)
  58. for k, v in lst:
  59. old_solutions[k].append(v)
  60.  
  61. return old_solutions
  62.  
  63.  
  64. def solution_check(comments):
  65. lst = []
  66.  
  67. for comment in comments:
  68. solution = re.split(':|\n', comment.body)
  69. solution = filter(lambda s: s.strip(), solution)
  70.  
  71. if len(solution)%2 is 1:
  72. continue
  73. for n in range(0, len(solution), 2):
  74. level = num(solution[n].strip())
  75. if level > 0:
  76. line = solution[n+1]
  77. lst.append((level,line))
  78.  
  79. solutions = defaultdict(list)
  80. for k, v in lst:
  81. solutions[k].append(v)
  82.  
  83. return solutions
  84.  
  85.  
  86. def num(s):
  87. try:
  88. return int(s)
  89. except ValueError:
  90. return -1
  91.  
  92.  
  93. if __name__ == '__main__':
  94. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement