コード例 #1
0
def check_investments():

    if (debug == 1):
        return
    
    while True:    
        if (len(awaiting) > 0):
            investment = awaiting[0]
            investor_id = investment.get_name()
            investor = users[investor_id]
            post = investment.get_ID()
            upvotes = reddit.submission(post).ups
            
            commentID = investment.get_comment()
            comment = reddit.comment(id=commentID)
            
            donep = investment.check()
            if (donep):
                win = investor.calculate(investment, upvotes)
                if (win > 0):
                    comment.edit(message.modify_invest_return(comment.body, win))
                else:
                    comment.edit(message.modify_invest_lose(comment.body))
                    
                done.append(awaiting.pop(0))
                print("Investment returned!")
コード例 #2
0
ファイル: main.py プロジェクト: SathusanK/memeinvestor_bot
def check_investments():

    print("Starting checking investments...")
    while True:
        time.sleep(60)
        done_ids = database.investment_find_done()
        for id_number in done_ids:
            # I know that everything can be compacted in a tuple
            # This way it is more understandable
            name = database.investment_get_name(id_number)
            postID = database.investment_get_post(id_number)
            upvotes_old = database.investment_get_upvotes(id_number)
            amount = database.investment_get_amount(id_number)
            responseID = database.investment_get_response(id_number)
            response = reddit.comment(id=responseID)

            # If comment is deleted, skip it
            try:
                commentID = database.investment_get_comment(id_number)
                comment = reddit.comment(id=commentID)
            except:
                response.edit(message.deleted_comment_org)
                continue

            post = reddit.submission(postID)
            upvotes_now = post.ups

            # Updating the investor's balance
            factor = calculate(upvotes_now, upvotes_old)
            balance = database.investor_get_balance(name)
            new_balance = int(balance + (amount * factor))
            database.investor_update_balance(name, new_balance)
            change = new_balance - balance

            # Updating the investor's variables
            active = database.investor_get_active(name)
            active -= 1
            database.investor_update_active(name, active)

            completed = database.investor_get_completed(name)
            completed += 1
            database.investor_update_completed(name, completed)

            # Marking the investment as done
            database.investment_update_done(id_number)

            # Editing the comment as a confirmation
            text = response.body
            if (factor > 1):
                response.edit(message.modify_invest_return(text, change))
                database.investment_update_success(id_number)
            else:
                lost_memes = int(amount - (amount * factor))
                response.edit(message.modify_invest_lose(text, lost_memes))

            print(f"Investment returned! {change}")
コード例 #3
0
def main():
    engine = create_engine(config.db)
    sm = 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)

    logging.info("Starting checking investments...")
    praw.models.Comment.edit_wrap = edit_wrap

    while True:
        sess = sm()
        then = int(time.time()) - 14400
        q = sess.query(Investment).filter(Investment.done == 0).filter(
            Investment.time < then)
        for investment in q.limit(10).all():
            investor_q = sess.query(Investor).filter(
                Investor.name == investment.name)
            investor = investor_q.first()

            if not investor:
                continue

            if investment.response != "0":
                response = reddit.comment(id=investment.response)
            else:
                response = EmptyResponse()

            # If comment is deleted, skip it
            try:
                reddit.comment(id=investment.comment)
            except:
                response.edit(message.deleted_comment_org)
                continue

            post = reddit.submission(investment.post)
            upvotes_now = post.ups

            # Updating the investor's balance
            factor = calculate(upvotes_now, investment.upvotes)
            amount = investment.amount
            balance = investor.balance

            new_balance = int(balance + (amount * factor))
            change = new_balance - balance

            # Updating the investor's variables
            update = {
                Investor.completed: investor.completed + 1,
                Investor.balance: new_balance,
            }
            investor_q.update(update, synchronize_session=False)

            # Editing the comment as a confirmation
            text = response.body
            if change > 0:
                logging.info("%s won %d" % (investor.name, change))
                response.edit_wrap(message.modify_invest_return(text, change))
            elif change == 0:
                logging.info("%s broke even and got back %d" %
                             (investor.name, change))
                response.edit_wrap(
                    message.modify_invest_break_even(text, change))
            else:
                lost_memes = int(amount - change)
                logging.info("%s lost %d" % (investor.name, lost_memes))
                response.edit_wrap(message.modify_invest_lose(
                    text, lost_memes))

            sess.query(Investment).\
                filter(Investment.id == investment.id).\
                update({
                    Investment.success: change > 0,
                    Investment.done: True
                }, synchronize_session=False)
            sess.commit()

        sess.close()
