public void DownloadPhotsets()
        {
            Flickr f = new Flickr();
            var l = f.PhotosetsGetList();

            RemoveNonExisting(l);
            AddOrUpdate(l);
        }
        /// <summary>
        /// Gets a data response for the given base url and parameters, 
        /// either using OAuth or not depending on which parameters were passed in.
        /// </summary>
        /// <param name="flickr">The current instance of the <see cref="Flickr"/> class.</param>
        /// <param name="baseUrl">The base url to be called.</param>
        /// <param name="parameters">A dictionary of parameters.</param>
        /// <returns></returns>
        public static string GetDataResponse(Flickr flickr, string baseUrl, Dictionary<string, string> parameters)
        {
            bool oAuth = parameters.ContainsKey("oauth_consumer_key");

            if (oAuth)
                return GetDataResponseOAuth(flickr, baseUrl, parameters);
            else
                return GetDataResponseNormal(flickr, baseUrl, parameters);
        }
        private static string GetDataResponseOAuth(Flickr flickr, string baseUrl, Dictionary<string, string> parameters)
        {
            string method = "POST";

            // Remove api key if it exists.
            if (parameters.ContainsKey("api_key")) parameters.Remove("api_key");
            if (parameters.ContainsKey("api_sig")) parameters.Remove("api_sig");

            if (!String.IsNullOrEmpty(Flickr.OAuthAccessTokenSecret) && !parameters.ContainsKey("oauth_signature"))
            {
                string sig = flickr.OAuthCalculateSignature(method, baseUrl, parameters, Flickr.OAuthAccessTokenSecret);
                parameters.Add("oauth_signature", sig);
            }

            // Calculate post data, content header and auth header
            string data = OAuthCalculatePostData(parameters);
            string authHeader = OAuthCalculateAuthHeader(parameters);

            // Download data.
            try
            {
                return DownloadData(method, baseUrl, data, PostContentType, authHeader);
            }
            catch (WebException ex)
            {
                if (ex.Status != WebExceptionStatus.ProtocolError) throw;

                var response = ex.Response as HttpWebResponse;
                if (response == null) throw;

                string responseData = null;

                using (var stream = response.GetResponseStream())
                {
                    if (stream != null)
                        using (var responseReader = new StreamReader(stream))
                        {
                            responseData = responseReader.ReadToEnd();
                            responseReader.Close();
                        }
                }
                if (response.StatusCode == HttpStatusCode.BadRequest ||
                    response.StatusCode == HttpStatusCode.Unauthorized)
                {

                    throw new OAuthException(responseData, ex);
                }

                if (String.IsNullOrEmpty(responseData)) throw;
                throw new WebException("WebException occurred with the following body content: " + responseData, ex, ex.Status, ex.Response);
            }
        }
