Example #1
0
        public static Account accountInfo(int account_id)
        {
            Account acc = new Account();

            using (WebClient wc = new WebClient()){
                wc.Headers.Add("Authorization", Imgur.getAuthBearer());
                try
                {
                    byte[] response   = wc.DownloadData("https://api.imgur.com/3/account/me");
                    string res        = System.Text.Encoding.Default.GetString(response);
                    var    JsonReader = JsonReaderWriterFactory.CreateJsonReader(response, new System.Xml.XmlDictionaryReaderQuotas());
                    var    root       = System.Xml.Linq.XElement.Load(JsonReader);
                    acc.id         = Int32.Parse(root.Element("data").Element("id").Value);
                    acc.url        = root.Element("data").Element("url").Value;
                    acc.bio        = root.Element("data").Element("bio").Value;
                    acc.reputation = float.Parse(root.Element("data").Element("reputation").Value);
                    acc.created    = Int32.Parse(root.Element("data").Element("created").Value);
                    if (!Int32.TryParse(root.Element("data").Element("pro_expiration").Value, out acc.pro_expiration))
                    {
                        acc.pro_expiration = 0;
                    }
                    acc.success = true;
                }
                catch (Exception e)
                {
                    acc.success = false;
                    acc.ex      = e;
                }
            }
            return(acc);
        }
Example #2
0
        public static ImageInfo toImgur(Bitmap bmp)
        {
            ImageConverter convert = new ImageConverter();

            byte[] toSend = (byte[])convert.ConvertTo(bmp, typeof(byte[]));
            using (WebClient wc = new WebClient())
            {
                NameValueCollection nvc = new NameValueCollection
                {
                    { "image", Convert.ToBase64String(toSend) }
                };
                wc.Headers.Add("Authorization", Imgur.getAuth());
                ImageInfo info = new ImageInfo();
                try
                {
                    byte[] response = wc.UploadValues("https://api.imgur.com/3/upload.xml", nvc);
                    string res      = System.Text.Encoding.Default.GetString(response);

                    var xmlDoc = new System.Xml.XmlDocument();
                    xmlDoc.LoadXml(res);
                    info.link       = new Uri(xmlDoc.SelectSingleNode("data/link").InnerText);
                    info.deletehash = xmlDoc.SelectSingleNode("data/deletehash").InnerText;
                    info.id         = xmlDoc.SelectSingleNode("data/id").InnerText;
                    info.success    = true;
                }
                catch (Exception e)
                {
                    info.success = false;
                    info.ex      = e;
                }
                return(info);
            }
        }
Example #3
0
        private void btnConfirmHelper()
        {
            btnConfirm.Invoke((MethodInvoker) delegate
            {
                btnConfirm.Enabled = false;
            });
            string pin = textboxPin.Text;

            if (String.IsNullOrWhiteSpace(pin))
            {
                pin = "Invalid PIN";
            }
            Imgur.AuthInfo info = Imgur.authAccount(textboxPin.Text);
            if (info.success)
            {
                Properties.Settings.Default.account_access_token  = info.access_token;
                Properties.Settings.Default.account_refresh_token = info.refresh_token;
                Properties.Settings.Default.account_id            = info.account_id;
                Properties.Settings.Default.account_authed        = true;
                Properties.Settings.Default.Save();
                refreshAccountStatus();
            }
            else
            {
                Globals.ErrorLog("Imgur.authAccount() Failed from btnConfirm_Click() : " + info.ex.Message, false);
            }

            btnConfirm.Invoke((MethodInvoker) delegate
            {
                btnConfirm.Enabled = true;
            });
        }
Example #4
0
 public static void deleteImage(ImageInfo info)
 {
     using (WebClient wc = new WebClient())
     {
         wc.Headers.Add("Authorization", Imgur.getAuth());
         byte[] response = wc.UploadValues(String.Format("https://api.imgur.com/3/image/{0}", info.deletehash), "DELETE", new NameValueCollection());
     }
 }
Example #5
0
        private void listViewLinks_ItemActivate(object sender, EventArgs e)
        {
            var toDelete = new Imgur.ImageInfo();

            toDelete.link       = new Uri(listViewLinks.SelectedItems[0].SubItems[0].Text);
            toDelete.deletehash = listViewLinks.SelectedItems[0].SubItems[1].Text;
            Imgur.deleteImage(toDelete);
            listViewLinks.SelectedItems[0].Remove();
            Notification.DisplayBubbleMessage(3, "Image Deletion", "You have deleted the image located at " + toDelete.link);
            GC.Collect();
        }
