Exemple #1
0
        private static async Task <Uri> GetImageUriAsync(HttpClient client, ScreenshotID screenshotID, CancellationToken cancellationToken = default)
        {
            Uri metadataUri = new Uri($"https://prnt.sc/{screenshotID}");

            using (HttpResponseMessage metadataResponse = await client.GetAsync(metadataUri, cancellationToken))
            {
                metadataResponse.EnsureSuccessStatusCode();

                HtmlDocument html = new HtmlDocument();
                html.Load(await metadataResponse.Content.ReadAsStreamAsync());
                // first attempt to get image from og:image meta tag
                string metaImageUrl = html.DocumentNode.SelectSingleNode("//meta[@property='og:image']").GetAttributeValue("content", null);
                // if null, try twitter:image:src
                if (string.IsNullOrWhiteSpace(metaImageUrl))
                {
                    metaImageUrl = html.DocumentNode.SelectSingleNode("//meta[@name='twitter:image:src']").GetAttributeValue("content", null);
                }
                // if failed, throw
                if (string.IsNullOrWhiteSpace(metaImageUrl))
                {
                    throw new KeyNotFoundException("No raw image URL found in Prnt.sc's response");
                }

                if (metaImageUrl.StartsWith("//"))
                {
                    metaImageUrl = metaImageUrl.Insert(0, "https:");
                }
                return(new Uri(metaImageUrl));
            }
        }
Exemple #2
0
        public Screenshot(ScreenshotID id, byte[] data, string directUrl)
        {
            if (data?.Any() != true)
            {
                throw new ArgumentException("Screenshot byte data is required", nameof(data));
            }
            if (string.IsNullOrWhiteSpace(directUrl))
            {
                throw new ArgumentException("Screenshot URL is required", nameof(directUrl));
            }

            this.ID        = id;
            this.Data      = data;
            this.DirectURL = directUrl;
        }
Exemple #3
0
        public static async Task <Stream> DownloadScreenshotStreamAsync(this HttpClient client, ScreenshotID screenshotID, CancellationToken cancellationToken = default)
        {
            Uri screenshotUri = await GetImageUriAsync(client, screenshotID, cancellationToken);

            using (HttpResponseMessage screenshotResponse = await client.GetAsync(screenshotUri, cancellationToken))
                return(await screenshotResponse.Content.ReadAsStreamAsync());
        }
Exemple #4
0
        public static async Task <Screenshot> DownloadScreenshotAsync(this HttpClient client, ScreenshotID screenshotID, CancellationToken cancellationToken = default)
        {
            Uri screenshotUri = await GetImageUriAsync(client, screenshotID, cancellationToken);

            using (HttpResponseMessage screenshotResponse = await client.GetAsync(screenshotUri, cancellationToken))
            {
                byte[] bytes = await screenshotResponse.Content.ReadAsByteArrayAsync();

                return(new Screenshot(screenshotID, bytes, screenshotUri.ToString()));
            }
        }