Пример #1
0
        public async Task GetImageAsync_Equal()
        {
            var mockUrl      = "https://api.imgur.com/3/image/zVpyzhW";
            var mockResponse = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(MockImageEndpointResponses.Imgur.GetImage)
            };

            var client   = new ImgurClient("123", "1234");
            var endpoint = new ImageEndpoint(client, new HttpClient(new MockHttpMessageHandler(mockUrl, mockResponse)));
            var image    = await endpoint.GetImageAsync("zVpyzhW").ConfigureAwait(false);

            Assert.NotNull(image);
            Assert.Equal("zVpyzhW", image.Id);
            Assert.Equal("Look Mom, it's Bambi!", image.Title);
            Assert.Equal(null, image.Description);
            Assert.Equal(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(1440259938), image.DateTime);
            Assert.Equal("image/gif", image.Type);
            Assert.Equal(true, image.Animated);
            Assert.Equal(426, image.Width);
            Assert.Equal(240, image.Height);
            Assert.Equal(26270273, image.Size);
            Assert.Equal(3185896, image.Views);
            Assert.InRange(image.Bandwidth, 1, long.MaxValue);
            Assert.Equal(VoteOption.Up, image.Vote);
            Assert.Equal(false, image.Favorite);
            Assert.Equal(false, image.Nsfw);
            Assert.Equal("Eyebleach", image.Section);
            Assert.Equal("http://i.imgur.com/zVpyzhW.gifv", image.Gifv);
            Assert.Equal("http://i.imgur.com/zVpyzhW.mp4", image.Mp4);
            Assert.Equal("http://i.imgur.com/zVpyzhWh.gif", image.Link);
            Assert.Equal(true, image.Looping);
            Assert.Equal(true, image.InGallery);
            Assert.Equal(595876, image.Mp4Size);
        }
Пример #2
0
        async Task <IImage> AccessTheWebAsync()
        {
            var client   = new ImgurClient(token);
            var endpoint = new ImageEndpoint(client);
            var images   = await endpoint.GetImageAsync(imagelink);

            return(images);
        }
Пример #3
0
 public string GetImage()
 {
     try
     {
         var client   = new ImgurClient("1c3d2dc52ecfa75", "157c59d9c9e52a7097376878b4e6b6867d934427");
         var endpoint = new ImageEndpoint(client);
         var image    = endpoint.GetImageAsync("IMAGE_ID").GetAwaiter().GetResult();
         return(image.Link);
     }
     catch (ImgurException imgurEx)
     {
         return(imgurEx.Message);
     }
 }
Пример #4
0
        public async Task GetImageAsync_WithIdNull_ThrowsArgumentNullException()
        {
            var client   = new ImgurClient("123", "1234");
            var endpoint = new ImageEndpoint(client);

            var exception =
                await
                Record.ExceptionAsync(
                    async() => await endpoint.GetImageAsync(null).ConfigureAwait(false))
                .ConfigureAwait(false);

            Assert.NotNull(exception);
            Assert.IsType <ArgumentNullException>(exception);
        }
Пример #5
0
        /**
         * Get an Image from the Account.
         *
         * @param id ID of the image.
         */
        private async Task <IImage> GetImageFromId(string id)
        {
            try
            {
                var image = await imageEndpoint.GetImageAsync(id);

                return(image);
            }
            catch (ImgurException imgurEx)
            {
                Debug.Write("An error occurred getting an image from Imgur.");
                Debug.Write(imgurEx.Message);
                return(null);
            }
        }
Пример #6
0
        public async Task GetImage()
        {
            try
            {
                var endpoint = new ImageEndpoint(Imgur);
                var image    = await endpoint.GetImageAsync("IMAGE_ID");

                Console.WriteLine("Image retrieved. Image Url: " + image.Link);
            }
            catch (ImgurException imgurEx)
            {
                Console.WriteLine("An error occurred getting an image from Imgur.");
                Console.WriteLine(imgurEx.Message);
            }
        }