コード例 #4
0
def main():
    logging.info("Starting calculator")

    killhandler = KillHandler()

    engine = create_engine(config.db, pool_recycle=60)
    sm = 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)

    praw.models.Comment.edit_wrap = edit_wrap

    stopwatch = Stopwatch()

    logging.info("Monitoring active investments...")

    while not killhandler.killed:
        try:
            sess = sm()

            then = int(time.time()) - config.investment_duration
            investment = sess.query(Investment).\
                filter(Investment.done == 0).\
                filter(Investment.time < then).\
                order_by(Investment.time.asc()).\
                first()

            if not investment:
                # Nothing matured yet; wait a bit before trying again
                time.sleep(5)
                continue

            duration = stopwatch.measure()

            investor = sess.query(Investor).filter(
                Investor.name == investment.name).one()

            logging.info(f"New mature investment: {investment.comment}")
            logging.info(f" -- by {investor.name}")

            if investment.response != "0":
                response = reddit.comment(id=investment.response)
            else:
                response = EmptyResponse()

            post = reddit.submission(investment.post)
            upvotes_now = post.ups  # <--- triggers a Reddit API call
            investment.final_upvotes = upvotes_now

            # Updating the investor's balance
            factor = formula.calculate(upvotes_now, investment.upvotes)
            amount = investment.amount
            balance = investor.balance

            new_balance = int(balance + (amount * factor))
            change = new_balance - balance
            profit = change - amount
            profit_str = f"{int((profit/amount)*100)}%"

            # Updating the investor's variables
            investor.completed += 1

            if new_balance < BalanceCap:
                investor.balance = new_balance

                # Editing the comment as a confirmation
                text = response.body  # <--- triggers a Reddit API call
                if profit > 0:
                    logging.info(f" -- profited {profit}")
                    response.edit_wrap(
                        message.modify_invest_return(text, upvotes_now, change,
                                                     profit_str, new_balance))
                elif profit == 0:
                    logging.info(f" -- broke even")
                    response.edit_wrap(
                        message.modify_invest_break_even(
                            text, upvotes_now, change, profit_str,
                            new_balance))
                else:
                    lost_memes = int(amount - change)
                    logging.info(f" -- lost {profit}")
                    response.edit_wrap(
                        message.modify_invest_lose(text, upvotes_now,
                                                   lost_memes, profit_str,
                                                   new_balance))
            else:
                # This investment pushed the investor's balance over the cap
                investor.balance = BalanceCap

                # Editing the comment as a confirmation
                text = response.body  # <--- triggers a Reddit API call
                logging.info(f" -- profited {profit} but got capped")
                response.edit_wrap(
                    message.modify_invest_capped(text, upvotes_now, change,
                                                 profit_str, BalanceCap))

            investment.success = (profit > 0)
            investment.profit = profit
            investment.done = True

            sess.commit()

            # Measure how long processing took
            duration = stopwatch.measure()
            logging.info(f" -- processed in {duration:5.2f}s")

            # Report the Reddit API call stats
            rem = int(reddit.auth.limits['remaining'])
            res = int(reddit.auth.limits['reset_timestamp'] - time.time())
            logging.info(
                f" -- API calls remaining: {rem:3d}, resetting in {res:3d}s")
        except Exception as e:
            logging.error(e)
            traceback.print_exc()
            time.sleep(10)
        finally:
            sess.close()