Ejemplo n.º 1
0
        public static async Task <HtmlNode> ElementFromURL(string url, CancellationToken cancellationToken, IDictionary <string, string> headers = null, IDictionary <string, string> cookies = null)
        {
            var html = new HtmlDocument().DocumentNode;
            var http = await HTTP.Request(url, cancellationToken, headers, cookies).ConfigureAwait(false);

            if (http.IsOK)
            {
                html = ElementFromStream(http.ContentStream);
            }

            return(html);
        }
        public static async Task <bool> Download(string url, string fileName, CancellationToken cancellationToken)
        {
            if (!Directory.Exists(DatabasePath))
            {
                Logger.Info($"Creating database directory \"{DatabasePath}\"");
                Directory.CreateDirectory(DatabasePath);
            }

            var encoding = new UTF8Encoding(false);
            var http     = await HTTP.Request(url, cancellationToken).ConfigureAwait(false);

            if (http.IsOK)
            {
                Logger.Info($"Database file \"{fileName}\" downloaded successfully");
                File.WriteAllText(Path.Combine(DatabasePath, fileName), http.Content, encoding);

                return(true);
            }

            return(false);
        }
Ejemplo n.º 3
0
        private static async Task <RemoteImageInfo> GetImageSizeAndValidate(RemoteImageInfo item, CancellationToken cancellationToken)
        {
            if (Plugin.Instance.Configuration.DisableImageValidation)
            {
                return(item);
            }

            var http = await HTTP.Request(item.Url, HttpMethod.Head, cancellationToken).ConfigureAwait(false);

            if (http.IsOK)
            {
                if (Plugin.Instance.Configuration.DisableImageSize)
                {
                    return(item);
                }

                http = await HTTP.Request(item.Url, cancellationToken).ConfigureAwait(false);

                if (http.IsOK)
                {
                    using (var img = SKBitmap.Decode(http.ContentStream))
                    {
                        if (img != null && img.Width > 100)
                        {
                            return(new RemoteImageInfo
                            {
                                ProviderName = item.ProviderName,
                                Url = item.Url,
                                Type = item.Type,
                                Height = img.Height,
                                Width = img.Width,
                            });
                        }
                    }
                }
            }

            return(null);
        }