Wikipedia:Projekt DotNetWikiBot Framework/Innocent bot/Invånarhistorik

using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Xml;
using System.Threading;
using DotNetWikiBot;

class MyBot : Bot
{
	public static void Main()
	{
		string filen = File.ReadAllText("tatorter.txt");
		Site site = new Site("http://sv.wikipedia.org", "Innocent bot", "********");
		PageList pl = new PageList(site);
		pl.FillFromLinksToPage("Mall:Ortsfakta Sverige");
		Console.WriteLine(pl.Count());
		foreach(Page p in pl)
		{
			p.Load();
			string koden = "";
			string namn = p.title;
			foreach(string kod in p.GetTemplateParameter("Ortsfakta Sverige" , "tätortskod"))
			{
				koden = kod;
				//Console.WriteLine(kod);
			}
			foreach(string nom in p.GetTemplateParameter("Ortsfakta Sverige" , "ortnamn"))
			{
				namn = nom;
			}
				
			if(koden != "")
			{
				if(!koden.StartsWith("T"))
					koden = "T"+koden;
				
				
				int position1 = filen.IndexOf(koden);
				//Console.WriteLine(position1);
				
				if(position1 != -1)
				{
					int position2 = filen.IndexOf("\n", position1);
					string nyversion = filen.Substring(position1,position2-position1);
					Console.WriteLine(nyversion);
					int position3 = nyversion.Length;
					string[] antal = {"-","-","-","-","-","-","-","-","-"};
					int i = 0;
					
					for(i=0;i<=8;i++)
					{
						int pos = nyversion.LastIndexOf(",",position3);
						
						if(pos != position3)
							antal[i]=nyversion.Substring(pos+1);
						
						Console.WriteLine(antal[i]);
						nyversion=nyversion.Substring(0,pos);
						position3=pos-1;
					}
					int pos2 = nyversion.LastIndexOf("$");
					int pos3 = nyversion.LastIndexOf("$", pos2-1);
					string fotnot = "";
					if(pos3 != -1)
						fotnot = nyversion.Substring(pos3+1,pos2-pos3-1);

					string referens = "<ref>{{webbref|url=http://www.scb.se/statistik/MI/MI0810/2005A01x/MI0810_2005A01x_SM_MI38SM0703.pdf|titel=Statistiska centralbyrån - Folkmängd i tätorter 1960-2005|hämtdatum=13 december 2010}}</ref>";

					string y1960 = "";
					string y1965 = "";
					string y1970 = "";
					string y1975 = "";
					string y1980 = "";
					string y1990 = "";
					string y1995 = "";
					string y2000 = "";
					string y2005 = "";
					string från = "1960";
					string till = "2005";
					if(antal[8] != "-")
						y1960 = antal[8];
					if(antal[7] != "-")
						y1965 = antal[7];
					if(antal[6] != "-")
						y1970 = antal[6];
					if(antal[5] != "-")
						y1975 = antal[5];
					if(antal[4] != "-")
						y1980 = antal[4];
					if(antal[3] != "-")
						y1990 = antal[3];
					if(antal[2] != "-")
						y1995 = antal[2];
					if(antal[1] != "-")
						y2000 = antal[1];
					if(antal[0] != "-")
						y2005 = antal[0];
					if(antal[0] == "-")
					{
						till = "2000";
						if(antal[1] == "-")
						{
							till = "1995";
							if(antal[2] == "-")
							{
								till = "1990";
								if(antal[3] == "-")
								{
									till = "1980";
									if(antal[4] == "-")
									{
										till = "1975";
										if(antal[5] == "-")
										{
											till = "1970";
											if(antal[6] == "-")
											{
												till = "1965";
												if(antal[7] == "-")
												{
													till = "1960";
												}
											}
										}
									}
								}
							}
						}
					}
					
					if(antal[8] == "-")
					{
						från = "1965";
						if(antal[7] == "-")
						{
							från = "1970";
							if(antal[6] == "-")
							{
								från = "1975";
								if(antal[5] == "-")
								{
									från = "1980";
									if(antal[4] == "-")
									{
										från = "1990";
										if(antal[3] == "-")
										{
											från = "1995";
											if(antal[2] == "-")
											{
												från = "2000";
												if(antal[1] == "-")
												{
													från = "2005";
												}
											}
										}
									}
								}
							}
						}
					}
					//Console.WriteLine(från+"-"+till);
					int maxsif = 0;
					int j = 0;
					for(j=0;j<=8;j++)
					{
						if(antal[j] != "-")
							if(maxsif < int.Parse(antal[j]))
								maxsif = int.Parse(antal[j]);
					}
					if(p.text.IndexOf("{{Befolkningsutveckling") == -1 && p.text.IndexOf("{{befolkningsutveckling") == -1)
					{
						int pos4 = p.text.IndexOf("<references/>");
						if(pos4 == -1)
						{
							int pos5 = p.text.IndexOf("[[Kategori:");
							p.text = p.text.Substring(0,pos5)+"\n== Befolkningsutveckling ==\n{{Befolkningsutveckling\n|från=}}\n\n== Referenser ==\n<references/>\n"+p.text.Substring(pos5);
						}
						else
						{
							int pos5 = p.text.LastIndexOf("\n==", pos4);
							p.text = p.text.Substring(0,pos5)+"\n== Befolkningsutveckling ==\n{{Befolkningsutveckling\n|från=}}\n"+p.text.Substring(pos5);
						}
						Console.WriteLine(p.text);
						string tmpStr = p.text;
						p.Save(p.text, "Förbereder för inlägg av {{[[Mall:Befolkningsutveckling|Befolkningsutveckling]]}}", true);
						p.Load();
						if(tmpStr != p.text)
							Thread.Sleep(7000);
						
					}
					
					if(p.text.IndexOf("{{Befolkningsutveckling") != -1 || p.text.IndexOf("{{befolkningsutveckling") != -1)
					{
						Console.WriteLine("Heja heja!!");
						foreach(string fr in p.GetTemplateParameter("Befolkningsutveckling" , "y1n"))
						{
							från = fr;
						}
						
						p.SetTemplateParameter("Befolkningsutveckling", "namn", namn, true);
						
						p.SetTemplateParameter("Befolkningsutveckling", "från", från, true);
						p.SetTemplateParameter("Befolkningsutveckling", "till", till, true);
						if(fotnot != "")
							p.SetTemplateParameter("Befolkningsutveckling", "fotnot", fotnot, true);
							
						p.SetTemplateParameter("Befolkningsutveckling", "ref", referens, true);
						p.SetTemplateParameter("Befolkningsutveckling", "max", maxsif.ToString(), true);
						if(antal[0] != "-")
							p.SetTemplateParameter("Befolkningsutveckling", "y2005", y2005, true);
						
						if(antal[1] != "-")
							p.SetTemplateParameter("Befolkningsutveckling", "y2000", y2000, true);
							
						if(antal[2] != "-")
							p.SetTemplateParameter("Befolkningsutveckling", "y1995", y1995, true);
						
						if(antal[3] != "-")
							p.SetTemplateParameter("Befolkningsutveckling", "y1990", y1990, true);
							
						if(antal[4] != "-")
							p.SetTemplateParameter("Befolkningsutveckling", "y1980", y1980, true);
							
						if(antal[5] != "-")
							p.SetTemplateParameter("Befolkningsutveckling", "y1975", y1975, true);
							
						if(antal[6] != "-")
							p.SetTemplateParameter("Befolkningsutveckling", "y1970", y1970, true);
						
						if(antal[7] != "-")
							p.SetTemplateParameter("Befolkningsutveckling", "y1965", y1965, true);
						
						if(antal[8] != "-")
							p.SetTemplateParameter("Befolkningsutveckling", "y1960", y1960, true);
						string tmpStr2 = p.text;
						p.Save(p.text, "Fyller i {{[[Mall:Befolkningsutveckling|Befolkningsutveckling]]}}", true);
						Console.WriteLine(p.text);
						p.Load();
						if(tmpStr2 != p.text)
							Thread.Sleep(30000);
					}
				}		
				
			}
		}
		
	}
}