Пример #1
0
        List <Models.FlickrPhoto> GetPhotosFromFlickr(int pageindex = 0)
        {
            List <Models.FlickrPhoto> retVal = null;

            FlickrNet.Flickr flickr = new FlickrNet.Flickr(Manager.Settings.Current.FlickrAPIKey);
            flickr.InstanceCacheDisabled = true;

            FlickrNet.FoundUser userInfo = flickr.PeopleFindByUserName(Manager.Settings.Current.FlickrUserName);
            string flickrUserId          = userInfo.UserId;

            ViewBag.FlickrFullName = userInfo.FullName;

            FlickrNet.Person user = flickr.PeopleGetInfo(flickrUserId);
            ViewBag.FlickrFullName = user.RealName;

            FlickrNet.PhotoSearchOptions option = new FlickrNet.PhotoSearchOptions();
            option.UserId = flickrUserId;
            option.Page   = pageindex;

            FlickrNet.PhotoCollection photosets = flickr.PhotosSearch(option);
            if (photosets != null && photosets.Count > 0)
            {
                DataTable photos = UnigateObject.Query(Manager.Settings.Current.PhotoListTableName)
                                   .Column("Slug")
                                   .SortDesc("CreateDate")
                                   .ToDataTable();

                List <string> slugs = new List <string>();
                if (photos != null && photos.Rows.Count > 0)
                {
                    slugs = photos.AsEnumerable().Select(p => p.Field <string>("Slug")).ToList();
                }

                retVal = photosets.Where(p => !slugs.Any(s => s == p.PhotoId)).Select(p => new Models.FlickrPhoto()
                {
                    DownloadUrl = !string.IsNullOrEmpty(p.LargeUrl) ? p.LargeUrl : (!string.IsNullOrEmpty(p.MediumUrl) ? p.MediumUrl : p.SmallUrl),
                    ViewUrl     = p.SmallUrl,
                    Title       = p.Title,
                    PhotoId     = p.PhotoId
                }).ToList <Models.FlickrPhoto>();
            }

            return(retVal);
        }
