Esempio n. 1
0
def archive_subscriber_matches():
    subscriptions = Subscription.get_valid_subscriptions()

    print "Found {} valid subscribers".format(len(subscriptions))
    for subscription in subscriptions:
        webapi_params = {
            "account_id": subscription.user_id,
            "date_min": None,
            "matches_requested": 100  # 100 Max
        }

        latest_match = SubscriptionLastMatch.query.\
            filter(SubscriptionLastMatch.user_id == subscription.user_id,
                   SubscriptionLastMatch.replay_found == True).\
            order_by(SubscriptionLastMatch.created_at.desc()).\
            first()

        if latest_match:
            webapi_params["date_min"] = latest_match.created_at_timestamp
        else:
            webapi_params["date_min"] = subscription.created_at_timestamp

        matches = steam.api.interface("IDOTA2Match_570").GetMatchHistory(
            **webapi_params).get("result")

        # Log this match check, as well as whether or not we found a match.
        last_match_log = SubscriptionLastMatch(subscription.user_id,
                                               len(matches.get("matches")) > 0)
        db.session.add(last_match_log)
        db.session.commit()

        print "Found {} matches for {}".format(len(matches.get("matches")),
                                               subscription.user_id)
        for match in matches.get("matches"):
            replay_exists = Replay.query.filter(
                Replay.id == match["match_id"]).count() > 0

            if not replay_exists:
                replay = Replay(match["match_id"])
                db.session.add(replay)
                db.session.commit()

                Replay.add_gc_job(replay)

                print "Added {} to database and job queue".format(
                    match["match_id"])
            else:
                print "Match {} already in database, skipping.".format(
                    match["match_id"])
def process_match_list(matches):
    """ Iterates through a list ofmatches and checks whether we already have them in our database. If we do not then
    this code will add the match to our database and create an associated GC job. """
    if len(matches) > 0:
        for match in matches:
            replay_exists = Replay.query.filter(
                Replay.id == match["match_id"]).count() > 0

            if not replay_exists:
                replay = Replay(match["match_id"])
                db.session.add(replay)
                db.session.commit()

                Replay.add_gc_job(replay)

                print "Added {} to database and job queue".format(
                    match["match_id"])
            else:
                print "Match {} already in database, skipping.".format(
                    match["match_id"])