Wikipedia:Projekt alla platser-städning/LSJ Rättningskod
Utseende
Detta är en kod skapad av LSJ för att rätta felet med kommunhuvudorter som diskuterats på Bybrunnen. Adville (diskussion) 10 december 2017 kl. 23.02 (CET)
Koden
[redigera | redigera wikitext]//Originally from [[Wikipedia:Projekt DotNetWikiBot Framework/Innocent bot/Ny parameter i Mall Ishockeyspelare]] //Extensively modified by Lsj using System; using System.IO; using System.Text; using System.Text.RegularExpressions; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Threading; using System.Net; using System.Web; using System.Data; using System.Linq; using DotNetWikiBot; class MyBot : Bot { public static int nedit = 0; public static int nerror = 0; public static string password = ""; public static string botkonto = "Lsjbot"; //Change! public static string makelang = "sv"; public static Site site; public static DateTime oldtime = DateTime.Now; public static int pausetime = 4; //time between saves, modified depending on task public static bool pauseaftersave = true; public static bool tryload(Page p, int iattempt) { int itry = 1; while (true) { try { p.Load(); return true; } catch (WebException e) { string message = e.Message; Console.Error.WriteLine(message); itry++; if (itry > iattempt) return false; } } } public static bool trysave(Page p, int iattempt) { return trysave(p, iattempt, site.defaultEditComment); } public static bool trysave(Page p, int iattempt, string editcomment) { int itry = 1; while (true) { try { //Bot.editComment = mp(60); p.Save(editcomment, false); DateTime newtime = DateTime.Now; while (newtime < oldtime) { newtime = DateTime.Now; Thread.Sleep(1000); } oldtime = newtime.AddSeconds(pausetime); if (pauseaftersave) { Console.WriteLine("<ret>"); Console.ReadKey(); } return true; } catch (WebException e) { string message = e.Message; Console.Error.WriteLine("ts we " + message); itry++; if (itry > iattempt) return false; else Thread.Sleep(600000);//milliseconds } catch (WikiBotException e) { string message = e.Message; Console.Error.WriteLine("ts wbe " + message); if (message.Contains("Bad title")) return false; itry++; if (itry > iattempt) return false; else Thread.Sleep(600000);//milliseconds } catch (IOException e) { string message = e.Message; Console.Error.WriteLine("ts ioe " + message); itry++; if (itry > iattempt) return false; else Thread.Sleep(600000);//milliseconds } } } public static void movepage(Site site, string frompage, string topage) { string magnitud = site.GetWebPage(site.address + "/wiki/Special:MovePage/" + HttpUtility.UrlEncode(frompage)); magnitud = magnitud.Substring(magnitud.IndexOf("\"editToken\":\"") + 13); magnitud = magnitud.Substring(0, magnitud.IndexOf("\"") - 1); string tmpStr = site.PostDataAndGetResult(site.address + "/w/api.php", "action=move&from=" + HttpUtility.UrlEncode(frompage) + "&to=" + HttpUtility.UrlEncode(topage) + "&token=" + HttpUtility.UrlEncode(magnitud) + "&reason=Flyttar artikel&redirect=true"); Console.WriteLine(tmpStr); make_redirect(site, frompage, topage, ""); } public static void make_redirect(Site site, string frompage, string topage, string cat) { Page pred = new Page(site, frompage); if (tryload(pred, 1)) { if (!pred.Exists()) { pred.text = "#REDIRECT [[" + topage + "]]"; if (!String.IsNullOrEmpty(cat)) pred.AddToCategory(cat); trysave(pred, 2); } } } public static void addpages(Site site,PageList pl) { //Page pp1 = new Page(site, "Aega monophthalma"); pl.Add(pp1); List<string> sl = new List<string>(); //sl.Add("Lista över insjöar i Arjeplogs kommun (1-1000)"); sl.Add("The Budget"); foreach (string s in sl) { Page pp = new Page(site, s); pl.Add(pp); } } public static string ReplaceOne(string textparam, string oldt, string newt, int position) { string text = textparam; int oldpos = text.IndexOf(oldt, position); if (oldpos < 0) return text; text = text.Remove(oldpos, oldt.Length); text = text.Insert(oldpos, newt); return text; } public static void Main() { Console.Write("Password: "); password = Console.ReadLine(); botkonto = "Lsjbot"; string makelang = "ceb"; site = new Site("https://"+makelang+".wikipedia.org", botkonto, password); site.defaultEditComment = "Fixing mistake"; site.minorEditByDefault = true; List<string> badcountry = new List<string>(); string admlink = ""; string botmall = ""; if (makelang == "sv") { admlink = "#1s administrativa indelning"; botmall = "robotskapad"; badcountry.Add("Albanien"); badcountry.Add("Botswana"); badcountry.Add("Kongo-Kinshasa"); badcountry.Add("Djibouti"); badcountry.Add("Västsahara"); badcountry.Add("Franska Guyana"); badcountry.Add("Gambia"); badcountry.Add("Guatemala"); badcountry.Add("Guinea-Bissau"); badcountry.Add("Haiti"); badcountry.Add("Jersey"); badcountry.Add("Kenya"); badcountry.Add("Kirgizistan"); badcountry.Add("Nordkorea"); badcountry.Add("Sydkorea"); badcountry.Add("Kosovo"); badcountry.Add("Laos"); badcountry.Add("Libanon"); badcountry.Add("Lesotho"); badcountry.Add("Litauen"); badcountry.Add("Marocko"); badcountry.Add("Moldavien"); badcountry.Add("Makedonien"); badcountry.Add("Mali"); badcountry.Add("Burma"); badcountry.Add("Mauritius"); } else if ( makelang == "ceb") { admlink = "administratibo nga mga dibisyon sa #1"; botmall = "paghimo ni bot"; badcountry.Add("Aland Islands"); badcountry.Add("Albanya"); badcountry.Add("Apganistan"); badcountry.Add("Awstriya"); badcountry.Add("Benin"); badcountry.Add("Bahamas"); badcountry.Add("Bhutan"); badcountry.Add("Botswana"); badcountry.Add("Swisa"); badcountry.Add("Kamerun"); badcountry.Add("Colombia"); badcountry.Add("Yibuti"); badcountry.Add("Republikang Dominikano"); badcountry.Add("Western Sahara"); badcountry.Add("Eritrea"); badcountry.Add("Finlandia"); badcountry.Add("Georgia (nasod)"); badcountry.Add("Gambia"); badcountry.Add("Guadeloupe"); badcountry.Add("Gresya"); badcountry.Add("Krowasya"); badcountry.Add("Haiti"); badcountry.Add("Iraq"); badcountry.Add("Islandya"); badcountry.Add("Jersey"); badcountry.Add("Hordanya"); badcountry.Add("Kenya"); badcountry.Add("Kirgistan"); badcountry.Add("Amihanang Korea"); badcountry.Add("Habagatang Korea"); badcountry.Add("Kosobo"); badcountry.Add("Laos"); badcountry.Add("Libano"); badcountry.Add("Lesotho"); badcountry.Add("Maruwekos"); badcountry.Add("Moldabya"); badcountry.Add("Mali"); badcountry.Add("Macao"); badcountry.Add("Martinique"); badcountry.Add("Maurisyo"); badcountry.Add("Namibya"); badcountry.Add("Nicaragua"); badcountry.Add("Olanda"); badcountry.Add("Polonya"); badcountry.Add("Katar"); badcountry.Add("Rumanya"); badcountry.Add("Serbya"); badcountry.Add("Rusya"); badcountry.Add("Arabyang Saudita"); badcountry.Add("Eslobakya"); badcountry.Add("Sierra Leone"); badcountry.Add("Senegal"); badcountry.Add("Chad"); badcountry.Add("Tailandya"); badcountry.Add("Turkmenistan"); badcountry.Add("Turkiya"); badcountry.Add("Uganda"); badcountry.Add("Vanuatu"); badcountry.Add("Samoa"); badcountry.Add("Yemen"); badcountry.Add("Mayotte"); } foreach (string countrynameml in badcountry) { nedit = 0; PageList pl = new PageList(site); PageList pl1 = new PageList(site); string thisadmlink = admlink.Replace("#1", countrynameml); //Select how to get pages. Uncomment as needed. //Add pages "by hand": //addpages(site,pl); //Find articles from a category //pl.FillAllFromCategoryTree("Geografi i Goiás"); //pl1.FillAllFromCategoryTree("Eufriesea"); //foreach (Page p in pl1) // pl.Add(p); //pl1.FillAllFromCategoryTree("Euglossa"); //foreach (Page p in pl1) // pl.Add(p); //pl1.FillAllFromCategoryTree("Eulaema"); //foreach (Page p in pl1) // pl.Add(p); //pl1.FillAllFromCategoryTree("Exaerete"); //foreach (Page p in pl1) // pl.Add(p); //pl.FillAllFromCategoryTree("Heyograpiya sa Wyoming (estado)");//done: //Find subcategories of a category //pl.FillSubsFromCategory("Svampars vetenskapliga namn"); //Find articles from all the links to an article pl.FillFromLinksToPage(thisadmlink); //Find articles containing a specific string //pl.FillFromSearchResults("insource:\" Bethune-baker\"", 4999); //Set specific article: //Page pp = new Page(site, "Citrontrogon");pl.Add(pp); //Skip all namespaces except articles: pl.RemoveNamespaces(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 100, 101 }); //Skip all namespaces except categories: //pl.RemoveNamespaces(new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 100, 101 }); Dictionary<string, string> replacedict = new Dictionary<string, string>(); //replacedict.Add("[[Washington, D.C. (ulohang dakbayan sa Estados Unidos)|Washington, D.C.]].", "[[Washington, D.C.]]"); //replacedict.Add("[[administratibo nga mga dibisyon sa Litwanya|kapital sa munisipyo]] sa [[Latvia]]", "[[administratibo nga mga dibisyon sa Latvia|kapital sa munisipyo]] sa [[Latvia]]"); Dictionary<string, string> regexdict = new Dictionary<string, string>(); //regexdict.Add(@"\| timezone *= \[\[Fernando de Noronha Time\|FNT\]\]", "| timezone = [[Brasilia Time|BRT]]"); //regexdict.Add(@"\| timezone_DST *= \[\[Amazon Summer Time\|AMST\]\]", "| timezone_DST = [[Brasilia Summer Time|BRST]]"); //regexdict.Add(@"\| utc_offset *= -2", "| utc_offset = -3"); //regexdict.Add(@"\| utc_offset_DST *= -3", "| utc_offset_DST = -2"); //regexdict.Add(@"\| category *= Parish", "| category = Civil parish"); List<string> linkword = new List<string>(); //linkword.Add("Catalogue of Life"); //Require title to contain one in requiretitle list: List<string> requiretitle = new List<string>(); //requiretitle.Add("Radioprogram nerlagda"); //Require ALL in requireword list: List<string> requireword = new List<string>(); //requireword.Add("obotskapad"); //requireword.Add("= -3\n"); //requireword.Add("Azerbajdzjan"); //requireword.Add("Latvia"); requireword.Add(botmall); //Require AT LEAST ONE in requireone list: List<string> requireone = new List<string>(); //requireone.Add("obotskapad"); //requireone.Add("himo ni bot"); List<string> vetoword = new List<string>(); //vetoword.Add("Argentina"); //vetoword.Add("Island"); //vetoword.Add("isländska"); vetoword.Add("|" + countrynameml); DateTime oldtime = DateTime.Now; oldtime = oldtime.AddSeconds(5); Console.WriteLine("Pages to change : " + pl.Count().ToString()); int iremain = pl.Count(); bool resume = true; foreach (Page p in pl) { iremain--; //Skip start of alphabet: //if (String.Compare(p.title,"Tir Koh") < 0 ) // continue; if (!resume) { if (p.title == "Ludington City Park") //also set resume=true/false before loop resume = true; else continue; } if (!tryload(p, 2)) continue; if (!p.Exists()) continue; string origtitle = p.title; //Follow redirect: if (p.IsRedirect()) { Console.WriteLine("Redirect"); p.title = p.RedirectsTo(); if (!tryload(p, 2)) continue; if (!p.Exists()) continue; } //Check so required title actually present: if (requiretitle.Count > 0) { bool onefound = false; foreach (string s in requiretitle) if (p.title.Contains(s)) onefound = true; if (!onefound) { Console.WriteLine("requiretitle not found"); continue; } } //Check so all required strings actually present: bool allfound = true; foreach (string s in requireword) if (!p.text.Contains(s)) allfound = false; if (!allfound) { Console.WriteLine("requireword not found"); continue; } if (requireone.Count > 0) { bool onefound = false; foreach (string s in requireone) if (p.text.Contains(s)) onefound = true; if (!onefound) { Console.WriteLine("requireone not found"); continue; } } //Check so no vetoword are present: bool vetofound = false; foreach (string s in vetoword) if (p.text.Contains(s)) vetofound = true; if (vetofound) { Console.WriteLine("vetoword found"); continue; } //If redirect, go back to redirect page: //if (origtitle != p.title) //{ // p.title = origtitle; // p.Load(); //} List<string> pcc = p.GetTemplateParameter(botmall, "1"); if ( pcc.Count == 0) { Console.WriteLine("No param in bot template"); Console.WriteLine(p.text.Substring(0, 100)); continue; } string pcountry = pcc.First(); if (pcountry == countrynameml) continue; string origtext = p.text; //Do the actual replacement: replacedict.Clear(); replacedict.Add("[["+thisadmlink+"|kapital sa munisipyo]] sa [["+pcountry+"]]", "[[" + admlink.Replace("#1", pcountry) + "|kapital sa munisipyo]] sa [[" + pcountry + "]]"); foreach (KeyValuePair<string, string> replacepair in replacedict) { p.text = p.text.Replace(replacepair.Key, replacepair.Value); } //foreach (KeyValuePair<string, string> replacepair in regexdict) //{ // p.text = Regex.Replace(p.text, replacepair.Key, replacepair.Value); //} //Save the result: if (p.text != origtext) { //Bot.editComment = "Ersätter och wikilänkar"; //isMinorEdit = true; if (trysave(p, 4)) { nedit++; if (nedit >= 4) { pauseaftersave = false; } } } Console.WriteLine(iremain.ToString() + " remaining."); //if ( p.title.Contains("sa Burma")) //{ // movepage(site, p.title, p.title.Replace("sa Burma", "sa Birmanya")); //} } Console.WriteLine("Total # edits = " + nedit.ToString()); } //while (false);// (nedit > 0); } }