Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading.Tasks;
- namespace BulbapediaWebScraper
- {
- class Program
- {
- public static void Show(string obj)
- {
- Console.WriteLine(obj);
- }
- public static void ShowEach<T>(List<T> list)
- {
- foreach (var entry in list)
- {
- Console.WriteLine(entry.ToString());
- }
- }
- public static List<string> Cut(string splitthis, string bythis)
- {
- return splitthis.Split(new string[] { bythis }, StringSplitOptions.None).ToList();
- }
- static void Main(string[] args)
- {
- StreamReader str = new StreamReader(File.OpenRead("pokemon.txt"));
- string d = str.ReadToEnd();
- str.Close();
- List<string> tmp = d.Split(']').ToList();
- List<string> pokemon = new List<string>();
- for (int i = 1; i < tmp.Count; i++)
- {
- pokemon.Add(tmp[i].Split(new string[] { "Name=" }, StringSplitOptions.None)[1].Split(new string[] { "\r\n" }, StringSplitOptions.None)[0]);
- }
- // Using pokemon.txt to get all names of Pokémon to look up because i'm lazy as fuck and am not going to write down
- List<int> excluded = new List<int>() { 28, 29, 30, 31, 32, 33, 82 };
- string filename = "1-802";
- string ret = "{";
- for (int k = 0; k < 802; k++)
- {
- if (excluded.Contains(k)) continue;
- WebClient client = new WebClient();
- string link = $"https://bulbapedia.bulbagarden.net/wiki/{pokemon[k]}_(Pok%C3%A9mon)";
- Stream stream = client.OpenRead(link);
- string data = new StreamReader(stream).ReadToEnd();
- string name = Cut(data, " (Pokémon)")[0].Split('>').Last();
- string t0 = Cut(data, "title=\"List of Pokémon by National Pokédex number\"><span style=\"color:#000;\">#")[1];
- string species = t0.Split('<')[0];
- Show(species + " : " + name);
- string img = Cut(t0, "src=\"//")[1].Split('"')[0];
- string shuffle = null;
- if (data.Contains("Shuffle" + species))
- {
- List<string> attempts = new List<string>() { null, "A", "EX", "S" };
- foreach (string attempt in attempts)
- {
- try
- {
- shuffle = Cut(Cut(data, $"Shuffle{species}{attempt}.png\" w")[1], "srcset=\"//")[1].Split(' ')[0];
- break;
- }
- catch (Exception) { }
- }
- }
- string kind = Cut(Cut(data, "title=\"Pokémon category\"><span style=\"color:#000;\">")[1], " Pokémon")[0];
- if (kind.Contains("<span"))
- {
- kind = kind.Split('>')[1].Split('<')[0];
- }
- List<string> t1 = Cut(data, "</b></span></a>");
- string type1 = t1[0].Split('>').Last();
- string type2 = t1[1].Split('>').Last();
- string t2 = null;
- try
- {
- t2 = Cut(Cut(data, "Abilities</span></a></b>")[1], "</td></tr></table>")[0];
- }
- catch (Exception)
- {
- t2 = Cut(Cut(data, "Ability</span></a></b>")[1], "</td></tr></table>")[0];
- }
- List<string> t3 = Cut(t2, "_(Ability)");
- t3.RemoveAt(t3.Count - 1);
- for (int i = 0; i < t3.Count; i++)
- {
- t3[i] = t3[i].Split('/').Last();
- }
- string abil1 = t3[0] == "Cacophony" ? "-----" : t3[0];
- string abil2 = t3[1] == "Cacophony" ? "-----" : t3[1];
- string ha1 = t3[t3.Count == 7 ? 3 : 4] == "Cacophony" ? "-----" : t3[t3.Count == 7 ? 3 : 4];
- string catchrate = Cut(Cut(data, "Catch_rate")[1], "<td> ")[1].Split(' ')[0];
- string height = Cut(Cut(data, ">Height<")[1], " m")[0].Split(' ').Last();
- string weight = Cut(Cut(data, ">Weight<")[1], " kg")[0].Split(' ').Last();
- string t4 = Cut(Cut(data, "Base experience yield")[1], "</td></tr></table>")[0];
- string exp = null;
- if (t4.Contains("V+"))
- {
- exp = Cut(t4, "<td> ").Last().Split('<')[0];
- }
- else
- {
- exp = Cut(Cut(t4, "<td> ").Last(), "\n")[0];
- }
- string evyield = null;
- string t5 = Cut(data, "EV yield")[1];
- List<string> t6 = Cut(t5, ";\"> ");
- for (int i = 1; i < 7; i++)
- {
- string ev = t6[i].Split('<')[0];
- evyield += ev + (i != 6 ? "," : null);
- }
- string growthrate = Cut(Cut(Cut(data, "Leveling rate")[1], "<td> ")[1], "\n")[0];
- string hatchtime = Cut(Cut(Cut(data, "Hatch time")[1], "<td> ")[1], " <")[0];
- hatchtime = hatchtime.Replace(" ", " ");
- string genderratio = null;
- string t7 = Cut(data, "Gender ratio")[1];
- if (t7.Contains(">Genderless<"))
- {
- genderratio = "Genderless";
- }
- else if (t7.Contains(">100% female<"))
- {
- genderratio = "100%";
- }
- else if (t7.Contains(">100% male<"))
- {
- genderratio = "0%";
- }
- else
- {
- double male = Convert.ToDouble(Cut(t7, "% ")[0].Split('>').Last());
- double female = Convert.ToDouble(100) - male;
- genderratio = female.ToString() + "%";
- }
- string stats = null;
- string t8 = Cut(data, ">At Lv. 100<")[1];
- List<string> t9 = new List<string>() { "HP", "Attack", "Defense", "Sp.Atk", "Sp.Def", "Speed" };
- for (int i = 0; i < t9.Count; i++)
- {
- stats += Cut(Cut(Cut(t8, $">{t9[i]}<")[1], "px;\"> ")[1], "\n")[0];
- if (i != t9.Count - 1) { stats += ","; }
- }
- StreamReader sr = new StreamReader(File.OpenRead("pokemon.txt"));
- string t10 = sr.ReadToEnd();
- List<string> pokes = t10.Split('[').ToList();
- string poke = pokes[k+1];
- sr.Close();
- string desc = Cut(Cut(poke, "Pokedex=")[1], "\r\n")[0];
- string t11 = null;
- List<string> evolutions = new List<string>();
- try
- {
- t11 = Cut(Cut(poke, "Evolutions=")[1], "\n")[0];
- if (string.IsNullOrEmpty(t11))
- {
- evolutions.Clear();
- goto done;
- }
- List<string> t12 = t11.Split(',').ToList();
- for (int i = 0; i < t12.Count - 2; i += 3)
- {
- if (t11.Contains("Ninjask"))
- {
- evolutions.Add("Nincada > Level 20 > Ninjask (& Shedinja)");
- break;
- }
- string t13 = t12[i + 1];
- string method = null;
- string cap2 = null;
- try
- {
- cap2 = char.ToUpper(t12[i + 2][0]) + t12[i + 2].Substring(1).ToLower();
- }
- catch (Exception) { }
- string mon = t12[i];
- switch (t13)
- {
- case "Level": method = "Level " + t12[i + 2]; break;
- case "LevelDay": method = "Level " + t12[i + 2] + " during day"; break;
- case "LevelNight": method = "Level " + t12[i + 2] + " during night"; break;
- case "LevelMale": method = "Level " + t12[i + 2] + " when male"; break;
- case "LevelFemale": method = "Level " + t12[i + 2] + " when female"; break;
- case "Item": method = cap2; break;
- case "Happiness": method = "220 Happiness"; break;
- case "HappinessDay": method = "220 Happiness during day"; break;
- case "HappinessNight": method = "220 Happiness during night"; break;
- case "TradeItem": method = "Trade with " + cap2; break;
- case "Trade": method = t13; break;
- case "Location": method = "Specific Location"; break;
- case "HasMove": method = "Knows " + cap2; break;
- case "DayHoldItem": method = "Hold " + cap2 + " during day"; break;
- case "NightHoldItem": method = "Hold " + cap2 + " during night"; break;
- case "AttackGreater": method = "Attack greater than " + t12[i + 2]; break;
- case "DefenseGreater": method = "Defense greater than " + t12[i + 2]; break;
- case "AtkDefEqual": method = "Atk and Def both" + t12[i + 2]; break;
- case "Silcoon": method = "Personality Value"; break;
- case "Cascoon": method = "Personality Value"; break;
- case "ItemMale": method = cap2 + " when male"; break;
- case "ItemFemale": method = cap2 + "when female"; break;
- case "Beauty": method = "Beauty"; break;
- case "HasInParty": method = "Have " + cap2 + " in party"; break;
- case "TradeSpecies": method = "Trade with " + cap2; break;
- case "LevelDarkInParty": method = "Level " + t12[i + 2] + " and Dark type in party"; break;
- case "LevelRain": method = "Level " + t12[i + 2] + " when raining"; break;
- }
- if (method.Contains("stone"))
- {
- method = method.Split(new string[] { "stone" }, StringSplitOptions.None)[0] + " Stone";
- }
- mon = char.ToUpper(mon[0]) + mon.Substring(1).ToLower();
- string ev = string.Format("{0} > {1} > {2}", name, method, mon);
- ev = ev.Replace("\r", null);
- evolutions.Add(ev);
- }
- }
- catch (Exception e) { }
- done:
- string evo = null;
- if (evolutions.Count > 0)
- {
- evo = ",\r\n \"evolutions\": [\r\n";
- evo += " \"" + evolutions[0] + "\"";
- for (int i = 1; i < evolutions.Count; i++)
- {
- evo += ",\r\n \"" + evolutions[i] + "\"";
- }
- evo += "\r\n ]";
- }
- string _type2 = null;
- if (type2 != "Unknown")
- {
- _type2 = "\r\n \"type2\": \"" + type2 + "\",";
- }
- string _abil2 = null;
- if (abil2 != "-----")
- {
- _abil2 = "\r\n \"ability2\": \"" + abil2 + "\",";
- }
- string _ha = null;
- if (ha1 != "-----")
- {
- _ha = "\r\n \"hiddenability\": \"" + ha1 + "\",";
- }
- ret += $@"
- ""{name}"": {{
- ""species"": ""{species}"",
- ""kind"": ""{kind}"",
- ""type1"": ""{type1}"",{_type2}
- ""ability1"": ""{abil1}"",{_abil2}{_ha}
- ""catchrate"": ""{catchrate}"",
- ""height"": ""{height}m"",
- ""weight"": ""{weight}kg"",
- ""base_exp"": ""{exp}"",
- ""evyield"": [{evyield}],
- ""levelingrate"": ""{growthrate}"",
- ""hatchtime"": ""{hatchtime}"",
- ""genderratio"": ""{genderratio}"",
- ""stats"": [{stats}],
- ""url"": ""{img}"",
- ""shuffle"": ""{shuffle}"",
- ""desc"": ""{desc}""{evo}
- }},
- ";
- }
- /*
- {
- "bulbasaur": {
- "species": "001",
- "kind": "Seed",
- "type1": "Grass",
- "type2": "Poison",
- "ability1": "Overgrow",
- "hiddenability": "Chlorophyll",
- "catchrate": "45",
- "height": "0.7m",
- "weight": "6.9kg",
- "base_exp": "64",
- "evyield": [0,0,0,1,0,0],
- "levelingrate": "Medium Slow",
- "hatchtime": "5140 - 5396",
- "genderratio": "12.5",
- "stats": [45,49,49,65,65,45],
- "evolutions": [
- "Bulbasaur > Level 16 > Ivysaur",
- "Ivysaur > Level 32 > Venusaur"
- ],
- "desc": "Bulbasaur can be seen napping in bright sunlight. There is a seed on its back. By soaking up the sun's rays, the seed grows progressively larger."
- }
- }*/
- if (File.Exists("pokemon"+filename+".json")) { File.Delete("pokemon"+filename+".json"); }
- StreamWriter sw = new StreamWriter(File.OpenWrite("pokemon"+filename+".json"));
- sw.Write(ret);
- sw.Close();
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment