Example #1
0
        public static ImgurInfo RetrieveImgurInfo(string hash, string deleteHash)
        {
            string url = config.ImgurApiUrl + "/image/" + hash;

            LOG.InfoFormat("Retrieving Imgur info for {0} with url {1}", hash, url);
            HttpWebRequest webRequest = (HttpWebRequest)NetworkHelper.CreateWebRequest(url);

            webRequest.Method = "GET";
            webRequest.ServicePoint.Expect100Continue = false;
            string responseString;

            try {
                using (WebResponse response = webRequest.GetResponse()) {
                    LogCredits(response);
                    using (StreamReader reader = new StreamReader(response.GetResponseStream(), true)) {
                        responseString = reader.ReadToEnd();
                    }
                }
            } catch (WebException wE) {
                if (wE.Status == WebExceptionStatus.ProtocolError)
                {
                    if (((HttpWebResponse)wE.Response).StatusCode == HttpStatusCode.NotFound)
                    {
                        return(null);
                    }
                }
                throw;
            }
            LOG.Debug(responseString);
            ImgurInfo imgurInfo = ImgurInfo.ParseResponse(responseString);

            imgurInfo.DeleteHash = deleteHash;
            return(imgurInfo);
        }
Example #2
0
        /// <summary>
        /// Retrieve information on an imgur image
        /// </summary>
        /// <param name="hash"></param>
        /// <param name="deleteHash"></param>
        /// <returns>ImgurInfo</returns>
        public static ImgurInfo RetrieveImgurInfo(string hash, string deleteHash)
        {
            string url = Config.ImgurApi3Url + "/image/" + hash + ".xml";

            Log.InfoFormat("Retrieving Imgur info for {0} with url {1}", hash, url);
            HttpWebRequest webRequest = NetworkHelper.CreateWebRequest(url, HTTPMethod.GET);

            webRequest.ServicePoint.Expect100Continue = false;
            SetClientId(webRequest);
            string responseString = null;

            try {
                using (WebResponse response = webRequest.GetResponse()) {
                    LogRateLimitInfo(response);
                    var responseStream = response.GetResponseStream();
                    if (responseStream != null)
                    {
                        using (StreamReader reader = new StreamReader(responseStream, true))
                        {
                            responseString = reader.ReadToEnd();
                        }
                    }
                }
            } catch (WebException wE) {
                if (wE.Status == WebExceptionStatus.ProtocolError)
                {
                    if (((HttpWebResponse)wE.Response).StatusCode == HttpStatusCode.NotFound)
                    {
                        return(null);
                    }
                }
                throw;
            }
            ImgurInfo imgurInfo = null;

            if (responseString != null)
            {
                Log.Debug(responseString);
                imgurInfo            = ImgurInfo.ParseResponse(responseString);
                imgurInfo.DeleteHash = deleteHash;
            }
            return(imgurInfo);
        }
Example #3
0
        /// <summary>
        /// Do the actual upload to Imgur
        /// For more details on the available parameters, see: http://api.imgur.com/resources_anon
        /// </summary>
        /// <param name="surfaceToUpload">ISurface to upload</param>
        /// <param name="outputSettings">OutputSettings for the image file format</param>
        /// <param name="title">Title</param>
        /// <param name="filename">Filename</param>
        /// <returns>ImgurInfo with details</returns>
        public static ImgurInfo UploadToImgur(ISurface surfaceToUpload, SurfaceOutputSettings outputSettings, string title, string filename)
        {
            IDictionary <string, object> uploadParameters = new Dictionary <string, object>();
            IDictionary <string, object> otherParameters  = new Dictionary <string, object>();

            // add title
            if (title != null && config.AddTitle)
            {
                otherParameters.Add("title", title);
            }
            // add filename
            if (filename != null && config.AddFilename)
            {
                otherParameters.Add("name", filename);
            }
            string responseString = null;

            if (config.AnonymousAccess)
            {
                // add key, we only use the other parameters for the AnonymousAccess
                otherParameters.Add("key", IMGUR_ANONYMOUS_API_KEY);
                HttpWebRequest webRequest = (HttpWebRequest)NetworkHelper.CreateWebRequest(config.ImgurApiUrl + "/upload.xml?" + NetworkHelper.GenerateQueryParameters(otherParameters));
                webRequest.Method      = "POST";
                webRequest.ContentType = "image/" + outputSettings.Format.ToString();
                webRequest.ServicePoint.Expect100Continue = false;
                try {
                    using (var requestStream = webRequest.GetRequestStream()) {
                        ImageOutput.SaveToStream(surfaceToUpload, requestStream, outputSettings);
                    }

                    using (WebResponse response = webRequest.GetResponse()) {
                        using (StreamReader reader = new StreamReader(response.GetResponseStream(), true)) {
                            responseString = reader.ReadToEnd();
                        }
                        LogCredits(response);
                    }
                } catch (Exception ex) {
                    LOG.Error("Upload to imgur gave an exeption: ", ex);
                    throw;
                }
            }
            else
            {
                OAuthSession oAuth = new OAuthSession(ImgurCredentials.CONSUMER_KEY, ImgurCredentials.CONSUMER_SECRET);
                oAuth.BrowserSize     = new Size(650, 500);
                oAuth.CallbackUrl     = "http://getgreenshot.org";
                oAuth.AccessTokenUrl  = "http://api.imgur.com/oauth/access_token";
                oAuth.AuthorizeUrl    = "http://api.imgur.com/oauth/authorize";
                oAuth.RequestTokenUrl = "http://api.imgur.com/oauth/request_token";
                oAuth.LoginTitle      = "Imgur authorization";
                oAuth.Token           = config.ImgurToken;
                oAuth.TokenSecret     = config.ImgurTokenSecret;
                if (string.IsNullOrEmpty(oAuth.Token))
                {
                    if (!oAuth.Authorize())
                    {
                        return(null);
                    }
                    if (!string.IsNullOrEmpty(oAuth.Token))
                    {
                        config.ImgurToken = oAuth.Token;
                    }
                    if (!string.IsNullOrEmpty(oAuth.TokenSecret))
                    {
                        config.ImgurTokenSecret = oAuth.TokenSecret;
                    }
                    IniConfig.Save();
                }
                try {
                    otherParameters.Add("image", new SurfaceContainer(surfaceToUpload, outputSettings, filename));
                    responseString = oAuth.MakeOAuthRequest(HTTPMethod.POST, "http://api.imgur.com/2/account/images.xml", uploadParameters, otherParameters, null);
                } catch (Exception ex) {
                    LOG.Error("Upload to imgur gave an exeption: ", ex);
                    throw;
                } finally {
                    if (oAuth.Token != null)
                    {
                        config.ImgurToken = oAuth.Token;
                    }
                    if (oAuth.TokenSecret != null)
                    {
                        config.ImgurTokenSecret = oAuth.TokenSecret;
                    }
                    IniConfig.Save();
                }
            }
            return(ImgurInfo.ParseResponse(responseString));
        }
