Beispiel #1
0
        public async Task <JsonCurrentlyAiring> GetCurrentlyAiring()
        {
            JArray latestAiring = await KitsuHandler.GetCurrentlyAiring();

            Dictionary <int, List <string> > airingAnimeTitles = new Dictionary <int, List <string> >();
            int i = 0;

            foreach (JObject anime in latestAiring)
            {
                List <string> titlelist = new List <string>();

                JObject attributes = anime.Value <JObject>("attributes");
                JObject titles     = attributes.Value <JObject>("titles");
                if (attributes["abbreviatedTitles"] != null)
                {
                    try
                    {
                        JArray   abbreviatedTitles        = attributes.Value <JArray>("abbreviatedTitles");
                        string[] abbreviatedTitlesStrings = abbreviatedTitles.ToObject <string[]>();


                        foreach (string title in abbreviatedTitlesStrings)
                        {
                            titlelist.Add(title);
                        }
                    }
                    catch
                    {
                    }
                }

                if (titles.Value <string>("en") != null)
                {
                    titlelist.Add(titles.Value <string>("en"));
                }

                if (titles.Value <string>("en_jp") != null)
                {
                    titlelist.Add(titles.Value <string>("en_jp"));
                }

                if (titles.Value <string>("en_us") != null)
                {
                    titlelist.Add(titles.Value <string>("en_us"));
                }

                airingAnimeTitles.Add(i, titlelist);

                i++;
            }


            JObject listWithAiringAnime = JObject.FromObject(airingAnimeTitles);

            List <JObject> latestAiringKitsu    = new List <JObject>();
            List <JObject> nonLatestAiringKitsu = new List <JObject>();

            JObject result = await NiblHandler.GetLatestFiles("21");

            JArray        array         = result.Value <JArray>("packs");
            List <string> animeIdsAdded = new List <string>();

            foreach (JObject pack in array)
            {
                if (pack["Video_Resolution"] != null)
                {
                    if (pack.Value <string>("Video_Resolution") == "720P")
                    {
                        string title_compare = pack.Value <string>("Title").ToLower();

                        Dictionary <int, List <double> > comparisonValues = new Dictionary <int, List <double> >();
                        foreach (KeyValuePair <int, List <string> > keyValuePair in airingAnimeTitles)
                        {
                            List <double> comparisonValue = new List <double>();
                            int           x = 0;
                            foreach (string title in keyValuePair.Value)
                            {
                                string title_lower      = title.ToLower();
                                double comparison_value = UtilityMethods.CalculateSimilarity(title_lower, title_compare);

                                //Console.WriteLine("Compare Handler, comparing: " + title_compare + ", " + title_lower + ", result = " + comparison_value);

                                comparisonValue.Add(comparison_value);
                                x++;
                            }
                            comparisonValues.Add(keyValuePair.Key, comparisonValue);
                        }

                        double max    = 0;
                        int    maxKey = -1;
                        foreach (KeyValuePair <int, List <double> > resultComparison in comparisonValues)
                        {
                            foreach (double resultComparison2 in resultComparison.Value)
                            {
                                if (resultComparison2 > max)
                                {
                                    max    = resultComparison2;
                                    maxKey = resultComparison.Key;
                                }
                            }
                        }

                        JObject currentlyairing = latestAiring[maxKey].ToObject <JObject>();
                        if (!animeIdsAdded.Contains(currentlyairing.Value <string>("id")) && max > 0.5)
                        {
                            currentlyairing.Add("latest_episode", pack);
                            latestAiringKitsu.Add(currentlyairing);

                            animeIdsAdded.Add(currentlyairing.Value <string>("id"));
                        }
                    }
                }
                else
                {
                    string title_compare = pack.Value <string>("Title").ToLower();

                    Dictionary <int, List <double> > comparisonValues = new Dictionary <int, List <double> >();
                    foreach (KeyValuePair <int, List <string> > keyValuePair in airingAnimeTitles)
                    {
                        List <double> comparisonValue = new List <double>();
                        int           x = 0;
                        foreach (string title in keyValuePair.Value)
                        {
                            string title_lower      = title.ToLower();
                            double comparison_value = UtilityMethods.CalculateSimilarity(title_lower, title_compare);

                            //Console.WriteLine("Compare Handler, comparing: " + title_compare + ", " + title_lower + ", result = " + comparison_value);

                            comparisonValue.Add(comparison_value);
                            x++;
                        }
                        comparisonValues.Add(keyValuePair.Key, comparisonValue);
                    }

                    double max    = 0;
                    int    maxKey = -1;
                    foreach (KeyValuePair <int, List <double> > resultComparison in comparisonValues)
                    {
                        foreach (double resultComparison2 in resultComparison.Value)
                        {
                            if (resultComparison2 > max)
                            {
                                max    = resultComparison2;
                                maxKey = resultComparison.Key;
                            }
                        }
                    }

                    JObject currentlyairing = latestAiring[maxKey].ToObject <JObject>();
                    if (!animeIdsAdded.Contains(currentlyairing.Value <string>("id")) && max > 0.5)
                    {
                        currentlyairing.Add("latest_episode", pack);
                        latestAiringKitsu.Add(currentlyairing);

                        animeIdsAdded.Add(currentlyairing.Value <string>("id"));
                    }
                }
            }

            foreach (JObject anime in latestAiring)
            {
                if (!animeIdsAdded.Contains(anime.Value <string>("id")))
                {
                    nonLatestAiringKitsu.Add(anime);
                }
            }

            JObject resultCurrentlyAiringKitsu = new JObject {
                ["airing_ordered"] = JToken.FromObject(latestAiringKitsu), ["airing_non_ordered"] = JToken.FromObject(nonLatestAiringKitsu)
            };

            JsonCurrentlyAiring airing = new JsonCurrentlyAiring
            {
                result = resultCurrentlyAiringKitsu
            };

            return(airing);
        }