Advertisement
Guest User

Huragrammar v1

a guest
Mar 6th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | None | 0 0
  1. import getpass
  2. import praw, os
  3. from threading import Thread
  4. from time import sleep
  5. from collections import deque
  6. import praw
  7.  
  8. USER_AGENT = 'Huragrammar' #lol
  9. usr = praw.Reddit(user_agent=USER_AGENT)
  10.  
  11.  
  12. class Database:
  13. def __init__(self, grammar_mappings):
  14. self.word_map = {}
  15. self.checked_comments = deque(maxlen=250)
  16.  
  17. with open(grammar_mappings, 'r') as mappings:
  18. for line in mappings:
  19. if '$' in line[0]:
  20. continue
  21. delimeter = line.index('~')
  22. word = line[0: delimeter]
  23. self.word_map[word] = line[delimeter + 1:].strip()
  24.  
  25.  
  26. db = Database('db\\grammar_mappings.txt')
  27.  
  28.  
  29. from time import strftime
  30.  
  31. log_file = 'log/' + strftime('%m%d')
  32. msg_file = 'msg/' + strftime('%m%d')
  33.  
  34. log_header = '%I:%M:%S '
  35. print_header = '%I:%M:%S '
  36.  
  37.  
  38. def file_setting(path, attribute):
  39. with open(path, 'r') as data_file:
  40. for line in data_file:
  41. if '$' in line:
  42. continue
  43. if attribute in line:
  44. param = line[line.index('=') + 1:].strip()
  45. if param.isdigit():
  46. return int(param)
  47. else:
  48. return param
  49.  
  50.  
  51. def code(text):
  52. message = ''
  53. for line in text.splitlines():
  54. message += ' ' + line + '\n'
  55. return message
  56.  
  57.  
  58. def bold(comment, words):
  59. quote = '\n'
  60. for line in comment.body.splitlines():
  61. new_line = line.replace('*', '')
  62. for word in words:
  63. new_line = new_line.replace(word, ' **' + word.strip() + '** ')
  64. quote += '> ' + new_line + '\n'
  65. return quote + ' \n'
  66.  
  67.  
  68. def log(s, id='thread-0'):
  69. msg = strftime(log_header) + 'Bot_' + id.lower().strip() + ': ' + s
  70. with open(log_file + 'log.txt', 'a') as l:
  71. l.write(msg + '\n')
  72. print(msg)
  73. return msg
  74.  
  75.  
  76. def log_message(s, author, id='thread-0'):
  77. msg = strftime(log_header) + 'Bot_' + id.lower().strip() + ': '
  78. print(msg + 'Message from ' + author.upper() + ': ' + s)
  79. with open(msg_file + 'msg.txt', 'a') as msg:
  80. msg.write('Message sent by ' + author.upper() + 'at ' + strftime(print_header) + '\n')
  81. msg.write(
  82. s + '\n----------------------------------------------------------------------------------------------------------------------------------\n')
  83.  
  84.  
  85.  
  86.  
  87. class Messages:
  88. start = 'ImprovedGrammarBot has detected a misspelling or incorrect use of grammar in your comment. \n\n'
  89.  
  90. signature = ('[FAQ](https://github.com/gabrielxh/ImprovedGrammarBot/wiki/FAQ)'
  91. ' | [Code](https://github.com/gabrielxh/ImprovedGrammarBot)'
  92. ' | [Hate Mail](https://github.com/gabrielxh/ImprovedGrammarBot/tree/master/ImprovedGrammarBot/msg) \n')
  93.  
  94. finish = ('\nComments with a negative score will be deleted. The author may reply with +/u/ImprovedGrammarBot'
  95. '-delete to remove this post and -ignore to be placed on the ignore list. ' + signature)
  96.  
  97. def login():
  98. global user_name
  99. user_name = input('Username: ')
  100. user_password = input('Password: ')
  101. try:
  102. print('Loging In...')
  103. usr.login(username=user_name,password=user_password)
  104. print('Logged In.')
  105. return 0
  106. except Exception:
  107. print("Error Logging in. check username and password")
  108. return 1
  109.  
  110.  
  111. def Downvote(userN):
  112. usr2 = usr.get_redditor(userN)
  113. print('Grammar Fixing ',userN,'.')
  114. limit= int(input('How many Comments? (10 recom..: )'))
  115. print('Requesting user comments....')
  116. generator = usr2.get_comments(sort='new',time='all',limit=limit, subreddit = target_sub)
  117. print('Correcting user comments....')
  118.  
  119. def __init__(self, reddit_connection, database):
  120. Thread.__init__(self)
  121. self.r = reddit_connection
  122. self.db = database
  123. for comment in generator:
  124. if comment.id not in self.db.checked_comments:
  125. self.db.checked_comments.append(c.id)
  126. message = ''
  127. mistakes = []
  128. # Get all the lower case words and sort them alphabetically
  129. comment_words = sorted([word for word in comment.body.split() if word.islower()])
  130. for mistake in self.db.word_map:
  131. for word in comment_words:
  132. if word > mistake:
  133. break
  134. if word == mistake:
  135. message += '- You wrote ' + word + ' which should have been *' + self.db.word_map[word] + '* \n\n'
  136. mistakes.append(mistake)
  137.  
  138. if len(mistakes) > 0:
  139. quote = '\n'
  140. reply = Messages.start + bold(comment, mistakes) + message + Messages.finish
  141. self.reply_correction(comment, reply)
  142.  
  143. print('Reddit Targeted Grammar Bot v2')
  144. print('/u/njmksr')
  145. #open to anyone
  146. print('---Reddit Login----')
  147. while login():
  148. continue
  149. userN = input('Who do you want to correct? ')
  150. target_sub = input('Subreddit: ')
  151. Downvote(userN)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement