Exemplo n.º 1
0
        public async Task Winnie(CommandContext ctx)
        {
            using (HttpClient client = new HttpClient())
            {
                try
                {
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Client-ID", Program.instance.auth[1]);
                    string result = await client.GetStringAsync("https://api.imgur.com/3/album/VyRD4/images");

                    ImgurResponse res = JsonConvert.DeserializeObject <ImgurResponse>(result);
                    if (res.success)
                    {
                        await ctx.RespondAsync(res.data[Rand.Instance.Next(res.data.Length)].link);
                    }
                    else
                    {
                        await ctx.RespondAsync("Huh? Imgur isn't working properly! ;~;");
                    }
                }
                catch (Exception)
                {
                    await ctx.RespondAsync("Huh? Imgur isn't working properly! ;~;");
                }
            }
        }
Exemplo n.º 2
0
        public async Task TestGetBadAccount()
        {
            var imgurClient     = AuthenticationHelpers.CreateClientAuthenticatedImgurClient();
            var accountEndpoint = new AccountEndpoint(imgurClient);
            ImgurResponse <Account> imgurResponse = null;

            try
            {
                imgurResponse = await accountEndpoint.GetAccountDetailsAsync("black-dicks (this account doesn't exist, perfect for le test)");
            }
            catch (ImgurResponseFailedException exception)
            {
                // Assert the Response
                Assert.IsNotNull(exception.ImgurResponse.Data);
                Assert.AreEqual(exception.ImgurResponse.Success, false);
                Assert.AreEqual(exception.ImgurResponse.Status, HttpStatusCode.BadRequest);

                // Assert the Data
                Assert.AreEqual(exception.ImgurResponse.Data.ErrorDescription, "A username is required.");
                Assert.AreEqual(exception.ImgurResponse.Data.Method, "GET");
                Assert.AreEqual(exception.ImgurResponse.Data.Request, "/3/account/black-dicks (this account doesn't exist, perfect for le test)");
            }

            Assert.IsNull(imgurResponse);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Parses a list of Gallery Objects
        /// </summary>
        /// <param name="jsonObject">The <see cref="JObject"/> response from imgur</param>
        public ImgurResponse <IGalleryObject[]> ParseGalleryObjectArrayResponse(JObject jsonObject)
        {
            var imgurResponse = new ImgurResponse <IGalleryObject[]>
            {
                Success = jsonObject.Value <bool>("success"),
                Status  = (HttpStatusCode)jsonObject.Value <Int32>("status")
            };

            var galleryObjects = new List <IGalleryObject>();

            foreach (var child in jsonObject.SelectToken("data").Children())
            {
                if (child.Value <bool>("is_album"))
                {
                    galleryObjects.Add(JsonConvert.DeserializeObject <GalleryAlbum>(child.ToString()));
                }
                else
                {
                    galleryObjects.Add(JsonConvert.DeserializeObject <GalleryImage>(child.ToString()));
                }
            }

            imgurResponse.Data = galleryObjects.ToArray();
            return(imgurResponse);
        }
Exemplo n.º 4
0
 private static T GetResponseData <T>(ImgurResponse <T> response)
 {
     if (response != null)
     {
         return(response.Data);
     }
     return(default);
Exemplo n.º 5
0
        /// <summary>
        /// Uploads a new image
        /// </summary>
        /// <param name="imageBytes">Image to be uploaded in byte array</param>
        /// <returns></returns>
        public ImgurUploadResponse Upload(byte[] imageBytes)
        {
            try
            {
                // Try to upload the image three times
                int triesRemaining = 3;
                while (triesRemaining > 0)
                {
                    using (var webClient = new WebClient())
                    {
                        // Throw our access token into the header
                        webClient.Headers.Add("Authorization", $"Bearer {_accessToken}");

                        // The image to be uploaded needs be passed in the URL - put it in a NVC to make this easier
                        var values = new NameValueCollection();
                        // Imgur needs images to be base64 data
                        values.Add("image", Convert.ToBase64String(imageBytes));

                        try
                        {
                            // Upload the image and await a response
                            byte[] response = webClient.UploadValues("https://api.imgur.com/3/image", values);

                            // Deserialise response to get url and id
                            string        str      = Encoding.UTF8.GetString(response);
                            ImgurResponse data     = JsonConvert.DeserializeObject <ImgurResponse>(str);
                            string        imageUrl = data.data.link;
                            string        imageId  = data.data.id;

                            // If we have managed to upload a new image to imgur then record the upload time and url so that we can delete it after some time
                            //TODO: Move this out of this class
                            //File.AppendAllText("ImgurUploads.txt", DateTime.Now + "!:!" + imageId);

                            return(new ImgurUploadResponse(imageUrl, imageId));
                        }
                        catch
                        {
                            triesRemaining--;

                            // The reason for failing at this point could be that the access token has expired.
                            GetNewAccessToken();
                        }
                    }
                }
                return(new ImgurUploadResponse(false, "Retry limit uploading image to Imgur exceeded."));
            }
            catch (Exception ex)
            {
                return(new ImgurUploadResponse(false, $"Error while uploading image to Imgur. Error was: {ex.Message}"));
            }
        }
Exemplo n.º 6
0
        private void AddImage(Buddy buddy, ImgurResponse imgurResponse)
        {
            if (imgurResponse == null)
            {
                return;
            }

            BuddyTab tab = GetTabPage(buddy);

            if (tab == null)
            {
                return;
            }

            tab.AddImage(imgurResponse.UploadLinks.DirectLink);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Parses a Gallery Object
        /// </summary>
        /// <param name="jsonObject">The <see cref="JObject"/> response from imgur</param>
        public ImgurResponse <IGalleryObject> ParseGalleryObjectResponse(JObject jsonObject)
        {
            var imgurResponse = new ImgurResponse <IGalleryObject>
            {
                Success = jsonObject.Value <bool>("success"),
                Status  = (HttpStatusCode)jsonObject.Value <Int32>("status")
            };

            if (jsonObject.SelectToken("data").Value <bool>("is_album"))
            {
                imgurResponse.Data = JsonConvert.DeserializeObject <GalleryAlbum>(jsonObject.SelectToken("data").ToString());
            }
            else
            {
                imgurResponse.Data = JsonConvert.DeserializeObject <GalleryImage>(jsonObject.SelectToken("data").ToString());
            }

            return(imgurResponse);
        }
 public ImgurResponseFailedException(ImgurResponse <Error> imgurResponse, string message, Exception innerException)
     : base(message, innerException)
 {
     ImgurResponse = imgurResponse;
 }
 public ImgurResponseFailedException(ImgurResponse<Error> imgurResponse, string message, Exception innerException)
     : base(message, innerException)
 {
     ImgurResponse = imgurResponse;
 }
Exemplo n.º 10
0
        private void AddImage(Buddy buddy, ImgurResponse imgurResponse)
        {
            if (imgurResponse == null)
                return;

            BuddyTab tab = GetTabPage(buddy);

            if (tab == null)
                return;

            tab.AddImage(imgurResponse.UploadLinks.DirectLink);
        }
Exemplo n.º 11
0
        public UrlInfo GetUrlInfo(string Url)
        {
            UrlInfo info = new UrlInfo();

            foreach (string ending in ImageEndings)
            {
                if (Url.Contains("." + ending))
                {
                    info.Type = UrlInfo.FileType.Image;
                    info.Url  = Url;
                    return(info);
                }
            }

            foreach (string ending in VideoEndings)
            {
                if (Url.Contains("." + ending))
                {
                    info.Type = UrlInfo.FileType.Video;
                    info.Url  = Url;
                    return(info);
                }
            }

            foreach (string ending in AnimationEndings)
            {
                if (Url.Contains("." + ending))
                {
                    info.Type = UrlInfo.FileType.Animation;
                    info.Url  = Url;
                    return(info);
                }
            }

            foreach (string keyWord in KeyWords)
            {
                if (Url.Contains(keyWord))
                {
                    info.Type = UrlInfo.FileType.Link;
                    info.Url  = Url;
                    return(info);
                }
            }

            if (Url.Contains("imgur"))
            {
                string     apireq  = "https://api.imgur.com/3/image/" + Url.Split('/').Last();
                WebRequest request = WebRequest.Create(apireq);
                request.Headers.Add("Authorization", "Client-ID 3f568dfd4116c39");
                var           response = request.GetResponse();
                StreamReader  reader   = new StreamReader(response.GetResponseStream());
                string        json     = reader.ReadToEnd();
                ImgurResponse data     = JsonConvert.DeserializeObject <ImgurResponse>(json);
                if (data.data.animated)
                {
                    if (data.data.has_sound)
                    {
                        info.Type = UrlInfo.FileType.Video;
                    }
                    else
                    {
                        info.Type = UrlInfo.FileType.Animation;
                    }
                }
                else
                {
                    info.Type = UrlInfo.FileType.Image;
                }
                info.Url = data.data.link;
                return(info);
            }

            info.Type = UrlInfo.FileType.Link;
            info.Url  = Url;

            return(info);
        }