public PhotosGetSizes ( string photoId ) : Sizes | ||
photoId | string | The id of the photo to fetch size information for. |
Résultat | Sizes |
public static string GetWallpaper(string theme, int minW, int minH, string dlFolder, out string flickrPage, List<string> bannedWalls, Action<string> banWallpaper) { flickrPage = ""; theme = theme.Replace("Twilight", "Sunset"); //because flickr usually returns nothing when the tag combination contains "Twilight" try { Flickr flickr = new Flickr("928a5bfb36cc1ea3160c6d236c2c76d4"); //search flickr PhotoSearchOptions opts = new PhotoSearchOptions(); opts.Tags = theme.Replace(" - ", ","); opts.TagMode = TagMode.AllTags; opts.SortOrder = PhotoSearchSortOrder.InterestingnessDescending; opts.Licenses.Add(LicenseType.AttributionCC); opts.Licenses.Add(LicenseType.AttributionNoDerivativesCC); opts.Licenses.Add(LicenseType.AttributionShareAlikeCC); opts.Licenses.Add(LicenseType.NoKnownCopyrightRestrictions); opts.SafeSearch = SafetyLevel.Safe; opts.ContentType = ContentTypeSearch.PhotosOnly; opts.MediaType = MediaType.Photos; opts.Extras = PhotoSearchExtras.OwnerName; PhotoCollection flickrResults = flickr.PhotosSearch(opts); if (flickrResults.Count == 0) //no suitable wallpapers found return ""; //choose random images for one that satisfies min. resolution SizeCollection sizes; Random rand = new Random((int)DateTime.Now.Ticks); string url = ""; int pick = -1, i; while (pick == -1) { pick = rand.Next(flickrResults.Count); if (bannedWalls.Contains(flickrResults[pick].WebUrl)) { //wallpaper banned, pick another flickrResults.RemoveAt(pick); pick = -1; } else { sizes = flickr.PhotosGetSizes(flickrResults[pick].PhotoId); for (i = sizes.Count - 1; i >= 0; i--) if (sizes[i].Width < minW || sizes[i].Height < minH) break; i++; if (i == sizes.Count) //image not large enough { banWallpaper(flickrResults[pick].WebUrl); //ban it so future searches can ignore it flickrResults.RemoveAt(pick); pick = -1; } else url = sizes[i].Source; } if (flickrResults.Count == 0) //no suitable wallpapers found return ""; } //get valid filename from title string filename = flickrResults[pick].Title; foreach (char c in System.IO.Path.GetInvalidFileNameChars()) filename = filename.Replace(c, '_'); filename += url.Substring(url.LastIndexOf('.')); //append file extension from url //dl image using (WebClient webClient = new WebClient()) { webClient.DownloadFile(url, dlFolder + filename); } flickrPage = flickrResults[pick].WebUrl; return dlFolder + filename; } catch (Exception e) { return ""; } }