Utilizador:TohaomgBot

Origem: Wikilivros, livros abertos por um mundo aberto.
Ir para: navegação, pesquisa

This bot replaces all inclusions of some raster image with its analogue of format SVG, e.g. "some_image.jpg" with "some_image.svg". The pair of images is determined manually. The code of the program can be seen below (I allow anyone to use, share or change it, but not to get any financial profit from it). Bot is written on C# programming language using DotNetWikiBot library.

Code

  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   
 33   beginning:
 34   edits = 0;
 35 
 36 
 37   //USER INPUT
 38   Console.WriteLine("Replace: ");
 39   string old_img = args.Length>=3?args[2]:Console.ReadLine();
 40 	if(args.Length>=3) {Console.WriteLine(old_img);}
 41   if(old_img.StartsWith("id="))
 42   {
 43    Site commons_site = new Site("https://commons.wikimedia.org/", username, password);
 44    Page commons_page = new Page(commons_site, Int64.Parse(old_img.Substring(3)));
 45    commons_page.Load();
 46    old_img = commons_page.title.Substring(5);
 47    Console.WriteLine(old_img);
 48  
 49    mc = Regex.Matches(commons_page.text, @"\|((.)+svg)");
 50    new_img = mc[0].Groups[1].Value;
 51    
 52    new_img = new_img.Replace("File:", "");
 53    if(new_img.Contains("|")) {new_img = new_img.Substring(new_img.IndexOf("|")+1);}
 54    if(new_img.Contains("_")) {new_img = new_img.Replace("_", " ");}
 55  
 56    Console.WriteLine("\n"+"with: \n" + new_img);
 57   }
 58   else
 59   {
 60    Console.WriteLine("\n"+"with: ");
 61    new_img = args.Length>=4?args[3]:Console.ReadLine();
 62    	if(args.Length>=4) {Console.WriteLine(new_img);}
 63    if(new_img==old_img)
 64    {Console.WriteLine("\nNew image can not be the same as the old one!\n"); goto beginning;}
 65   }
 66   string old_img_underlined = old_img.Replace(" ", "_");
 67   Console.WriteLine();
 68  
 69 
 70   //WHAT WIKIS TO PROCESS
 71   string htmlCode;
 72   string img_usage_list = "https://" + "commons.wikimedia.org/w/index.php?title=Special:GlobalUsage&limit=500&target=" + 
 73 	old_img.Replace(" ", "+");
 74   bool goto_next_page = true;
 75   sites_list.Clear();
 76 
 77   while(goto_next_page)
 78   {
 79    try {using (WebClient client = new WebClient()) {htmlCode = client.DownloadString(img_usage_list);}}
 80    catch {Console.WriteLine("No page for this image found!"); goto beginning;}
 81   
 82    mc = Regex.Matches(htmlCode, @"\s([-\w]+\.(\w)+\.org)");
 83    if(mc.Count==0) {Console.WriteLine("No inclusions found!"); goto beginning;}
 84    foreach (Match m in mc)
 85    {
 86 	if(!sites_list.Contains(m.Groups[1].Value) && m.Groups[1].Value!="nl.wikipedia.org" && m.Groups[1].Value!="simple.wikipedia.org")
 87 		{sites_list.Add(m.Groups[1].Value);}
 88    }
 89 
 90    goto_next_page = !htmlCode.Contains("| next 500)");
 91    if(goto_next_page)
 92    {
 93 	mc = Regex.Matches(htmlCode, @"\|\s\<a href" + "=\"(/w/index.php" + @"\?title=Special:GlobalUsage&amp;limit=500&amp;from=" + "[^\"]+)\"");
 94     img_usage_list = "https://" + "commons.wikimedia.org" + mc[0].Groups[1].Value.Replace("&amp;", "&");
 95    }
 96   }
 97 
 98 
 99   //PROCESSING
