def check_user_first_giveaway(group_webpage, users, addition_date, days_to_create_ga=0, min_ga_time=0, min_entries=1,
                              min_value=0.0, min_num_of_reviews=0, min_score=0,
                              alt_min_value=0.0, alt_min_num_of_reviews=0, alt_min_score=0,
                              alt2_min_game_value=0, alt2_min_steam_num_of_reviews=0, alt2_min_steam_score=0,
                              check_entered_giveaways=False):
    group = MySqlConnector.load_group(group_webpage, load_users_data=False, limit_by_time=True, ends_after_str=addition_date)
    if not group:
        return None
    users_list = users.split(',')
    user_added_time = datetime.datetime.strptime(addition_date, '%Y-%m-%d')
    user_end_time=dict()
    user_first_giveaway=dict()
    succesfully_ended=dict()
    user_no_giveaway=set()
    for group_giveaway in group.group_giveaways.values():
        game_name = group_giveaway.game_name
        user_name = group_giveaway.creator
        if (user_name in users_list
            and
                len(group_giveaway.groups) == 1
            and
                (days_to_create_ga == 0 or group_giveaway.start_time <= user_added_time + datetime.timedelta(days=days_to_create_ga + 1))
            and
                (min_ga_time == 0
                or (min_ga_time > 0 and group_giveaway.end_time - group_giveaway.start_time >= datetime.timedelta(days=min_ga_time - 1)))):
            game_data = MySqlConnector.get_game_data(game_name)
            check_game_data(game_data, game_name)
            if game_is_according_to_requirements(game_data, min_value, min_num_of_reviews, min_score, alt_min_value, alt_min_num_of_reviews, alt_min_score, alt2_min_game_value, alt2_min_steam_num_of_reviews, alt2_min_steam_score):
                if user_name not in user_end_time or group_giveaway.end_time < user_end_time[user_name]:
                    user_end_time[user_name] = group_giveaway.end_time
                if user_name not in user_first_giveaway:
                    user_first_giveaway[user_name] = set()
                user_first_giveaway[user_name].add((group_giveaway, game_data))

                if datetime.datetime.now() > group_giveaway.end_time and group_giveaway.has_winners() and len(group_giveaway.entries) > min_entries:
                    if user_name not in succesfully_ended:
                        succesfully_ended[user_name] = set()
                    succesfully_ended[user_name].add(group_giveaway.link)

    for user in users_list:
        if user not in user_end_time.keys():
            user_no_giveaway.add(user)

    partial_group_webpage = group_webpage.split(SteamGiftsConsts.STEAMGIFTS_LINK)[1]
    user_entered_giveaway=dict()
    for group_giveaway in group.group_giveaways.values():
        if check_entered_giveaways and user_added_time < group_giveaway.end_time:
            for user in users_list:
                if user in group_giveaway.entries \
                        and group_giveaway.entries[user].entry_time >= user_added_time \
                        and (user not in user_end_time or group_giveaway.entries[user].entry_time < user_end_time[user])\
                        and (len(group_giveaway.groups) == 1 or not SteamGiftsScrapingUtils.is_user_in_group(user, filter(lambda x: x != partial_group_webpage, group_giveaway.groups))):
                    #TODO: Add "Whitelist detected" warning
                    if user not in user_entered_giveaway:
                        user_entered_giveaway[user] = set()
                    user_entered_giveaway[user].add(group_giveaway)

    time_to_create_over = False
    if days_to_create_ga > 0 and datetime.date.today() > user_added_time.date() + datetime.timedelta(days=days_to_create_ga + 1):
        time_to_create_over = True

    return user_first_giveaway, succesfully_ended, user_no_giveaway, user_entered_giveaway, time_to_create_over
