Beispiel #1
0
        public static IHtmlContent GetRecommendedMalAnimeHtml(int malAnimeId, GetRecsViewModel model)
        {
            string animeTitle    = model.Results.AnimeInfo[malAnimeId].Title;
            string encodedString = string.Format(@"<a href={0} class=""recommendation"">{1}</a>",
                                                 AttributeString(GetMalAnimeUrl(malAnimeId, animeTitle)), EncodeToString(animeTitle));

            return(new HtmlString(encodedString));
        }
        public async Task <IActionResult> GetRecs([FromBody] AnimeRecsInputJson input,
                                                  [FromServices] IOptionsSnapshot <Config.RecommendationsConfig> recConfig,
                                                  [FromServices] IMyAnimeListApiFactory malApiFactory, [FromServices] IAnimeRecsClientFactory recClientFactory,
                                                  [FromServices] IAnimeRecsDbConnectionFactory dbConnFactory, [FromServices] IRazorViewEngine viewEngine,
                                                  [FromServices] ITempDataProvider tempProvider)
        {
            if (!ModelState.IsValid)
            {
                AjaxError error = new AjaxError(ModelState);
                _logger.LogDebug("Invalid input received for GetRecs: {0}", error.Message);
                return(BadRequest(error));
            }

            if (input.RecSourceName == null)
            {
                input.RecSourceName = recConfig.Value.DefaultRecSource;
            }

            try
            {
                MalUserLookupResults userLookup = await GetUserLookupAsync(input, malApiFactory).ConfigureAwait(false);

                Dictionary <int, MalListEntry> animeList = new Dictionary <int, MalListEntry>();
                foreach (MyAnimeListEntry listEntry in userLookup.AnimeList)
                {
                    animeList[listEntry.AnimeInfo.AnimeId] = new AnimeRecs.RecEngine.MAL.MalListEntry((byte?)listEntry.Score, listEntry.Status, (short)listEntry.NumEpisodesWatched);
                }

                Dictionary <int, MalListEntry> animeWithheld = WithholdAnime(input, animeList);

                MalRecResults <IEnumerable <IRecommendation> > recResults = await GetRecommendationsAsync(input, recConfig.Value, animeList, animeWithheld, recClientFactory).ConfigureAwait(false);

                GetRecsViewModel viewModel = new GetRecsViewModel(
                    results: recResults,
                    userId: userLookup.UserId,
                    userName: userLookup.CanonicalUserName,
                    userLookup: userLookup,
                    userAnimeList: animeList,
                    maximumRecommendationsToReturn: recConfig.Value.MaximumRecommendationsToReturn,
                    maximumRecommendersToReturn: recConfig.Value.MaximumRecommendersToReturn,
                    animeWithheld: animeWithheld,
                    dbConnectionFactory: dbConnFactory
                    );

                RecResultsAsHtmlJson resultsJson = await GetResultHtmlAsync(viewModel, input, viewEngine, tempProvider).ConfigureAwait(false);

                return(Ok(resultsJson));
            }
            catch (ShortCircuitException ex)
            {
                return(ex.Result);
            }
        }
        private async Task <RecResultsAsHtmlJson> GetResultHtmlAsync(GetRecsViewModel viewModel, AnimeRecsInputJson input,
                                                                     IRazorViewEngine viewEngine, ITempDataProvider tempProvider)
        {
            // Support detailed results for the AnimeRecs recommendation type
            string viewName;

            if (viewModel.Results.RecommendationType.Equals(AnimeRecs.RecService.DTO.RecommendationTypes.AnimeRecs) && input.DisplayDetailedResults)
            {
                viewName = viewModel.Results.RecommendationType + "_complex";
            }
            else
            {
                viewName = viewModel.Results.RecommendationType;
            }

            IView            view;
            ViewEngineResult viewSearchResult = viewEngine.FindView(ControllerContext, viewName, isMainPage: false);

            // Try to use a view specific for the recommendation type, fall back to the generic view if no specific view exists.
            if (viewSearchResult.Success)
            {
                view = viewSearchResult.View;
            }
            else
            {
                string           fallbackViewName         = "Fallback";
                ViewEngineResult fallbackViewSearchResult = viewEngine.FindView(ControllerContext, fallbackViewName, isMainPage: false);
                if (!fallbackViewSearchResult.Success)
                {
                    throw new Exception($"Failed to find fallback view named {fallbackViewName}.");
                }
                view = fallbackViewSearchResult.View;
            }

            // Render the view to a string
            using (StringWriter writer = new StringWriter())
            {
                ViewDataDictionary viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary())
                {
                    Model = viewModel
                };

                TempDataDictionary tempDataDictionary = new TempDataDictionary(HttpContext, tempProvider);

                ViewContext viewcontext = new ViewContext(ControllerContext, view, viewDictionary, tempDataDictionary,
                                                          writer, new HtmlHelperOptions());

                await view.RenderAsync(viewcontext).ConfigureAwait(false);

                string renderedHtml = writer.ToString();
                return(new RecResultsAsHtmlJson(renderedHtml));
            }
        }
