Beispiel #1
0
        /// <summary>
        /// Finds the closest anime (based on the user query)
        /// </summary>
        public void GetAndSetToClosestAnime()
        {
            searchResultOfAnime = jikanInterface.SearchAnime(_animeName).Result;
            if (searchResultOfAnime == null)
            {
                GetAndSetToRandomAnime();
            }
            else
            {
                _animeName = searchResultOfAnime.Results.First().Title;
                _malId     = searchResultOfAnime.Results.First().MalId;
                anime      = jikanInterface.GetAnime(_malId).Result;

                //If NSFW content is looked up, search up another anime
                if (anime.Rating.Contains("Rx"))
                {
                    GetAndSetToRandomAnime();
                }
                else
                {
                    animeName       = _animeName;
                    _scoreOfAnime   = (float)searchResultOfAnime.Results.First().Score;
                    _reviewsOfAnime = jikanInterface.GetAnimeReviews(_malId).Result;
                    _animePictures  = jikanInterface.GetAnimePictures(_malId).Result;
                }
            }
        }
Beispiel #2
0
        public async Task GetAnimeGetManga_CorrectId_ShouldUseIMalEntityInterface()
        {
            IMalEntity berserk = await jikan.GetManga(2);

            IMalEntity bebop = await jikan.GetAnime(1);

            List <IMalEntity> entities = new List <IMalEntity>();

            entities.Add(berserk);
            entities.Add(bebop);

            Assert.Contains(1, entities.Select(x => x.MalId));
        }
        public Anime GetAnime(long animeId)
        {
            var context = _serviceProvider.CreateScope().ServiceProvider.GetRequiredService <ApplicationDbContext>();
            var anime   = context.Animes.FirstOrDefault(x => x.MalId == animeId);

            if (anime == null)
            {
                try
                {
                    anime = _jikan.GetAnime(animeId).GetAwaiter().GetResult();

                    context.Animes.Add(anime);
                    var scheduled = context.ScheduledAnimes.FirstOrDefault(x => x.AnimeId == anime.MalId);
                    if (scheduled != null)
                    {
                        context.ScheduledAnimes.Remove(scheduled);
                    }
                    context.SaveChanges();
                }
                catch (JikanRequestException je)
                {
                    // wait and try again
                    Thread.Sleep(500);
                    anime = GetAnime(animeId);
                }
                catch (Exception e)
                {
                    _logger.LogError($"{nameof(JikanService)}: something went wrong.", e.Message, e.InnerException);
                }
            }

            return(anime);
        }
Beispiel #4
0
        // tries to get the detailed anime information about anime with the given mal id, retries after given delay until the internet connection is working
        public async Task <Anime> TryToGetAnimeInfo(long malId, int millisecondsDelay, IJikan jikanWrapper)
        {
            Anime output = null;

            // if there is no response from API wait for the given time and retry
            while (output == null)
            {
                try
                {
                    // get detailed anime info from the jikan API
                    output = await jikanWrapper.GetAnime(malId);
                }
                catch (System.Net.Http.HttpRequestException)
                {
                    throw new NoInternetConnectionException("No internet connection");
                }
                catch (JikanDotNet.Exceptions.JikanRequestException)
                {
                    await Task.Delay(millisecondsDelay);
                }
                finally
                {
                    await Task.Delay(millisecondsDelay);
                }
            }
            return(output);
        }
Beispiel #5
0
        private void DoWork(object state)
        {
            var dbContext = _serviceProvider.CreateScope().ServiceProvider.GetRequiredService <ApplicationDbContext>();

            var   oldestRecord = dbContext.ScheduledAnimes.OrderBy(x => x.AddedDateTime).FirstOrDefault();
            Anime anime        = null;

            if (oldestRecord != null)
            {
                try
                {
                    anime = _jikan.GetAnime(oldestRecord.AnimeId).GetAwaiter().GetResult();

                    dbContext.Animes.AddIfNotExists(anime);
                    dbContext.ScheduledAnimes.Remove(oldestRecord);

                    dbContext.SaveChanges();

                    _logger.LogInformation($"{tag}: downloaded anime \"{anime?.Title}\"");
                }
                catch (Exception e)
                {
                    _logger.LogError($"{tag}: error.", e.Message, e.InnerException);

                    dbContext.ScheduledAnimes.AddIfNotExists(oldestRecord);
                    dbContext.SaveChanges();
                }
            }
        }
