/// <summary> /// Create a list of table entries from a padoru collection /// </summary> /// <param name="pCollection">the padoru collection to create the list from</param> /// <returns>the table of contents data</returns> async Task <ToCData> CreateData(PadoruCollection pCollection) { //initialize jikan to resolve character names, shows, etc Jikan jikan = new Jikan(); //enumerate all padorus List <ToCEntry> tocCharacters = new List <ToCEntry>(); List <ToCCreator> tocCreators = new List <ToCCreator>(); List <ToCShow> tocShows = new List <ToCShow>(); foreach (PadoruEntry pEntry in pCollection.Entries) { //create entry with base values ToCEntry toc = new ToCEntry() { CharacterName = pEntry.Name, ContributorName = pEntry.ImageContributor, //CreatorName = pEntry.ImageCreator, SourceUrl = pEntry.ImageSource, ImageUrl = pEntry.ImageUrl }; //get character info from jikan if (pEntry.MALId.HasValue) { //get mal character Character charInfo = await jikan.GetCharacter(pEntry.MALId.Value); if (charInfo != null) { #region Get Character's shows List <ToCShow> cShows = new List <ToCShow>(); //get anime this character is in if (charInfo.Animeography.Count > 0) { foreach (MALImageSubItem anime in charInfo.Animeography) { //check if shows list already contains this anime ToCShow tocAnime = null; if (tocShows.HasAnyWhere((s) => s.MalId.Equals(anime.MalId))) { //show already exists, try to get it tocAnime = tocShows.Where((s) => s.MalId.Equals(anime.MalId)).FirstOrDefault(); } //create new toc show if still null (not in list or get from list failed) if (tocAnime == null) { tocAnime = new ToCShow() { Name = anime.Name, MalUrl = anime.Url, MalId = anime.MalId }; } //add current anime to list of shows cShows.Add(tocAnime); } } //get manga this character is in if (charInfo.Mangaography.Count > 0) { foreach (MALImageSubItem manga in charInfo.Mangaography) { //check if shows list already contains this anime ToCShow tocManga = null; if (tocShows.HasAnyWhere((s) => s.MalId.Equals(manga.MalId))) { //show already exists, try to get it tocManga = tocShows.Where((s) => s.MalId.Equals(manga.MalId)).FirstOrDefault(); } //create new toc show if still null (not in list or get from list failed) if (tocManga == null) { tocManga = new ToCShow() { Name = manga.Name, MalUrl = manga.Url, MalId = manga.MalId }; } //add current anime to list of shows cShows.Add(tocManga); } } //add current character to all shows foreach (ToCShow show in cShows) { show.Characters.Add(toc); } //set shows of character toc.CharacterShows = cShows; //add all shows to global list tocShows.AddRange(cShows); #endregion //override name and set mal id toc.CharacterName = charInfo.Name; toc.MalId = charInfo.MalId.ToString(); //set character nickname list if (charInfo.Nicknames.Count > 0) { toc.CharacterNicks = charInfo.Nicknames.ToArray(); } } } #region Set Creator Info //check if there is already a creator entry matching the current entrys creator name ToCCreator creator = null; if (tocCreators.HasAnyWhere((c) => c.Name.EqualsIgnoreCase(pEntry.ImageCreator))) { //already has a entry, try use that creator = tocCreators.Where((c) => c.Name.EqualsIgnoreCase(pEntry.ImageCreator)).FirstOrDefault(); } //create new creator entry if is still null (no creator found or get failed) if (creator == null) { //Create new creator creator = new ToCCreator() { Name = pEntry.ImageCreator }; //add it to the list of creators tocCreators.Add(creator); } //add this entry to the creator and this creator to the current entry creator.Entries.Add(toc); toc.Creator = creator; #endregion //add toc entry to list tocCharacters.Add(toc); } return(new ToCData() { Characters = tocCharacters, Creators = tocCreators, Shows = tocShows }); }
/// <summary> /// Create a Character page for the given toc entry /// </summary> /// <param name="toc">the toc entry to create a character page for</param> void CreateCharacterPage(ToCEntry toc, string savePath) { //create file Utils.CreateFileDir(savePath); using (TextWriter page = File.AppendText(savePath)) { //add page header page.WriteLine($"# {toc.CharacterName}"); page.WriteLine(); //add inline image //page.WriteLine($"![padoru]({toc.ImageUrl} \"{toc.CharacterName}\")"); page.WriteLine($"<img src=\"{toc.ImageUrl}\" height=\"300\">"); //add image info page.WriteLine(); page.WriteLine("### Image Info"); if (!string.IsNullOrWhiteSpace(toc.SourceUrl) && Uri.TryCreate(toc.SourceUrl, UriKind.Absolute, out Uri sourceUri)) { //Get the name of the page this was posted on string postedOn = sourceUri.Host; postedOn = postedOn.Replace("www.", ""); page.WriteLine($"* **Posted on:** [{postedOn}]({toc.SourceUrl})"); } //creator with creator page linked if (string.IsNullOrWhiteSpace(toc.Creator.PageUrl)) { page.WriteLine($"* **Created by:** {toc.Creator.Name}"); } else { page.WriteLine($"* **Created by:** [{toc.Creator.Name}]({toc.Creator.PageUrl})"); } page.WriteLine($"* **Contributor:** {toc.ContributorName}"); //add character info page.WriteLine(); page.WriteLine("### Character Info"); if (string.IsNullOrWhiteSpace(toc.MalUrl)) { page.WriteLine($"* **Name:** {toc.CharacterName}"); } else { page.WriteLine($"* **Name:** [{toc.CharacterName}]({toc.MalUrl})"); } //character nicknames if (toc.CharacterNicks != null && toc.CharacterNicks.Length > 0) { page.WriteLine("* **Nicknames:**"); foreach (string nick in toc.CharacterNicks) { page.WriteLine($" * {nick}"); } } //character shows + links if (toc.CharacterShows != null && toc.CharacterShows.Count > 0) { page.WriteLine("* **Shows:**"); foreach (ToCShow show in toc.CharacterShows) { bool hasPage = !string.IsNullOrWhiteSpace(show.PageUrl); bool hasMal = !string.IsNullOrWhiteSpace(show.MalUrl); if (hasPage && hasMal) { //has both show page and mal page, link to both page.WriteLine($" * [{show.Name}]({show.PageUrl}) - [__MAL__]({show.MalUrl})"); } else if (hasPage && !hasMal) { //has a show page but no mal page, link only show page page.WriteLine($" * [{show.Name}]({show.PageUrl})"); } else if (!hasPage && hasMal) { //has a mal page but no show page, link only mal page page.WriteLine($" * [{show.Name}]({show.MalUrl})"); } else { //has no mal or show page, link none page.WriteLine($" * {show.Name}"); } } } //empty lines to pad eventual additional entries page.WriteLine(); page.WriteLine(); } }