예제 #1
0
 private void RecordSourceError(SourceResult result)
 {
     lock (this.sourceErrors)
     {
         this.sourceErrors.Add(result);
     }
 }
예제 #2
0
 public SourceErrorResultAction HandleSourceError(SourceResult sourceResult)
 {
     if (sourceResult == null)
     {
         return(SourceErrorResultAction.Abort);
     }
     this.RecordSourceError(sourceResult);
     if (sourceResult.CaughtException != null)
     {
         this.PartnershipClient.HandleException(sourceResult.CaughtException);
     }
     return(SourceErrorResultAction.Ignore);
 }
예제 #3
0
        private static string ErrorFormatForOperation(IOperationResult result)
        {
            SourceResult result2 = result as SourceResult;

            if (result2 != null)
            {
                switch (result2.Operation)
                {
                case SourceOperationType.LoadingCache:
                    return(null);

                case SourceOperationType.LoadingCacheFile:
                    return(Resources.ErrLoadingCache);

                case SourceOperationType.Verifying:
                    return(null);

                case SourceOperationType.VerifyingFile:
                    return(Resources.ErrVerifyingFile);

                case SourceOperationType.FileSystemChange:
                    return(Resources.ErrFileSystemChange);
                }
                return(Resources.UnknownOperation);
            }
            SyncResult result3 = result as SyncResult;

            if (result3 != null)
            {
                switch (result3.Operation)
                {
                case SyncOperationType.TransferTo:
                    return(Resources.ErrCopyToPhone);

                case SyncOperationType.TransferFrom:
                    return(Resources.ErrCopyToPc);

                case SyncOperationType.DeferredTransferTo:
                    return(Resources.ErrTranscode);

                case SyncOperationType.Update:
                    return(Resources.ErrUpdatePhone);

                case SyncOperationType.Delete:
                    return(Resources.ErrDeleteFromPhone);
                }
            }
            return(Resources.UnknownOperation);
        }
예제 #4
0
        public async Task <SourceResult> SourceChargeable(Source source)
        {
            _logger.LogInformation(GetLogMessage("Source: {0}; Source.Customer: {1}; Source.Card: {2}"), source.Id, source.Customer, source.Card?.Last4);

            var retVal = new SourceResult()
            {
                SourceId = source.Id
            };

            var entity = await Repository.Queryable()
                         .Where(x => x.Id == source.Id)
                         .FirstOrDefaultAsync();

            if (entity == null)
            {
                entity = new StripeSource
                {
                    Id          = source.Id,
                    ObjectState = ObjectState.Added
                };
            }
            else
            {
                entity.ObjectState = ObjectState.Modified;
                entity.Updated     = DateTimeOffset.UtcNow;
            }

            entity.ClientSecret = source.ClientSecret;
            entity.Created      = source.Created;
            entity.Status       = source.Status;
            entity.Flow         = source.Flow;
            entity.Type         = source.Type;
            entity.Amount       = source.Amount;

            var records = Repository.InsertOrUpdateGraph(entity, true);

            _logger.LogDebug(GetLogMessage("{0} updated records"), records);

            if (records > 0)
            {
                retVal.Succeeded = true;
            }

            return(retVal);
        }
예제 #5
0
                public override async Task <ProcessStageResult> ProcessAsync(Context context)
                {
                    IDatabaseContext dbContext = _dbProvider.GetContext();

                    HashSet <string> existingPlayers = (await dbContext.Player.GetAllAsync())
                                                       .Select(p => p.NflId)
                                                       .ToHashSet(StringComparer.OrdinalIgnoreCase);

                    SourceResult <List <PlayerWeekStats> > result = await _source.GetAsync(context.Week);

                    List <string> newIds = (result.Value.Select(s => s.NflId))
                                           .Where(id => !existingPlayers.Contains(id) && !Core.Teams.IsTeam(id))
                                           .ToList();

                    context.PlayerWeekStats = result.Value;
                    context.FetchNflIds     = newIds;

                    return(ProcessResult.Continue);
                }