Пример #7
0
        async void GetImages()
        {
            var client   = new ImgurClient(ClientID);
            var endpoint = new GalleryEndpoint(client);
            var gallery  = await endpoint.GetGalleryAsync();

            foreach (var item in gallery)
            {
                if (item.GetType() == typeof(Imgur.API.Models.Impl.GalleryImage))
                {
                    var endp = new ImageEndpoint(client);
                    var im   = endp.GetImageAsync(item.GetType().GetProperty("Id").GetValue(item).ToString());
                    ImagesFromGallery.Add(im.Result);
                }
            }
        }
Пример #8
0
        public async Task GetImage(string imageId)
        {
            try
            {
                var client   = new ImgurClient("4d5ac665bcb6d0e", "16fc948803ebe3dd614cb08ec074d90179f477c4");
                var endpoint = new ImageEndpoint(client);
                var image    = await endpoint.GetImageAsync(imageId);

                Debug.Write("Image retrieved. Image Url: " + image.Link);
            }
            catch (ImgurException imgurEx)
            {
                Debug.Write("An error occurred getting an image from Imgur.");
                Debug.Write(imgurEx.Message);
            }
        }
Пример #9
0
        public async Task GetImage()
        {
            try
            {
                var endpoint = new ImageEndpoint(client);
                var image    = await endpoint.GetImageAsync("  ");

                CurrentSearch.Clear();
                CurrentSearch.Add(fillPicture(image));
                Debug.Write("Image retrieved. Image Url: " + image.Link);
            }
            catch (ImgurException imgurEx)
            {
                Debug.Write("An error occurred getting an image from Imgur.");
                Debug.Write(imgurEx.Message);
            }
        }
Пример #10
0
        public async Task GetImageAsync_WithImageNull_ThrowsArgumentNullException()
        {
            var apiClient = new ApiClient("123");
            var endpoint  = new ImageEndpoint(apiClient, new HttpClient());

            var exception = await Record.ExceptionAsync(async() =>
            {
                await endpoint.GetImageAsync(null);
            });

            Assert.NotNull(exception);
            Assert.IsType <ArgumentNullException>(exception);

            var argNullException = (ArgumentNullException)exception;

            Assert.Equal("imageId", argNullException.ParamName);
        }
Пример #11
0
 private string DownloadImage(int imageId)
 {
     try
     {
         var client   = new ImgurClient("a7d89efd3720246", "f67e11527f51f7dae687900806901fed26b07e42");
         var endpoint = new ImageEndpoint(client);
         var image    = endpoint.GetImageAsync("IMAGE_ID").GetAwaiter().GetResult();
         Debug.Write("Image retrieved. Image Url: " + image.Link);
         return(image.Link);
     }
     catch (ImgurException imgurEx)
     {
         Debug.Write("An error occurred getting an image from Imgur.");
         Debug.Write(imgurEx.Message);
         return(string.Empty);
     }
 }
Пример #12
0
        /// <summary>
        /// Gets image from its ID assigned.
        /// </summary>
        /// <param name="imageID"></param>
        /// <returns></returns>
        public static async Task <string> GetImage(string imageID)
        {
            try
            {
                await InitiateClient(false);

                disable = false;
                var image = await endpoint.GetImageAsync(imageID);

                return(image.Link);
            }
            catch (Exception n)
            {
                ExtensionMethods.WriteToLog(ExtensionMethods.LogType.ErrorLog, n, $"From Get Image: {imageID}");
                throw;
            }
        }
Пример #13
0
        public async Task GetImageAsync_Equal()
        {
            var mockUrl      = "https://api.imgur.com/3/image/mvWNMH4";
            var mockResponse = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(MockImageResponses.GetImage)
            };

            var apiClient  = new ApiClient("123");
            var httpClient = new HttpClient(new MockHttpMessageHandler(mockUrl, mockResponse));
            var endpoint   = new ImageEndpoint(apiClient, httpClient);
            var response   = await endpoint.GetImageAsync("mvWNMH4");

            Assert.NotNull(response);
            Assert.IsAssignableFrom <IImage>(response);
            Assert.Equal("mvWNMH4", response.Id);
        }
Пример #14
0
        public async Task BossFight()
        {
            using (this.Context.Channel.EnterTypingState())
            {
                IUserMessage introMessage = await this.ReplyAsync("Wild boss appears! *cue boss music*");

                BotWebAgent webAgent  = new BotWebAgent(Globals.Settings.RedditUsername, Globals.Settings.RedditPass, Globals.Settings.RedditClientID, Globals.Settings.RedditSecret, "https://github.com/JordanZeotni/CSharpDewott");
                Reddit      reddit    = new Reddit(webAgent, false);
                Subreddit   subreddit = reddit.GetSubreddit("/r/Bossfight/");

                Post selectedPost = subreddit.GetTop(FromTime.All).Where(e => !e.NSFW).ToList()[Globals.Random.Next(0, subreddit.Posts.Count(e => !e.NSFW))];

                Regex linkParser = new Regex(@"\b(?:https?://|www\.)\S+\b", RegexOptions.Compiled | RegexOptions.IgnoreCase);

                string imageLink = selectedPost.Url.ToString();
                if (imageLink.ToLower().Contains("imgur"))
                {
                    ImgurClient   client   = new ImgurClient(Globals.Settings.ImgurClientId);
                    ImageEndpoint endpoint = new ImageEndpoint(client);
                    imageLink = (await endpoint.GetImageAsync(Regex.Replace(Regex.Replace(imageLink, @".+imgur\.com/", string.Empty), @"\..+", string.Empty))).Link;
                }

                EmbedBuilder builder = new EmbedBuilder
                {
                    Author = new EmbedAuthorBuilder
                    {
                        Name = selectedPost.Title,
                        Url  = selectedPost.Shortlink
                    },
                    ImageUrl = imageLink,
                    Footer   = new EmbedFooterBuilder
                    {
                        Text    = $"Author: {selectedPost.Author.Name}",
                        IconUrl = "https://media.glassdoor.com/sqll/796358/reddit-squarelogo-1490630845152.png"
                    }
                };

                builder.WithColor(Color.OrangeRed);

                await introMessage.DeleteAsync();

                await this.ReplyAsync(string.Empty, false, builder.Build());
            }
        }
Пример #15
0
        public async Task UpdateImage_UpdatesImage()
        {
            var apiClient  = _fixture.GetApiClientWithKey();
            var httpClient = new HttpClient();

            var filePath = Path.Combine(Directory.GetCurrentDirectory(), "banana.jpg");

            using var fileStream = File.OpenRead(filePath);

            var imageEndpoint = new ImageEndpoint(apiClient, httpClient);

            var imageUpload = await imageEndpoint.UploadImageAsync(fileStream, title : "uploaded");

            await imageEndpoint.UpdateImageAsync(imageUpload.DeleteHash, title : "updated");

            var image = await imageEndpoint.GetImageAsync(imageUpload.Id);

            var imageDeleted = await imageEndpoint.DeleteImageAsync(imageUpload.DeleteHash);

            Assert.NotNull(imageUpload);
            Assert.Equal("uploaded", imageUpload.Title);
            Assert.Equal("updated", image.Title);
            Assert.True(imageDeleted);
        }
        public async Task GetImageAsync_WithImage_AreEqual(IImage actualImage)
        {
            var client   = new MashapeClient(ClientId, ClientSecret, MashapeKey, OAuth2Token);
            var endpoint = new ImageEndpoint(client);

            var expectedImage = await endpoint.GetImageAsync(actualImage.Id);

            Assert.AreEqual(actualImage.Id, expectedImage.Id);
            Assert.AreEqual(actualImage.Title, expectedImage.Title);
            Assert.AreEqual(actualImage.Description, expectedImage.Description);
            Assert.AreEqual(actualImage.DateTime, expectedImage.DateTime);
            Assert.AreEqual(actualImage.Type, expectedImage.Type);
            Assert.AreEqual(actualImage.Animated, expectedImage.Animated);
            Assert.AreEqual(actualImage.Width, expectedImage.Width);
            Assert.AreEqual(actualImage.Height, expectedImage.Height);
            Assert.AreEqual(actualImage.Size, expectedImage.Size);
            Assert.AreEqual(actualImage.Link, expectedImage.Link);
            Assert.AreEqual(actualImage.Gifv, expectedImage.Gifv);
            Assert.AreEqual(actualImage.Mp4, expectedImage.Mp4);
            Assert.AreEqual(actualImage.Webm, expectedImage.Webm);
            Assert.AreEqual(actualImage.Looping, expectedImage.Looping);
            Assert.AreEqual(actualImage.Favorite, expectedImage.Favorite);
            Assert.AreEqual(actualImage.Nsfw, expectedImage.Nsfw);
        }
