示例#1
0
        public void UrlFetcher_follows_meta_refresh_full()
        {
            // arrange
            const string expected    = "http://www.newsweek.com/street-arts-new-class-war-272/street-arts-new-class-war-272?piano_d=1";
            const string htmlContent = @"<html><head><meta http-equiv=""refresh"" content=""1;URL='" + expected + @"'""></head><body></body></html>";

            // act
            string actual = UrlFetcher.ParseMetaRedirect(null, htmlContent);

            // assert
            Assert.AreEqual(expected, actual);
        }
        public async Task Semplice1()
        {
            const string outputDir = "SampleWebOutput";

            var fetcher = new UrlFetcher();

            var nReadabilityTranscoder    = new ReadabilityTranscoder();
            var nReadabilityWebTranscoder = new ReadabilityWebTranscoder(nReadabilityTranscoder, fetcher);

            var webTranscodingInput = new WebTranscodeRequest("https://vanschneider.com/the-art-of-pricing-freelance-projects");

            var result = await nReadabilityWebTranscoder.TranscodeAsync(webTranscodingInput);

            Assert.IsTrue(result.ContentExtracted);

            string extractedContent = result.Content;

            File.WriteAllText(
                Path.Combine(outputDir, string.Format("SampleOutput_a1.html")),
                extractedContent,
                Encoding.UTF8);
        }
        // SampleWebOutput
        // [Test]
        public async Task NewYorker()
        {
            const string outputDir = "SampleWebOutput";


            var fetcher = new UrlFetcher();

            var nReadabilityTranscoder    = new ReadabilityTranscoder();
            var nReadabilityWebTranscoder = new ReadabilityWebTranscoder(nReadabilityTranscoder, fetcher);

            var request = new WebTranscodeRequest("https://www.newyorker.com/culture/postscript/a-few-words-about-jerry-stiller");

            var result = await nReadabilityWebTranscoder.TranscodeAsync(request);

            Assert.IsTrue(result.ContentExtracted);


            string extractedContent = result.Content;

            File.WriteAllText(
                Path.Combine(outputDir, string.Format("SampleOutput_newyorker.html")),
                extractedContent,
                Encoding.UTF8);
        }
示例#4
0
 public void UseSimpleNavigation()
 {
     var fetcher  = new UrlFetcher();
     var contents = fetcher.Fetch(new Uri("http://www.google.com"));
 }