Beispiel #4
0
        public static IHtmlContent GetStreamLinksHtml(int malAnimeId, GetRecsViewModel model, IUrlHelper url)
        {
            List <string> streamLinks = new List <string>();

            if (model.StreamsByAnime.ContainsKey(malAnimeId))
            {
                foreach (streaming_service_anime_map serviceMap in model.StreamsByAnime[malAnimeId]
                         .OrderBy(map => map.streaming_service_id))
                {
                    StreamingService service = (StreamingService)serviceMap.streaming_service_id;
                    string           imagePath;
                    string           altText;
                    string           titleText;
                    switch (service)
                    {
                    case StreamingService.Crunchyroll:
                        imagePath = url.Content("Content/crunchyroll_icon.png");
                        altText   = "Crunchyroll";
                        titleText = "Watch on Crunchyroll";
                        break;

                    case StreamingService.Funimation:
                        imagePath = url.Content("Content/funimation_icon.png");
                        altText   = "Funimation";
                        titleText = "Watch on Funimation";
                        break;

                    case StreamingService.Viz:
                        imagePath = url.Content("Content/viz_icon.png");
                        altText   = "Viz";
                        titleText = "Watch on Viz";
                        break;

                    case StreamingService.Hulu:
                        imagePath = url.Content("Content/hulu_icon.png");
                        altText   = "Hulu";
                        titleText = "Watch on Hulu";
                        break;

                    case StreamingService.Viewster:
                        imagePath = url.Content("Content/viewster_icon.png");
                        altText   = "Viewster";
                        titleText = "Watch on Viewster";
                        break;

                    case StreamingService.Daisuki:
                        imagePath = url.Content("Content/daisuki_icon.png");
                        altText   = "Daisuki";
                        titleText = "Watch on Daisuki";
                        break;

                    case StreamingService.AnimeNetwork:
                        imagePath = url.Content("Content/anime_network_icon.png");
                        altText   = "Anime Network";
                        titleText = "Watch on The Anime Network";
                        break;

                    case StreamingService.AmazonPrime:
                        imagePath = url.Content("Content/amazon_prime_icon.png");
                        altText   = "Amazon Prime";
                        titleText = "Watch on Amazon Prime";
                        break;

                    case StreamingService.AmazonAnimeStrike:
                        imagePath = url.Content("Content/amazon_anime_strike_icon.png");
                        altText   = "Anime Strike";
                        titleText = "Watch on Amazon Anime Strike";
                        break;

                    case StreamingService.Hidive:
                        imagePath = url.Content("Content/hidive_icon.png");
                        altText   = "Hidive";
                        titleText = "Watch on Hidive";
                        break;

                    default:
                        continue;
                    }

                    string imgHtml = string.Format(@"<img src={0} title={1} alt={2} />",
                                                   AttributeString(imagePath), AttributeString(titleText), AttributeString(altText));

                    string linkHtml = string.Format(@"<a href={0}>{1}</a>", AttributeString(serviceMap.streaming_url), imgHtml);
                    streamLinks.Add(linkHtml);
                }
            }

            string linksHtml = string.Join(" ", streamLinks);

            return(new HtmlString(linksHtml));
        }