from argParseLog import addLoggingArgs, handleLoggingArgs log = logging.getLogger(__name__) if __name__ == '__main__': ap = argparse.ArgumentParser() ap.add_argument('-d', '--database', help='The database to read.', default='boardgames.db') ap.add_argument('-u', '--unit', help='List post count by time ago given.', choices=['second', 'minute', 'hour', 'day', 'week', 'month', 'year']) ap.add_argument('-v', '--value', help='How many of the unit to have. i.e. -v 1 -u \'day\'' ' is one day ago.', type=int) ap.add_argument('-c', '--count', help='Number of results to return.', type=int) addLoggingArgs(ap) args = ap.parse_args() handleLoggingArgs(args) db = sqlite3.connect(args.database) # select count(*) from comments where datetime(timestamp, 'unixepoch') > datetime('now', '-1 hour'); then = db.execute("SELECT datetime('now', '-{} {}')".format(args.value, args.unit)).fetchone()[0] rows = db.execute("SELECT cid,username,datetime(timestamp, 'unixepoch') FROM comments WHERE " "datetime(timestamp, 'unixepoch') > datetime('now', '-{} {}')".format( args.value, args.unit)).fetchall() ess = 's' if args.value > 1 else '' print('Found {} comments from the last {} {}{}. Comments newer than {}'.format( len(rows), args.value, args.unit, ess, then)) print('Oldest comment: {}'.format(rows[0][2])) # there is probably an SQL way to do this much much better.
def start_bot(): ap = argparse.ArgumentParser() botname = 'r2d8' dbname = '{}-bot.db'.format(botname) sleepTime = 5 ap.add_argument( '-d', '--database', help='The bot database. Default is {}'.format(dbname), default='{}'.format(dbname)) ap.add_argument( '-r', '--read', help='Mark all existing queries as read without responding, then exit.', action='store_true', # default is false, flag present => true dest='mark_read') ap.add_argument( '-s', '--sleep', help='Time to sleep between API checks. Default is {} seconds.'.format(sleepTime), default=sleepTime, type=int) ap.add_argument( '-o', '--once', help='Run once and quit', action='store_true') ap.add_argument( '-t', '--target', help='Run the tool on a specific comment ID target', default=None) ap.add_argument( '--command', help='When run in target mode, overrides/simulates a specific bot command', default=None) ap.add_argument( '-c', '--config', help='Config file location for PRAW', default=None) ap.add_argument( '-f', '--footer', help='Custom footer to append to the message', default='') addLoggingArgs(ap) args = ap.parse_args() handleLoggingArgs(args) dbname = args.database if args.database else dbname sleepTime = args.sleep if args.sleep else sleepTime hp = HTMLParser() # quiet requests logging.getLogger("requests").setLevel(logging.WARNING) logging.getLogger("prawcore").setLevel(logging.WARNING) logging.getLogger("urllib3").setLevel(logging.WARNING) reddit = oauth_login(config_file_path = args.config) if args.config else oauth_login() bdb = BotDatabase(args.database) log.info('Bot database opened/created.') ch = CommentHandler(botname, bdb) log.info('Comment/notification handler created.') CMDMAP = { 'getinfo': ch.getInfo, 'repair': ch.repairComment, 'xyzzy': ch.xyzzy, 'alias': ch.alias, 'getaliases': ch.getaliases, 'getparentinfo': ch.getParentInfo, 'getinfoparent': ch.getParentInfo, # 'getthreadinfo': ch.getThreadInfo, 'expandurls': ch.expandURLs, 'tryagain': ch.removalRequest, 'shame': ch.removalRequest } BOTCMD_REGEX = re.compile('/?u/{}\s(\w+)((?:\s\w+)*)'.format(botname), re.IGNORECASE) CONFIG = { 'footer': args.footer } # target is like once, but we don't even need to scan for mentions if args.target: log.info('Executing specific target') comment = reddit.comment(args.target) if not bdb.comment_exists(comment): bdb.add_comment(comment) if args.mark_read: return commands = [args.command.split()] if args.command else BOTCMD_REGEX.findall(comment.body) for cmd in commands: primaryCommand = cmd[0].lower() if primaryCommand in CMDMAP: comment.body = hp.unescape(comment.body) CMDMAP[primaryCommand]( comment, subcommands=cmd[1].split(), config=CONFIG) else: log.info('Got unknown command: {}'.format(primaryCommand)) return log.info('Waiting for new PMs and/or notifications.') while True: try: for comment in list(reddit.inbox.mentions()) + list(reddit.inbox.unread()): log.debug('got {}'.format(comment.id)) if not bdb.comment_exists(comment): bdb.add_comment(comment) if args.mark_read: continue for cmd in BOTCMD_REGEX.findall(comment.body): primaryCommand = cmd[0].lower() if primaryCommand in CMDMAP: comment.body = hp.unescape(comment.body) CMDMAP[primaryCommand]( comment, subcommands=cmd[1].split(), config=CONFIG) else: log.info('Got unknown command: {}'.format(primaryCommand)) except Exception as e: log.error('Caught exception: {}'.format(e)) # get_mentions is non-blocking if args.mark_read or args.once: exit(0) sleep(sleepTime)
def run_bot(): ap = argparse.ArgumentParser() botname = 'r2d8' dbname = '{}-bot.db'.format(botname) ap.add_argument(u'-d', u'--database', help=u'The bot database. Default is {}'.format(dbname), default=u'{}'.format(dbname)) ap.add_argument( '-r', '--read', help='Mark all existing queries as read without responding, then exit.', default=False, action='store_true', dest="mark_read") addLoggingArgs(ap) args = ap.parse_args() handleLoggingArgs(args) dbname = args.database if args.database else dbname hp = HTMLParser() # quiet requests logging.getLogger(u"requests").setLevel(logging.WARNING) logging.getLogger(u"prawcore").setLevel(logging.WARNING) logging.getLogger(u"urllib3").setLevel(logging.WARNING) reddit = oauth_login() bdb = BotDatabase(args.database) log.info(u'Bot database opened/created.') ch = CommentHandler(botname, bdb) log.info(u'Comment/notification handler created.') botcmds = re.compile(u'/?u/{}\s(\w+)'.format(botname), re.IGNORECASE) cmdmap = { u'getinfo': ch.getInfo, u'repair': ch.repairComment, u'xyzzy': ch.xyzzy, u'alias': ch.alias, u'getaliases': ch.getaliases, u'getparentinfo': ch.getParentInfo, u'getinfoparent': ch.getParentInfo } log.info(u'Waiting for new PMs and/or notifications.') while True: try: for comment in list(reddit.inbox.mentions()) + list( reddit.inbox.unread()): log.debug(u'got {}'.format(comment.id)) if not bdb.comment_exists(comment): bdb.add_comment(comment) if args.mark_read: continue for cmd in [ c.lower() for c in botcmds.findall(comment.body) ]: if cmd in cmdmap: comment.body = hp.unescape(comment.body) cmdmap[cmd](comment) else: log.info(u'Got unknown command: {}'.format(cmd)) except Exception as e: log.error(u'Caught exception: {}'.format(e)) # get_mentions is non-blocking if args.mark_read: exit(0) sleep(5)