Пример #17
0
        public async Task GetImageAsync_AreEqual()
        {
            var fakeHttpMessageHandler = new FakeHttpMessageHandler();
            var fakeResponse           = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(ImageEndpointResponses.Imgur.GetImageResponse)
            };

            fakeHttpMessageHandler.AddFakeResponse(new Uri("https://api.imgur.com/3/image/zVpyzhW"), fakeResponse);

            var client   = new ImgurClient("123", "1234");
            var endpoint = new ImageEndpoint(client, new HttpClient(fakeHttpMessageHandler));
            var image    = await endpoint.GetImageAsync("zVpyzhW");

            Assert.IsNotNull(image);
            Assert.AreEqual("zVpyzhW", image.Id);
            Assert.AreEqual("Look Mom, it's Bambi!", image.Title);
            Assert.AreEqual(null, image.Description);
            Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(1440259938), image.DateTime);
            Assert.AreEqual("image/gif", image.Type);
            Assert.AreEqual(true, image.Animated);
            Assert.AreEqual(426, image.Width);
            Assert.AreEqual(240, image.Height);
            Assert.AreEqual(26270273, image.Size);
            Assert.AreEqual(1583864, image.Views);
            Assert.AreEqual(41608539674872, image.Bandwidth);
            Assert.AreEqual(null, image.Vote);
            Assert.AreEqual(false, image.Favorite);
            Assert.AreEqual(false, image.Nsfw);
            Assert.AreEqual("Eyebleach", image.Section);
            Assert.AreEqual("http://i.imgur.com/zVpyzhW.gifv", image.Gifv);
            Assert.AreEqual("http://i.imgur.com/zVpyzhW.webm", image.Webm);
            Assert.AreEqual("http://i.imgur.com/zVpyzhW.mp4", image.Mp4);
            Assert.AreEqual("http://i.imgur.com/zVpyzhWh.gif", image.Link);
            Assert.AreEqual(true, image.Looping);
        }
Пример #18
0
        public async Task <IEnumerable <IImage> > GetImgurImageAsync(string id, CancellationToken token)
        {
            try
            {
                token.ThrowIfCancellationRequested();
                var error = string.Empty;

                //TODO:: Move to config
                //       Given this is using entirely anonymous calls, there is no reason to keep the id and secret...well, a secret.
                //       If this ever becomes popular, and we want to make authenticated calls; then I will register a new client for that purpose.
                var client = new ImgurClient("5b7f2dbb306592e", "c6321d265e604df392cabd1c1d1fd5002bb918f0");

                var albumEndpoint = new AlbumEndpoint(client);
                try
                {
                    var album = await albumEndpoint.GetAlbumAsync(id);

                    if (album != null)
                    {
                        return(album.Images);
                    }
                }
                catch (Exception e)
                {
                    error = e.Message;
                }

                token.ThrowIfCancellationRequested();
                var imageEndpoint = new ImageEndpoint(client);

                try
                {
                    var image = await imageEndpoint.GetImageAsync(id);

                    if (image != null)
                    {
                        return new List <IImage> {
                                   image
                        }
                    }
                    ;
                }
                catch (Exception e)
                {
                    error = error + e.Message;
                }

                if (!string.IsNullOrWhiteSpace(error))
                {
                    Log.Error(error);
                }

                return(new List <IImage>());
            }
            catch (ImgurException imgurEx)
            {
                Log.Error("An error occurred getting an image from Imgur.");
                Log.Error(imgurEx.Message);
                return(null);
            }
        }
Пример #19
0
 public async Task GetImageAsync_WithIdNull_ThrowsArgumentNullException()
 {
     var client   = new ImgurClient("123", "1234");
     var endpoint = new ImageEndpoint(client);
     await endpoint.GetImageAsync(null);
 }