Пример #1
0
        static async Task <UpdateResult> TryGetNextPage(SubscriptionInfo sub)
        {
            try
            {
                var response = await httpClient.GetAsync(sub.GetNextPageUrl());

                if (!response.IsSuccessStatusCode)
                {
                    if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
                    {
                        Console.WriteLine($"No page yet for {sub.GetNextPageUrl()}. Stopping...");
                        return(UpdateResult.UpToDate);
                    }

                    Console.Error.WriteLine($"Failed to get content at url {sub.GetNextPageUrl()}. With status code {response.StatusCode}, reason \"{response.ReasonPhrase}\".");
                    return(UpdateResult.Error);
                }

                string body = await response.Content.ReadAsStringAsync();

                SubscriptionInfo.ContentInfo imageInfo = sub.FindImage(body);

                return(await GetImageIfNotExists(imageInfo));
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e.Message);
                return(UpdateResult.Error);
            }
        }
Пример #2
0
        static async Task <UpdateResult> GetImageIfNotExists(SubscriptionInfo.ContentInfo imageInfo)
        {
            string path = Path.Combine(subscriptions.SaveDir, imageInfo.localFilePath);

            Directory.CreateDirectory(Path.GetDirectoryName(path));

            if (File.Exists(path))
            {
                if (options.Verbose)
                {
                    Console.WriteLine($"File {path} already exists. Skipping.");
                }

                // Act as if it is new content to update files with.
                return(UpdateResult.ContentFound);
            }

            if (options.Verbose)
            {
                Console.WriteLine($"Downloading image from {imageInfo.imageURL} and saving to \"{path}\".");
            }

            try
            {
                using (var response = await httpClient.GetAsync(imageInfo.imageURL))
                {
                    using (var fs = new FileStream(path, FileMode.CreateNew))
                        await response.Content.CopyToAsync(fs);
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e.Message);
                return(UpdateResult.Error);
            }

            return(UpdateResult.ContentFound);
        }