Hoppa till innehållet

Wikipedia:Projekt DotNetWikiBot Framework/GameOnBot/SaknarLänkarMellanSrcOchWp

Från Wikipedia
// Write your own bot scripts and functions in this file.
// Run "Compile & Run.bat" file - it will compile this file as executable and launch it.
// Compiled XML autodocumentation for DotNetWikiBot namespace is available as "Documentation.chm".

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

class MyBot : Bot
{
	public static void Main()
	{

		CompareInfo Compare = CultureInfo.InvariantCulture.CompareInfo;

		Site site = new Site("http://sv.wikipedia.org", user, password);
		Site site2 = new Site("http://sv.wikisource.org", user, password);
		Page myPage2;
		PageList pl = new PageList(site2);
		
		pl.FillFromCategoryTree("Kategori:Författare");
		string tmpStr = "";
		string tmpStr2 = "";
		string pageTitle = "";
		int start = 0;
		int end   = 0;
		int i     = 0;
		int antal = 0;
		int type  = -1;

		foreach (Page myPage in pl)
		{
			type = -1;
			myPage.Load();
			tmpStr = myPage.text;
			pageTitle = myPage.title.Remove(0, 11);

			start = Compare.IndexOf(tmpStr, "{{wikipedialänk", CompareOptions.IgnoreCase);
			if(start != -1)
				type = 0;

			if(start == -1)
			{
				start = Compare.IndexOf(tmpStr, "{{wikipedia", CompareOptions.IgnoreCase);
				if(start != -1)
					type = 1;
			}
			if(start == -1)
			{
				start = Compare.IndexOf(tmpStr, "[[w:", CompareOptions.IgnoreCase);
				if(start != -1)
					type = 2;
			}

			if(type == -1)
			{
				System.IO.StreamWriter file = new System.IO.StreamWriter("saknarwp.txt", true);
				file.WriteLine(myPage.title);
				file.Close();
			}
			else
			{
				if(type == 2)
				{
					string tmpStr3 = myPage.text;
					string tmpStr4 = "";
					string linkStr = "";
					int start3 = 0;
					int end3 = 0;
					
					start3 = tmpStr3.LastIndexOf("\n", start);
					end3 = tmpStr3.IndexOf("\n", start);
					linkStr = tmpStr3.Substring(start, end3 - start);
					//linkStr contains from "[[w:" to "]]"
					if(linkStr.Contains("|"))
						linkStr = linkStr.Substring(4, linkStr.IndexOf("|") - 4);
					else
						linkStr = linkStr.Substring(4, linkStr.IndexOf("]]") - 4);
					if(start3 >= 0)
						tmpStr4 = tmpStr3.Substring(0, start3);
					else
						tmpStr4 = tmpStr3;
					tmpStr4 += "\r\n{{wikipedialänk";
					if(linkStr != pageTitle)
						tmpStr4 += linkStr;
					tmpStr4 += "}}";
					tmpStr4 += tmpStr3.Substring(end3);
					tmpStr3 = tmpStr4;
					tmpStr = tmpStr3;
					myPage.Save(tmpStr, "Lägger in/ändrar länk till wikipedia", true);
					Thread.Sleep(7000);
					tmpStr = linkStr;
				}
				else
				{
					for(i = start; i < tmpStr.Length; i++)
					{
						if(tmpStr[i] == '{')
							antal++;
						if(tmpStr[i] == '}')
							antal--;
						if(antal == 0 && i != start)
						{
							end = i;
							break;
						}
					}
					tmpStr = tmpStr.Remove(0, start + 2);
					tmpStr = tmpStr.Remove(tmpStr.IndexOf("}"));
					if(type == 0)
						tmpStr = tmpStr.Remove(0, 13);
					else
						tmpStr = tmpStr.Remove(0, 9);
					if(tmpStr.Contains("|"))
						tmpStr = tmpStr.Remove(0, tmpStr.IndexOf("|") + 1);
					
					if(tmpStr == pageTitle)
					{
						//Change the ws page, remove |pagetitle in template call?
					}

					if(tmpStr.Length == 0)
					{
						tmpStr = pageTitle;
					}
				}

					
				//load page on wp

				type = -1;
				myPage2 = new Page(site, tmpStr);

				myPage2.Load();
				tmpStr2 = myPage2.text;

				start = Compare.IndexOf(tmpStr2, "{{wikisource", CompareOptions.IgnoreCase);
				if(start != -1)
					type = 0;
				if(type == -1)
				{
					Console.WriteLine("saknarws:" + tmpStr);

					tmpStr2 = tmpStr2.Replace("== Externa länkar ==", "== Externa länkar ==\r\n* {{wikisource författare}}");
					tmpStr2 = tmpStr2.Replace("==Externa länkar==", "== Externa länkar ==\r\n* {{wikisource författare}}");				
					start = Compare.IndexOf(tmpStr2, "== Externa länkar ==", CompareOptions.IgnoreCase);				
					if(start == -1)
					{
						System.IO.StreamWriter file = new System.IO.StreamWriter("saknarws.txt", true);
						file.WriteLine(myPage.title);
						file.Close();
					}
					else
							myPage2.Save(tmpStr2, "Lägger in länk till wikisource", true);
					}
					Thread.Sleep(7000);
				}
			}	
		}	
	}
}