<commit_before>﻿using System;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using System.Net.Http;
using Steam.Models.SteamCommunity;
using SteamWebAPI2.Interfaces;
using SteamWebAPI2.Utilities;

namespace NadekoBot.Core.Services
{
    public class SteamService : INService
    {
        private readonly IBotCredentials _creds;
        private readonly IBotConfigProvider _config;

        /// <summary>
        /// Workshop name cache
        /// </summary>
        private ConcurrentDictionary<string, Tuple<DateTime, string>> steamWorkshopNameCache;

        /// <summary>
        /// Steam PlayerSummary cache
        /// </summary>
        static ConcurrentDictionary<ulong, Tuple<DateTime, PlayerSummaryModel>> steamPlayerCache = new ConcurrentDictionary<ulong, Tuple<DateTime, PlayerSummaryModel>>();

        /// <summary>
        /// SteamUser WebAPI Interface
        /// </summary>
        static SteamUser steamInterface;

        public SteamService(IBotCredentials creds, IBotConfigProvider config)
        {
            _creds = creds;
            _config = config;
            steamWorkshopNameCache = new ConcurrentDictionary<string, Tuple<DateTime, string>>();

            // Build SteamUser SteamWebAPI interface
            if (!string.IsNullOrWhiteSpace(_creds.SteamApiKey))
                steamInterface = new SteamUser(_creds.SteamApiKey);
        }

        public async Task<PlayerSummaryModel> GetSteamUserData(ulong id)
        {
            if (steamInterface == null) return null;
            Tuple<DateTime, PlayerSummaryModel> newPlayerData = null;
            if (steamPlayerCache.TryGetValue(id, out newPlayerData))
            {
                if (newPlayerData.Item1 > DateTime.UtcNow)
                {
                    newPlayerData = null;
                }
            }
            if (newPlayerData == null)
            {
                ISteamWebResponse<PlayerSummaryModel> playerData = await Task.Run(async () =>
                {
                    ISteamWebResponse<PlayerSummaryModel> msg = await steamInterface.GetPlayerSummaryAsync(id);
                    return msg;
                });

                newPlayerData = new Tuple<DateTime, PlayerSummaryModel>(DateTime.UtcNow.AddHours(1), playerData.Data);

                steamPlayerCache.AddOrUpdate(id, newPlayerData,
                    (key, existingVal) =>
                    {
                        return newPlayerData;
                    });
            }
            return newPlayerData?.Item2;
        }

        public async Task<string> GetSteamWorkshopName(string workshopId)
        {
            if (string.IsNullOrWhiteSpace(workshopId)) return null;

            Tuple<DateTime, string> newWorkshopName = null;
            if (steamWorkshopNameCache.TryGetValue(workshopId, out newWorkshopName))
            {
                if (newWorkshopName.Item1 > DateTime.UtcNow)
                {<commit_msg>more steam patching<commit_after>﻿using System;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using System.Net.Http;
using Steam.Models.SteamCommunity;
using SteamWebAPI2.Interfaces;
using SteamWebAPI2.Utilities;

namespace NadekoBot.Core.Services
{
    public class SteamService : INService
    {
        private readonly IBotCredentials _creds;
        private readonly IBotConfigProvider _config;

        /// <summary>
        /// Workshop name cache
        /// </summary>
        private ConcurrentDictionary<string, Tuple<DateTime, string>> steamWorkshopNameCache;

        /// <summary>
        /// Steam PlayerSummary cache
        /// </summary>
        static ConcurrentDictionary<ulong, Tuple<DateTime, PlayerSummaryModel>> steamPlayerCache = new ConcurrentDictionary<ulong, Tuple<DateTime, PlayerSummaryModel>>();

        /// <summary>
        /// SteamUser WebAPI Interface
        /// </summary>
        static SteamUser steamInterface;

        public SteamService(IBotCredentials creds, IBotConfigProvider config)
        {
            _creds = creds;
            _config = config;
            steamWorkshopNameCache = new ConcurrentDictionary<string, Tuple<DateTime, string>>();

            // Build SteamUser SteamWebAPI interface
            if (!string.IsNullOrWhiteSpace(_creds.SteamApiKey))
                steamInterface = new SteamUser(_creds.SteamApiKey);
        }

        public async Task<PlayerSummaryModel> GetSteamUserData(ulong id)
        {
            if (steamInterface == null) return null;
            Tuple<DateTime, PlayerSummaryModel> newPlayerData = null;
            if (steamPlayerCache.TryGetValue(id, out newPlayerData))
            {
                if (newPlayerData.Item1 > DateTime.UtcNow)
                {
                    newPlayerData = null;
                }
            }
            if (newPlayerData == null)
            {
                ISteamWebResponse<PlayerSummaryModel> playerData = await Task.Run(async () =>
                {
                    ISteamWebResponse<PlayerSummaryModel> msg = await steamInterface.GetPlayerSummaryAsync(id);
                    return msg;
                });

                if (playerData != null)
                {
                    newPlayerData = new Tuple<DateTime, PlayerSummaryModel>(DateTime.UtcNow.AddHours(1), playerData.Data);

                    steamPlayerCache.AddOrUpdate(id, newPlayerData,
                        (key, existingVal) =>
                        {
                            return newPlayerData;
                        });
                }
            }
            return newPlayerData?.Item2;
        }

        public async Task<string> GetSteamWorkshopName(string workshopId)
        {
            if (string.IsNullOrWhiteSpace(workshopId)) return null;

            Tuple<DateTime, string> newWorkshopName = null;
            if (steamWorkshopNameCache.TryGetValue(workshopId, out newWorkshopName))
            {
                if (newWorkshopName.Item1 > DateTime.UtcNow)
                {
{'fraction_non_alphanumeric': 0.08280678417026938, 'fraction_numerical': 0.0019953441968739607, 'mean_word_length': 1.915656810470189, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6330569', 'n_tokens_mistral': 1528, 'n_tokens_neox': 1477, 'n_words': 309}