예제 #4
0
        /// <summary>
        /// Gets a data response for the given base url and parameters,
        /// either using OAuth or not depending on which parameters were passed in.
        /// </summary>
        /// <param name="flickr">The current instance of the <see cref="Flickr"/> class.</param>
        /// <param name="baseUrl">The base url to be called.</param>
        /// <param name="parameters">A dictionary of parameters.</param>
        /// <returns></returns>
        public static string GetDataResponse(Flickr flickr, string baseUrl, Dictionary <string, string> parameters)
        {
            bool oAuth = parameters.ContainsKey("oauth_consumer_key");

            if (oAuth)
            {
                return(GetDataResponseOAuth(flickr, baseUrl, parameters));
            }
            else
            {
                return(GetDataResponseNormal(flickr, baseUrl, parameters));
            }
        }
        /// <summary>
        /// return SetID
        /// </summary>
        /// <param name="file"></param>
        /// <returns>SetID</returns>
        public string AddPhoto(FFile file)
        {
            var tittle = GetTittleFromPath(file.Path);
            var set = db.Sets.FirstOrDefault(r => r.UserID == Flickr.User.UserId && r.Tittle.ToLower() == tittle.ToLower());

            if (set == null)
            {
                set = GetOrCreateThenAssign(tittle, file.PhotoID);
            }
            else
            {
                Flickr f = new Flickr();
                f.PhotosetsAddPhoto(set.SetsID, file.PhotoID);
            }

            return set.SetsID;
        }
        private static string GetDataResponseNormal(Flickr flickr, string baseUrl, Dictionary<string, string> parameters)
        {
            string method = "POST";

            string data = String.Empty;

            foreach (var k in parameters)
            {
                data += k.Key + "=" + Uri.EscapeDataString(k.Value) + "&";
            }

            if (method == "GET" && data.Length > 2000) method = "POST";

            if (method == "GET")
                return DownloadData(method, baseUrl + "?" + data, null, null, null);
            else
                return DownloadData(method, baseUrl, data, PostContentType, null);
        }
        public Set GetOrCreateThenAssign(string tittle, string photoID)
        {
            Set set;
            lock (lockForCreateFlickrSets)
            {
                Flickr f = new Flickr();
                set = db.Sets.FirstOrDefault(r => r.UserID == Flickr.User.UserId && r.Tittle.ToLower() == tittle.ToLower());
                if (set == null)
                {
                    var fSet = f.PhotosetsCreate(tittle, photoID);
                    if (fSet == null)
                    {

                    }
                    else
                    {
                        set = db.Sets.Add(new Set() { SetsID = fSet.PhotosetId, UserID = Flickr.User.UserId, Tittle = tittle });
                        db.SaveChanges();
                    }
                }
                else
                {
                    f.PhotosetsAddPhoto(set.SetsID, photoID);
                }
            }
            return set;
        }
        async static Task DownloadSet()
        {
            if (string.IsNullOrEmpty(CurrentDownloadSetId))
            {

            }
            else
            {
                try
                {
                    var task = Task.Factory.StartNew(() =>
                    {

                        Flickr flickr = new Flickr();
                        var list = flickr.PhotosetsGetPhotos(CurrentDownloadSetId);

                        SetLogic sL = new SetLogic();
                        string saveFolder = sL.GetDownloadFolderPath(CurrentDownloadSetId);

                        if (list == null || list.Count == 0 || string.IsNullOrEmpty(saveFolder))
                        { }
                        else
                        {
                            ParallelOptions opt = new ParallelOptions();
                            opt.MaxDegreeOfParallelism = MaxUpload;
                            
                            opt.CancellationToken = DownloadCancellationToken;

                            try
                            {
                                var r1 = Parallel.ForEach(list, opt
                                    , photo =>
                                    {
                                        var sizes = flickr.PhotosGetSizes(photo.PhotoId);
                                        if (sizes == null || sizes.Count == 0) { }
                                        else
                                        {
                                            var org = sizes.FirstOrDefault(r => r.Label == "Original" && r.MediaType == MediaType.Photos);
                                            if (org == null) { }
                                            else
                                            {
                                                Uri uri = new Uri(org.Source);
                                             
                                                string filename = Path.GetFileName(uri.LocalPath);

                                                WebClient webClient = new WebClient();
                                                string filePath = Path.Combine(saveFolder, filename);

                                                if (File.Exists(filePath))
                                                { }
                                                else
                                                {
                                                    webClient.DownloadFile(org.Source, filePath);
                                                    FlickrLogic.Log(filePath, NoticeType.DownloadDone, "Downloaded");
                                                }
                                            }
                                        }

                                    }
                                );
                            }
                            catch (Exception ex)
                            {

                            }
                        }


                    });
                    await task;
                }
                catch (Exception ex)
                {


                }
            }
        }
        public PhotoCollection GetPhoto_ByHashCode(string hashCode)
        {
            Flickr flickr = new Flickr();

            var op = new PhotoSearchOptions();
            op.Tags = string.Format("MD5:{0}", hashCode);

            var l = flickr.PhotosSearch(op);

            return l;
        }
 public FlickrLogic(string path)
     : base(path)
 {
     fFileLogic = new FFileLogic(path);
     flickr = new Flickr();
 }
