Exemplo n.º 1
0
        public static async Task <IEnumerable <AnilistAnime> > GetAnimeByNameAsync(string name)
        {
            string schema = string.Empty;
            //Load the Schema File into a stream
            var resourceStream = Assembly.GetEntryAssembly().GetManifestResourceStream("Shintenbou.Rest.Schema.AnilistAnime.graphql");

            //Copy it to the StreamReader and read the content
            using (var sr = new StreamReader(resourceStream))
                schema = await sr.ReadToEndAsync();
            //Initializing Anilist Request
            var req = new AnilistRequest
            {
                //Add query schema
                Query     = schema,
                Variables = new Dictionary <string, string>
                {
                    //Add variables, if the user didnt provide a name then have it as null.
                    {
                        "search", (name?.Replace("'", string.Empty))
                    }
                }
            };

            return(await SendRequestAsync <IEnumerable <AnilistAnime> >(req));
        }
Exemplo n.º 2
0
        public async Task SubscribeToAnime([Remainder] string args)
        {
            if (Context.Guild == null)
            {
                return;
            }

            if (string.IsNullOrEmpty(args))
            {
                await ReplyAsync($"{Context.User.Mention}, uh... you didn't specify an anime. Please do so.");

                return;
            }

            // future proofing for the possibility for extra arguments.
            string anime = args;

            // search for the anime.
            AniListModel[] animeList = await AnilistRequest.FindAnime(anime);

            if (animeList.Length == 0)
            {
                return;
            }


            GuildUserWaiter guildUserWaiter = new GuildUserWaiter(Context.Guild.Id, Context.User.Id, WaiterFunction,
                                                                  animeList);

            guildUserWaiter.ParentMessage = Context.Message;

            var s = new StringBuilder("What anime?\n```Ini\n");

            for (int i = 0; i < animeList.Length; i++)
            {
                s.AppendLine($"{i + 1} \t= {animeList[i].Title.EnglishTitle ?? animeList[i].Title.RomajiTitle}");
            }

            s.AppendLine("```");

            IMessage msg = await ReplyAsync(s.ToString());

            guildUserWaiter.AddAssociatedMessage(msg);
            ResponseModule.ResponseModule.AddWaiter(guildUserWaiter);
        }
Exemplo n.º 3
0
        public static async Task <AnilistUser?> GetUserByNameAsync(string name, int page = 1, int perPage = 25)
        {
            string schema         = string.Empty;
            var    resourceStream = Assembly.GetEntryAssembly().GetManifestResourceStream("Shintenbou.Rest.Schema.AnilistUser.graphql");

            using (var sr = new StreamReader(resourceStream))
                schema = await sr.ReadToEndAsync();
            var req = new AnilistRequest
            {
                Query     = schema,
                Variables = new Dictionary <string, string>()
            };

            req.Variables.Add("name", (name?.Replace("'", string.Empty)));
            req.Variables.Add("pageNumber", $"{page}");
            req.Variables.Add("perPage", $"{perPage}");
            return(await SendRequestAsync <AnilistUser?>(req));
        }
Exemplo n.º 4
0
        private static async Task <T> SendRequestAsync <T>(AnilistRequest request)
        {
            try
            {
                await _semaphore.WaitAsync();

                using (var reqMessage = new HttpRequestMessage())
                {
                    reqMessage.Content = new StringContent(JsonConvert.SerializeObject(request));
                    reqMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                    reqMessage.Method     = HttpMethod.Post;
                    reqMessage.RequestUri = new Uri("https://graphql.anilist.co/");
                    if (_previousHeaders.Key == 0)
                    {
                        await Task.Delay(_previousHeaders.Value);
                    }
                    var response = await _client.SendAsync(reqMessage);

                    var content = await response.Content.ReadAsStringAsync();

                    if (response.IsSuccessStatusCode)
                    {
                        _previousHeaders = new KeyValuePair <int, int>(int.Parse(response.Headers.GetValues("X-RateLimit-Remaining").First()), int.Parse(response.Headers.GetValues("X-RateLimit-Reset").First()));
                        return(JsonConvert.DeserializeObject <T>(JObject.Parse(content)["data"]["Page"]["media"].ToString()));
                    }
                    else
                    {
                        var error = JsonConvert.DeserializeObject <AnilistErrorResponse>(content);
                        Console.WriteLine($"API Error:\n{string.Join("\n", error.Errors.Select(x => x.Message))}");
                        var stream = File.CreateText($"{AppContext.BaseDirectory}/errordump.txt");
                        await stream.WriteLineAsync($"Errors:\n{string.Join("\n", error.Errors.Select(x => x.Message))}");

                        stream.Close();
                        stream.Dispose();
                        return(default(T));
                    }
                }
            }
            finally
            {
                _semaphore.Release();
            }
        }
Exemplo n.º 5
0
        public static async Task <IEnumerable <AnilistManga> > GetMangaByNameAsync(string name)
        {
            string schema         = string.Empty;
            var    resourceStream = Assembly.GetEntryAssembly().GetManifestResourceStream("Shintenbou.Rest.Schema.AnilistManga.graphql");

            using (var sr = new StreamReader(resourceStream))
                schema = await sr.ReadToEndAsync();
            var req = new AnilistRequest
            {
                Query     = schema,
                Variables = new Dictionary <string, string>
                {
                    {
                        "search", (name?.Replace("'", string.Empty))
                    }
                }
            };

            return(await SendRequestAsync <IEnumerable <AnilistManga> >(req));
        }