M3rein

Untitled

Mar 5th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace BulbapediaWebScraper
  10. {
  11. class Program
  12. {
  13. public static void Show(string obj)
  14. {
  15. Console.WriteLine(obj);
  16. }
  17.  
  18. public static void ShowEach<T>(List<T> list)
  19. {
  20. foreach (var entry in list)
  21. {
  22. Console.WriteLine(entry.ToString());
  23. }
  24. }
  25.  
  26. public static List<string> Cut(string splitthis, string bythis)
  27. {
  28. return splitthis.Split(new string[] { bythis }, StringSplitOptions.None).ToList();
  29. }
  30.  
  31. static void Main(string[] args)
  32. {
  33. StreamReader str = new StreamReader(File.OpenRead("pokemon.txt"));
  34. string d = str.ReadToEnd();
  35. str.Close();
  36. List<string> tmp = d.Split(']').ToList();
  37. List<string> pokemon = new List<string>();
  38. for (int i = 1; i < tmp.Count; i++)
  39. {
  40. pokemon.Add(tmp[i].Split(new string[] { "Name=" }, StringSplitOptions.None)[1].Split(new string[] { "\r\n" }, StringSplitOptions.None)[0]);
  41. }
  42. // 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
  43.  
  44. List<int> excluded = new List<int>() { 28, 29, 30, 31, 32, 33, 82 };
  45. string filename = "1-802";
  46.  
  47. string ret = "{";
  48. for (int k = 0; k < 802; k++)
  49. {
  50. if (excluded.Contains(k)) continue;
  51. WebClient client = new WebClient();
  52. string link = $"https://bulbapedia.bulbagarden.net/wiki/{pokemon[k]}_(Pok%C3%A9mon)";
  53. Stream stream = client.OpenRead(link);
  54. string data = new StreamReader(stream).ReadToEnd();
  55.  
  56. string name = Cut(data, " (Pokémon)")[0].Split('>').Last();
  57. string t0 = Cut(data, "title=\"List of Pokémon by National Pokédex number\"><span style=\"color:#000;\">#")[1];
  58. string species = t0.Split('<')[0];
  59. Show(species + " : " + name);
  60.  
  61. string img = Cut(t0, "src=\"//")[1].Split('"')[0];
  62.  
  63. string shuffle = null;
  64. if (data.Contains("Shuffle" + species))
  65. {
  66. List<string> attempts = new List<string>() { null, "A", "EX", "S" };
  67. foreach (string attempt in attempts)
  68. {
  69. try
  70. {
  71. shuffle = Cut(Cut(data, $"Shuffle{species}{attempt}.png\" w")[1], "srcset=\"//")[1].Split(' ')[0];
  72. break;
  73. }
  74. catch (Exception) { }
  75. }
  76. }
  77.  
  78. string kind = Cut(Cut(data, "title=\"Pokémon category\"><span style=\"color:#000;\">")[1], " Pokémon")[0];
  79. if (kind.Contains("<span"))
  80. {
  81. kind = kind.Split('>')[1].Split('<')[0];
  82. }
  83.  
  84. List<string> t1 = Cut(data, "</b></span></a>");
  85. string type1 = t1[0].Split('>').Last();
  86. string type2 = t1[1].Split('>').Last();
  87.  
  88. string t2 = null;
  89. try
  90. {
  91. t2 = Cut(Cut(data, "Abilities</span></a></b>")[1], "</td></tr></table>")[0];
  92. }
  93. catch (Exception)
  94. {
  95. t2 = Cut(Cut(data, "Ability</span></a></b>")[1], "</td></tr></table>")[0];
  96. }
  97. List<string> t3 = Cut(t2, "_(Ability)");
  98. t3.RemoveAt(t3.Count - 1);
  99. for (int i = 0; i < t3.Count; i++)
  100. {
  101. t3[i] = t3[i].Split('/').Last();
  102. }
  103. string abil1 = t3[0] == "Cacophony" ? "-----" : t3[0];
  104. string abil2 = t3[1] == "Cacophony" ? "-----" : t3[1];
  105. string ha1 = t3[t3.Count == 7 ? 3 : 4] == "Cacophony" ? "-----" : t3[t3.Count == 7 ? 3 : 4];
  106.  
  107. string catchrate = Cut(Cut(data, "Catch_rate")[1], "<td> ")[1].Split(' ')[0];
  108.  
  109. string height = Cut(Cut(data, ">Height<")[1], " m")[0].Split(' ').Last();
  110.  
  111. string weight = Cut(Cut(data, ">Weight<")[1], " kg")[0].Split(' ').Last();
  112.  
  113. string t4 = Cut(Cut(data, "Base experience yield")[1], "</td></tr></table>")[0];
  114. string exp = null;
  115. if (t4.Contains("V+"))
  116. {
  117. exp = Cut(t4, "<td> ").Last().Split('<')[0];
  118. }
  119. else
  120. {
  121. exp = Cut(Cut(t4, "<td> ").Last(), "\n")[0];
  122. }
  123.  
  124. string evyield = null;
  125. string t5 = Cut(data, "EV yield")[1];
  126. List<string> t6 = Cut(t5, ";\"> ");
  127. for (int i = 1; i < 7; i++)
  128. {
  129. string ev = t6[i].Split('<')[0];
  130. evyield += ev + (i != 6 ? "," : null);
  131. }
  132.  
  133. string growthrate = Cut(Cut(Cut(data, "Leveling rate")[1], "<td> ")[1], "\n")[0];
  134.  
  135. string hatchtime = Cut(Cut(Cut(data, "Hatch time")[1], "<td> ")[1], "&#160;<")[0];
  136. hatchtime = hatchtime.Replace("&#160;", " ");
  137.  
  138. string genderratio = null;
  139. string t7 = Cut(data, "Gender ratio")[1];
  140. if (t7.Contains(">Genderless<"))
  141. {
  142. genderratio = "Genderless";
  143. }
  144. else if (t7.Contains(">100% female<"))
  145. {
  146. genderratio = "100%";
  147. }
  148. else if (t7.Contains(">100% male<"))
  149. {
  150. genderratio = "0%";
  151. }
  152. else
  153. {
  154. double male = Convert.ToDouble(Cut(t7, "% ")[0].Split('>').Last());
  155. double female = Convert.ToDouble(100) - male;
  156. genderratio = female.ToString() + "%";
  157. }
  158.  
  159. string stats = null;
  160. string t8 = Cut(data, ">At Lv. 100<")[1];
  161. List<string> t9 = new List<string>() { "HP", "Attack", "Defense", "Sp.Atk", "Sp.Def", "Speed" };
  162. for (int i = 0; i < t9.Count; i++)
  163. {
  164. stats += Cut(Cut(Cut(t8, $">{t9[i]}<")[1], "px;\"> ")[1], "\n")[0];
  165. if (i != t9.Count - 1) { stats += ","; }
  166. }
  167.  
  168. StreamReader sr = new StreamReader(File.OpenRead("pokemon.txt"));
  169. string t10 = sr.ReadToEnd();
  170. List<string> pokes = t10.Split('[').ToList();
  171. string poke = pokes[k+1];
  172. sr.Close();
  173. string desc = Cut(Cut(poke, "Pokedex=")[1], "\r\n")[0];
  174.  
  175. string t11 = null;
  176. List<string> evolutions = new List<string>();
  177. try
  178. {
  179. t11 = Cut(Cut(poke, "Evolutions=")[1], "\n")[0];
  180. if (string.IsNullOrEmpty(t11))
  181. {
  182. evolutions.Clear();
  183. goto done;
  184. }
  185. List<string> t12 = t11.Split(',').ToList();
  186. for (int i = 0; i < t12.Count - 2; i += 3)
  187. {
  188. if (t11.Contains("Ninjask"))
  189. {
  190. evolutions.Add("Nincada > Level 20 > Ninjask (& Shedinja)");
  191. break;
  192. }
  193. string t13 = t12[i + 1];
  194. string method = null;
  195. string cap2 = null;
  196. try
  197. {
  198. cap2 = char.ToUpper(t12[i + 2][0]) + t12[i + 2].Substring(1).ToLower();
  199. }
  200. catch (Exception) { }
  201.  
  202. string mon = t12[i];
  203. switch (t13)
  204. {
  205. case "Level": method = "Level " + t12[i + 2]; break;
  206. case "LevelDay": method = "Level " + t12[i + 2] + " during day"; break;
  207. case "LevelNight": method = "Level " + t12[i + 2] + " during night"; break;
  208. case "LevelMale": method = "Level " + t12[i + 2] + " when male"; break;
  209. case "LevelFemale": method = "Level " + t12[i + 2] + " when female"; break;
  210. case "Item": method = cap2; break;
  211. case "Happiness": method = "220 Happiness"; break;
  212. case "HappinessDay": method = "220 Happiness during day"; break;
  213. case "HappinessNight": method = "220 Happiness during night"; break;
  214. case "TradeItem": method = "Trade with " + cap2; break;
  215. case "Trade": method = t13; break;
  216. case "Location": method = "Specific Location"; break;
  217. case "HasMove": method = "Knows " + cap2; break;
  218. case "DayHoldItem": method = "Hold " + cap2 + " during day"; break;
  219. case "NightHoldItem": method = "Hold " + cap2 + " during night"; break;
  220. case "AttackGreater": method = "Attack greater than " + t12[i + 2]; break;
  221. case "DefenseGreater": method = "Defense greater than " + t12[i + 2]; break;
  222. case "AtkDefEqual": method = "Atk and Def both" + t12[i + 2]; break;
  223. case "Silcoon": method = "Personality Value"; break;
  224. case "Cascoon": method = "Personality Value"; break;
  225. case "ItemMale": method = cap2 + " when male"; break;
  226. case "ItemFemale": method = cap2 + "when female"; break;
  227. case "Beauty": method = "Beauty"; break;
  228. case "HasInParty": method = "Have " + cap2 + " in party"; break;
  229. case "TradeSpecies": method = "Trade with " + cap2; break;
  230. case "LevelDarkInParty": method = "Level " + t12[i + 2] + " and Dark type in party"; break;
  231. case "LevelRain": method = "Level " + t12[i + 2] + " when raining"; break;
  232. }
  233. if (method.Contains("stone"))
  234. {
  235. method = method.Split(new string[] { "stone" }, StringSplitOptions.None)[0] + " Stone";
  236. }
  237. mon = char.ToUpper(mon[0]) + mon.Substring(1).ToLower();
  238.  
  239. string ev = string.Format("{0} > {1} > {2}", name, method, mon);
  240. ev = ev.Replace("\r", null);
  241. evolutions.Add(ev);
  242. }
  243. }
  244. catch (Exception e) { }
  245.  
  246. done:
  247.  
  248. string evo = null;
  249. if (evolutions.Count > 0)
  250. {
  251. evo = ",\r\n \"evolutions\": [\r\n";
  252. evo += " \"" + evolutions[0] + "\"";
  253. for (int i = 1; i < evolutions.Count; i++)
  254. {
  255. evo += ",\r\n \"" + evolutions[i] + "\"";
  256. }
  257. evo += "\r\n ]";
  258. }
  259.  
  260. string _type2 = null;
  261. if (type2 != "Unknown")
  262. {
  263. _type2 = "\r\n \"type2\": \"" + type2 + "\",";
  264. }
  265. string _abil2 = null;
  266. if (abil2 != "-----")
  267. {
  268. _abil2 = "\r\n \"ability2\": \"" + abil2 + "\",";
  269. }
  270. string _ha = null;
  271. if (ha1 != "-----")
  272. {
  273. _ha = "\r\n \"hiddenability\": \"" + ha1 + "\",";
  274. }
  275.  
  276. ret += $@"
  277. ""{name}"": {{
  278. ""species"": ""{species}"",
  279. ""kind"": ""{kind}"",
  280. ""type1"": ""{type1}"",{_type2}
  281. ""ability1"": ""{abil1}"",{_abil2}{_ha}
  282. ""catchrate"": ""{catchrate}"",
  283. ""height"": ""{height}m"",
  284. ""weight"": ""{weight}kg"",
  285. ""base_exp"": ""{exp}"",
  286. ""evyield"": [{evyield}],
  287. ""levelingrate"": ""{growthrate}"",
  288. ""hatchtime"": ""{hatchtime}"",
  289. ""genderratio"": ""{genderratio}"",
  290. ""stats"": [{stats}],
  291. ""url"": ""{img}"",
  292. ""shuffle"": ""{shuffle}"",
  293. ""desc"": ""{desc}""{evo}
  294. }},
  295. ";
  296. }
  297. /*
  298. {
  299. "bulbasaur": {
  300. "species": "001",
  301. "kind": "Seed",
  302. "type1": "Grass",
  303. "type2": "Poison",
  304. "ability1": "Overgrow",
  305. "hiddenability": "Chlorophyll",
  306. "catchrate": "45",
  307. "height": "0.7m",
  308. "weight": "6.9kg",
  309. "base_exp": "64",
  310. "evyield": [0,0,0,1,0,0],
  311. "levelingrate": "Medium Slow",
  312. "hatchtime": "5140 - 5396",
  313. "genderratio": "12.5",
  314. "stats": [45,49,49,65,65,45],
  315.  
  316. "evolutions": [
  317. "Bulbasaur > Level 16 > Ivysaur",
  318. "Ivysaur > Level 32 > Venusaur"
  319. ],
  320. "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."
  321. }
  322. }*/
  323.  
  324. if (File.Exists("pokemon"+filename+".json")) { File.Delete("pokemon"+filename+".json"); }
  325. StreamWriter sw = new StreamWriter(File.OpenWrite("pokemon"+filename+".json"));
  326. sw.Write(ret);
  327. sw.Close();
  328. Console.ReadKey();
  329. }
  330. }
  331. }
Advertisement
Add Comment
Please, Sign In to add comment