示例#1
0
        //public Downloader(SearchOptions so) : this(so, new List<string>()) { }

        //public Downloader(DateTime previousDownload, SearchOptions so) : this(previousDownload, so, new List<string>()) { }

        public Downloader(SearchOptions so) {
            Provider = new WallHaven();
            SearchOptions = so;
            //LastDownload = previousDownload;

            DownloadTimer.Elapsed += DownloadTimerElapsed;

            //SetDownloadInterval(DownloadInterval);
        }
        public SearchOptionsViewModel(ISettingsProvider sp, SearchOptions so) {
            SettingsProvider = sp;
            SearchOps = so;

            // load the settings
            SearchOps.Search = sp.Search;
            SearchOps.Category = sp.Category;
            SearchOps.Purity = sp.Purity;
            SearchOps.Resolution = sp.Resolution;
            SearchOps.AspectRatio = sp.AspectRatio;
            SearchOps.SortType = sp.SortType;
            SearchOps.SortOrder = sp.SortOrder;

            Resolution = new ResolutionViewModel(so.Resolution);
            Resolution.PropertyChanged += Resolution_PropertyChanged;

            // pre-fill the aspect ratio list with all available aspect ratios
            // check/tick only those that are also present in the search options list
            var rs = WallHaven.SupportedAspectRatios.Select(r => {
                return new RatioViewModel(r.X, r.Y, SearchOps.AspectRatio.Ratios.Contains(r));
            }).ToList();

            AspectRatio = new ObservableCollection<RatioViewModel>(rs);

            foreach (var r in AspectRatio) {
                r.PropertyChanged += r_PropertyChanged;
            }

            AspectRatio.CollectionChanged += AspectRatio_CollectionChanged;
        }
示例#3
0
        public string GetFormattedSearchURL(SearchOptions searchOptions) {
            // http://alpha.wallhaven.cc
            // search?q=landscape&
            // categories=100&
            // purity=100&
            // resolutions=1920x1200,2560x1440,2560x1600& (1024x768,1280x800,1366x768,1280x960,1440x900,1600x900,1280x1024,1600x1200,1680x1050,1920x1080,1920x1200,2560x1440,2560x1600,3840x1080,5760x1080,3840x2160)
            // ratios=16x10,21x9,32x9,48x9&  (4x3, 5x4, 16x9, 16x10, 21x9, 32x9, 48x9)
            // sorting=date_added&  (date_added,relevance,random,views,favorites)
            // order=desc  (desc, asc)
            StringBuilder builder = new StringBuilder(MainUrl);

            builder.Append("/search?q=");
            builder.Append(Uri.EscapeDataString(searchOptions.Search));

            builder.Append("&categories=" + CategoryToSearchString(searchOptions.Category));
            builder.Append("&purity=" + PurityToSearchString(searchOptions.Purity));

            string resolutions = ResolutionToSearchString(searchOptions.Resolution);

            if (!string.IsNullOrEmpty(resolutions))
                builder.Append("&resolutions=" + Uri.EscapeDataString(resolutions));

            string ratio = AspectRatioToSearchString(searchOptions.AspectRatio);

            if (!string.IsNullOrEmpty(ratio))
                builder.Append("&ratios=" + Uri.EscapeDataString(ratio));

            builder.Append("&sorting=" + SortingTypeToSearchString(searchOptions.SortType));
            builder.Append("&order=" + SortingOrderToSearchString(searchOptions.SortOrder));

            System.Diagnostics.Trace.TraceInformation("URL: {0}", builder.ToString());

            return builder.ToString();
        }
示例#4
0
        public async Task<IEnumerable<Wallpaper>> GetWallpapers(SearchOptions searchOptions, List<Wallpaper> previousDownloads) {
            if (!ValidSearch(searchOptions)) {
                System.Diagnostics.Trace.TraceError("Search options are not valid");
                return new List<Wallpaper>();
            }

            HtmlDocument document = new HtmlDocument();
            HttpClient client = new HttpClient();

            try {
                var html = await client.GetStringAsync(GetFormattedSearchURL(searchOptions));
                document.LoadHtml(html);
            } catch(Exception e) {
                System.Diagnostics.Trace.TraceError("Error loading search page: {0}", e.Message);
                return new List<Wallpaper>();
            }

            IEnumerable<HtmlNode> listItems = document.DocumentNode.SelectNodes("//div[@id='thumbs']//li/figure");
            List<Wallpaper> wallpapers = new List<Wallpaper>();

            if (listItems == null) {
                return wallpapers;
            }

            //System.Diagnostics.Trace.TraceInformation("Found " + listItems.Count() + " potential images on the page");

            if (listItems.Count() > 0) {
                foreach (HtmlNode node in listItems) {
                    HtmlNode a = node.SelectSingleNode("a[@class='preview']");

                    string url = a.GetAttributeValue("href", "");

                    if (!String.IsNullOrEmpty(url)) {
                        // in: http://alpha.wallhaven.cc/wallpaper/52153
                        // out: http://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-52153.jpg

                        var idString = url.Split('/').LastOrDefault();
                        int id = 0;

                        if (Int32.TryParse(idString, out id)) {
                            // don't download the newest one if it is already in our previously downloaded list
                            if (searchOptions.SortType == SortType.Newest && searchOptions.SortOrder == SortOrder.Descending) {
                                if (previousDownloads.Exists(w => w.Id == id))
                                    break;
                            }

                            Extension ext = Extension.Jpg;

                            try {
                                // check that the file actually exists on wallhaven                       
                                var httpRequestMessage = new HttpRequestMessage(HttpMethod.Head, string.Format(DirectUrl, id, ext.ToString().ToLower()));
                                HttpResponseMessage response = await client.SendAsync(httpRequestMessage);

                                // jpg version doesn't exist, only other option is png
                                if (!response.IsSuccessStatusCode) {
                                    ext = Extension.Png;
                                }
                            } catch (Exception e) {
                                System.Diagnostics.Trace.TraceError("Error checking extension: {0}", e.Message);
                                continue;
                            }

                            //System.Diagnostics.Trace.TraceInformation("Validated and added image {0}.{1}", id, ext.ToString().ToLower());

                            wallpapers.Add(new Wallpaper(string.Format(DirectUrl, id, ext.ToString().ToLower()), id, ext));

                            // don't bother going past the first item since it is the newest
                            if (searchOptions.SortType == SortType.Newest && searchOptions.SortOrder == SortOrder.Descending)
                                break;
                        } else {
                            System.Diagnostics.Trace.TraceError("Cannot find valid ID for url {0}", url);
                        }
                    }

                    //if (searchOptions.Limit > 0 && wallpapers.Count == searchOptions.Limit)
                    //    break;
                }
            }

            return wallpapers;
        }
示例#5
0
        bool ValidSearch(SearchOptions searchOptions) {
            if (string.IsNullOrEmpty(searchOptions.Search))
                return false;

            if (searchOptions.Resolution.Resolutions.Count == 0)
                return false;

            if (searchOptions.AspectRatio.Ratios.Count == 0)
                return false;

            return true;
        }