예제 #6
0
        public override async Task <ProcessStageResult> ProcessAsync(TContext context)
        {
            Debug.Assert(context.FetchNflIds != null, $"'{nameof(context.FetchNflIds)}' list must be set before this stage runs.");

            if (!context.FetchNflIds.Any())
            {
                LogInformation("No players to add. Continuing to next stage.");
                return(ProcessResult.Continue);
            }

            LogDebug($"Will fetch {context.FetchNflIds.Count} players.");

            IDatabaseContext dbContext = _dbProvider.GetContext();

            foreach (string nflId in context.FetchNflIds)
            {
                Debug.Assert(!Core.Teams.IsTeam(nflId),
                             $"NFL id '{nflId}' represents a team so shouldn't be fetched as a player.");

                SourceResult <PlayerAdd> result = null;
                try
                {
                    result = await _playerAddSource.GetAsync(nflId);
                }
                catch (SourceDataScrapeException ex)
                {
                    _logger.LogError(ex, $"Failed to fetch player info for '{nflId}', will skip adding them.");
                    continue;
                }

                await dbContext.Player.AddAsync(result.Value);

                if (result.FetchedFromWeb)
                {
                    await _throttle.DelayAsync();
                }

                LogInformation($"Successfully fetched '{nflId}'.");
            }

            return(ProcessResult.Continue);
        }
예제 #7
0
        private async Task <TeamWeekStatsCacheData> CreateCacheDataAsync(WeekInfo week)
        {
            var data = new TeamWeekStatsCacheData();

            _logger.LogInformation($"Resolving team week stats for week '{week}'.");

            List <string> gameIds = await _weekMatchups.GetGameIdsForWeekAsync(week);

            foreach (var id in gameIds)
            {
                SourceResult <TeamWeekStatsSourceModel> result = await _source.GetAsync((id, week));

                data.UpdateWith(result.Value.HomeTeamStats);
                data.UpdateWith(result.Value.AwayTeamStats);

                _logger.LogDebug($"Resolved team week stats for game '{id}'.");
            }

            return(data);
        }
예제 #8
0
                public override async Task <ProcessStageResult> ProcessAsync(Context context)
                {
                    var stats = new List <TeamWeekStats>();

                    List <string> gameIds = await _weekMatchupsCache.GetGameIdsForWeekAsync(context.Week);

                    foreach (var gameId in gameIds)
                    {
                        SourceResult <TeamWeekStatsSourceModel> result = await _source.GetAsync((gameId, context.Week));

                        stats.Add(result.Value.HomeTeamStats);
                        stats.Add(result.Value.AwayTeamStats);

                        if (result.FetchedFromWeb)
                        {
                            await _throttle.DelayAsync();
                        }
                    }

                    context.TeamWeekStats = stats;
                    return(ProcessResult.Continue);
                }
예제 #9
0
        public SourceResultMessage(string sourceName, SourceResult result, AudioSource source)
            : this()
        {
            if (sourceName == null)
            {
                throw new ArgumentNullException("sourceName");
            }

            this.SourceName   = sourceName;
            this.SourceResult = result;
            this.Source       = source;

            if (source == null)
            {
                switch (result)
                {
                case Messages.SourceResult.NewSource:
                case Messages.SourceResult.SourceRemoved:
                case Messages.SourceResult.Succeeded:
                    throw new ArgumentNullException("source", "source can not be null if the result didn't fail");
                }
            }
        }
예제 #10
0
 public ReceivedAudioSourceEventArgs(string sourceName, AudioSource source, SourceResult result)
 {
     this.SourceName = sourceName;
     this.Result = result;
     this.Source = source;
 }
예제 #11
0
        public async Task <List <string> > GetGameIdsForWeekAsync(WeekInfo week)
        {
            SourceResult <List <WeekMatchup> > result = await _cache.GetOrCreateAsync(CacheKey(week), () => _source.GetAsync(week));

            return(result.Value.Select(m => m.NflGameId).ToList());
        }
예제 #12
0
        public async Task <List <WeekMatchup> > GetMatchupsForWeekAsync(WeekInfo week)
        {
            SourceResult <List <WeekMatchup> > result = await _cache.GetOrCreateAsync(CacheKey(week), () => _source.GetAsync(week));

            return(result.Value);
        }