100   try
101   {
102    Site wiki_site; PageList inclusions; int site_counter = 1, page_counter; string lang;
103    foreach (string st_f in sites_list)
104    {
105     string st = st_f;
106     rejects = 0;
107     Console.WriteLine("\n" + site_counter + ". " + st);
108 
109     try_connect_again:
110     try {wiki_site = new Site("https://" + st, username, password);}
111     catch(System.Net.WebException)
112     {
113      Console.WriteLine("No internet connection or wrong sitename!");
114      Console.WriteLine("Check the connection and type sitename again (type 'skip' to skip):");
115      st = Console.ReadLine();
116      if(st=="skip") {continue;}
117      goto try_connect_again;
118     }
119     catch(DotNetWikiBot.WikiBotException)
120     {Console.WriteLine("Failed to login! This site will be skipped."); continue;}
121 
122     inclusions = new PageList(wiki_site);
123     inclusions.FillFromPagesUsingImage(old_img);
124     Console.WriteLine("\t" + inclusions.Count() + " inclusions found" + "\n");
125     lang = GetLang(st);
126   
127     page_counter = 1;
128     foreach (Page wiki_page in inclusions)
129     {
130      Console.WriteLine("\t" + page_counter + ". " + wiki_page.title);
131 	 
132 	 if(st_f=="www.wikidata.org")
133 	 {
134 	  System.Windows.Forms.Clipboard.SetText(new_img);
135 	  System.Diagnostics.Process.Start("http://" + "www.wikidata.org/wiki/" + wiki_page.title);
136 	  continue;
137 	 }
138 
139      Console.Write("\t");
140      wiki_page.Load();
141      if(wiki_page.Exists() && (wiki_page.text.Contains(old_img) || wiki_page.text.Contains(old_img_underlined)))
142      {
143       wiki_page.text = wiki_page.text.Replace(old_img, new_img);
144       wiki_page.text = wiki_page.text.Replace(old_img_underlined, new_img);
145       Console.Write("\t");
146       try {edits++; wiki_page.Save(summary[lang], true); if(st.StartsWith("pt.")) {Thread.Sleep(10000);}}
147       catch (DotNetWikiBot.WikiBotException)
148       {
149        edits--;
150        Console.WriteLine("\t\t" + "Site did not allow this edit");
151        if(++rejects > 3)
152        {Console.WriteLine("\t" + "To many rejects. This site will be skipped"); break;}
153       }
154 	  catch(System.Net.WebException e)
155 	  {
156 	   Console.WriteLine("\t\t" + "Network error: ");
157 	   Console.WriteLine("\t\t" + e.Message);
158 	   Console.WriteLine("\t\t" + "Waiting one minute");
159 	   Thread.Sleep(60000);
160 	  }
161 	  catch(Exception e) {Console.WriteLine("\t\tUnhandled exception: " + e.GetType()); Console.WriteLine("\t\t" + e.Message);}
162      }
163      else {Console.WriteLine("\t\t" + "Image not present");}
164 
165      page_counter++;
166     }
167     site_counter++;
168    }
169   }
170 
171   catch(Exception e)
172   {
173    Console.WriteLine("Unhandled exception: " + e.GetType());
174    Console.WriteLine(e.Message);
175    Console.ReadKey();
176   }
177 
178   //WRITING TO LOG
179   finally
180   {
181    if(edits>=3)
182    {
183     if(old_img.Length>64) {old_img = old_img.Substring(0,60) + "...";}
184     if(new_img.Length>64) {new_img = new_img.Substring(0,60) + "...";}
185 
186     System.IO.StreamWriter outfile = new System.IO.StreamWriter("log.txt", true);
187     outfile.Write("\r\n" + old_img);
188     int tabs = (old_img.Length)/8;
189     tabs = 8 - tabs;
190     while (tabs!=0) {outfile.Write("\t"); tabs--;}
191 
192     outfile.Write(new_img);
193     tabs = (new_img.Length)/8;
194     tabs = 8 - tabs;
195     while (tabs!=0) {outfile.Write("\t"); tabs--;}
196 
197     outfile.Write(DateTime.Now.ToString("dd.MM.yyyy (HH:mm)"));
198     outfile.Write("\t{0,4}", edits);
199     outfile.Close();
200    }
201    Console.WriteLine("DONE! - " + edits + " edits done\n");
202   }
203   if(args.Length>=3) {return;}
204   goto beginning;
205  }
206 
207  
208  //CHOSING LANGUAGE OF EDIT SUMMARY
209  public static string GetLang(string site)
210  {
211   string[] rulangs = {"ru.", "kk.", "hy.", "uz.", "ky.", "tg.", "crh.", "gag.", "tt.", "av.", 
212                          "kbd.", "ab.", "ba.", "bxr.", "os.", "kv.", "krc.", "mrj.", "lbe.", "lez.", 
213                          "mdf.", "ce.", "mhr.", "koi.", "sah.", "cu.", "tyv.", "udm.", "xal.", "cv.", 
214 			 "myv.", "ady"};
215   string[] delangs = {"de.", "lb.", "li.", "als.", "bar.", "ksh."};
216   string[] pllangs = {"pl.", "szl.", "csb.", "hsb.", "dsb."};
217   string[] srlangs = {"sr.", "sh.", "bs.", "mk."};
218 
219   if (site.StartsWith("uk.") || site.StartsWith("rue.")) {return "uk";}
220   else if (site.StartsWith("be.") || site.StartsWith("be-tarask.")) {return "be";}
221   else if (site.StartsWith("hr.") || site.StartsWith("sl.")) {return "hr";}
222   else if (site.StartsWith("pt.") || site.StartsWith("es.") || site.StartsWith("gl.")) {return "pt";}
223   else if (site.StartsWith("hu.")) {return "hu";}
224   else if (site.StartsWith("ar.")) {return "ar";}
225   else if (site.StartsWith("fa.")) {return "fa";}
226   else if (Array.IndexOf(srlangs, site.Substring(0,3))>=0) {return "sr";}
227   else if (Array.IndexOf(pllangs, site.Substring(0,3))>=0 || Array.IndexOf(pllangs, site.Substring(0,4))>=0) {return "pl";}
228   else if (Array.IndexOf(delangs, site.Substring(0,3))>=0 || Array.IndexOf(delangs, site.Substring(0,4))>=0) {return "de";}
229   else if (Array.IndexOf(rulangs, site.Substring(0,3))>=0 || Array.IndexOf(rulangs, site.Substring(0,4))>=0) {return "ru";}
230   else {return "en";}
231  }
232 }