Wikipedia:Projekt DotNetWikiBot Framework/Innocent bot/JPL
Utseende
using System; using System.IO; using System.Text; using System.Text.RegularExpressions; using System.Collections; using System.Xml; using System.Threading; using System.Net; using System.Web; using DotNetWikiBot; class MyBot : Bot { static StringBuilder sb = new StringBuilder(); static byte[] buf = new byte[8192]; static int count; static string tempString; private static string getPage(string url) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream resStream = response.GetResponseStream(); sb.Length = 0; tempString = null; count = 0; do { count = resStream.Read(buf, 0, buf.Length); if (count != 0) { tempString = Encoding.UTF8.GetString(buf, 0, count); sb.Append(tempString); } } while (count > 0); return sb.ToString(); } private static string getURLPost(string uri, string parameters) { WebRequest webRequest = WebRequest.Create (uri); byte[] bytes = Encoding.ASCII.GetBytes (parameters); Stream outputStream = null; webRequest.Method = "POST"; webRequest.ContentType = "application/x-www-form-urlencoded"; try { webRequest.ContentLength = bytes.Length; outputStream = webRequest.GetRequestStream(); outputStream.Write(bytes, 0, bytes.Length); } catch (WebException ex) { Console.WriteLine(ex.Message + "Error with request!"); } finally { if (outputStream != null) outputStream.Close(); } try { WebResponse webResponse = webRequest.GetResponse(); if (webResponse == null) return null; StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream()); return myStreamReader.ReadToEnd().Trim(); } catch (WebException ex) { Console.WriteLine(ex.Message + "Error with response!"); } return null; } private static string avrunda(string siffra, string sigma) { siffra = siffra.Replace(".", ","); sigma = sigma.Replace(".", ","); if(siffra.IndexOf("<br>") != -1) siffra = siffra.Substring(0, siffra.IndexOf("<br>")); double en = double.Parse(siffra); double edn = 0; if(sigma.IndexOf("<br>") != -1) sigma = sigma.Substring(0, sigma.IndexOf("<br>")); if(sigma != "n/a") edn = double.Parse(sigma); else edn = en/1000; double l = Math.Round(Math.Log10(edn)); en = Math.Round(en*Math.Pow(10, -l))/Math.Pow(10, -l); return en.ToString(); } private static string au(string siffra, string sigma) { siffra = siffra.Replace(".", ","); sigma = sigma.Replace(".", ","); string h = avrunda(siffra, sigma); int l = h.Replace(",", "").Length; double km = double.Parse(siffra)*149597870.691; string k = km.ToString(); double skm = 0; string sk = ""; if(sigma != "n/a") { skm = double.Parse(sigma)*149597870.691; sk = skm.ToString(); } else sk = sigma; return avrunda(k, sk); } private static string enkel(string kod, string src) { src = src.Substring(src.IndexOf("sbdb_help.cgi?name="+kod)); src = src.Substring(src.IndexOf("size=\"2\">")+9); string e = "0"+src.Substring(0,src.IndexOf("</font>")); src = src.Substring(src.IndexOf("size=\"2\">")+9); string ed = src.Substring(0,src.IndexOf("</font>")); return avrunda(e, ed); } private static string enkel2(string kod, string src) { if(src.IndexOf("sbdb_help.cgi?name="+kod) != -1) { src = src.Substring(src.IndexOf("sbdb_help.cgi?name="+kod)); src = src.Substring(src.IndexOf("size=\"2\">")+9); string e = "0"+src.Substring(0,src.IndexOf("</font>")); src = src.Substring(src.IndexOf("size=\"2\">")+9); src = src.Substring(src.IndexOf("size=\"2\">")+9); string ed = src.Substring(0,src.IndexOf("</font>")); return ed; } else return ""; } private static string knepigare1(string kod, string src) { src = src.Substring(src.IndexOf("sbdb_help.cgi?name="+kod)); src = src.Substring(src.IndexOf("size=\"2\">")+9); string a = "0"+src.Substring(0,src.IndexOf("</font>")); src = src.Substring(src.IndexOf("size=\"2\">")+9); string ed = src.Substring(0,src.IndexOf("</font>")); return avrunda(a, ed); } private static string knepigare2(string kod, string src) { src = src.Substring(src.IndexOf("sbdb_help.cgi?name="+kod)); src = src.Substring(src.IndexOf("size=\"2\">")+9); string a = "0"+src.Substring(0,src.IndexOf("</font>")); src = src.Substring(src.IndexOf("size=\"2\">")+9); string ed = src.Substring(0,src.IndexOf("</font>")); return au(a, ed); } private static string tusen(string value) { if(value.IndexOf(",")!=-1) value=value.Substring(0, value.IndexOf(",")); string value1 = value; if(value.Length > 3) { string def = value.Substring(0,value.Length-3); string hepp = value.Substring(value.Length-3); value1 = tusen(def)+" "+hepp; } return value1; } /**/ public static void Main() { DateTime time = DateTime.Now; string datum = String.Format("{0:d MMMMMMMMMM yyyy}", time); bool okToRun = true; string r = File.ReadAllText("Räknare.txt"); string str = "http://ssd.jpl.nasa.gov/sbdb.cgi?sstr="; int r1 = int.Parse(r)+1; r = r1.ToString(); str = str + r; string arkiv = ""; string tmpStr = getURLPost("http://www.webcitation.org/archive.php", "url=" + str + "&email=la.vallen@yahoo.se"); arkiv = "http://"+tmpStr.Substring(tmpStr.IndexOf("An archive of this page should shortly be available at </p><br /><p>") + 83, 29); if(!arkiv.StartsWith("http://www.webcitation.org")) okToRun = false; /**/ if(okToRun) { string src = getPage(str); src = src.Substring(src.IndexOf("<!-- START OF PAGE CONTENT -->")); //Hämtar namnet på kroppen src = src.Substring(src.IndexOf("<table bgcolor=")); src = src.Substring(src.IndexOf("<font face=")); src = src.Substring(src.IndexOf("<b>")+3); string titel = src.Substring(0,src.IndexOf("</b>")); string kod = ""; string namn = titel; if(titel.IndexOf("(") != -1) { namn = titel.Substring(0,titel.IndexOf("(")).Trim(); kod = titel.Substring(titel.IndexOf("(")+1); kod = kod.Substring(0,kod.IndexOf(")")); } if(namn == r) namn = ""; if(titel.IndexOf(" ") != -1) titel = titel.Substring(titel.IndexOf(" ")).Trim(); //Hämtar klassifikation src = src.Substring(src.IndexOf("<b>Classification:</b>")); string klass = src.Substring(0,src.IndexOf("</a></font>")); klass = klass.Substring(klass.LastIndexOf(">")+1); klass = klass.Replace("Amor", "[[Amor-asteroid]]"); klass = klass.Replace("Main-belt Asteroid", "[[Asteroidbältet]]"); bool neo = false; bool pha = false; if(src.Substring(0,300).IndexOf("NEO") != -1) neo = true; if(src.Substring(0,300).IndexOf("PHA") != -1) pha = true; //Hämtar Epoken src = src.Substring(src.IndexOf("Orbital Elements at Epoch")); src = src.Substring(src.IndexOf("(")+1); string epok = src.Substring(0, src.IndexOf(")")); if(epok.IndexOf(".")!= -1) epok = epok.Substring(0, epok.IndexOf(".")); epok = String.Format("{0:d MMMMMMMMMM yyyy}", DateTime.Parse(epok)); //Hämtar 'e' string e = enkel("e", src); //Hämtar 'a' string akm = knepigare2("a", src); string a = knepigare1("a", src); akm = tusen(akm); //Hämtar 'q' string qkm = knepigare2("q", src); string q = knepigare1("q", src); qkm = tusen(qkm); //Hämtar 'i' string ii = enkel("i", src); //Hämtar 'node' string node = enkel("om", src); //Hämtar 'peri' string peri = enkel("w", src); //Hämtar 'M' string M = enkel("ma", src); //Hämtar 'per' string per = enkel("per", src); per = tusen(per.Substring(0,per.IndexOf(",")))+per.Substring(per.IndexOf(",")); src = src.Substring(src.IndexOf("sbdb_help.cgi?name=per")); src = src.Substring(src.IndexOf("size=\"2\">")+9); src = src.Substring(src.IndexOf("<br>")+4); string y = src.Substring(0,src.IndexOf("</font>")); y = y.Replace(".", ","); double v1 = double.Parse(a)*149597870.691*3.1415927*2/double.Parse(y)/365.25/24/3600; string v = v1.ToString(); v = v.Substring(0, v.IndexOf(",")+4); Console.WriteLine(v); //Hämtar 'Q' string Qkm = knepigare2("ad", src); string Q = knepigare1("ad", src); Qkm = tusen(Qkm); //Hämtar 'H' string H = enkel2("H", src); H = H.Replace(".", ","); //Hämtar alternate designation string alt = ""; if(src.IndexOf("<b>Alternate Designations</b>") != -1) { src = src.Substring(src.IndexOf("<b>Alternate Designations</b>")); src = src.Substring(src.IndexOf("size=\"2\">")+9); string s = src.Substring(0,src.IndexOf("</font>")); s = s.Replace(" ",""); s = s.Replace("<b>","'''"); s = s.Replace("</b>","'''"); string [] alta = s.Split('='); foreach(string s3 in alta) { string s2 = s3.Trim(); s2 = s2.Replace(" ", " "); alt = alt + s2+", "; } alt = alt.Substring(0, alt.Length -2); } if(neo) klass = klass+"<br>[[Jordnära objekt]]"; if(pha) klass = klass+"<br>[[Potentiellt farliga objekt]]"; //"[[NASA]] [[Jet Propulsion Laboratory|JPL]] Small-Body Database Browser on" Site site = new Site("http://sv.wikipedia.org", "Innocent bot", "********"); string title = namn; if(titel == "") title = kod; Page p = new Page(site, title); p.Load(); File.WriteAllText("prefe.txt", p.text); if(p.Exists()) { if(p.text.ToLower().Trim().StartsWith("#omdirigering") || p.text.ToLower().Trim().StartsWith("#redirect")) { string s = p.text.Substring(p.text.IndexOf("[[")+2); s = s.Substring(0, s.IndexOf("]]")).Trim(); p.title = s; p.Load(); File.WriteAllText("prefe.txt", p.text); } string referens = "<ref name=\"JPL\">{{Webbref|url=http://ssd.jpl.nasa.gov/sbdb.cgi?sstr="+r+"|titel=JPL Small-Body Database Browser on "+titel+"|utgivare=[[NASA]], [[Jet Propulsion Laboratory|JPL]]|språk=engelska|"; if(arkiv != "") referens = referens + "arkivurl="+arkiv+"|arkivdatum="+datum+"|"; referens = referens + "hämtdatum="+datum+"}}</ref>"; string texten = p.text; if(Mallhantering.finnsmallen(texten, "Infobox planet")) { texten = Mallhantering.sättvärde(texten, "Infobox planet", 0, "omloppsbana_ref", referens); texten = Mallhantering.sättvärde(texten, "Infobox planet", 0, "mp_namn", namn); texten = Mallhantering.sättvärde(texten, "Infobox planet", 0, "mp_kategori", klass); texten = Mallhantering.sättvärde(texten, "Infobox planet", 0, "epok", epok); texten = Mallhantering.sättvärde(texten, "Infobox planet", 0, "alt_namn", alt); texten = Mallhantering.sättvärde(texten, "Infobox planet", 0, "namn", titel); texten = Mallhantering.sättvärde(texten, "Infobox planet", 0, "aphelium", Q+" [[Astronomisk enhet|AU]]<br />"+Qkm+" [[Kilometer|km]]"); texten = Mallhantering.sättvärde(texten, "Infobox planet", 0, "perihelium", q+" AU<br />"+qkm+" km"); texten = Mallhantering.sättvärde(texten, "Infobox planet", 0, "halv_storaxel", a+" AU<br />"+akm+" km"); texten = Mallhantering.sättvärde(texten, "Infobox planet", 0, "excentricitet", e); texten = Mallhantering.sättvärde(texten, "Infobox planet", 0, "period", per+" d ("+y+" år)"); texten = Mallhantering.sättvärde(texten, "Infobox planet", 0, "medelhastighet", v+" km/s"); texten = Mallhantering.sättvärde(texten, "Infobox planet", 0, "inklination", ii+"°"); texten = Mallhantering.sättvärde(texten, "Infobox planet", 0, "uppst_nod", node+"°"); texten = Mallhantering.sättvärde(texten, "Infobox planet", 0, "medelanomali", M+"°"); texten = Mallhantering.sättvärde(texten, "Infobox planet", 0, "peri_arg", peri+"°"); texten = Mallhantering.sättvärde(texten, "Infobox planet", 0, "abs_magnitud", H); Bot.editComment = "Uppdaterar banelement mm från [[Jet Propulsion Laboratory|JPL]] med arkivurl."; if(arkiv == "") Bot.editComment = "Uppdaterar banelement mm från [[Jet Propulsion Laboratory|JPL]]."; Bot.isMinorEdit = true; p.text = texten; p.Save(); Console.WriteLine(texten); } } } } }