Beispiel #1
0
        public async Task <List <ChannelIdentifier> > LoadFromDisk()
        {
            if (File.Exists(FileName))
            {
                var livestreamFileText = File.ReadAllText(FileName);
                LivestreamFileData livestreamFileData;
                try
                {
                    livestreamFileData = JsonConvert.DeserializeObject <LivestreamFileData>(livestreamFileText);
                }
                catch (JsonSerializationException)
                {
                    livestreamFileData = new LivestreamFileData();
                }

                if (livestreamFileData.FileVersion == 0)
                {
                    var fileEntries = JsonConvert.DeserializeObject <List <LivestreamFileEntry> >(livestreamFileText);

                    await ConvertTwitchUsernamesToChannelIds(fileEntries);
                    await PopulateDisplayNames(fileEntries);

                    livestreamFileData.FileVersion           = LivestreamFileData.CurrentFileVersion;
                    livestreamFileData.LivestreamFileEntries = fileEntries;

                    // in case something went wrong in the conversion make a copy of the existing file first
                    File.Copy(FileName, $"{FileName}_{DateTime.Now:yyyyMMddHHmmss}.bak");
                    SaveToDisk(livestreamFileData);
                }

                return(livestreamFileData.LivestreamFileEntries.Select(entry =>
                {
                    var apiClient = apiClientFactory.GetByName(entry.StreamProvider);
                    return new ChannelIdentifier(apiClient, entry.ChannelId)
                    {
                        ImportedBy = entry.ImportedBy,
                        DisplayName = entry.DisplayName
                    };
                }).ToList());
            }

            return(new List <ChannelIdentifier>());
        }
Beispiel #2
0
        public void SaveToDisk(IEnumerable <ChannelIdentifier> livestreams)
        {
            if (livestreams == null)
            {
                return;
            }

            var livestreamFileEntries = livestreams.Select(x => new LivestreamFileEntry()
            {
                ChannelId      = x.ChannelId,
                StreamProvider = x.ApiClient.ApiName,
                ImportedBy     = x.ImportedBy,
                DisplayName    = x.DisplayName
            }).ToList();

            var livestreamFileData = new LivestreamFileData()
            {
                FileVersion           = LivestreamFileData.CurrentFileVersion,
                LivestreamFileEntries = livestreamFileEntries
            };

            SaveToDisk(livestreamFileData);
        }
Beispiel #3
0
 private void SaveToDisk(LivestreamFileData livestreamFileData)
 {
     File.WriteAllText(FileName, JsonConvert.SerializeObject(livestreamFileData, Formatting.Indented));
 }