public HltbUserStats GetUserData() { if (GetIsUserLoggedIn()) { hltbUserStats = new HltbUserStats(); hltbUserStats.Login = (UserLogin.IsNullOrEmpty()) ? HowLongToBeat.PluginDatabase.Database.UserHltbData.Login : UserLogin; hltbUserStats.UserId = (UserId == 0) ? HowLongToBeat.PluginDatabase.Database.UserHltbData.UserId : UserId; hltbUserStats.TitlesList = new List <TitleList>(); try { List <HttpCookie> Cookies = webViews.GetCookies(); Cookies = Cookies.Where(x => x.Domain.Contains("howlongtobeat")).ToList(); #if DEBUG logger.Debug($"HowLongToBeat [Ignored] - Cookies: {JsonConvert.SerializeObject(Cookies)}"); #endif // Get list games var formContent = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("n", UserLogin), new KeyValuePair <string, string>("c", "user_beat"), new KeyValuePair <string, string>("p", string.Empty), new KeyValuePair <string, string>("y", string.Empty) }); string response = Web.PostStringDataCookies(UrlUserStatsGameList, formContent, Cookies).GetAwaiter().GetResult(); HtmlParser parser = new HtmlParser(); IHtmlDocument htmlDocument = parser.Parse(response); foreach (var ListGame in htmlDocument.QuerySelectorAll("table.user_game_list tbody")) { TitleList titleList = new TitleList(); var tr = ListGame.QuerySelectorAll("tr"); var td = tr[0].QuerySelectorAll("td"); titleList.UserGameId = ListGame.GetAttribute("id").Replace("user_sel_", string.Empty).Trim(); titleList.GameName = td[0].QuerySelector("a").InnerHtml.Trim(); titleList.Platform = td[0].QuerySelector("span").InnerHtml.Trim(); titleList.Id = int.Parse(td[0].QuerySelector("a").GetAttribute("href").Replace("game?id=", string.Empty)); // Get game details formContent = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("option", titleList.UserGameId), new KeyValuePair <string, string>("option_b", "comp_all") }); response = Web.PostStringDataCookies(UrlUserStatsGameDetails, formContent, Cookies).GetAwaiter().GetResult(); parser = new HtmlParser(); htmlDocument = parser.Parse(response); var GameDetails = htmlDocument.QuerySelectorAll("div.user_game_detail > div"); // Game status type titleList.GameStatuses = new List <GameStatus>(); foreach (var GameStatus in GameDetails[0].QuerySelectorAll("span")) { switch (GameStatus.InnerHtml.ToLower()) { case "playing": titleList.GameStatuses.Add(new GameStatus { Status = StatusType.Playing }); break; case "backlog": titleList.GameStatuses.Add(new GameStatus { Status = StatusType.Backlog }); break; case "replays": titleList.GameStatuses.Add(new GameStatus { Status = StatusType.Replays }); break; case "custom tab": titleList.GameStatuses.Add(new GameStatus { Status = StatusType.CustomTab }); break; case "completed": titleList.GameStatuses.Add(new GameStatus { Status = StatusType.Completed }); break; case "retired": titleList.GameStatuses.Add(new GameStatus { Status = StatusType.Retired }); break; } } // Game status time int iPosUserData = 1; if (GameDetails[1].InnerHtml.ToLower().Contains("<h5>progress</h5>")) { List <string> ListTime = GameDetails[1].QuerySelector("span").InnerHtml .Replace("<strong>", string.Empty).Replace("</strong>", string.Empty) .Split('/').ToList(); for (int i = 0; i < titleList.GameStatuses.Count; i++) { titleList.GameStatuses[i].Time = ConvertStringToLongUser(ListTime[i]); } iPosUserData = 2; } // User data titleList.HltbUserData = new HltbData(); for (int i = 0; i < GameDetails[iPosUserData].Children.Count(); i++) { string tempTime = string.Empty; // Completion date if (GameDetails[iPosUserData].Children[i].InnerHtml.ToLower().Contains("completion")) { if (GameDetails[iPosUserData].Children[i].QuerySelectorAll("p").FirstOrDefault() != null) { tempTime = GameDetails[iPosUserData].Children[i].QuerySelectorAll("p").FirstOrDefault().InnerHtml; titleList.Completion = Convert.ToDateTime(tempTime); } } if (GameDetails[iPosUserData].Children[i].InnerHtml.ToLower().Contains("main story")) { i++; tempTime = GameDetails[iPosUserData].Children[i].QuerySelector("span").InnerHtml; titleList.HltbUserData.MainStory = ConvertStringToLongUser(tempTime); } if (GameDetails[iPosUserData].Children[i].InnerHtml.ToLower().Contains("main+extras")) { i++; tempTime = GameDetails[iPosUserData].Children[i].QuerySelector("span").InnerHtml; titleList.HltbUserData.MainExtra = ConvertStringToLongUser(tempTime); } if (GameDetails[iPosUserData].Children[i].InnerHtml.ToLower().Contains("completionist")) { i++; tempTime = GameDetails[iPosUserData].Children[i].QuerySelector("span").InnerHtml; titleList.HltbUserData.Completionist = ConvertStringToLongUser(tempTime); } if (GameDetails[iPosUserData].Children[i].InnerHtml.ToLower().Contains("solo")) { i++; tempTime = GameDetails[iPosUserData].Children[i].QuerySelector("span").InnerHtml; titleList.HltbUserData.Solo = ConvertStringToLongUser(tempTime); } if (GameDetails[iPosUserData].Children[i].InnerHtml.ToLower().Contains("co-op")) { i++; tempTime = GameDetails[iPosUserData].Children[i].QuerySelector("span").InnerHtml; titleList.HltbUserData.CoOp = ConvertStringToLongUser(tempTime); } if (GameDetails[iPosUserData].Children[i].InnerHtml.ToLower().Contains("vs")) { i++; tempTime = GameDetails[iPosUserData].Children[i].QuerySelector("span").InnerHtml; titleList.HltbUserData.Vs = ConvertStringToLongUser(tempTime); } } #if DEBUG logger.Debug($"HowLongToBeat [Ignored] - titleList: {JsonConvert.SerializeObject(titleList)}"); #endif hltbUserStats.TitlesList.Add(titleList); } } catch (Exception ex) { Common.LogError(ex, "HowLongToBeat"); _PlayniteApi.Notifications.Add(new NotificationMessage( "HowLongToBeat-Import-Error", "HowLongToBeat" + System.Environment.NewLine + ex.Message, NotificationType.Error, () => _plugin.OpenSettingsView())); return(null); } return(hltbUserStats); } else { _PlayniteApi.Notifications.Add(new NotificationMessage( "HowLongToBeat-Import-Error", "HowLongToBeat" + System.Environment.NewLine + resources.GetString("LOCNotLoggedIn"), NotificationType.Error, () => _plugin.OpenSettingsView())); return(null); } }
public void SetCurrentPlayTime(Game game, long elapsedSeconds) { try { if (howLongToBeatClient.GetIsUserLoggedIn()) { GameHowLongToBeat gameHowLongToBeat = Database.Get(game.Id); if (gameHowLongToBeat != null) { TimeSpan time = TimeSpan.FromSeconds(game.Playtime + elapsedSeconds); var platform = hltbPlatforms.FindAll(x => game.Platform.Name.ToLower().Contains(x.Name.ToLower())).First(); if (platform != null) { string Platform = platform.Name; var HltbData = GetUserHltbData(gameHowLongToBeat.GetData().Id); int edit_id = 0; HltbPostData hltbPostData = new HltbPostData(); if (HltbData != null) { if (howLongToBeatClient.EditIdExist(HltbData.UserGameId)) { edit_id = int.Parse(HltbData.UserGameId); hltbPostData = howLongToBeatClient.GetSubmitData(edit_id.ToString()); } } else { if (HltbData != null) { string tmpEditId = howLongToBeatClient.FindIdExisting(edit_id.ToString()); if (!tmpEditId.IsNullOrEmpty()) { edit_id = int.Parse(tmpEditId); hltbPostData = howLongToBeatClient.GetSubmitData(tmpEditId); } } } if (hltbPostData == null) { logger.Warn($"HowLongToBeat - No hltbPostData for {game.Name}"); return; } hltbPostData.user_id = Database.UserHltbData.UserId; hltbPostData.edit_id = edit_id; hltbPostData.game_id = gameHowLongToBeat.GetData().Id; hltbPostData.custom_title = gameHowLongToBeat.GetData().Name; hltbPostData.platform = Platform; hltbPostData.list_p = "1"; hltbPostData.protime_h = (time.Hours + (24 * time.Days)).ToString(); hltbPostData.protime_m = time.Minutes.ToString(); hltbPostData.protime_s = time.Seconds.ToString(); howLongToBeatClient.PostData(hltbPostData); } else { logger.Warn($"HowLongToBeat - No platform find for {game.Name}"); } } } else { _PlayniteApi.Notifications.Add(new NotificationMessage( "HowLongToBeat-Import-Error", "HowLongToBeat" + System.Environment.NewLine + resources.GetString("LOCNotLoggedIn"), NotificationType.Error, () => _plugin.OpenSettingsView())); } } catch (Exception ex) { Common.LogError(ex, "HowLongToBeat"); _PlayniteApi.Notifications.Add(new NotificationMessage( "HowLongToBeat-Import-Error", "HowLongToBeat" + System.Environment.NewLine + ex.Message, NotificationType.Error, () => _plugin.OpenSettingsView())); } }