Пример #2
0
        public void GetNewWallpaper()
        {
            NotifyIconText = "Retrieving next picture...";
            NotifyIconIcon = WallpaperFlickr.Properties.Resources.flickrwait;
            IsNotifyFail   = false;

            if (ApiKey.Equals(string.Empty))
            {
                NotifyIconText = "API key missing";
                NotifyIconIcon = WallpaperFlickr.Properties.Resources.flickrbad;
                IsNotifyFail   = true;
                return;
            }

            FlickrNet.Flickr flickr = new FlickrNet.Flickr();
            flickr.ApiKey = ApiKey;

            FlickrNet.PhotoCollection photos = null;

            switch (_settings.SearchOrFaves)
            {
            case 0:
                FlickrNet.PhotoSearchOptions options = new FlickrNet.PhotoSearchOptions();
                if (!Tags.Trim().Equals(string.Empty))
                {
                    options.Tags    = Tags;
                    options.TagMode = GetTagMode();
                }
                if (!UserId.Trim().Equals(string.Empty))
                {
                    FlickrNet.FoundUser fuser;
                    string   UserName     = "";
                    string[] AllUserNames = UserId.Split(',');
                    UserName = AllUserNames[new Random().Next(0, AllUserNames.GetUpperBound(0) + 1)];
                    try
                    {     // Exception handler added by CLR 2010-06-11
                        fuser = flickr.PeopleFindByUserName(UserName.Trim());
                    }
                    catch (Exception ex)
                    {
                        FailWithError(ex);
                        return;
                    }
                    if (!fuser.UserId.Equals(string.Empty))
                    {
                        options.UserId = fuser.UserId;
                    }
                }
                options.PrivacyFilter = FlickrNet.PrivacyFilter.PublicPhotos;
                options.SortOrder     = GetSortOrder();
                options.PerPage       = 365;

                try
                {
                    photos = flickr.PhotosSearch(options);
                    //photos = flickr.PhotosGetRecent(); // this was me trying to do Explore stuff, but failed
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.Message);
                    FailWithError(ex);
                    return;
                }
                options = null;
                break;

            case 1:
                try
                {
                    FlickrNet.FoundUser fuser;
                    fuser  = flickr.PeopleFindByUserName(FaveUserId);
                    photos = flickr.FavoritesGetPublicList(fuser.UserId);
                }
                catch (Exception ex)
                {
                    FailWithError(ex);
                    return;
                }
                break;

            case 2:
                // do explore
                try
                {
                    photos = flickr.InterestingnessGetList();
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.Message);
                    FailWithError(ex);
                    return;
                }
                break;

            default:
                break;
            }


            clsWallpaper wallpaper = new clsWallpaper();

            Random pn = new Random();

            if (photos.Count == 0)
            {
                NotifyIconText = "Specified parameters return no photographs from Flickr";
                NotifyIconIcon = WallpaperFlickr.Properties.Resources.flickrbad;
                IsNotifyFail   = true;
                return;
            }
            else
            {
                int chosePhoto = pn.Next(0, photos.Count);
                //FlickrNet.Sizes fs = flickr.PhotosGetSizes("4570943273");
                FlickrNet.SizeCollection fs;
                bool LoadedWallpaper = false;
                try
                {
                    fs = flickr.PhotosGetSizes(photos[chosePhoto].PhotoId);
                    // Load the last size (which should be "Original"). Doing all this
                    // because photo.OriginalURL just causes an exception
                    LoadedWallpaper = wallpaper.Load(fs[fs.Count - 1].Source, _settings,
                                                     getDisplayStyle(), Application.ExecutablePath, photos[chosePhoto].WebUrl);
                }
                catch (Exception ex) // load failed with an exception
                {
                    FailWithError(ex);
                    return;
                }

                if (!LoadedWallpaper) // load failed, but didn't cause an exception
                {
                    NotifyIconText = "Failed to load wallpaper";
                    NotifyIconIcon = WallpaperFlickr.Properties.Resources.flickrbad;
                    IsNotifyFail   = true;
                    return;
                }

                // Get further info about the photo to display in the tooltip
                FlickrNet.PhotoInfo fi;
                try
                {
                    fi = flickr.PhotosGetInfo(photos[chosePhoto].PhotoId);
                }
                catch (Exception ex)
                {
                    FailWithError(ex);
                    return;
                }

                // Set thumbnail
                NotifyIconIcon = TinyPictureVersion(FileSystem.MyPath() + "\\wallpaper\\_CurrentPaper.bmp");

                FlickrNet.Person fuser;
                string           notifyText = "";
                fuser      = flickr.PeopleGetInfo(photos[chosePhoto].UserId);
                notifyText = fuser.UserName + ": " + photos[chosePhoto].Title;
                string description = fi.Description;
                string location    = "\n";
                if (fi.Location != null)
                {
                    if (fi.Location.County != null)
                    {
                        location += fi.Location.County.Description + ", " + fi.Location.Country.Description;
                    }
                    else
                    {
                        location += fi.Location.Country.Description;
                    }
                }
                description = System.Web.HttpUtility.HtmlDecode(Regex.Replace(description, "<[^>]*>", ""));

                NotifyIconText           = notifyText.Substring(0, Math.Min(63, notifyText.Length));
                NotifyIconBalloonTipText = fi.DateTaken.ToLongDateString() +
                                           location + "\n" + description;
                NotifyIconBalloonTipTitle = photos[chosePhoto].Title;

                if (ShowBubbles)
                {
                    NotifyPropertyChanged("PopupBalloon");
                }
            }

            wallpaper = null;
            flickr    = null;
            photos    = null;
            //notifyIcon1.Icon = WallpaperFlickr.Properties.Resources.flickr;
        }