def get_group_summary(group_webpage, start_time):
    group = MySqlConnector.load_group(group_webpage, limit_by_time=start_time, starts_after_str=start_time)
    if not group:
        return None
    all_group_users = group.group_users.keys()
    users_created = dict()
    users_entered = dict()
    users_won = dict()
    group_games_count=0
    group_games_value=0.0
    group_games_without_data=0
    group_games_total_score=0.0
    group_games_total_num_of_reviews=0.0
    group_total_entered=0.0
    group_total_won=0.0
    for group_giveaway in group.group_giveaways.values():
        creator = group_giveaway.creator
        # Go over all giveaways started after "addition_date"
        if not start_time or start_time <= group_giveaway.end_time.strftime('%Y-%m-%d %H:%M:%S'):
            group_games_count += 1
            try:
                game_data = MySqlConnector.get_game_data(group_giveaway.game_name)
            except Exception as e:
                LogUtils.log_error(u'Crashed while trying to load game data of ' + group_giveaway.game_name.decode('utf-8') + u'. Reason: ' + str(e))
                traceback.print_exc()
                continue
            if not game_data:
                LogUtils.log_error(u'Could not load game data: ' + group_giveaway.game_name.decode('utf-8'))
            score = -1
            num_of_reviews = -1
            value = -1
            if game_data:
                value = game_data.value
                group_games_value += value
                score = game_data.steam_score
                num_of_reviews = game_data.num_of_reviews
            game_data_available = score != -1 and num_of_reviews != -1
            if game_data_available:
                group_games_total_score += score
                group_games_total_num_of_reviews += num_of_reviews
            else:
                group_games_without_data += 1

            # Number of created GAs, Total Value, Number of GAs with data, Total Score, Total NumOfReviews
            if creator in all_group_users:
                if creator not in users_created:
                    users_created[creator] = [0, 0, 0, 0, 0]
                users_created[creator][0] += 1
                if value != -1:
                    users_created[creator][1] += value
                if game_data_available:
                    users_created[creator][2] += 1
                    if score != -1:
                        users_created[creator][3] += score
                    if num_of_reviews != -1:
                        users_created[creator][4] += num_of_reviews

            giveaway_entries = group_giveaway.entries.values()
            num_of_giveaway_entries = len(giveaway_entries)
            for entry in giveaway_entries:
                user_name = entry.user_name
                if user_name not in all_group_users:
                    continue
                group_total_entered += 1
                # Number of entered GAs, Number of Unique GAs, Total Value, Number of GAs with data, Total Score, Total NumOfReviews
                if user_name not in users_entered:
                    users_entered[user_name] = [0, 0, 0, 0, 0, 0, 0]
                users_entered[user_name][0] += 1
                users_entered[user_name][1] += (1.0 / num_of_giveaway_entries)
                if len(group_giveaway.groups) == 1:
                    users_entered[user_name][2] += 1
                if value != -1:
                    users_entered[user_name][3] += value
                if game_data_available:
                    users_entered[user_name][4] += 1
                    if score != -1:
                        users_entered[user_name][5] += score
                    if num_of_reviews != -1:
                        users_entered[user_name][6] += num_of_reviews

                if entry.winner:
                    group_total_won += 1
                    # Number of won GAs, Total Value, Number of GAs with data, Total Score, Total NumOfReviews
                    if user_name not in users_won:
                        users_won[user_name] = [0, 0, 0, 0, 0]
                    users_won[user_name][0] += 1
                    if value != -1:
                        users_won[user_name][1] += value
                    if game_data_available:
                        users_won[user_name][2] += 1
                        if score != -1:
                            users_won[user_name][3] += score
                        if num_of_reviews != -1:
                            users_won[user_name][4] += num_of_reviews


    group_average_game_value = group_games_value / group_games_count
    group_average_game_score = group_games_total_score / (group_games_count - group_games_without_data)
    group_average_game_num_of_reviews = group_games_total_num_of_reviews / (group_games_count - group_games_without_data)
    # Total Games Value, Average games value, Average Game Score, Average Game NumOfReviews, Average number of entered per game, Average number of created per user, Average number of entrered per user, Average number of won per user
    total_group_data = (group_games_value, group_average_game_value, group_average_game_score, group_average_game_num_of_reviews, group_total_entered / group_games_count, float(group_games_count) / len(all_group_users), group_total_entered / len(all_group_users), group_total_won / len(all_group_users))

    # Number of created GAs, Total Value, Average Value, Average Score, Average NumOfReviews
    # Number of entered GAs, Percentage of unique, Average Value, Average Score, Average Num Of Reviews
    # If avialable: number of won GAs, Winning percentage, Average Value, Average Score, Average Num Of Reviews
    users_data = dict()
    for user in users_created.keys():
        if user not in users_data:
            users_data[user] = [(),(),()]
        # Number of created GAs, Total Value, Number of GAs with data, Total Score, Total NumOfReviews
        user_data = users_created[user]
        # Number of created GAs, Total Value, Average Value, Average Score, Average NumOfReviews
        if user_data[2] > 0:
            users_data[user][0] = (user_data[0], user_data[1], float(user_data[1]) / user_data[0], float(user_data[3]) / user_data[2], float(user_data[4]) / user_data[2])
        else:
            users_data[user][0] = (user_data[0], user_data[1], float(user_data[1]) / user_data[0], 0, 0)

    for user in users_entered.keys():
        if user not in users_data:
            users_data[user] = [(),(),()]
        # Number of entered GAs, Total number of entries in GAs he entered, Number of Shared GAs, Total Value,
        # Number of GAs with data, Total Score, Total NumOfReviews
        user_data = users_entered[user]
        # Number of entered GAs, Probability of winning, Percentage of unique, Total Value, Average Value, Average Score, Average Num Of Reviews
        if user_data[3] > 0:
            users_data[user][1] = (user_data[0], user_data[1] * 100, float(user_data[2]) / user_data[0] * 100, user_data[3], float(user_data[3]) / user_data[0], float(user_data[5]) / user_data[4], float(user_data[6]) / user_data[4])
        else:
            users_data[user][1] = (user_data[0], user_data[1] * 100, float(user_data[2]) / user_data[0] * 100, user_data[3], float(user_data[3]) / user_data[0], 0, 0)
        if user in users_won:
            # Number of won GAs, Total Value, Number of GAs with data, Total Score, Total NumOfReviews
            user_data = users_won[user]
            # Number of won GAs, Winning percentage, Total value, Average Value, Average Score, Average Num Of Reviews
            if user_data[2] > 0:
                users_data[user][2] = (user_data[0], float(user_data[0]) / users_data[user][1][0] * 100, user_data[1], float(user_data[1]) / user_data[0], float(user_data[3]) / user_data[2], float(user_data[4]) / user_data[2])
            else:
                users_data[user][2] = (user_data[0], float(user_data[0]) / users_data[user][1][0] * 100, user_data[1], float(user_data[1]) / user_data[0], 0, 0)

    return total_group_data, users_data