private static void threadFunction(ScanForm form, int initialPage) { int lastPageHash = 0; int page = initialPage; while (!threadMustStop) { string body; using (var client = new WebClient()) { body = client.DownloadString(searchUri + page); } int t = body.GetHashCode(); if (lastPageHash == t) { threadMustStop = true; break; } lastPageHash = t; form.BeginInvoke(new Action(() => { form.labelActualPage.Text = page.ToString(); })); int pos = 0; long lastId = -1; while (!threadMustStop) { pos = body.IndexOf("?multiverseid=", pos + 1, StringComparison.Ordinal); if (pos == -1) { break; } int pos2 = body.IndexOf("\"", pos, StringComparison.Ordinal); try { long id = Convert.ToInt64(body.Substring(pos + 14, pos2 - pos - 14)); if (lastId != id) { searchById(form, id); lastId = id; } } catch (Exception exc) { if (MessageBox.Show("Error: " + exc.Message, "Stop scanning?", MessageBoxButtons.YesNo) == DialogResult.Yes) { threadMustStop = true; } } } page++; } form.BeginInvoke(new Action(() => { form.buttonStartScanning.Enabled = true; })); }
private static void searchById(ScanForm form, long id) { string body; using (var client = new WebClient()) { client.Encoding = Encoding.UTF8; body = client.DownloadString(cardUri + id); } int pos = body.IndexOf("<div class=\"value\">", StringComparison.Ordinal); // IGNORED int pos2 = body.IndexOf("</div>", pos, StringComparison.Ordinal); pos = body.IndexOf("<div class=\"value\">", pos2, StringComparison.Ordinal); pos2 = body.IndexOf("</div>", pos, StringComparison.Ordinal); var name = body.Substring(pos + 19, pos2 - pos - 19).Trim(" \n\r\t".ToCharArray()); pos = body.IndexOf("Types:", pos2, StringComparison.Ordinal); pos = body.IndexOf("<div class=\"value\">", pos, StringComparison.Ordinal); pos2 = body.IndexOf("</div>", pos, StringComparison.Ordinal); var type = body.Substring(pos + 19, pos2 - pos - 19).Trim(" \n\r\t".ToCharArray()); pos = body.IndexOf("</span>", pos2, StringComparison.Ordinal); // Rarity is inside a span pos2 = body.LastIndexOf(">", pos, StringComparison.Ordinal); var rarity = body.Substring(pos2 + 1, pos - pos2 - 1).Trim(" \n\r\t".ToCharArray()); CardData card = new CardData { id = id, name = name, type = type, rarity = rarity }; form.BeginInvoke(new Action(() => { form.actualize(card); })); DataConnection conn = new DataConnection(); int n = conn.executeNonQuery(@"INSERT IGNORE INTO tbCards (multiverseId,name,type,rarity) VALUES (@id,@name,@type,@rarity)", new Dictionary <String, Object> { { "@id", id }, { "@name", name }, { "@type", type }, { "@rarity", rarity } }); if (n < 0) { if (MessageBox.Show("Error: " + conn.getLastError(), "Stop scanning?", MessageBoxButtons.YesNo) == DialogResult.Yes) { threadMustStop = true; } } }