예제 #1
0
        public static async Task ModifyFavourite(int id, FavouriteType type, bool add)
        {
            string idFieldName;
            string actionId;

            switch (type)
            {
            case FavouriteType.Anime:
                idFieldName = "aid";
                actionId    = add ? "13" : "14";
                break;

            case FavouriteType.Manga:
                idFieldName = "mid";
                actionId    = add ? "38" : "39";
                break;

            case FavouriteType.Character:
                idFieldName = "cid";
                actionId    = add ? "42" : "43";
                break;

            case FavouriteType.Person:
                idFieldName = "vaid";
                actionId    = add ? "47" : "48";
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(type), type, null);
            }

            try
            {
                var client = await MalHttpContextProvider.GetHttpContextAsync();

                var charCont = new List <KeyValuePair <string, string> >
                {
                    new KeyValuePair <string, string>(idFieldName, id.ToString()),
                    new KeyValuePair <string, string>("csrf_token", client.Token)
                };
                var contentchar = new FormUrlEncodedContent(charCont);
                await client.PostAsync($"/includes/ajax.inc.php?s=1&t={actionId}", contentchar);
            }
            catch (WebException)
            {
                MalHttpContextProvider.ErrorMessage("Favourites");
            }
        }
예제 #2
0
        public static async Task <List <MalNotification> > GetNotifications()
        {
            try
            {
                var client = await MalHttpContextProvider.GetHttpContextAsync();

                var response =
                    await client.GetAsync("https://myanimelist.net/notification");


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

                var output = new List <MalNotification>();
                try
                {
                    var scriptBeginPos = doc.IndexOf("\nwindow.MAL.notification", StringComparison.CurrentCultureIgnoreCase);
                    if (scriptBeginPos == -1)
                    {
                        return(output);
                    }
                    var scriptBegin   = doc.Substring(scriptBeginPos);
                    var startPos      = scriptBegin.IndexOf('=');
                    var endPos        = scriptBegin.IndexOf(';');
                    var json          = scriptBegin.Substring(startPos + 1, endPos - startPos - 1);
                    var notifications =
                        JsonConvert.DeserializeObject <MalScrappedRootNotification>(json);

                    foreach (var notification in notifications.items)
                    {
                        output.Add(new MalNotification(notification));
                    }
                }
                catch (Exception)
                {
                    //hatml
                }

                return(output);
            }
            catch (WebException)
            {
                //inner background task exception ¯\_(ツ)_/¯
                return(new List <MalNotification>());
            }
        }
예제 #3
0
        public static async Task <bool> MarkNotifiactionsAsRead(IEnumerable <MalNotification> notification)
        {
            try
            {
                var client = await MalHttpContextProvider.GetHttpContextAsync();

                var dto      = new ReadNotificationDTO(notification, client.Token);
                var content  = new StringContent(JsonConvert.SerializeObject(dto));
                var response = await client.PostAsync("/notification/api/check-items-as-read.json", content);

                return((await response.Content.ReadAsStringAsync()).Contains("true"));
            }
            catch (Exception e)
            {
                MalHttpContextProvider.ErrorMessage("Notifications");
                return(false);
            }
        }