Hoppa till innehållet

Wikipedia:Projekt DotNetWikiBot Framework/Innocent iwbot/Tar bort iw-länkar till raderade artiklar

Från Wikipedia
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 DotNetWikiBot;

class MyBot : Bot
{
	public static void Main()
	{
		string[] wikipedior = {"da", "de", "en", "es", "fi", "fr", "it", "nn", "no", "pl"};//list of all wikipedia-projects that I am removing iw-links in 
		foreach(string pedia in wikipedior)
		{
			Site site = new Site("http://"+pedia+".wikipedia.org", "Innocent iwbot", ********);
			string filnamn = "Link_"+pedia+".txt";//File-name for file with collected deleted iw-links
			string var = File.ReadAllText(filnamn);
			foreach(string del in var.Split('|'))
			{
				if(del.Trim().Length > 0)
				{
					string titel = del.Substring(0,del.IndexOf("\r\n")).Trim();//title of page
					string iw = del.Substring(del.IndexOf("[[")+2);
					iw = iw.Substring(0,iw.IndexOf("]]")).Trim();//link to deleted article
					Page p = new Page(site, titel);
					p.Load();
					if(p.text.IndexOf(iw) != -1)
					{
						string orginaltext = p.text;
						string [] alla = p.GetInterWikiLinks();
						p.RemoveInterWikiLinks();
						List<string> iwlänkarna = new List<string> (alla);
						iwlänkarna.Remove(iw);
						if(iwlänkarna.Count != 0)//skip if there no longer exist any iw-links
						{
							p.AddInterWikiLinks(iwlänkarna);
							p.SortInterWikiLinks();
						}
						p.Save(p.text, "Robot removes iw-link to deleted page [[:"+iw+"]]", true);
						Console.WriteLine("Remove: "+iw+" from page: "+pedia+":"+p.title);
						p.Load();
						if(p.text != orginaltext)
						{
							string räknare = File.ReadAllText("Räknare.txt");
							int rednum = int.Parse(räknare) + 1;
							File.WriteAllText("Räknare.txt", rednum.ToString());
							Bot.Wait(30);
						}
					}
				}
			}
			File.WriteAllText(filnamn, "");
		}
	}
}