Jump to content
Wikimedia Meta-Wiki

User:TohaomgBot

From Meta, a Wikimedia project coordination wiki
This user account is a bot  operated by Tohaomg (talk). It is not a sock puppet, but rather an automated or semi-automated account for making repetitive edits that would be extremely tedious to do manually.
Administrators: if this bot is malfunctioning or causing harm, please block it.

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

[edit ]
usingSystem;
usingSystem.Net;
usingSystem.Threading;
usingSystem.Collections.Generic;
usingSystem.Text.RegularExpressions;
usingDotNetWikiBot;

classImageReplacer:Bot
{
[STAThread]
publicstaticvoidMain(string[]args)
{
stringusername=args[0],password=args[1];
stringnew_img;
List<string>sites_list=newList<string>();
Dictionary<string,string>summary=newDictionary<string,string>();
intedits,rejects;
MatchCollectionmc;

summary.Add("en","BOT: Replaced raster image with an image of format SVG.");
summary.Add("de","BOT: Ersetzt das Rasterbild mit einem Bild des Formats SVG.");
summary.Add("pt","BOT: Substituição de imagem raster por formato SVG.");
summary.Add("uk","БОТ: Замінено растрове зображення на зображення в форматі SVG.");
summary.Add("ru","БОТ: Заменено растровое изображение на изображение в формате SVG.");
summary.Add("be","БОТ: Заменены растравы малюнак на малюнак у фармаце SVG.");
summary.Add("pl","BOT: Zamień grafikę rastrową na obraz w formacie SVG.");
summary.Add("sr","BOT: Zamenjena rasterska slika sa slikom u formatu SVG.");
summary.Add("hr","BOT: Zamijenjena rasterska slika slikom u formatu SVG.");
summary.Add("hu","BOT: Kép(ek) cseréje SVG formátumú változatra.");
summary.Add("ar","BOT: استبدال صورة نقطية بصورة متجهية متغيرة (SVG).");
summary.Add("fa","BOT: جایگزینی پرونده با نسخهٔ برداری SVG");
summary.Add("nv","BÉÉSH HINÁANII: Eʼelyaaígíí łahgo áyiilaa; SVG choolʼį́.");

beginning:
edits=0;


//USER INPUT
Console.WriteLine("Replace: ");
stringold_img=args.Length>=3?args[2]:Console.ReadLine();
if(args.Length>=3){Console.WriteLine(old_img);}
if(old_img.StartsWith("id="))
{
Sitecommons_site=newSite("https://commons.wikimedia.org/",username,password);
Pagecommons_page=newPage(commons_site,Int64.Parse(old_img.Substring(3)));
commons_page.Load();
old_img=commons_page.title.Substring(5);
Console.WriteLine(old_img);

mc=Regex.Matches(commons_page.text,@"\|((.)+svg)");
new_img=mc[0].Groups[1].Value;

new_img=new_img.Replace("File:","");
if(new_img.Contains("|")){new_img=new_img.Substring(new_img.IndexOf("|")+1);}
if(new_img.Contains("_")){new_img=new_img.Replace("_"," ");}

Console.WriteLine("\n"+"with: \n"+new_img);
}
else
{
Console.WriteLine("\n"+"with: ");
new_img=args.Length>=4?args[3]:Console.ReadLine();
if(args.Length>=4){Console.WriteLine(new_img);}
if(new_img==old_img)
{Console.WriteLine("\nNew image can not be the same as the old one!\n");gotobeginning;}
}
stringold_img_underlined=old_img.Replace(" ","_");
Console.WriteLine();


//WHAT WIKIS TO PROCESS
stringhtmlCode;
stringimg_usage_list="https://"+"commons.wikimedia.org/w/index.php?title=Special:GlobalUsage&limit=500&target="+
old_img.Replace(" ","+");
boolgoto_next_page=true;
sites_list.Clear();

while(goto_next_page)
{
try{using(WebClientclient=newWebClient()){htmlCode=client.DownloadString(img_usage_list);}}
catch{Console.WriteLine("No page for this image found!");gotobeginning;}

mc=Regex.Matches(htmlCode,@"\s([-\w]+\.(\w)+\.org)");
if(mc.Count==0){Console.WriteLine("No inclusions found!");gotobeginning;}
foreach(Matchminmc)
{
if(!sites_list.Contains(m.Groups[1].Value)&&m.Groups[1].Value!="nl.wikipedia.org"&&m.Groups[1].Value!="simple.wikipedia.org")
{sites_list.Add(m.Groups[1].Value);}
}

goto_next_page=!htmlCode.Contains("| next 500)");
if(goto_next_page)
{
mc=Regex.Matches(htmlCode,@"\|\s\<a href"+"=\"(/w/index.php"+@"\?title=Special:GlobalUsage&amp;limit=500&amp;from="+"[^\"]+)\"");
img_usage_list="https://"+"commons.wikimedia.org"+mc[0].Groups[1].Value.Replace("&amp;","&");
}
}


//PROCESSING
try
{
Sitewiki_site;PageListinclusions;intsite_counter=1,page_counter;stringlang;
foreach(stringst_finsites_list)
{
stringst=st_f;
rejects=0;
Console.WriteLine("\n"+site_counter+". "+st);

try_connect_again:
try{wiki_site=newSite("https://"+st,username,password);}
catch(System.Net.WebException)
{
Console.WriteLine("No internet connection or wrong sitename!");
Console.WriteLine("Check the connection and type sitename again (type 'skip' to skip):");
st=Console.ReadLine();
if(st=="skip"){continue;}
gototry_connect_again;
}
catch(DotNetWikiBot.WikiBotException)
{Console.WriteLine("Failed to login! This site will be skipped.");continue;}

inclusions=newPageList(wiki_site);
inclusions.FillFromPagesUsingImage(old_img);
Console.WriteLine("\t"+inclusions.Count()+" inclusions found"+"\n");
lang=GetLang(st);

page_counter=1;
foreach(Pagewiki_pageininclusions)
{
Console.WriteLine("\t"+page_counter+". "+wiki_page.title);

if(st_f=="www.wikidata.org")
{
System.Windows.Forms.Clipboard.SetText(new_img);
System.Diagnostics.Process.Start("http://"+"www.wikidata.org/wiki/"+wiki_page.title);
continue;
}

Console.Write("\t");
wiki_page.Load();
if(wiki_page.Exists()&&(wiki_page.text.Contains(old_img)||wiki_page.text.Contains(old_img_underlined)))
{
wiki_page.text=wiki_page.text.Replace(old_img,new_img);
wiki_page.text=wiki_page.text.Replace(old_img_underlined,new_img);
Console.Write("\t");
try{edits++;wiki_page.Save(summary[lang],true);if(st.StartsWith("pt.")){Thread.Sleep(10000);}}
catch(DotNetWikiBot.WikiBotException)
{
edits--;
Console.WriteLine("\t\t"+"Site did not allow this edit");
if(++rejects>3)
{Console.WriteLine("\t"+"To many rejects. This site will be skipped");break;}
}
catch(System.Net.WebExceptione)
{
Console.WriteLine("\t\t"+"Network error: ");
Console.WriteLine("\t\t"+e.Message);
Console.WriteLine("\t\t"+"Waiting one minute");
Thread.Sleep(60000);
}
catch(Exceptione){Console.WriteLine("\t\tUnhandled exception: "+e.GetType());Console.WriteLine("\t\t"+e.Message);}
}
else{Console.WriteLine("\t\t"+"Image not present");}

page_counter++;
}
site_counter++;
}
}

catch(Exceptione)
{
Console.WriteLine("Unhandled exception: "+e.GetType());
Console.WriteLine(e.Message);
Console.ReadKey();
}

//WRITING TO LOG
finally
{
if(edits>=3)
{
if(old_img.Length>64){old_img=old_img.Substring(0,60)+"...";}
if(new_img.Length>64){new_img=new_img.Substring(0,60)+"...";}

System.IO.StreamWriteroutfile=newSystem.IO.StreamWriter("log.txt",true);
outfile.Write("\r\n"+old_img);
inttabs=(old_img.Length)/8;
tabs=8-tabs;
while(tabs!=0){outfile.Write("\t");tabs--;}

outfile.Write(new_img);
tabs=(new_img.Length)/8;
tabs=8-tabs;
while(tabs!=0){outfile.Write("\t");tabs--;}

outfile.Write(DateTime.Now.ToString("dd.MM.yyyy (HH:mm)"));
outfile.Write("\t{0,4}",edits);
outfile.Close();
}
Console.WriteLine("DONE! - "+edits+" edits done\n");
}
if(args.Length>=3){return;}
gotobeginning;
}


//CHOSING LANGUAGE OF EDIT SUMMARY
publicstaticstringGetLang(stringsite)
{
string[]rulangs={"ru.","kk.","hy.","uz.","ky.","tg.","crh.","gag.","tt.","av.",
"kbd.","ab.","ba.","bxr.","os.","kv.","krc.","mrj.","lbe.","lez.",
"mdf.","ce.","mhr.","koi.","sah.","cu.","tyv.","udm.","xal.","cv.",
"myv.","ady"};
string[]delangs={"de.","lb.","li.","als.","bar.","ksh."};
string[]pllangs={"pl.","szl.","csb.","hsb.","dsb."};
string[]srlangs={"sr.","sh.","bs.","mk."};

if(site.StartsWith("uk.")||site.StartsWith("rue.")){return"uk";}
elseif(site.StartsWith("be.")||site.StartsWith("be-tarask.")){return"be";}
elseif(site.StartsWith("hr.")||site.StartsWith("sl.")){return"hr";}
elseif(site.StartsWith("pt.")||site.StartsWith("es.")||site.StartsWith("gl.")){return"pt";}
elseif(site.StartsWith("hu.")){return"hu";}
elseif(site.StartsWith("ar.")){return"ar";}
elseif(site.StartsWith("fa.")){return"fa";}
elseif(site.StartsWith("nv.")){return"nv";}
elseif(Array.IndexOf(srlangs,site.Substring(0,3))>=0){return"sr";}
elseif(Array.IndexOf(pllangs,site.Substring(0,3))>=0||Array.IndexOf(pllangs,site.Substring(0,4))>=0){return"pl";}
elseif(Array.IndexOf(delangs,site.Substring(0,3))>=0||Array.IndexOf(delangs,site.Substring(0,4))>=0){return"de";}
elseif(Array.IndexOf(rulangs,site.Substring(0,3))>=0||Array.IndexOf(rulangs,site.Substring(0,4))>=0){return"ru";}
else{return"en";}
}
}

AltStyle によって変換されたページ (->オリジナル) /