Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import getpass
- import praw, os
- from threading import Thread
- from time import sleep
- from collections import deque
- import praw
- USER_AGENT = 'Huragrammar' #lol
- usr = praw.Reddit(user_agent=USER_AGENT)
- class Database:
- def __init__(self, grammar_mappings):
- self.word_map = {}
- self.checked_comments = deque(maxlen=250)
- with open(grammar_mappings, 'r') as mappings:
- for line in mappings:
- if '$' in line[0]:
- continue
- delimeter = line.index('~')
- word = line[0: delimeter]
- self.word_map[word] = line[delimeter + 1:].strip()
- db = Database('db\\grammar_mappings.txt')
- from time import strftime
- log_file = 'log/' + strftime('%m%d')
- msg_file = 'msg/' + strftime('%m%d')
- log_header = '%I:%M:%S '
- print_header = '%I:%M:%S '
- def file_setting(path, attribute):
- with open(path, 'r') as data_file:
- for line in data_file:
- if '$' in line:
- continue
- if attribute in line:
- param = line[line.index('=') + 1:].strip()
- if param.isdigit():
- return int(param)
- else:
- return param
- def code(text):
- message = ''
- for line in text.splitlines():
- message += ' ' + line + '\n'
- return message
- def bold(comment, words):
- quote = '\n'
- for line in comment.body.splitlines():
- new_line = line.replace('*', '')
- for word in words:
- new_line = new_line.replace(word, ' **' + word.strip() + '** ')
- quote += '> ' + new_line + '\n'
- return quote + ' \n'
- def log(s, id='thread-0'):
- msg = strftime(log_header) + 'Bot_' + id.lower().strip() + ': ' + s
- with open(log_file + 'log.txt', 'a') as l:
- l.write(msg + '\n')
- print(msg)
- return msg
- def log_message(s, author, id='thread-0'):
- msg = strftime(log_header) + 'Bot_' + id.lower().strip() + ': '
- print(msg + 'Message from ' + author.upper() + ': ' + s)
- with open(msg_file + 'msg.txt', 'a') as msg:
- msg.write('Message sent by ' + author.upper() + 'at ' + strftime(print_header) + '\n')
- msg.write(
- s + '\n----------------------------------------------------------------------------------------------------------------------------------\n')
- class Messages:
- start = 'ImprovedGrammarBot has detected a misspelling or incorrect use of grammar in your comment. \n\n'
- signature = ('[FAQ](https://github.com/gabrielxh/ImprovedGrammarBot/wiki/FAQ)'
- ' | [Code](https://github.com/gabrielxh/ImprovedGrammarBot)'
- ' | [Hate Mail](https://github.com/gabrielxh/ImprovedGrammarBot/tree/master/ImprovedGrammarBot/msg) \n')
- finish = ('\nComments with a negative score will be deleted. The author may reply with +/u/ImprovedGrammarBot'
- '-delete to remove this post and -ignore to be placed on the ignore list. ' + signature)
- def login():
- global user_name
- user_name = input('Username: ')
- user_password = input('Password: ')
- try:
- print('Loging In...')
- usr.login(username=user_name,password=user_password)
- print('Logged In.')
- return 0
- except Exception:
- print("Error Logging in. check username and password")
- return 1
- def Downvote(userN):
- usr2 = usr.get_redditor(userN)
- print('Grammar Fixing ',userN,'.')
- limit= int(input('How many Comments? (10 recom..: )'))
- print('Requesting user comments....')
- generator = usr2.get_comments(sort='new',time='all',limit=limit, subreddit = target_sub)
- print('Correcting user comments....')
- def __init__(self, reddit_connection, database):
- Thread.__init__(self)
- self.r = reddit_connection
- self.db = database
- for comment in generator:
- if comment.id not in self.db.checked_comments:
- self.db.checked_comments.append(c.id)
- message = ''
- mistakes = []
- # Get all the lower case words and sort them alphabetically
- comment_words = sorted([word for word in comment.body.split() if word.islower()])
- for mistake in self.db.word_map:
- for word in comment_words:
- if word > mistake:
- break
- if word == mistake:
- message += '- You wrote ' + word + ' which should have been *' + self.db.word_map[word] + '* \n\n'
- mistakes.append(mistake)
- if len(mistakes) > 0:
- quote = '\n'
- reply = Messages.start + bold(comment, mistakes) + message + Messages.finish
- self.reply_correction(comment, reply)
- print('Reddit Targeted Grammar Bot v2')
- print('/u/njmksr')
- #open to anyone
- print('---Reddit Login----')
- while login():
- continue
- userN = input('Who do you want to correct? ')
- target_sub = input('Subreddit: ')
- Downvote(userN)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement