Exemplo n.º 1
0
 def test_base(self):
     try:
         self.submitter.main()
     except DoneException:
         pass
     submission = self.reddit.subreddit().stream.submissions()[0]
     replies = submission.replies
     self.assertEqual(len(replies), 1)
     self.assertEqual(replies[0].body,
                      message.invest_no_fee('u/' + submission.author.name))
Exemplo n.º 2
0
def post_reply(submission):
    # We don't need to post a sticky on stickied posts
    if submission.stickied:
        logging.info(" -- skipping (stickied)")
        return
    if submission.distinguished:
        logging.info(" -- skipping (distinguished)")
        return

    # Post a comment to let people know where to invest
    if config.CLOSED:
        bot_reply = submission.reply_wrap(message.CLOSED_ORG)
    else:
        bot_reply = submission.reply_wrap(message.invest_no_fee(f"u/{submission.author.name}"))

    # Sticky the comment
    if config.IS_MODERATOR:
        bot_reply.mod.distinguish(how="yes", sticky=True)
        bot_reply.mod.approve()
        if config.CLOSED:
            bot_reply.mod.lock()
            bot_reply = None
    logging.info(" -- Reply %s", bot_reply)
    return bot_reply
Exemplo n.º 3
0
 def test_base(self):
     self.submitter.main()
     submission = self.reddit.subreddit().stream.submissions()[0]
     replies = submission.replies
     self.assertEqual(len(replies), 1)
     self.assertEqual(replies[0].body, message.invest_no_fee("u/" + submission.author.name))
Exemplo n.º 4
0
def main():
    """
    This is the main function that listens to new submissions
    and then posts the ATTENTION sticky comment.
    """
    logging.info("Starting submitter...")
    logging.info(
        "Sleeping for 8 seconds. Waiting for the database to turn on...")
    time.sleep(8)

    killhandler = KillHandler()

    engine = create_engine(config.DB, pool_recycle=60, pool_pre_ping=True)
    sess_maker = scoped_session(sessionmaker(bind=engine))

    reddit = praw.Reddit(client_id=config.CLIENT_ID,
                         client_secret=config.CLIENT_SECRET,
                         username=config.USERNAME,
                         password=config.PASSWORD,
                         user_agent=config.USER_AGENT)

    # We will test our reddit connection here
    if not utils.test_reddit_connection(reddit):
        exit()

    logging.info("Starting checking submissions...")

    stopwatch = Stopwatch()

    sess = sess_maker()
    submission_time = int(time.time())

    minimum_fee = config.SUBMISSION_MIN_FEE

    for submission in reddit.subreddit('+'.join(config.SUBREDDITS)).\
        stream.submissions(skip_existing=True):

        duration = stopwatch.measure()

        logging.info("New submission: %s", submission)
        logging.info(" -- retrieved in %ss", duration)

        # We don't need to post a sticky on stickied posts
        if submission.stickied:
            logging.info(" -- skipping (stickied)")
            continue

        # We are looking if the post is created in the past
        # so we won't double charge it
        if submission.created_utc < submission_time:
            logging.info(" -- skipping (timeout)")
            continue

        submission_time = int(submission.created_utc)
        logging.info(" -- Submission timestamp: %s", \
                     time.asctime(time.gmtime(submission_time)))

        bot_reply = 0
        delete_post = False

        # This is a bit of a controversial update, so im gonna make it
        # agile to switch between different modes
        if not config.SUBMISSION_FEE:
            # Post a comment to let people know where to invest
            bot_reply = submission.reply_wrap(
                message.invest_no_fee(f"u/{submission.author.name}"))
        else:
            # If a poster doesn't have an account, delete the post
            # if he has, take 1000 MemeCoins and invest them
            investor = sess.query(Investor).\
                filter(Investor.name == submission.author.name).\
                first()

            if not investor:
                bot_reply = submission.reply_wrap(message.NO_ACCOUNT_POST_ORG)
                delete_post = True
                logging.info(" -- Not a registered investor!")
            elif investor.balance < minimum_fee:
                bot_reply = submission.reply_wrap(
                    message.modify_pay_to_post(investor.balance))
                delete_post = True
                logging.info(" -- Not enough funds!")
            else:
                # We will make it 6%
                required_fee = int(investor.balance *
                                   (config.SUBMISSION_FEE_PERCENT / 100))
                if required_fee < minimum_fee:
                    required_fee = minimum_fee
                new_balance = investor.balance - required_fee
                investor.balance = new_balance
                bot_reply = submission.\
                    reply_wrap(message.modify_invest_place_here(required_fee,
                                                                f"u/{submission.author.name}"))
                sess.commit()

        # Sticky the comment
        if config.IS_MODERATOR:
            bot_reply.mod.distinguish(how='yes', sticky=True)
            bot_reply.mod.approve()
            if delete_post:
                logging.info(" -- Deleting the post...")
                #Should we hide or just delete the post?
                submission.mod.remove()

        # Measure how long processing took
        duration = stopwatch.measure()
        logging.info(" -- processed in %.2fs", duration)

        if killhandler.killed:
            logging.info("Termination signal received - exiting")
            break