private static void GetSidFromCookies() { var newSid = WebClient.ReadCookieValue(BaseAddress, "sid"); if (string.Equals(_sid, newSid)) { return; } _sid = newSid; _log.Info("New SID: {0}", newSid); }
private static async Task Login() { try { if (_isLoggedIn) { return; } if (string.IsNullOrWhiteSpace(_sid)) { // buka homepage var homepage = await WebClient.GetOrPostStringAsync(new WebRequest { Url = BaseAddress }); Dump.ToFile("Mobilism.html", homepage); GetSidFromCookies(); // verifikasi login if (!homepage.Contains(LoginPath)) { _log.Ignore("Status: sudah login..."); _isLoggedIn = true; return; } } var auth = ReadAccount(); var redirect = WebUtility.UrlEncode(LoginPath); var content = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("username", auth.Username), new KeyValuePair <string, string>("password", auth.Password), new KeyValuePair <string, string>("login", "Login"), new KeyValuePair <string, string>("autologin", "on"), new KeyValuePair <string, string>("viewonline", "on"), new KeyValuePair <string, string>("redirect", redirect), new KeyValuePair <string, string>("sid", _sid), new KeyValuePair <string, string>("redirect", "index.php") }); var headers = new List <WebHeader> { new WebHeader { Key = "Referer", Value = $"{LoginAddress}&sid={_sid}" } }; var loginRequest = new WebRequest { Url = LoginAddress, Headers = headers, Method = WebMethod.Post, Content = content }; var loginpage = await WebClient.GetOrPostStringAsync(loginRequest); Dump.ToFile("Mobilism-Login.html", loginpage); GetSidFromCookies(); // cek html atau kode alien? if (!loginpage.Contains("html")) { throw new Exception("Result bukan html!"); } // verifikasi login if (!loginpage.Contains(LoginPath)) { _log.Info("Status: berhasil login..."); _isLoggedIn = true; } } catch (Exception e) { _log.Error(e.Message); } }
private async void GetThreadPost(WebRequest threadRequest) { if (_callbackMode) { await BotClient.AnswerCallbackQueryAsync(_callback.Id, "Tunggu sebentar..."); } string content; try { // akses thread/search content = await WebClient.GetOrPostStringAsync(threadRequest); // cek html atau kode alien? if (!content.Contains("html")) { throw new Exception("Result bukan html!"); } } catch (Exception e) { _log.Error(e.Message); await BotClient.SendTextAsync(_message, "Mohon maaf...\n" + "Plugin mobilism sedang mengalami gangguan.\n" + "Cobalah beberapa saat lagi."); return; } _log.Debug("Regex link download..."); var pattern = "<div.+class=\"content\".+<span.*>(.+?)</span><br />.+" + "Requirements:(.+?)<br />[\\s\\S]+" + "Download Instructions:([\\s\\S]+?)</div>"; var post = Regex.Match(content, pattern); if (!post.Success) { _log.Error("Post download tidak ditemukan!"); await BotClient.SendTextAsync(_message, "Mohon maaf...\nPlugin mobilism tidak bisa membaca thread."); return; } _log.Debug("Menyiapkan link download..."); var title = post.Groups[1].Value.RemoveHtmlTag(); var require = post.Groups[2].Value.RemoveHtmlTag(); var link = post.Groups[3].Value.RemoveHtmlTag(true); var request = _message.FromNameWithMention(ParseMode.Html, true); var respon = $"<b>{title}</b>" + $"\n—— —— —— —— —— ——\n" + $"Requirements : {require}\n" + $"Download Instructions :\n{link}"; if (_message.IsGroupChat()) { respon = respon + $"\n—— —— —— —— —— ——\n" + $"Requested By : {request}"; } await BotClient.SendTextAsync(_message, respon, parse : ParseMode.Html, preview : false); }
private async void GetThreadList(WebRequest threadRequest, bool threadMode, bool isApp, string keywords) { // search mode harus login if (!threadMode) { await Login(); if (!_isLoggedIn) { // coba login sekali lagi await Login(); if (!_isLoggedIn) { _log.Warning("Gangguan login..."); await BotClient.SendTextAsync(_message, "Mohon maaf...\nPlugin mobilism saat ini sedang mengalami gangguan tidak bisa login!"); return; } } } string content; try { // akses thread/search content = await WebClient.GetOrPostStringAsync(threadRequest); // cek html atau kode alien? if (!content.Contains("html")) { throw new Exception("Result bukan html!"); } // verifikasi login if (content.Contains(LoginPath)) { _log.Warning("Status: logout..."); _isLoggedIn = false; if (!threadMode) { throw new Exception("Search mode harus login!"); } } } catch (Exception e) { _log.Error(e.Message); await BotClient.SendTextAsync(_message, "Mohon maaf...\n" + "Plugin mobilism sedang mengalami gangguan.\n" + "Cobalah beberapa saat lagi."); return; } // search tidak ketemu if (!threadMode && content.Contains("No suitable matches were found.")) { _log.Warning("Pencarian tidak ditemukan!"); await BotClient.SendTextAsync(_message, $"Maaf kaka...\n" + $"{Bot.Name} gak nemu yg dicari.\n" + $"Coba pakai keyword selain \"{keywords}\"."); return; } _log.Debug("Regex tabel..."); // regex tabel thread var findTable = Regex.Match(content, @"<table.+>([\s\S]+)<\/table>", RegexOptions.Multiline | RegexOptions.IgnoreCase); if (!findTable.Success) { _log.Error("Tabel tidak ditemukan!"); await BotClient.SendTextAsync(_message, "Mohon maaf...\nPlugin mobilism tidak bisa membaca daftar thread."); return; } _log.Debug("Regex list..."); const string patternSearch = "<td>\n.+\n.+\n" + "<a.+href=\"(.+)\".+topictitle.+>(.+)<\\/a>.+\n.+" + "<a.+username.+>(.+)<\\/a>.+" + "<small>(.+)<\\/small>\n.+Android.+\n" + "<\\/td>"; const string patternThread = "<tr.+class(?:=\"\"|)>\n.+\n.+\n" + "<a.+href=\"(.+)\".+topictitle.+>(.+)<\\/a>\n.+\n.+" + "<a.+username.+>(.+)<\\/a>.+" + "<small>(.+)<\\/small>\n.+\n.+\n.+\n.+\n.+\n.+\n.+\n" + "<\\/tr>"; // regex cari per-baris dalam tabel var table = findTable.Groups[1].Value; var rows = Regex.Matches(table, threadMode ? patternThread : patternSearch, RegexOptions.Multiline); if (rows.Count == 0) { _log.Warning("Rows tidak ditemukan!"); if (threadMode) { await BotClient.SendTextAsync(_message, $"Maaf kaka...\n{Bot.Name} gak nemu daftar thread."); } else { await BotClient.SendTextAsync(_message, $"Maaf kaka...\n" + $"{Bot.Name} gak nemu yg dicari.\n" + $"Coba pakai keyword selain \"{keywords}\"."); } return; } var count = 1; var maximum = 10; var threads = new List <Thread>(); foreach (Match row in rows) { var f = string.Empty; var t = string.Empty; // regex f=number & t=number var matches = Regex.Matches(row.Groups[1].Value, @"([f|t]=[\d]+)"); foreach (Match match in matches) { var text = match.Groups[1].Value; if (text.StartsWith("f")) { f = text; } if (text.StartsWith("t")) { t = text; } } var thread = new Thread { LinkPath = $"/viewtopic.php?{f}&{t}", Title = row.Groups[2].Value, Username = row.Groups[3].Value, PostDate = row.Groups[4].Value }; threads.Add(thread); if (count < maximum) { count++; } else { break; } } _log.Debug("Menyiapkan list..."); var total = threads.Count; var padding = total.ToString().Length; var respon = "<b>Mobilism</b> : " + (isApp ? "Aplikasi\n" : "Permainan\n") + (threadMode ? "" : $"<b>Pencarian</b> : {keywords}\n") + "<b>Hasil</b> : " + (total < 10 ? total.ToString() : "10") + "/" + total + " threads\n" + "—— —— —— —— —— ——\n"; count = 1; foreach (var thread in threads) { // tambahkan ke teks respon var num = count.ToString().PadLeft(padding, '0'); respon += $"<b>{num}</b>. <a href=\"{BaseAddress}{thread.LinkPath}\">{thread.Title}</a> " + $"by <b>{thread.Username}</b> — {thread.PostDate}.\n"; count++; } var buttonRows = new List <List <InlineKeyboardButton> >(); if (threads.Count > maximum / 2) { // reset counter count = 1; // button baris 1 var partOne = threads.Take(total / 2).ToList(); var buttonRowOne = new List <InlineKeyboardButton>(); foreach (var thread in partOne) { var num = count.ToString().PadLeft(padding, '0'); var button = InlineKeyboardButton.WithCallbackData(num, "cmd=mobilism&data=" + thread.LinkPath); buttonRowOne.Add(button); count++; } buttonRows.Add(buttonRowOne); // button baris 2 var partTwo = threads.Skip(total / 2).ToList(); var buttonRowTwo = new List <InlineKeyboardButton>(); foreach (var thread in partTwo) { var num = count.ToString().PadLeft(padding, '0'); var button = InlineKeyboardButton.WithCallbackData(num, "cmd=mobilism&data=" + thread.LinkPath); buttonRowTwo.Add(button); count++; } buttonRows.Add(buttonRowTwo); } else { // reset counter count = 1; // button jadi 1 baris var buttonRow = new List <InlineKeyboardButton>(); foreach (var thread in threads) { var num = count.ToString().PadLeft(padding, '0'); var buttonColumn = InlineKeyboardButton.WithCallbackData(num, "cmd=mobilism&data=" + thread.LinkPath); buttonRow.Add(buttonColumn); count++; } buttonRows.Add(buttonRow); } var responWithButtons = respon + "—— —— —— —— —— ——\nLink download, pencet tombol nomor dibawah :"; var buttons = new InlineKeyboardMarkup(buttonRows.ToArray()); var sentMessage = await BotClient.SendTextAsync(_message, responWithButtons, parse : ParseMode.Html, button : buttons, preview : false); if (sentMessage == null) { return; } // schedule edit pesan keluar var schedule = new ScheduleData { ChatId = sentMessage.Chat.Id, MessageId = sentMessage.MessageId, DateTime = DateTime.Now.AddMinutes(10), Operation = ScheduleData.Type.Edit, Text = respon + "—— —— —— —— —— ——\nTombol download telah kadaluarsa.", ParseMode = ParseMode.Html }; Schedule.RegisterNew(schedule); }