Хэрэглэгшэ:TohaomgBot

Сүлөөтэ Нэбтэрхэй Толи — Википеэдиһээ
Шууд ошохо: тамаралга, хайха

Этот бот заменяет все вхождения определённого растрового изображения на его аналог в формате SVG, например "какое-то_изображение.jpg" на "какое-то_изображение.svg". Пара изображений подбирается вручную. Код программы можно увидеть внизу (Я разрешаю кому угодно использовать, распространять и изменять его, но не получать от него финансовую прибыль). Бот написан на языке C# с использованием библиотеки DotNetWikiBot.

Код

  1 using System;
  2 using System.Net;
  3 using System.Threading;
  4 using System.Collections.Generic;
  5 using System.Text.RegularExpressions;
  6 using DotNetWikiBot;
  7 
  8 class ImageReplacer:Bot
  9 {
 10  [STAThread]
 11  public static void Main(string[] args)
 12  {
 13   string username = args[0], password = args[1];
 14   string new_img;
 15   List<string> sites_list = new List<string>();
 16   Dictionary<string, string> summary = new Dictionary<string, string>();
 17   int edits, rejects;
 18   MatchCollection mc;
 19   
 20   summary.Add("en", "BOT: Replaced raster image with an image of format SVG.");
 21   summary.Add("de", "BOT: Ersetzt das rasterbild mit einem bild des formats SVG.");
 22   summary.Add("pt", "BOT: Substituição de imagem raster por formato SVG.");
 23   summary.Add("uk", "БОТ: Замінено растрове зображення на зображення в форматі SVG.");
 24   summary.Add("ru", "БОТ: Заменено растровое изображение на изображение в формате SVG.");
 25   summary.Add("be", "БОТ: Заменены растравы малюнак на малюнак у фармаце SVG.");
 26   summary.Add("pl", "BOT: Zamień grafikę rastrową na obraz w formacie SVG.");
 27   summary.Add("sr", "BOT: Zamenjena rasterska slika sa slikom u formatu SVG.");
 28   summary.Add("hr", "BOT: Zamijenjena rasterska slika slikom u formatu SVG.");
 29   summary.Add("hu", "BOT: Kép(ek) cseréje SVG-formátumú változatra.");
 30   summary.Add("ar", "BOT: استبدال صورة نقطية بصورة متجهية متغيرة (SVG).");
 31   summary.Add("fa", "BOT: جایگزینی پرونده با نسخهٔ برداری SVG");
 32   summary.Add("nv", "BÉÉSH HINÁANII: Eʼelyaaígíí łahgo áyiilaa; SVG choolʼį́.");
 33   
 34   beginning:
 35   edits = 0;
 36 
 37 
 38   //USER INPUT
 39   Console.WriteLine("Replace: ");
 40   string old_img = args.Length>=3?args[2]:Console.ReadLine();
 41 	if(args.Length>=3) {Console.WriteLine(old_img);}
 42   if(old_img.StartsWith("id="))
 43   {
 44    Site commons_site = new Site("https://commons.wikimedia.org/", username, password);
 45    Page commons_page = new Page(commons_site, Int64.Parse(old_img.Substring(3)));
 46    commons_page.Load();
 47    old_img = commons_page.title.Substring(5);
 48    Console.WriteLine(old_img);
 49  
 50    mc = Regex.Matches(commons_page.text, @"\|((.)+svg)");
 51    new_img = mc[0].Groups[1].Value;
 52    
 53    new_img = new_img.Replace("File:", "");
 54    if(new_img.Contains("|")) {new_img = new_img.Substring(new_img.IndexOf("|")+1);}
 55    if(new_img.Contains("_")) {new_img = new_img.Replace("_", " ");}
 56  
 57    Console.WriteLine("\n"+"with: \n" + new_img);
 58   }
 59   else
 60   {
 61    Console.WriteLine("\n"+"with: ");
 62    new_img = args.Length>=4?args[3]:Console.ReadLine();
 63    	if(args.Length>=4) {Console.WriteLine(new_img);}
 64    if(new_img==old_img)
 65    {Console.WriteLine("\nNew image can not be the same as the old one!\n"); goto beginning;}
 66   }
 67   string old_img_underlined = old_img.Replace(" ", "_");
 68   Console.WriteLine();
 69  
 70 
 71   //WHAT WIKIS TO PROCESS
 72   string htmlCode;
 73   string img_usage_list = "https://" + "commons.wikimedia.org/w/index.php?title=Special:GlobalUsage&limit=500&target=" + 
 74 	old_img.Replace(" ", "+");
 75   bool goto_next_page = true;
 76   sites_list.Clear();
 77 
 78   while(goto_next_page)
 79   {
 80    try {using (WebClient client = new WebClient()) {htmlCode = client.DownloadString(img_usage_list);}}
 81    catch {Console.WriteLine("No page for this image found!"); goto beginning;}
 82   
 83    mc = Regex.Matches(htmlCode, @"\s([-\w]+\.(\w)+\.org)");
 84    if(mc.Count==0) {Console.WriteLine("No inclusions found!"); goto beginning;}
 85    foreach (Match m in mc)
 86    {
 87 	if(!sites_list.Contains(m.Groups[1].Value) && m.Groups[1].Value!="nl.wikipedia.org" && m.Groups[1].Value!="simple.wikipedia.org")
 88 		{sites_list.Add(m.Groups[1].Value);}
 89    }
 90 
 91    goto_next_page = !htmlCode.Contains("| next 500)");
 92    if(goto_next_page)
 93    {
 94 	mc = Regex.Matches(htmlCode, @"\|\s\<a href" + "=\"(/w/index.php" + @"\?title=Special:GlobalUsage&amp;limit=500&amp;from=" + "[^\"]+)\"");
 95     img_usage_list = "https://" + "commons.wikimedia.org" + mc[0].Groups[1].Value.Replace("&amp;", "&");
 96    }
 97   }
 98 
 99 
100   //PROCESSING
101   try
102   {
103    Site wiki_site; PageList inclusions; int site_counter = 1, page_counter; string lang;
104    foreach (string st_f in sites_list)
105    {
106     string st = st_f;
107     rejects = 0;
108     Console.WriteLine("\n" + site_counter + ". " + st);
109 
110     try_connect_again:
111     try {wiki_site = new Site("https://" + st, username, password);}
112     catch(System.Net.WebException)
113     {
114      Console.WriteLine("No internet connection or wrong sitename!");
115      Console.WriteLine("Check the connection and type sitename again (type 'skip' to skip):");
116      st = Console.ReadLine();
117      if(st=="skip") {continue;}
118      goto try_connect_again;
119     }
120     catch(DotNetWikiBot.WikiBotException)
121     {Console.WriteLine("Failed to login! This site will be skipped."); continue;}
122 
123     inclusions = new PageList(wiki_site);
124     inclusions.FillFromPagesUsingImage(old_img);
125     Console.WriteLine("\t" + inclusions.Count() + " inclusions found" + "\n");
126     lang = GetLang(st);
127   
128     page_counter = 1;
129     foreach (Page wiki_page in inclusions)
130     {
131      Console.WriteLine("\t" + page_counter + ". " + wiki_page.title);
132 	 
133 	 if(st_f=="www.wikidata.org")
134 	 {
135 	  System.Windows.Forms.Clipboard.SetText(new_img);
136 	  System.Diagnostics.Process.Start("http://" + "www.wikidata.org/wiki/" + wiki_page.title);
137 	  continue;
138 	 }
139 
140      Console.Write("\t");
141      wiki_page.Load();
142      if(wiki_page.Exists() && (wiki_page.text.Contains(old_img) || wiki_page.text.Contains(old_img_underlined)))
143      {
144       wiki_page.text = wiki_page.text.Replace(old_img, new_img);
145       wiki_page.text = wiki_page.text.Replace(old_img_underlined, new_img);
146       Console.Write("\t");
147       try {edits++; wiki_page.Save(summary[lang], true); if(st.StartsWith("pt.")) {Thread.Sleep(10000);}}
148       catch (DotNetWikiBot.WikiBotException)
149       {
150        edits--;
151        Console.WriteLine("\t\t" + "Site did not allow this edit");
152        if(++rejects > 3)
153        {Console.WriteLine("\t" + "To many rejects. This site will be skipped"); break;}
154       }
155 	  catch(System.Net.WebException e)
156 	  {
157 	   Console.WriteLine("\t\t" + "Network error: ");
158 	   Console.WriteLine("\t\t" + e.Message);
159 	   Console.WriteLine("\t\t" + "Waiting one minute");
160 	   Thread.Sleep(60000);
161 	  }
162 	  catch(Exception e) {Console.WriteLine("\t\tUnhandled exception: " + e.GetType()); Console.WriteLine("\t\t" + e.Message);}
163      }
164      else {Console.WriteLine("\t\t" + "Image not present");}
165 
166      page_counter++;
167     }
168     site_counter++;
169    }
170   }
171 
172   catch(Exception e)
173   {
174    Console.WriteLine("Unhandled exception: " + e.GetType());
175    Console.WriteLine(e.Message);
176    Console.ReadKey();
177   }
178 
179   //WRITING TO LOG
180   finally
181   {
182    if(edits>=3)
183    {
184     if(old_img.Length>64) {old_img = old_img.Substring(0,60) + "...";}
185     if(new_img.Length>64) {new_img = new_img.Substring(0,60) + "...";}
186 
187     System.IO.StreamWriter outfile = new System.IO.StreamWriter("log.txt", true);
188     outfile.Write("\r\n" + old_img);
189     int tabs = (old_img.Length)/8;
190     tabs = 8 - tabs;
191     while (tabs!=0) {outfile.Write("\t"); tabs--;}
192 
193     outfile.Write(new_img);
194     tabs = (new_img.Length)/8;
195     tabs = 8 - tabs;
196     while (tabs!=0) {outfile.Write("\t"); tabs--;}
197 
198     outfile.Write(DateTime.Now.ToString("dd.MM.yyyy (HH:mm)"));
199     outfile.Write("\t{0,4}", edits);
200     outfile.Close();
201    }
202    Console.WriteLine("DONE! - " + edits + " edits done\n");
203   }
204   if(args.Length>=3) {return;}
205   goto beginning;
206  }
207 
208  
209  //CHOSING LANGUAGE OF EDIT SUMMARY
210  public static string GetLang(string site)
211  {
212   string[] rulangs = {"ru.", "kk.", "hy.", "uz.", "ky.", "tg.", "crh.", "gag.", "tt.", "av.", 
213                          "kbd.", "ab.", "ba.", "bxr.", "os.", "kv.", "krc.", "mrj.", "lbe.", "lez.", 
214                          "mdf.", "ce.", "mhr.", "koi.", "sah.", "cu.", "tyv.", "udm.", "xal.", "cv.", 
215 			 "myv.", "ady"};
216   string[] delangs = {"de.", "lb.", "li.", "als.", "bar.", "ksh."};
217   string[] pllangs = {"pl.", "szl.", "csb.", "hsb.", "dsb."};
218   string[] srlangs = {"sr.", "sh.", "bs.", "mk."};
219 
220   if (site.StartsWith("uk.") || site.StartsWith("rue.")) {return "uk";}
221   else if (site.StartsWith("be.") || site.StartsWith("be-tarask.")) {return "be";}
222   else if (site.StartsWith("hr.") || site.StartsWith("sl.")) {return "hr";}
223   else if (site.StartsWith("pt.") || site.StartsWith("es.") || site.StartsWith("gl.")) {return "pt";}
224   else if (site.StartsWith("hu.")) {return "hu";}
225   else if (site.StartsWith("ar.")) {return "ar";}
226   else if (site.StartsWith("fa.")) {return "fa";}
227   else if (site.StartsWith("nv.")) {return "nv";}
228   else if (Array.IndexOf(srlangs, site.Substring(0,3))>=0) {return "sr";}
229   else if (Array.IndexOf(pllangs, site.Substring(0,3))>=0 || Array.IndexOf(pllangs, site.Substring(0,4))>=0) {return "pl";}
230   else if (Array.IndexOf(delangs, site.Substring(0,3))>=0 || Array.IndexOf(delangs, site.Substring(0,4))>=0) {return "de";}
231   else if (Array.IndexOf(rulangs, site.Substring(0,3))>=0 || Array.IndexOf(rulangs, site.Substring(0,4))>=0) {return "ru";}
232   else {return "en";}
233  }
234 }
"https://bxr.wikipedia.org/wiki/Хэрэглэгшэ:TohaomgBot" холбооһоо абагдаһан