예제 #13
0
 public SourceErrorResultAction HandleSourceError(SourceResult sourceResult)
 {
     if (sourceResult == null)
     {
         return SourceErrorResultAction.Abort;
     }
     this.RecordSourceError(sourceResult);
     if (sourceResult.CaughtException != null)
     {
         this.PartnershipClient.HandleException(sourceResult.CaughtException);
     }
     return SourceErrorResultAction.Ignore;
 }
예제 #14
0
 public ReceivedAudioSourceEventArgs(string sourceName, AudioSource source, SourceResult result)
 {
     this.SourceName = sourceName;
     this.Result     = result;
     this.Source     = source;
 }
예제 #15
0
        internal void RequestSourceMessage(MessageEventArgs <RequestSourceMessage> e)
        {
            var request = (RequestSourceMessage)e.Message;

            SourceResult result = SourceResult.FailedUnknown;

            IUserInfo requestingUser = context.Users[e.Connection];

            if (requestingUser == null)
            {
                return;
            }

            if (!context.GetPermission(PermissionName.RequestSource, requestingUser))
            {
                result = SourceResult.FailedPermissions;
            }

            if (String.IsNullOrEmpty(request.Name) ||
                AudioCodecArgs.IsInvalidBitrate(request.AudioSettings.Bitrate) ||
                AudioCodecArgs.IsInvalidComplexity(request.AudioSettings.Complexity) ||
                AudioCodecArgs.IsInvalidFrequency(request.AudioSettings.SampleRate) ||
                AudioCodecArgs.IsInvalidFrameSize(request.AudioSettings.SampleRate, request.AudioSettings.Channels, request.AudioSettings.FrameSize))
            {
                result = SourceResult.FailedInvalidArguments;
            }

            AudioSource source = null;

            try
            {
                if (result == SourceResult.FailedUnknown)
                {
                    int bitrate = context.Settings.DefaultAudioBitrate;
                    if (request.AudioSettings.Bitrate != 0)
                    {
                        bitrate = request.AudioSettings.Bitrate.Trim(context.Settings.MinimumAudioBitrate, context.Settings.MaximumAudioBitrate);
                    }

                    if (manager.IsSourceNameTaken(requestingUser, request.Name))
                    {
                        result = SourceResult.FailedDuplicateSourceName;
                    }
                    else
                    {
                        source = manager.Create(request.Name, requestingUser,
                                                new AudioCodecArgs(request.AudioSettings)
                        {
                            Bitrate = bitrate
                        });
                        result = SourceResult.Succeeded;
                    }
                }
            }
            catch (OverflowException)
            {
                result = SourceResult.FailedLimit;
            }
            finally
            {
                e.Connection.SendAsync(new SourceResultMessage(request.Name, result, source));
                if (result == SourceResult.Succeeded)
                {
                    foreach (IConnection connection in context.Connections)
                    {
                        if (connection == e.Connection)
                        {
                            continue;
                        }

                        connection.SendAsync(new SourceResultMessage(request.Name, SourceResult.NewSource, source));
                    }
                }
            }
        }
