Exemple #1
0
        public static IndexWorkspace GetWorkspace(string indexDirectory)
        {
            var index = IndexWorkspace.Create(GetIndexDirectory(indexDirectory));

            if (index == null)
            {
                Log.Logger.Error("The index.json file doesn't exist. Run \"index\" first.");
                Environment.Exit(1);
            }

            return(index);
        }
        public static async Task Run(string indexDirectory, string channelId)
        {
            indexDirectory = Helpers.GetIndexDirectory(indexDirectory);

            if (string.IsNullOrEmpty(channelId))
            {
                // Maybe we are updating an existing index?
                var index = IndexWorkspace.Create(indexDirectory);
                if (index != null)
                {
                    channelId = index.Index.Channel.Id;
                }
            }

            if (string.IsNullOrEmpty(channelId))
            {
                Log.Error("You must provide a channel id.");
                Environment.Exit(1);
            }

            Log.Logger.Information("Getting channel info for {channelId}...", channelId);
            var channel = await GetChannel(channelId);

            Log.Logger.Information("Getting uploaded videos for channel {channel}...", channel.Title);
            var videos = await GetVideos(channel.UploadPlaylistId);

            Log.Logger.Information("Saving channel info...");
            var indexFile = Path.Combine(indexDirectory, "index.json");

            if (File.Exists(indexFile))
            {
                File.Delete(indexFile);
            }

            await File.WriteAllTextAsync(indexFile, JsonConvert.SerializeObject(new ChannelIndex
            {
                Channel = channel,
            }, Formatting.Indented));

            Log.Information("Discovering existing videos...");
            var existingVideos = IndexWorkspace.Create(indexDirectory).GetVideos();

            Log.Information("Saving videos...");

            var videosDirectory = Path.Combine(indexDirectory, "videos");

            if (!Directory.Exists(videosDirectory))
            {
                Directory.CreateDirectory(videosDirectory);
            }

            foreach (var video in videos)
            {
                if (existingVideos.Contains(video))
                {
                    continue;
                }
                Log.Information("Saving {video}...", video.Title);
                var videoFile = Path.Combine(videosDirectory, $"{video.Id}.json");
                if (File.Exists(videoFile))
                {
                    File.Delete(videoFile);
                }
                await File.WriteAllTextAsync(videoFile, JsonConvert.SerializeObject(video, Formatting.Indented));
            }

            // Find deleted videos
            existingVideos = IndexWorkspace.Create(indexDirectory).GetVideos();
            foreach (var existingVideo in existingVideos)
            {
                if (existingVideo.Deleted)
                {
                    continue;
                }

                if (!videos.Contains(existingVideo))
                {
                    // This video is deleted
                    Log.Warning("Deleted: {title}", existingVideo.Title);
                    var videoFile = Path.Combine(videosDirectory, $"{existingVideo.Id}.json");
                    if (File.Exists(videoFile))
                    {
                        File.Delete(videoFile);
                    }
                    existingVideo.Deleted = true;
                    await File.WriteAllTextAsync(videoFile, JsonConvert.SerializeObject(existingVideo, Formatting.Indented));
                }
            }

            // TODO: Check if videos are deleted...

            Log.Information("Done!");
        }