Example #4
0
        /// <summary>
        /// Do the actual upload to Imgur
        /// For more details on the available parameters, see: http://api.imgur.com/resources_anon
        /// </summary>
        /// <param name="surfaceToUpload">ISurface to upload</param>
        /// <param name="outputSettings">OutputSettings for the image file format</param>
        /// <param name="title">Title</param>
        /// <param name="filename">Filename</param>
        /// <returns>ImgurInfo with details</returns>
        public static ImgurInfo UploadToImgur(ISurface surfaceToUpload, SurfaceOutputSettings outputSettings, string title, string filename)
        {
            IDictionary <string, object> otherParameters = new Dictionary <string, object>();

            // add title
            if (title != null && Config.AddTitle)
            {
                otherParameters["title"] = title;
            }
            // add filename
            if (filename != null && Config.AddFilename)
            {
                otherParameters["name"] = filename;
            }
            string responseString = null;

            if (Config.AnonymousAccess)
            {
                // add key, we only use the other parameters for the AnonymousAccess
                //otherParameters.Add("key", IMGUR_ANONYMOUS_API_KEY);
                HttpWebRequest webRequest = NetworkHelper.CreateWebRequest(Config.ImgurApi3Url + "/upload.xml?" + NetworkHelper.GenerateQueryParameters(otherParameters), HTTPMethod.POST);
                webRequest.ContentType = "image/" + outputSettings.Format;
                webRequest.ServicePoint.Expect100Continue = false;

                SetClientId(webRequest);
                try {
                    using (var requestStream = webRequest.GetRequestStream()) {
                        ImageOutput.SaveToStream(surfaceToUpload, requestStream, outputSettings);
                    }

                    using (WebResponse response = webRequest.GetResponse())
                    {
                        LogRateLimitInfo(response);
                        var responseStream = response.GetResponseStream();
                        if (responseStream != null)
                        {
                            using (StreamReader reader = new StreamReader(responseStream, true))
                            {
                                responseString = reader.ReadToEnd();
                            }
                        }
                    }
                } catch (Exception ex) {
                    Log.Error("Upload to imgur gave an exeption: ", ex);
                    throw;
                }
            }
            else
            {
                var oauth2Settings = new OAuth2Settings
                {
                    AuthUrlPattern     = AuthUrlPattern,
                    TokenUrl           = TokenUrl,
                    RedirectUrl        = "https://imgur.com",
                    CloudServiceName   = "Imgur",
                    ClientId           = ImgurCredentials.CONSUMER_KEY,
                    ClientSecret       = ImgurCredentials.CONSUMER_SECRET,
                    AuthorizeMode      = OAuth2AuthorizeMode.EmbeddedBrowser,
                    BrowserSize        = new Size(680, 880),
                    RefreshToken       = Config.RefreshToken,
                    AccessToken        = Config.AccessToken,
                    AccessTokenExpires = Config.AccessTokenExpires
                };

                // Copy the settings from the config, which is kept in memory and on the disk

                try
                {
                    var webRequest = OAuth2Helper.CreateOAuth2WebRequest(HTTPMethod.POST, Config.ImgurApi3Url + "/upload.xml", oauth2Settings);
                    otherParameters["image"] = new SurfaceContainer(surfaceToUpload, outputSettings, filename);

                    NetworkHelper.WriteMultipartFormData(webRequest, otherParameters);

                    responseString = NetworkHelper.GetResponseAsString(webRequest);
                }
                finally
                {
                    // Copy the settings back to the config, so they are stored.
                    Config.RefreshToken       = oauth2Settings.RefreshToken;
                    Config.AccessToken        = oauth2Settings.AccessToken;
                    Config.AccessTokenExpires = oauth2Settings.AccessTokenExpires;
                    Config.IsDirty            = true;
                    IniConfig.Save();
                }
            }
            if (string.IsNullOrEmpty(responseString))
            {
                return(null);
            }
            return(ImgurInfo.ParseResponse(responseString));
        }