Ejemplo n.º 1
0
        public async Task <AnimeStat> GetAnimeStats2()
        {
            using (HttpClient client = new HttpClient())
            {
                int                 id    = 1;
                AnimeStat           stats = null;
                HttpResponseMessage res   = await client.GetAsync("http://api.jikan.moe/v3/anime/" + id + "/stats");

                if (res.IsSuccessStatusCode)
                {
                    stats = await res.Content.ReadAsAsync <AnimeStat>();

                    Console.WriteLine(stats.Scores[1].Votes);
                }
                Console.WriteLine("cd 5 sec");
                await Task.Delay(3000);

                stats = null;
                res   = await client.GetAsync("http://api.jikan.moe/v3/anime/2/stats");

                if (res.IsSuccessStatusCode)
                {
                    stats = await res.Content.ReadAsAsync <AnimeStat>();

                    Console.WriteLine(stats.Completed);
                }

                if (stats == null)
                {
                    Console.WriteLine("stats is null");
                }
                return(stats);
            }
        }
Ejemplo n.º 2
0
        //public async void Start()
        //{
        //        //AnimeStats stats = await GetAnimeStats("http://api.jikan.moe/v3/anime/1/stats");
        //        //Console.WriteLine((await GetAnimeStats("http://api.jikan.moe/v3/anime/1/stats")).Completed);
        //}

        public async Task <AnimeStat> GetAnimeStats(List <int> animeIds)
        {
            List <TotalAnimeStat> rows = new List <TotalAnimeStat>();

            double totalItems = animeIds.Count;

            Console.WriteLine("total items:  " + totalItems);
            double current = 0;
            string log     = "failed on ids: ";

            using (HttpClient client = new HttpClient())
            {
                foreach (var id in animeIds)
                {
                    try
                    {
                        Console.WriteLine("ID: " + id);
                        await Task.Delay(3000);

                        AnimeStat           stats = null;
                        HttpResponseMessage res   = await client.GetAsync("http://api.jikan.moe/v3/anime/" + id + "/stats");

                        if (res.IsSuccessStatusCode)
                        {
                            stats = await res.Content.ReadAsAsync <AnimeStat>();

                            double n     = 0;
                            double total = 0;
                            for (int i = 1; i < 11; i++)
                            {
                                int votes = 0;
                                try
                                {
                                    votes = stats.Scores[i].Votes;
                                }
                                catch (Exception)
                                {
                                }

                                n     = n + votes;
                                total = total + (i * votes);
                            }

                            TotalAnimeStat row = new TotalAnimeStat();

                            try
                            {
                                row.ID            = id;
                                row.Completed     = stats.Completed;
                                row.Watching      = stats.Watching;
                                row.Dropped       = stats.Dropped;
                                row.On_hold       = stats.On_hold;
                                row.Plan_to_Watch = stats.Plan_to_Watch;
                                row.Total_Members = stats.Total;
                                row.Score         = total / n;
                            }
                            catch (Exception)
                            {
                                row.ID            = id;
                                row.Completed     = 0;
                                row.Watching      = 0;
                                row.Dropped       = 0;
                                row.On_hold       = 0;
                                row.Plan_to_Watch = 0;
                                row.Total_Members = 0;
                                row.Score         = 0;
                            }

                            try
                            {
                                row.Vote_1 = stats.Scores[1].Votes;
                            }
                            catch (Exception)
                            {
                                row.Vote_1 = 0;
                            }
                            try
                            {
                                row.Vote_2 = stats.Scores[2].Votes;
                            }
                            catch (Exception)
                            {
                                row.Vote_2 = 0;
                            }
                            try
                            {
                                row.Vote_3 = stats.Scores[3].Votes;
                            }
                            catch (Exception)
                            {
                                row.Vote_3 = 0;
                            }

                            try
                            {
                                row.Vote_4 = stats.Scores[4].Votes;
                            }
                            catch (Exception)
                            {
                                row.Vote_4 = 0;
                            }
                            try
                            {
                                row.Vote_5 = stats.Scores[5].Votes;
                            }
                            catch (Exception)
                            {
                                row.Vote_5 = 0;
                            }
                            try
                            {
                                row.Vote_6 = stats.Scores[6].Votes;
                            }
                            catch (Exception)
                            {
                                row.Vote_6 = 0;
                            }

                            try
                            {
                                row.Vote_7 = stats.Scores[7].Votes;
                            }
                            catch (Exception)
                            {
                                row.Vote_7 = 0;
                            }
                            try
                            {
                                row.Vote_8 = stats.Scores[8].Votes;
                            }
                            catch (Exception)
                            {
                                row.Vote_8 = 0;
                            }
                            try
                            {
                                row.Vote_9 = stats.Scores[9].Votes;
                            }
                            catch (Exception)
                            {
                                row.Vote_9 = 0;
                            }

                            try
                            {
                                row.Vote_10 = stats.Scores[10].Votes;
                            }
                            catch (Exception)
                            {
                                row.Vote_10 = 0;
                            }

                            rows.Add(row);
                        }
                        else
                        {
                            TotalAnimeStat row = new TotalAnimeStat()
                            {
                                ID            = id,
                                Completed     = 0,
                                Watching      = 0,
                                Dropped       = 0,
                                On_hold       = 0,
                                Plan_to_Watch = 0,
                                Total_Members = 0,
                                Score         = 0,
                                Vote_1        = 0,
                                Vote_2        = 0,
                                Vote_3        = 0,
                                Vote_4        = 0,
                                Vote_5        = 0,
                                Vote_6        = 0,
                                Vote_7        = 0,
                                Vote_8        = 0,
                                Vote_9        = 0,
                                Vote_10       = 0
                            };
                            rows.Add(row);
                        }
                    }
                    catch (Exception)
                    {
                        //anime id 35380 failed
                        log = log + id + ", ";
                    }

                    current = current + 1;
                    string process = (current / totalItems * 100) + "%";
                    Console.WriteLine(process);
                }
            }

            TextWriter textWriter = new StreamWriter(@".\NewAnimeList.csv");
            var        csv        = new CsvWriter(textWriter);

            csv.WriteRecords(rows);
            textWriter.Close();
            Console.WriteLine(log);
            Console.WriteLine("Completed");
            return(null);
        }