コード例 #1
0
ファイル: HtmlHelpers.cs プロジェクト: mageomageos/animerecs
        public static IHtmlString GetStreamLinksHtml(int malAnimeId, GetRecsViewModel model, IRenderContext renderContext)
        {
            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 = renderContext.ParsePath("~/Content/crunchyroll_icon.png");
                            altText = "Crunchyroll";
                            titleText = "Watch on Crunchyroll";
                            break;
                        case StreamingService.Funimation:
                            imagePath = renderContext.ParsePath("~/Content/funimation_icon.png");
                            altText = "Funimation";
                            titleText = "Watch on Funimation";
                            break;
                        case StreamingService.Viz:
                            imagePath = renderContext.ParsePath("~/Content/viz_icon.png");
                            altText = "Viz";
                            titleText = "Watch on Viz";
                            break;
                        case StreamingService.Hulu:
                            imagePath = renderContext.ParsePath("~/Content/hulu_icon.png");
                            altText = "Hulu";
                            titleText = "Watch on Hulu";
                            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 NonEncodedHtmlString(linksHtml);
        }
コード例 #2
0
        private Response DoGetRecs(AnimeRecsInputJson input)
        {
            if (input.RecSourceName == null)
            {
                input.RecSourceName = _config.DefaultRecSource;
            }

            MalUserLookupResults userLookup = GetUserLookup(input);

            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 = GetRecommendations(input, animeList, animeWithheld);

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

            RecResultsAsHtmlJson resultsJson = GetResultHtml(viewModel, input);
            return Response.AsJson(resultsJson);
        }
コード例 #3
0
ファイル: HtmlHelpers.cs プロジェクト: mageomageos/animerecs
        public static IHtmlString 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 NonEncodedHtmlString(encodedString);
        }
コード例 #4
0
        private RecResultsAsHtmlJson GetResultHtml(GetRecsViewModel viewModel, AnimeRecsInputJson input)
        {
            // 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;
            }

            // Try to use a view specific for the recommendation type, fall back to the generic view if no specific view exists.
            string viewPath = "Modules/GetRecs/" + viewName;
            ViewLocationResult viewLocation = _viewLocator.LocateView(viewPath, this.Context);
            if (viewLocation == null)
            {
                viewPath = "Modules/GetRecs/Fallback";
                viewLocation = _viewLocator.LocateView(viewPath, this.Context);
            }

            // Render view to string
            ViewLocationContext locationContext = new ViewLocationContext() { Context = this.Context, ModulePath = this.ModulePath, ModuleName = "GetRecs" };
            IRenderContext renderContext = _renderContextFactory.GetRenderContext(locationContext);
            using (Response renderedView = _viewEngine.RenderView(viewLocation, viewModel, renderContext, isPartial: true))
            using (MemoryStream stream = new MemoryStream())
            {
                // Write rendered view context to memory stream
                renderedView.Contents(stream);
                // Read contents from memory stream
                stream.Position = 0;
                using (StreamReader reader = new StreamReader(stream))
                {
                    string renderedHtml = reader.ReadToEnd();
                    return new RecResultsAsHtmlJson(renderedHtml);
                }
            }
        }