משתמש:Lavallen/First code

מתוך ויקיפדיה, האנציקלופדיה החופשית
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 System.Net;
using System.Web;
using DotNetWikiBot;


class MyBot : Bot
{
	
	public static void Main()
	{
		Site site = new Site("http://he.wikipedia.org", "Username", "password");//log in to hewiki
		Site site2 = new Site("link to your local wiki", "Username", "password");//log in to your local wiki
		Page myPage = new Page(site, "User:Innocent bot/Cities");
		myPage.Load();
		string alla = myPage.text;
		string[] städer;
		städer = alla.Trim().Replace("\n", "#").Split('#');
		Console.WriteLine(städer.Length);
		Page myPage2 = new Page(site, "User:Innocent bot/States");
		myPage2.Load();
		//On svwp, a template with multi-country-purpose is used. Here you have to change depending on the template-infrastructure on hewiki
		string mall = "{{Infobox city\n"+
						"|name            = <name>\n"+
						"|other_name      = \n"+
						"|category        = <category>\n"+
						"|motto           = \n"+
						"|pict            = \n"+
						"|pict_text       = \n"+
						"|flag            = \n"+
						"|insignia        = \n"+
						"|land            = USA\n"+
						"|state           = <state>\n"+
						"|county          = <county>\n"+
						"|mayor           = \n"+
						"|population      = <population>\n"+
						"|population_note = <population_note>\n"+
						"|population_year = <population_year>\n"+
						"|founded_year    = \n"+
						"|hight           = \n"+
						"|area            = <area>\n"+
						"|area_note       = <area_note>\n"+
						"|area_year       = <area_year>\n"+
						"|area_land       = <area_land>\n"+
						"|area_water      = <area_water>\n"+
						"|lat_g = <lat_g>|lat_m = <lat_m>|lat_s = <lat_s>|lat_NS = <lat_NS>\n"+//On svwp, the coordinates can be installed in the infobox, instead of adding them to 
						"|long_g = <long_g>|long_m = <long_m>|long_s = <long_s>|long_EW = <long_EW>\n"+//a Coord-template.
						"|url             = \n"+
						"|footnote        = \n"+
						"|map             = \n"+//These maps are made by the help of the Geobox-template on svwp. I do not know if you
						"|map_text        = \n"+//have something similair on he-wiki, 
						"|positionmap     = \n"+
						"|map  2          = \n"+
						"|map2_text       = \n"+
						"|timezone        = <timezone>\n"+
						"|UTC diff        = <UTC diff>\n"+
						"|timezone_DST    = <timezone_DST>\n"+
						"|DST diff        = <DST diff>\n"+
						"|FIPS code       = <fips>\n"+
						"}}\n\n\n== References ==\n<references/>";
		for(int i = 1; i<städer.Length-2; i++)
		{
			string a = städer[i];
			string fips = a.Substring(18,7);
			a = a.Substring(32);
			a = a.Replace("</option>", "");
			string name = a.Substring(0,a.IndexOf("(")).Trim();
			Console.WriteLine(fips);
			Console.WriteLine(name);
			string category = a.Substring(a.IndexOf("(")+1);
			category = category.Substring(0,category.IndexOf(")"));
			//category is city/town/CDP/village. I write this in English in the template, and let the template "translate" it
			//to swedish. It is a little hard in swedish, since we do not have separate words for a 'town', a 'city' and a larger 'village'
			string state = myPage2.GetTemplateParameter("Nummer", fips.Substring(0,2))[0];//Get the name of state "nn" from "User:Innocent bot/States"
			Console.WriteLine(name+" "+category+" "+state);
			if(state == "Alabama")//This is not essential, but since I have made one or two states at the time, I have used it,
			{
				Page myPage3 = new Page(site2, "Project:Project USA/Cities/"+name+", "+state);//You have to use "site2" instead of "site" here.
				myPage3.Load();
				if(!myPage3.Exists())//This line check if the page already have been made, to not do redo something
				{
					myPage3.text = mall;
					myPage3.text = myPage3.text.Replace("<state>", "[["+state+"]]");
					myPage3.text = myPage3.text.Replace("<name>", name);
					myPage3.text = myPage3.text.Replace("<category>", category);
					myPage3.text = myPage3.text.Replace("<fips>", fips);
					Console.WriteLine(myPage3.text);
					
					myPage3.Save(myPage3.text, "Set up a projectpage for "+name+", "+delstat, false);
					Bot.Wait(10);//Wait 10 seconds between edits...
				}
			}
			
		}

		
	}
}