Example #1
0
 public MyAnimeListEntry(decimal?score, AnimeCompletionStatus status, int numEpisodesWatched, UncertainDate myStartDate,
                         UncertainDate myFinishDate, DateTime myLastUpdate, MalAnimeInfoFromUserLookup animeInfo, ICollection <string> tags)
 {
     Score              = score;
     Status             = status;
     NumEpisodesWatched = numEpisodesWatched;
     MyStartDate        = myStartDate;
     MyFinishDate       = myFinishDate;
     MyLastUpdate       = myLastUpdate;
     AnimeInfo          = animeInfo;
     Tags = tags;
 }
Example #2
0
        /// <summary>
        /// Parses XML obtained from malappinfo.php.
        /// </summary>
        /// <param name="doc"></param>
        /// <returns></returns>
        public static MalUserLookupResults Parse(XDocument doc)
        {
            Logging.Log.Trace("Parsing XML.");

            XElement error = doc.Root.Element("error");

            if (error != null && (string)error == "Invalid username")
            {
                throw new MalUserNotFoundException("No MAL list exists for this user.");
            }
            else if (error != null)
            {
                throw new MalApiException((string)error);
            }

            if (!doc.Root.HasElements)
            {
                throw new MalUserNotFoundException("No MAL list exists for this user.");
            }

            XElement myinfo            = GetExpectedElement(doc.Root, "myinfo");
            int      userId            = GetElementValueInt(myinfo, "user_id");
            string   canonicalUserName = GetElementValueString(myinfo, "user_name");

            List <MyAnimeListEntry> entries = new List <MyAnimeListEntry>();

            IEnumerable <XElement> animes = doc.Root.Elements("anime");

            foreach (XElement anime in animes)
            {
                int    animeId = GetElementValueInt(anime, "series_animedb_id");
                string title   = GetElementValueString(anime, "series_title");

                string   synonymList = GetElementValueString(anime, "series_synonyms");
                string[] rawSynonyms = synonymList.Split(SynonymSeparator, StringSplitOptions.RemoveEmptyEntries);

                // filter out synonyms that are the same as the main title
                HashSet <string> synonyms = new HashSet <string>(rawSynonyms.Where(synonym => !synonym.Equals(title, StringComparison.Ordinal)));

                int          seriesTypeInt = GetElementValueInt(anime, "series_type");
                MalAnimeType seriesType    = (MalAnimeType)seriesTypeInt;

                int numEpisodes = GetElementValueInt(anime, "series_episodes");

                int             seriesStatusInt = GetElementValueInt(anime, "series_status");
                MalSeriesStatus seriesStatus    = (MalSeriesStatus)seriesStatusInt;

                string        seriesStartString = GetElementValueString(anime, "series_start");
                UncertainDate seriesStart       = UncertainDate.FromMalDateString(seriesStartString);

                string        seriesEndString = GetElementValueString(anime, "series_end");
                UncertainDate seriesEnd       = UncertainDate.FromMalDateString(seriesEndString);

                string seriesImage = GetElementValueString(anime, "series_image");

                MalAnimeInfoFromUserLookup animeInfo = new MalAnimeInfoFromUserLookup(animeId: animeId, title: title,
                                                                                      type: seriesType, synonyms: synonyms, status: seriesStatus, numEpisodes: numEpisodes, startDate: seriesStart,
                                                                                      endDate: seriesEnd, imageUrl: seriesImage);


                int numEpisodesWatched = GetElementValueInt(anime, "my_watched_episodes");

                string        myStartDateString = GetElementValueString(anime, "my_start_date");
                UncertainDate myStartDate       = UncertainDate.FromMalDateString(myStartDateString);

                string        myFinishDateString = GetElementValueString(anime, "my_finish_date");
                UncertainDate myFinishDate       = UncertainDate.FromMalDateString(myFinishDateString);

                decimal rawScore = GetElementValueDecimal(anime, "my_score");
                decimal?myScore  = rawScore == 0 ? (decimal?)null : rawScore;

                int completionStatusInt           = GetElementValueInt(anime, "my_status");
                CompletionStatus completionStatus = (CompletionStatus)completionStatusInt;

                long     lastUpdatedUnixTimestamp = GetElementValueLong(anime, "my_last_updated");
                DateTime lastUpdated = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc) + TimeSpan.FromSeconds(lastUpdatedUnixTimestamp);

                string        rawTagsString = GetElementValueString(anime, "my_tags");
                string[]      untrimmedTags = rawTagsString.Split(TagSeparator, StringSplitOptions.RemoveEmptyEntries);
                List <string> tags          = new List <string>(untrimmedTags.Select(tag => tag.Trim()));

                MyAnimeListEntry entry = new MyAnimeListEntry(score: myScore, status: completionStatus, numEpisodesWatched: numEpisodesWatched,
                                                              myStartDate: myStartDate, myFinishDate: myFinishDate, myLastUpdate: lastUpdated, animeInfo: animeInfo, tags: tags);

                entries.Add(entry);
            }

            MalUserLookupResults results = new MalUserLookupResults(userId: userId, canonicalUserName: canonicalUserName, animeList: entries);

            Logging.Log.Trace("Parsed XML.");
            return(results);
        }