예제 #11
0
        private static string GetDataResponseOAuth(Flickr flickr, string baseUrl, Dictionary <string, string> parameters)
        {
            string method = "POST";

            // Remove api key if it exists.
            if (parameters.ContainsKey("api_key"))
            {
                parameters.Remove("api_key");
            }
            if (parameters.ContainsKey("api_sig"))
            {
                parameters.Remove("api_sig");
            }

            if (!String.IsNullOrEmpty(Flickr.OAuthAccessTokenSecret) && !parameters.ContainsKey("oauth_signature"))
            {
                string sig = flickr.OAuthCalculateSignature(method, baseUrl, parameters, Flickr.OAuthAccessTokenSecret);
                parameters.Add("oauth_signature", sig);
            }

            // Calculate post data, content header and auth header
            string data       = OAuthCalculatePostData(parameters);
            string authHeader = OAuthCalculateAuthHeader(parameters);

            // Download data.
            try
            {
                return(DownloadData(method, baseUrl, data, PostContentType, authHeader));
            }
            catch (WebException ex)
            {
                if (ex.Status != WebExceptionStatus.ProtocolError)
                {
                    throw;
                }

                var response = ex.Response as HttpWebResponse;
                if (response == null)
                {
                    throw;
                }

                string responseData = null;

                using (var stream = response.GetResponseStream())
                {
                    if (stream != null)
                    {
                        using (var responseReader = new StreamReader(stream))
                        {
                            responseData = responseReader.ReadToEnd();
                            responseReader.Close();
                        }
                    }
                }
                if (response.StatusCode == HttpStatusCode.BadRequest ||
                    response.StatusCode == HttpStatusCode.Unauthorized)
                {
                    throw new OAuthException(responseData, ex);
                }

                if (String.IsNullOrEmpty(responseData))
                {
                    throw;
                }
                throw new WebException("WebException occurred with the following body content: " + responseData, ex, ex.Status, ex.Response);
            }
        }
        public static void ResetOAuth()
        {
            FUserLogic l = new FUserLogic();
            var v = l.GetFirst();
            if (v != null)
            {
                OAuthAccessToken = v.OAuthAccessToken;
                OAuthAccessTokenSecret = v.OAuthAccessTokenSecret;

                Flickr flickr = new Flickr();
                User = flickr.TestLogin();

                if (User == null || string.IsNullOrEmpty(User.UserName))
                {
                    OAuthAccessToken = "";
                    OAuthAccessTokenSecret = "";
                }
                else
                {
                    SetLogic setL = new SetLogic();
                    setL.DownloadPhotsets();

                    FFolderLogic.Scan();
                }
            }
        }
        private void loginToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //How to get token for user
            Flickr f = new Flickr();
            var r = f.OAuthGetRequestToken("oob");
            //Token = "72157637144674393-a524ed4dd4f6fe99"
            //TokenSecret = "a7f980526e160420"
            //Go to: http://www.flickr.com/services/oauth/authorize?oauth_token=72157637144674393-a524ed4dd4f6fe99

            string url = @"http://www.flickr.com/services/oauth/authorize?oauth_token=" + r.Token;
            ProcessStartInfo sInfo = new ProcessStartInfo(url);
            Process.Start(sInfo);

            Enter_Oauth_Verifier f1 = new Enter_Oauth_Verifier();
            f1.ShowDialog();
            var code = f1.Code;
            if (string.IsNullOrEmpty(code))
            {

            }
            else
            {
                //var v = f.OAuthGetAccessToken(r, "283-464-080");
                //OAuthAccessToken = "72157637144747383-7edb312576a89e3f";
                //OAuthAccessTokenSecret = "0095950bcad5ee34";

                var v = f.OAuthGetAccessToken(r, code);
                if (v != null)
                {
                    FUserLogic lo = new FUserLogic();
                    lo.CreateOrUpdate(v.UserId, v.Token, v.TokenSecret, v.Username, v.FullName);

                    //cancel any uploading process
                    FlickrLogic.CancellationTokenSrc.Cancel();

                    Flickr.ResetOAuth();
                    LoadGUIByUser();
                }
            }
        }