Esempio n. 1
0
        public static MalAnime SearchListForShow(string username, string title)
        {
            // Clean out any accented characters from the show title
            var tempBytes = System.Text.Encoding.GetEncoding("ISO-8859-8").GetBytes(title);

            title = System.Text.Encoding.UTF8.GetString(tempBytes);

            var searchRequest = WebRequest.Create(string.Format(MalListUrl, username)) as HttpWebRequest;

            searchRequest.Method = "GET";

            var searchResponse = (HttpWebResponse)searchRequest.GetResponse();

            string animeList = string.Empty;

            using (var reader = new System.IO.StreamReader(searchResponse.GetResponseStream(), ASCIIEncoding.ASCII))
            {
                // this is XML
                animeList = reader.ReadToEnd();
            }

            var responseAsXml = new XmlDocument();

            responseAsXml.LoadXml(animeList);
            var responseAsJson = JsonConvert.SerializeXmlNode(responseAsXml.GetElementsByTagName("myanimelist")[0]);
            var malList        = JsonConvert.DeserializeObject <MyAnimeList>(responseAsJson);

            var currentlyWatching = from show in malList.AnimeList.AllAnime
                                    where show.MyStatus == MalStatus.Watching
                                    select show;

            MalAnime currentShow = new MalAnime();

            try
            {
                currentShow = (from show in currentlyWatching
                               where (TitleComparer.Compute(show.Title, title) < 5 || show.Title.ToLower().Contains(title.ToLower()))
                               select show).First();
            }
            catch
            {
                //Usually the title from Plex is the English title and not the default Japanese title. Check the Synonyms.

                foreach (var show in currentlyWatching)
                {
                    foreach (var altTitle in show.Synonyms.Replace("; ", ";").ToLower().Split(';'))
                    {
                        currentShow = (altTitle.Contains(title.ToLower())) ? show : null;
                        if (currentShow != null)
                        {
                            break;
                        }
                    }

                    if (currentShow != null)
                    {
                        break;
                    }
                }

                if (currentShow == null)
                {
                    // Last resort, split the show by spaces and try to find a word
                    try
                    {
                        foreach (var word in title.Split(' '))
                        {
                            var showByWord = (from show in currentlyWatching
                                              where show.Title.Contains(word)
                                              select show);

                            if (showByWord.Any())
                            {
                                currentShow = showByWord.First();
                                break;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.CreateLogEntry(LogType.Error, ex, DateTime.Now);
                        currentShow = null;
                    }
                }
            }

            if (string.IsNullOrEmpty(currentShow.Title))
            {
                Logger.CreateLogEntry(LogType.Error, "MAL Search was not successful.", DateTime.Now);
                throw new Exception("MAL show was not found successfully.");
            }

            Logger.CreateLogEntry(LogType.Success, $"MAL API search was successful for {title}. ID: {currentShow.Id} | Total Episodes: {currentShow.Episodes}", DateTime.Now);

            return(currentShow);
        }
Esempio n. 2
0
        public static async Task <bool> UpdateListAsync(RegisterValues values, PlexRequest plexRequest)
        {
            var entries = await GetListForUser(values.UserName);

            var title = plexRequest.Metadata.GrandparentTitle;

            AniListAnime currentShow = new AniListAnime();

            try
            {
                currentShow = entries.Entries
                              .Where(x => TitleComparer.Compute(x.MediaItem.Title.UserPreferred, title) < 5 ||
                                     x.MediaItem.Title.UserPreferred.ToLower().Contains(title.ToLower())).First();
            }
            catch
            {
                try
                {
                    currentShow = entries.Entries.Select(x => x)
                                  .OrderByDescending(x => TitleComparer.Compute(x.MediaItem.Title.UserPreferred.ToLower(), title.ToLower()))
                                  .First();
                }
                catch (Exception ex)
                {
                    Logger.CreateLogEntry(LogType.Error, ex, DateTime.Now);
                    currentShow = null;
                }
            }

            if (currentShow == null)
            {
                return(false);
            }

            var query = FormUpdateQuery(currentShow.EntryId, currentShow.MediaItem.Id, plexRequest.Metadata.Index);

            var            token   = values.AniListAccessToken;
            HttpWebRequest request = WebRequest.Create(BaseUrl + query) as HttpWebRequest;

            request.Method = "POST";
            request.Headers["Authorization"] = "Bearer " + token;

            AniListSearchResponse aniListResponse;

            try
            {
                var response = (HttpWebResponse)await request.GetResponseAsync();

                using (var reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII))
                {
                    var aniListResponseString = reader.ReadToEnd();
                    aniListResponse = JsonConvert.DeserializeObject <AniListSearchResponse>(aniListResponseString);
                }
            }
            catch (Exception ex)
            {
                return(false);
            }

            return(true);
        }