예제 #16
0
        private async Task <SourceResult> GetRating(HttpClient client, SourceResult result, bool unknownIsQuestionable, bool ignoreExplicit)
        {
            async Task <Match> WebRequest(string url, string pattern)
            {
                Regex regex             = new Regex(pattern, RegexOptions.IgnoreCase);
                HttpResponseMessage res = await client.GetAsync(url);

                Match webMatch = regex.Match((await res.Content.ReadAsStringAsync()));

                return(webMatch);
            }

            // TODO: Test how effective the regex is without the backup urls
            Match match = null;

            switch (result.Index)
            {
            case SiteIndex.DoujinshiMangaLexicon:
                match = await WebRequest(result.Url[0], @"<td>.*?<b>Adult:<\/b><\/td><td>(.*)<\/td>");

                if (match.Success)
                {
                    result.Rating = match.Groups[1].Value == "Yes" ? SourceRating.Nsfw : SourceRating.Safe;
                }
                else
                {
                    result.Rating = SourceRating.Unknown;
                }
                break;

            case SiteIndex.Pixiv:
            case SiteIndex.PixivArchive:
                match = await WebRequest(result.Url[0], @"<div class=""introduction-modal""><p class=""title"">(.*?)<\/p>");

                if (!match.Success)
                {
                    result.Rating = SourceRating.Safe;
                }
                else
                {
                    result.Rating = match.Groups[1].Value.ToLowerInvariant().Contains("r-18") ? SourceRating.Nsfw : SourceRating.Safe;
                }
                break;

            case SiteIndex.Gelbooru:
            case SiteIndex.Danbooru:
            case SiteIndex.SankakuChannel:
            case SiteIndex.IdolComplex:
                match = await WebRequest(result.Url[0], @"<li>Rating: (.*?)<\/li>");

                if (!match.Success)
                {
                    result.Rating = SourceRating.Unknown;
                }
                else
                {
                    result.Rating = (SourceRating)Array.IndexOf(new[] { null, "Safe", "Questionable", "Explicit" }, match.Groups[1].Value);
                }
                break;

            case SiteIndex.Yandere:
            case SiteIndex.Konachan:
                match = await WebRequest(result.Url[0], @"<li>Rating: (.*?) <span class="".*?""><\/span><\/li>");

                if (!match.Success)
                {
                    result.Rating = SourceRating.Unknown;
                }
                else
                {
                    result.Rating = (SourceRating)Array.IndexOf(new[] { null, "Safe", "Questionable", "Explicit" }, match.Groups[1].Value);
                }
                break;

            case SiteIndex.e621:
                match = await WebRequest(result.Url[0], @"<li>Rating: <span class="".*?"">(.*)<\/span><\/li>");

                if (!match.Success)
                {
                    result.Rating = SourceRating.Unknown;
                }
                else
                {
                    result.Rating = (SourceRating)Array.IndexOf(new[] { null, "Safe", "Questionable", "Explicit" }, match.Groups[1].Value);
                }
                break;

            case SiteIndex.FAKKU:
            case SiteIndex.TwoDMarket:
            case SiteIndex.nHentai:
                result.Rating = SourceRating.Nsfw;
                break;

            case SiteIndex.DeviantArt:
                match = await WebRequest(result.Url[0], @"<h1>Mature Content<\/h1>");

                result.Rating = match.Success ? SourceRating.Nsfw : SourceRating.Safe;
                break;

            default:
                result.Rating = SourceRating.Unknown;
                break;
            }

            if (unknownIsQuestionable && result.Rating is SourceRating.Unknown)
            {
                result.Rating = SourceRating.Questionable;
            }

            if (ignoreExplicit && (result.Rating is SourceRating.Questionable || result.Rating is SourceRating.Nsfw))
            {
                return(null);
            }

            return(result);
        }
예제 #17
0
        public SourceResultMessage(string sourceName, SourceResult result, AudioSource source)
            : this()
        {
            if (sourceName == null)
                throw new ArgumentNullException ("sourceName");

            this.SourceName = sourceName;
            this.SourceResult = result;
            this.Source = source;

            if (source == null)
            {
                switch (result)
                {
                    case Messages.SourceResult.NewSource:
                    case Messages.SourceResult.SourceRemoved:
                    case Messages.SourceResult.Succeeded:
                        throw new ArgumentNullException ("source", "source can not be null if the result didn't fail");
                }
            }
        }
예제 #18
0
 private void RecordSourceError(SourceResult result)
 {
     lock (this.sourceErrors)
     {
         this.sourceErrors.Add(result);
     }
 }
예제 #19
0
        public override void ReadPayload(ISerializationContext context, IValueReader reader)
        {
            this.SourceName = reader.ReadString ();
            this.SourceResult = (SourceResult)reader.ReadByte ();

            switch (this.SourceResult)
            {
                case Messages.SourceResult.NewSource:
                case Messages.SourceResult.SourceRemoved:
                case Messages.SourceResult.Succeeded:
                    this.Source = new AudioSource (context, reader);
                    break;
            }
        }