示例#5
0
        private async void ChangeWallpaper()
        {
            try
            {
                //TODO -
                // Don't need internet access for local folders
                //
                while (true)
                {
                    _cts = new CancellationTokenSource();
                    var sources = await _db.GetSourcesAsync();

                    var access = await NetworkTester.HasInternetAccess();

                    if (access || sources.Any(x => x.Source == Source.Local))
                    {
                        if (sources.Count > 0)
                        {
                            if (!wasLocal)
                            {
                                foreach (var file in currentImages)
                                {
                                    File.Delete(file);
                                }
                            }
                            currentImages.Clear();
                            OptionEnabled = true;
                            NotifyOfPropertyChange(() => OptionEnabled);
                            WallpaperSource randSource;
                            if (access)
                            {
                                randSource = sources[_rand.Next(sources.Count)];
                            }
                            else
                            {
                                sources    = sources.Where(x => x.Source == Source.Local).ToList();
                                randSource = sources[_rand.Next(sources.Count)];
                            }
                            if (randSource.Source == Source.Wallhaven)
                            {
                                randSource.WallhavenOptions = await _db.GetWallhavenOptions(randSource);
                            }
                            Image image      = null;
                            var   isMultiple = await _db.UseMultiple();

                            string tempFile = null;
                            wasLocal = (randSource.Source == Source.Local);
                            if (isMultiple)
                            {
                                //testing for multiple monitors
                                var screens = System.Windows.Forms.Screen.AllScreens;
                                //this needs to be modified to allow vertically stacked monitors.
                                //right now only side by side monitors will work
                                using (var bmp = new Bitmap(screens.Sum(x => x.Bounds.Width), screens.Max(x => x.Bounds.Height)))
                                    using (var g = Graphics.FromImage(bmp))
                                    {
                                        //get a random image for each screen
                                        foreach (var s in screens)
                                        {
                                            string imageUrl = await UrlFetcher.GetRandomImageUrl(randSource, await _db.ImgurClientId(), currentImages);

                                            if (String.IsNullOrEmpty(imageUrl))
                                            {
                                                if (String.IsNullOrEmpty(tempFile))
                                                {
                                                    continue;
                                                }
                                                //do nothing, leave the temp file as the current file
                                            }
                                            else
                                            {
                                                if (randSource.Source != Source.Local)
                                                {
                                                    tempFile = await WebImage.DownloadImage(imageUrl);
                                                }
                                                else
                                                {
                                                    tempFile = imageUrl;
                                                }
                                            }

                                            currentImages.Add(tempFile);
                                            image = Image.FromFile(tempFile);
                                            //we need to scale the image properly to fit the screen
                                            image = WebImage.ScaleImage(image, s.Bounds.Width, s.Bounds.Height);
                                            //now figure out the location
                                            var x = ((s.Bounds.Width - image.Width) / 2) + s.Bounds.Location.X;
                                            var y = ((s.Bounds.Height - image.Height) / 2) + s.Bounds.Location.Y;

                                            //draw it onto the screen
                                            g.DrawImage(image, new Rectangle(x, y, image.Width, image.Height));

                                            newImages.Add(image);
                                        }
                                        //now get the file out of it
                                        g.Save();
                                        tempFile = Path.Combine(Environment.ExpandEnvironmentVariables("%temp%"), "stitched.bmp");
                                        bmp.Save(tempFile);
                                    }
                            }
                            else
                            {
                                string imageUrl = await UrlFetcher.GetRandomImageUrl(randSource, await _db.ImgurClientId(), currentImages);

                                if (!String.IsNullOrEmpty(imageUrl))
                                {
                                    if (randSource.Source != Source.Local)
                                    {
                                        tempFile = await WebImage.DownloadImage(imageUrl);
                                    }
                                    else
                                    {
                                        tempFile = imageUrl;
                                    }
                                    image = Image.FromFile(tempFile);
                                    newImages.Add(image);
                                    currentImages.Add(tempFile);
                                }
                            }
                            if (!currentImages.All(x => String.IsNullOrEmpty(x)))
                            {
                                DesktopBackground.SetWallpaper(tempFile, isMultiple);
                                ToolTipInfo = string.Format("Source: {0}\r\nResolution: {1}",
                                                            randSource.Source == Source.Reddit ? randSource.Query : randSource.Source + " / " + randSource.Query, newImages.Aggregate("", (c, v) => $"{c}\r\n{v.Width}x{v.Height}"));
                                NotifyOfPropertyChange(() => ToolTipInfo);
                            }
                            foreach (var i in newImages)
                            {
                                i.Dispose();
                            }
                            newImages.Clear();
                        }
                        else
                        {
                            ToolTipInfo = "Please add sources!";
                            NotifyOfPropertyChange(() => ToolTipInfo);
                            OptionEnabled = false;
                            NotifyOfPropertyChange(() => OptionEnabled);
                        }
                        await Task.Delay(TimeSpan.FromMinutes(await _db.Interval()), _cts.Token).ContinueWith(tsk => { });
                    }
                    else
                    {
                        ToolTipInfo = "Waiting for internet access...";
                        NotifyOfPropertyChange(() => ToolTipInfo);
                        OptionEnabled = false;
                        NotifyOfPropertyChange(() => OptionEnabled);
                        await Task.Delay(TimeSpan.FromSeconds(30));
                    }
                }
            }
            catch (Exception e)
            {
                e = e;
            }
        }
示例#6
0
 private static void FetchUrlInNewThread(string url, int count)
 {
     var urlFetcher = new UrlFetcher();
     urlFetcher.FetchUrl(url, count);
 }