public void IncorrectUsernameMangaChapterUpdateTest() { string username = System.Environment.GetEnvironmentVariable("MAL_USERNAME"); username += "test"; string password = System.Environment.GetEnvironmentVariable("MAL_PASSWORD"); // Monster int mangaId = 1; MangaUpdate partialBaseInfo = new MangaUpdate() { Chapter = 1 }; using (MyAnimeListApi api = new MyAnimeListApi()) { using (GetUserMangaDetailsTest helper = new GetUserMangaDetailsTest()) { Assert.Throws <MalApiRequestException>(() => helper.Login(username, password)); Assert.Throws <MalApiRequestException>(() => api.UpdateMangaForUser(mangaId, partialBaseInfo, username, password)); } } }
public void UpdateMangaForUserCanceled() { string username = System.Environment.GetEnvironmentVariable("MAL_USERNAME"); string password = System.Environment.GetEnvironmentVariable("MAL_PASSWORD"); // Monster int mangaId = 1; MangaUpdate updateInfo = new MangaUpdate() { Chapter = 162, Volume = 18, Status = MangaCompletionStatus.Completed, Score = 10, TimesReread = 2, RereadValue = 4, // high DateStart = new DateTime(2017, 12, 10), DateFinish = new DateTime(2017, 12, 15), Priority = 1, // medium EnableDiscussion = 1, EnableRereading = 1, Comments = "test updated comment, test updated comment2", // ScanGroup = "scan_group_updated", Tags = "test updated tag, test updated tag 2" }; using (MyAnimeListApi api = new MyAnimeListApi()) { CancellationTokenSource tokenSource = new CancellationTokenSource(); Task <string> userLookupTask = api.UpdateMangaForUserAsync(mangaId, updateInfo, username, password, tokenSource.Token); tokenSource.Cancel(); Assert.Throws <TaskCanceledException>(() => userLookupTask.GetAwaiter().GetResult()); } }
static void Main(string[] args) { // MalApi uses the Common.Logging logging abstraction. // You can hook it up to any logging library that has a Common.Logging adapter. // See App.config for an example of hooking up MalApi to NLog. // Note that you will also need the appropriate NLog and Common.Logging.NLogXX packages installed. // Hooking up logging is not necessary but can be useful. // With the configuration in this example and with this example program, you will see lines like: // Logged from MalApi: Getting anime list for MAL user LordHighCaptain using URI https://myanimelist.net/malappinfo.php?status=all&type=anime&u=LordHighCaptain // Logged from MalApi: Successfully retrieved anime list for user LordHighCaptain using (MyAnimeListApi api = new MyAnimeListApi()) { api.UserAgent = "MalApiExample"; api.TimeoutInMs = 15000; var animeUpdateInfo = new AnimeUpdate() { Episode = 26, Status = AnimeCompletionStatus.Completed, Score = 9, }; string userUpdateAnime = api.UpdateAnimeForUser(1, animeUpdateInfo, "user", "password"); var mangaUpdateInfo = new MangaUpdate() { Chapter = 20, Volume = 3, Score = 8, Status = MangaCompletionStatus.Completed }; string userUpdateManga = api.UpdateMangaForUser(952, mangaUpdateInfo, "user", "password"); MalUserLookupResults userLookup = api.GetAnimeListForUser("user"); foreach (MyAnimeListEntry listEntry in userLookup.AnimeList) { Console.WriteLine("Rating for {0}: {1}", listEntry.AnimeInfo.Title, listEntry.Score); } Console.WriteLine(); Console.WriteLine(); RecentUsersResults recentUsersResults = api.GetRecentOnlineUsers(); foreach (string user in recentUsersResults.RecentUsers) { Console.WriteLine("Recent user: {0}", user); } Console.WriteLine(); Console.WriteLine(); int eurekaSevenID = 237; AnimeDetailsResults eurekaSeven = api.GetAnimeDetails(eurekaSevenID); Console.WriteLine("Eureka Seven genres: {0}", string.Join(", ", eurekaSeven.Genres)); } }
public void UpdateMangaTagsForUserTest() { string username = System.Environment.GetEnvironmentVariable("MAL_USERNAME"); string password = System.Environment.GetEnvironmentVariable("MAL_PASSWORD"); // Monster int mangaId = 1; MangaUpdate partialBaseInfo = new MangaUpdate() { Tags = "test base tag, test base tag 2" }; MangaUpdate partialUpdateInfo = new MangaUpdate() { Tags = "test updated tag, test updated tag 2" }; using (MyAnimeListApi api = new MyAnimeListApi()) { using (GetUserMangaDetailsTest helper = new GetUserMangaDetailsTest()) { helper.Login(username, password); string result = api.UpdateMangaForUser(mangaId, partialBaseInfo, username, password); Assert.Equal("Updated", result); MangaUpdate baseReults = helper.GetUserMangaDetailsAsync(username, mangaId); // Assert first update against base info Assert.Equal(partialBaseInfo.Tags, baseReults.Tags); result = api.UpdateMangaForUser(mangaId, partialUpdateInfo, username, password); Assert.Equal("Updated", result); MangaUpdate updatedResults = helper.GetUserMangaDetailsAsync(username, mangaId); // Assert second update with update info Assert.Equal(partialUpdateInfo.Tags, updatedResults.Tags); // Assert that only the tags has been changed Assert.Equal(baseReults.Chapter, updatedResults.Chapter); Assert.Equal(baseReults.Volume, updatedResults.Volume); Assert.Equal(baseReults.Status, updatedResults.Status); Assert.Equal(baseReults.Score, updatedResults.Score); Assert.Equal(baseReults.TimesReread, updatedResults.TimesReread); Assert.Equal(baseReults.RereadValue, updatedResults.RereadValue); Assert.Equal(baseReults.DateStart, updatedResults.DateStart); Assert.Equal(baseReults.DateFinish, updatedResults.DateFinish); Assert.Equal(baseReults.Priority, updatedResults.Priority); Assert.Equal(baseReults.EnableDiscussion, updatedResults.EnableDiscussion); Assert.Equal(baseReults.EnableRereading, updatedResults.EnableRereading); Assert.Equal(baseReults.Comments, updatedResults.Comments); Assert.NotEqual(baseReults.Tags, updatedResults.Tags); } } }
public async Task <MangaUpdate> GetUserMangaDetailsAsync(string username, int mangaId, CancellationToken cancellationToken) { string url = string.Format(UserMangaDetailsUri, mangaId); HttpRequestMessage request = InitNewRequest(url, HttpMethod.Get); MangaUpdate results = await ProcessRequestAsync(request, ScrapeUserMangaDetailsFromHtml, cancellationToken : cancellationToken, baseErrorMessage : string.Format("Failed getting user manga details for user {0} manga ID {1}.", username, mangaId)) .ConfigureAwait(continueOnCapturedContext: false); return(results); }
public void TestScrapeUserMangaDetailsFromHtml() { string html; using (StreamReader reader = Helpers.GetResourceStream("Monster.htm")) { html = reader.ReadToEnd(); } MangaUpdate info = new MangaUpdate() { Chapter = 162, Volume = 18, Status = MangaCompletionStatus.Completed, Score = 10, TimesReread = 2, RereadValue = 4, // high DateStart = new DateTime(2017, 12, 10), DateFinish = new DateTime(2017, 12, 15), Priority = 1, // medium EnableDiscussion = 1, EnableRereading = 1, Comments = "test updated comment, test updated comment2", // ScanGroup = "scan_group_updated", Tags = "test updated tag, test updated tag 2" }; using (MyAnimeListApi api = new MyAnimeListApi()) { MangaUpdate results = MalDetailScrapingUtils.ScrapeUserMangaDetailsFromHtml(html); Assert.Equal(info.Chapter, results.Chapter); Assert.Equal(info.Volume, results.Volume); Assert.Equal(info.Status, results.Status); Assert.Equal(info.Score, results.Score); Assert.Equal(info.TimesReread, results.TimesReread); Assert.Equal(info.RereadValue, results.RereadValue); Assert.Equal(info.DateStart, results.DateStart); Assert.Equal(info.DateFinish, results.DateFinish); Assert.Equal(info.Priority, results.Priority); Assert.Equal(info.EnableDiscussion, results.EnableDiscussion); Assert.Equal(info.EnableRereading, results.EnableRereading); Assert.Equal(info.Comments, results.Comments); Assert.Equal(info.Tags, results.Tags); } }
// internal for unit testing internal MangaUpdate ScrapeUserMangaDetailsFromHtml(string userMangaDetailsHtml) { MangaUpdate results = new MangaUpdate(); var doc = new HtmlDocument(); doc.LoadHtml(userMangaDetailsHtml); // Chapter results.Chapter = doc.GetElementbyId("add_manga_num_read_chapters").GetAttributeValue("value", -1); // Volume results.Volume = doc.GetElementbyId("add_manga_num_read_volumes").GetAttributeValue("value", -1); // Status var parentNode = doc.GetElementbyId("add_manga_status"); foreach (var childNode in parentNode.ChildNodes) { if (childNode.Attributes["selected"] != null) { results.Status = (MangaCompletionStatus)childNode.GetAttributeValue("value", -1); break; } } // Enable rereading results.EnableRereading = doc.GetElementbyId("add_manga_is_rereading").Attributes["checked"] == null ? 0 : 1; // Score parentNode = doc.GetElementbyId("add_manga_score"); foreach (var childNode in parentNode.ChildNodes) { if (childNode.Attributes["selected"] != null) { results.Score = childNode.GetAttributeValue("value", -1); break; } } // Start date int day = -1; int month = -1; int year = -1; parentNode = doc.GetElementbyId("add_manga_start_date_month"); foreach (var childNode in parentNode.ChildNodes) { if (childNode.Attributes["selected"] != null) { month = childNode.GetAttributeValue("value", -1); break; } } parentNode = doc.GetElementbyId("add_manga_start_date_day"); foreach (var childNode in parentNode.ChildNodes) { if (childNode.Attributes["selected"] != null) { day = childNode.GetAttributeValue("value", -1); break; } } parentNode = doc.GetElementbyId("add_manga_start_date_year"); foreach (var childNode in parentNode.ChildNodes) { if (childNode.Attributes["selected"] != null) { year = childNode.GetAttributeValue("value", -1); break; } } if (month == -1 || day == -1 || year == -1) { results.DateStart = null; } else { results.DateStart = new DateTime(year, month, day); } // Date finish parentNode = doc.GetElementbyId("add_manga_finish_date_month"); foreach (var childNode in parentNode.ChildNodes) { if (childNode.Attributes["selected"] != null) { month = childNode.GetAttributeValue("value", -1); break; } } parentNode = doc.GetElementbyId("add_manga_finish_date_day"); foreach (var childNode in parentNode.ChildNodes) { if (childNode.Attributes["selected"] != null) { day = childNode.GetAttributeValue("value", -1); break; } } parentNode = doc.GetElementbyId("add_manga_finish_date_year"); foreach (var childNode in parentNode.ChildNodes) { if (childNode.Attributes["selected"] != null) { year = childNode.GetAttributeValue("value", -1); break; } } if (month == -1 || day == -1 || year == -1) { results.DateFinish = null; } else { results.DateFinish = new DateTime(year, month, day); } // Tags results.Tags = doc.GetElementbyId("add_manga_tags").InnerText; // Priority parentNode = doc.GetElementbyId("add_manga_priority"); foreach (var childNode in parentNode.ChildNodes) { if (childNode.Attributes["selected"] != null) { results.Priority = childNode.GetAttributeValue("value", -1); break; } } // Times reread results.TimesReread = doc.GetElementbyId("add_manga_num_read_times").GetAttributeValue("value", -1); // Reread value parentNode = doc.GetElementbyId("add_manga_reread_value"); foreach (var childNode in parentNode.ChildNodes) { if (childNode.Attributes["selected"] != null) { results.RereadValue = childNode.GetAttributeValue("value", -1); break; } } // Comments results.Comments = doc.GetElementbyId("add_manga_comments").InnerText; // Enable discussion parentNode = doc.GetElementbyId("add_manga_is_asked_to_discuss"); foreach (var childNode in parentNode.ChildNodes) { if (childNode.Attributes["selected"] != null) { // Because 'Enable discussion = 1' sent for update sets the first options of the dropdown, which corresponds to 0 and vice versa... int temp = childNode.GetAttributeValue("value", -1); temp = temp == 1 ? 0 : 1; results.EnableDiscussion = temp; break; } } return(results); }
public void UpdateMangaForUserTest() { string username = System.Environment.GetEnvironmentVariable("MAL_USERNAME"); string password = System.Environment.GetEnvironmentVariable("MAL_PASSWORD"); // Monster int mangaId = 1; MangaUpdate baseInfo = new MangaUpdate() { Chapter = 1, Volume = 2, Status = MangaCompletionStatus.Reading, Score = 3, TimesReread = 1, RereadValue = 2, // low DateStart = new DateTime(2017, 10, 1), DateFinish = new DateTime(2017, 10, 5), Priority = 0, // low EnableDiscussion = 0, EnableRereading = 0, Comments = "base comment,base comment 2", // ScanGroup = "scan_group", Tags = "test base tag, test base tag 2" }; MangaUpdate updateInfo = new MangaUpdate() { Chapter = 162, Volume = 18, Status = MangaCompletionStatus.Completed, Score = 10, TimesReread = 2, RereadValue = 4, // high DateStart = new DateTime(2017, 12, 10), DateFinish = new DateTime(2017, 12, 15), Priority = 1, // medium EnableDiscussion = 1, EnableRereading = 1, Comments = "test updated comment, test updated comment2", // ScanGroup = "scan_group_updated", Tags = "test updated tag, test updated tag 2" }; using (MyAnimeListApi api = new MyAnimeListApi()) { using (GetUserMangaDetailsTest helper = new GetUserMangaDetailsTest()) { helper.Login(username, password); string result = api.UpdateMangaForUser(mangaId, baseInfo, username, password); Assert.Equal("Updated", result); MangaUpdate baseReults = helper.GetUserMangaDetailsAsync(username, mangaId); // Assert first update against base info Assert.Equal(baseInfo.Chapter, baseReults.Chapter); Assert.Equal(baseInfo.Volume, baseReults.Volume); Assert.Equal(baseInfo.Status, baseReults.Status); Assert.Equal(baseInfo.Score, baseReults.Score); Assert.Equal(baseInfo.TimesReread, baseReults.TimesReread); Assert.Equal(baseInfo.RereadValue, baseReults.RereadValue); Assert.Equal(baseInfo.DateStart, baseReults.DateStart); Assert.Equal(baseInfo.DateFinish, baseReults.DateFinish); Assert.Equal(baseInfo.Priority, baseReults.Priority); Assert.Equal(baseInfo.EnableDiscussion, baseReults.EnableDiscussion); Assert.Equal(baseInfo.EnableRereading, baseReults.EnableRereading); Assert.Equal(baseInfo.Comments, baseReults.Comments); Assert.Equal(baseInfo.Tags, baseReults.Tags); result = api.UpdateMangaForUser(mangaId, updateInfo, username, password); Assert.Equal("Updated", result); MangaUpdate updatedResults = helper.GetUserMangaDetailsAsync(username, mangaId); // Assert second update with update info Assert.Equal(updateInfo.Chapter, updatedResults.Chapter); Assert.Equal(updateInfo.Volume, updatedResults.Volume); Assert.Equal(updateInfo.Status, updatedResults.Status); Assert.Equal(updateInfo.Score, updatedResults.Score); Assert.Equal(updateInfo.TimesReread, updatedResults.TimesReread); Assert.Equal(updateInfo.RereadValue, updatedResults.RereadValue); Assert.Equal(updateInfo.DateStart, updatedResults.DateStart); Assert.Equal(updateInfo.DateFinish, updatedResults.DateFinish); Assert.Equal(updateInfo.Priority, updatedResults.Priority); Assert.Equal(updateInfo.EnableDiscussion, updatedResults.EnableDiscussion); Assert.Equal(updateInfo.EnableRereading, updatedResults.EnableRereading); Assert.Equal(updateInfo.Comments, updatedResults.Comments); Assert.Equal(updateInfo.Tags, updatedResults.Tags); // Assert all values have been changed Assert.NotEqual(baseReults.Chapter, updatedResults.Chapter); Assert.NotEqual(baseReults.Volume, updatedResults.Volume); Assert.NotEqual(baseReults.Status, updatedResults.Status); Assert.NotEqual(baseReults.Score, updatedResults.Score); Assert.NotEqual(baseReults.TimesReread, updatedResults.TimesReread); Assert.NotEqual(baseReults.RereadValue, updatedResults.RereadValue); Assert.NotEqual(baseReults.DateStart, updatedResults.DateStart); Assert.NotEqual(baseReults.DateFinish, updatedResults.DateFinish); Assert.NotEqual(baseReults.Priority, updatedResults.Priority); Assert.NotEqual(baseReults.EnableDiscussion, updatedResults.EnableDiscussion); Assert.NotEqual(baseReults.EnableRereading, updatedResults.EnableRereading); Assert.NotEqual(baseReults.Comments, updatedResults.Comments); Assert.NotEqual(baseReults.Tags, updatedResults.Tags); } } }