Beispiel #6
0
        public async Task GetAnimeGetManga_CorrectId_ShouldUseIMalEntityInterface()
        {
            // Given
            IMalEntity berserk = await _jikan.GetManga(2);

            IMalEntity bebop = await _jikan.GetAnime(1);

            List <IMalEntity> entities = new List <IMalEntity>
            {
                berserk,
                bebop
            };

            // When
            var ids = entities.Select(x => x.MalId);

            // Then
            ids.Should().Contain(1);
        }
Beispiel #7
0
        public void ShouldUseIMalEntityInterface()
        {
            IMalEntity berserk = Task.Run(() => jikan.GetManga(2)).Result;
            IMalEntity bebop   = Task.Run(() => jikan.GetAnime(1)).Result;

            List <IMalEntity> entities = new List <IMalEntity>();

            entities.Add(berserk);
            entities.Add(bebop);

            Assert.Contains(1, entities.Select(x => x.MalId));
        }
Beispiel #8
0
        public async Task <MalAnimeUpdateData> GetAnimeDataAsync(long malId)
        {
            var parsedData = await _jikanClient.GetAnime(malId);

            if (parsedData is null)
            {
                return(null);
            }

            return(new MalAnimeUpdateData(
                       parsedData.Title,
                       parsedData.Synopsis,
                       parsedData.TitleEnglish,
                       parsedData.TitleJapanese,
                       (parsedData.TitleSynonyms != null && parsedData.TitleSynonyms.Any()) ? string.Join(';', parsedData.TitleSynonyms) : string.Empty,
                       parsedData.Members,
                       EmptyStringIfPlaceholder(parsedData.ImageURL),
                       parsedData.Aired?.From,
                       parsedData.Type,
                       parsedData.Status,
                       parsedData.Premiered
                       ));
        }
Beispiel #9
0
        private async Task <JikanDotNet.Anime> SendSingleAnimeRequest(long malId, short retryCount)
        {
            JikanDotNet.Anime anime = null;
            await Task.Delay(3000 + retryCount * 10000);

            try
            {
                anime = await jikan.GetAnime(malId);
            }
            catch (Exception ex)
            {
                if (retryCount < 10)
                {
                    if (ex.InnerException is JikanRequestException && (ex.InnerException as JikanRequestException).ResponseCode == System.Net.HttpStatusCode.TooManyRequests)
                    {
                        retryCount++;
                        return(await SendSingleAnimeRequest(malId, retryCount));
                    }
                }
            }

            return(anime);
        }
Beispiel #10
0
 public void GetAnime_WrongIdDoNotSurpressExceptions_ShouldThrowJikanRequestExceptionGetAnime()
 {
     Assert.ThrowsAnyAsync <JikanRequestException>(() => jikan.GetAnime(2));
 }
Beispiel #11
0
        public async Task GetAnime_CorrectId_ShouldReturnNotNullAnime(long malId)
        {
            Anime returnedAnime = await _jikan.GetAnime(malId);

            Assert.NotNull(returnedAnime);
        }
Beispiel #12
0
 public void ShouldThrowJikanRequestExceptionGetAnime()
 {
     Assert.ThrowsAnyAsync <JikanRequestException>(() => jikan.GetAnime(2));
 }
        protected override async void OnAppearing()
        {
            BindingContext = await jikan.GetAnime(_anime.MalId);

            base.OnAppearing();
        }
Beispiel #14
0
        public void ShouldReturnNotNullAnime(long malId)
        {
            Anime returnedAnime = Task.Run(() => jikan.GetAnime(malId)).Result;

            Assert.NotNull(returnedAnime);
        }
Beispiel #15
0
        public async Task GetAnime_CorrectId_ShouldReturnNotNullAnime(long malId)
        {
            // When
            Anime returnedAnime = await _jikan.GetAnime(malId);

            // Then
            returnedAnime.Should().NotBeNull();
        }