public static async Task <IReadOnlyCollection <ChannelSheet> > Channels(SheetsCfg sheetsCfg, ILogger log)
        {
            var service           = GetService(sheetsCfg);
            var userChannelSheets = await sheetsCfg.UserSheets
                                    .Select((v, i) => new
                                            { v.SheetId, v.Email, Weight = 1 }) // I used to weight some users higher (that's why there is that logic). Now we weight them all the same (1)
                                    .BlockFunc(async s => new {
                Channels = await SheetValues <UserChannelSheet>(service, s.SheetId, "Channels", log)
                           .WithWrappedException($"error reading user sheet {s.SheetId}", log),
                s.SheetId,
                s.Weight,
                s.Email
            }, sheetsCfg.Parallel);

            var userChannelsById = userChannelSheets
                                   .SelectMany(u => u.Channels.Select(c => new { u.SheetId, u.Weight, Channel = c, u.Email }))
                                   .ToMultiValueDictionary(c => c.Channel.Id);

            var mainChannels = await MainChannels(sheetsCfg, service, log);

            var channels = mainChannels
                           .Select(mc => {
                var ucs = userChannelsById[mc.Id].NotNull()
                          .Where(uc => uc.Channel.Complete == "TRUE").ToList();

                var classifications = ucs.Select(uc =>
                                                 new UserChannelStore2 {
                    Email         = uc.Email,
                    ChannelId     = uc.Channel.Id,
                    SoftTags      = SoftTags(uc.Channel),
                    Relevance     = uc.Channel.Relevance,
                    LR            = uc.Channel.LR,
                    SheetId       = uc.SheetId,
                    Notes         = uc.Channel.Notes,
                    Weight        = uc.Weight,
                    MainChannelId = mc.MainChannelId
                }).ToList();

                var totalWeight  = classifications.Sum(c => c.Weight);
                var distinctTags = classifications.SelectMany(t => t.SoftTags).Distinct();
                var softTags     = distinctTags
                                   .Select(t => classifications.Sum(s => s.SoftTags.Contains(t) ? s.Weight : 0) > totalWeight / 2d ? t : null).NotNull().ToList();

                var res = new ChannelSheet {
                    Id            = mc.Id,
                    Title         = mc.Title,
                    LR            = AverageLR(ucs.Select(c => c.Channel.LR).ToList()),
                    MainChannelId = mc.MainChannelId,
                    HardTags      = new[] { mc.HardTag1, mc.HardTag2, mc.HardTag3 }.Where(t => t.HasValue()).OrderBy(t => t).ToList(),
                    SoftTags      = softTags,
                    Relevance     = ucs.Any() ? ucs.Average(s => s.Channel.Relevance) / 10d : 1,
                    UserChannels  = classifications
                };
                return(res);
            }
                                   ).ToList();

            return(channels);
        }
        public static async Task SyncNewChannelsForTags(SheetsCfg sheetsCfg, string mainChannelSheetId, IReadOnlyCollection <string> sheetIds)
        {
            var service   = GetService(sheetsCfg);
            var mainSheet = await service.Spreadsheets.Get(mainChannelSheetId).ExecuteAsync();

            var channelSheet = mainSheet.Sheets.FirstOrDefault(s => s.Properties.Title == "Channels");
            var lastRow      = channelSheet.Data.Max(d => d.StartRow);
        }
Exemple #3
0
        static SheetsService GetService(SheetsCfg sheetsCfg)
        {
            //var creds = new ClientSecrets {ClientId = sheetsCfg.Creds.Name, ClientSecret = sheetsCfg.Creds.Secret};
            var creds   = GoogleCredential.FromJson(sheetsCfg.CredJson.ToString()).CreateScoped(SheetsService.Scope.SpreadsheetsReadonly);
            var service = new SheetsService(new BaseClientService.Initializer {
                HttpClientInitializer = creds,
                ApplicationName       = Setup.AppName
            });

            return(service);
        }
Exemple #4
0
 static async Task <IReadOnlyCollection <MainChannelSheet> > MainChannels(SheetsCfg sheetsCfg, SheetsService service, ILogger log) =>
 await SheetValues <MainChannelSheet>(service, sheetsCfg.MainChannelSheetId, "Channels", log);
Exemple #5
0
 public static Task <IReadOnlyCollection <MainChannelSheet> > MainChannels(SheetsCfg sheetsCfg, ILogger log) =>
 MainChannels(sheetsCfg, GetService(sheetsCfg), log);