Example #6
0
        private void _UploadImage()
        {
            try
            {
                this.InvokeIfRequired(() =>
                {
                    while (this.Handle == null)
                    {
                        Application.DoEvents();
                    }
                    this.Hide();
                });

                Imgur.ImageInfo PictureLink = new Imgur.ImageInfo();
                PictureLink.success = false;

                bool bShouldRetry = Properties.Settings.Default.ShouldRetryUpload;
                int  numTries     = Properties.Settings.Default.ShouldRetryUpload ? Properties.Settings.Default.NumRetries + 1 : 1;

                for (int i = 1; i <= numTries && !PictureLink.success; i++)
                {
                    PictureLink = Imgur.toImgur(curImg);
                }

                if (PictureLink.success)
                {
                    bool shouldClipboard = Properties.Settings.Default.ClipboardOnUpload;
                    Notification.DisplayBubbleMessage(3, "Imgur Upload Completed", "Your image is live at " + PictureLink.link + "!" + (shouldClipboard ? " This link has been copied to your clipboard." : ""));
                    if (shouldClipboard)
                    {
                        Clipboard.SetText(PictureLink.link.ToString());
                    }
                    GC.Collect();
                    Globals.getMainForm().addImgurItem(PictureLink.link, PictureLink.deletehash);
                    curImg.Dispose();
                }
                else
                {
                    Globals.ErrorLog("Imgur.toImgur() failed : " + PictureLink.ex.Message, true);
                }

                this.Invoke((MethodInvoker) delegate
                {
                    this.Close();
                });
            }
            catch (Exception ex)
            {
                Globals.ErrorLog("_UploadImage() Failed: " + ex.Message, false);
            }
        }
Example #7
0
        private void refreshAccountStatus()
        {
            bool linked = Properties.Settings.Default.account_authed;

            lblLinked.InvokeIfRequired(() => {
                lblLinked.Text      = linked ? "Linked!" : "Not Linked!";
                lblLinked.ForeColor = linked ? Color.Green : Color.Red;
            });


            Imgur.Account acc = new Imgur.Account();
            acc.url = "none";
            if (linked)
            {
                acc = Imgur.accountInfo(Properties.Settings.Default.account_id);
                if (!acc.success)
                {
                    Globals.ErrorLog("Imgur.accountInfo() Failed from refreshAccountStatus() : " + acc.ex.Message, false);
                    acc.url = "Failed To Retrieve Username";
                    if (acc.ex.Message.Contains("403")) //Forbidden
                    {
                        UnlinkAccount();
                        Notification.DisplayBubbleMessage(5, "InfiniPad", "InfiniPad lost access to your Imgur account. Please relink it to continue usage.");
                        acc.url = "none";
                    }
                }
            }

            lblUsername.InvokeIfRequired(() =>
            {
                lblUsername.Text = linked ? "Signed in as " + acc.url : "";
            });

            lblAccountID.InvokeIfRequired(() =>
            {
                lblAccountID.Text = linked ? "ID: " + acc.id : "";
            });

            btnUnlink.InvokeIfRequired(() =>
            {
                btnUnlink.Visible = Properties.Settings.Default.account_authed;
            });
        }
Example #8
0
 public static AuthInfo authAccount(string pin)
 {
     using (WebClient wc = new WebClient())
     {
         NameValueCollection nvc = new NameValueCollection
         {
             { "client_id", APIKeys.ImgurClientID },
             { "client_secret", APIKeys.ImgurClientSecret },
             { "grant_type", "pin" },
             { "pin", pin },
         };
         wc.Headers.Add("Authorization", Imgur.getAuth());
         AuthInfo info = new AuthInfo();
         try
         {
             byte[] response   = wc.UploadValues("https://api.imgur.com/oauth2/token", nvc);
             string res        = System.Text.Encoding.Default.GetString(response);
             var    JsonReader = JsonReaderWriterFactory.CreateJsonReader(response, new System.Xml.XmlDictionaryReaderQuotas());
             var    root       = System.Xml.Linq.XElement.Load(JsonReader);
             info.access_token     = root.Element("access_token").Value;
             info.expires_in       = root.Element("expires_in").Value;
             info.token_type       = root.Element("token_type").Value;
             info.scope            = root.Element("scope").Value;
             info.refresh_token    = root.Element("refresh_token").Value;
             info.account_id       = Int32.Parse(root.Element("account_id").Value);
             info.account_username = root.Element("account_username").Value;
             info.success          = true;
         }
         catch (Exception e)
         {
             info.success = false;
             info.ex      = e;
         }
         return(info);
     }
 }