User:KingAragorn Bot/Scripts: Difference between revisions

From Tolkien Gateway
 
(3 intermediate revisions by one other user not shown)
Line 32: Line 32:


PageList p = new PageList(site);
PageList p = new PageList(site);
  p.FillFromLinksToPage("Melkor");
  p.FillFromLinksToPage("Erebor");
p.Load();
p.Load();
foreach (Page i in p)
foreach (Page i in p) {
i.text = i.text.Replace("[[Melkor]]", "[[Morgoth|Melkor]]");
i.text = i.text.Replace("[[Erebor]]", "[[Lonely Mountain|Erebor]]");
}
 
foreach (Page j in p) {
j.text = j.text.Replace("[[Erebor|Lonely Mountain]]", "[[Lonely Mountain]]");
}
 
p.SaveSmoothly(1, "Bot message: re-linked.", true);
p.SaveSmoothly(1, "Bot message: re-linked.", true);
}  
}  
Line 90: Line 96:
PageList p = new PageList(site);
PageList p = new PageList(site);
p.FillAllFromCategory("Images from games");
p.FillSubsFromCategoryTree("Images");
p.FilterNamespaces(new int[] {14});
p.FilterNamespaces(new int[] {14});
foreach(Page myPage in p) {
foreach(Page myPage in p) {
String pagename = myPage.title;
String pagename1 = myPage.title;
String pagename2 =  pagename1.Replace("Category:", "");
String pagename3 =  pagename2.Replace("Maps of ", "");
myPage.Load();
myPage.Load();
myPage.text = "{{DEFAULTSORT:" + pagename + "}}";
myPage.RemoveTemplate("DEFAULTSORT");
myPage.AddTemplate("{{DEFAULTSORT:" + pagename3 + "}}");
myPage.text =  myPage.text.Replace("Category:", "");
myPage.text =  myPage.text.Replace("Images of ", "");
myPage.text =  myPage.text.Replace("Images from ", "");
myPage.AddToCategory("Images from games");
myPage.Save(myPage.text);
Wait(1);
}
}
p.SaveSmoothly(1, "Bot comment: fixed DEFAULTSORT.", true);
}  
}  


Line 144: Line 145:
}
}


</pre>
===Get list of article names and save to file===
<pre>
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Xml;
using DotNetWikiBot;
class MyBot : Bot
{
public static void Main()
{
Site site = new Site("http://tolkiengateway.net", "KingAragorn Bot", "********");
site.fetchRate = 5000;
PageList p = new PageList(site);
p.FillFromAllPages("The Tolkien Calendar 1993", 0 , false, 5000);
p.SaveTitlesToFile("tg.txt");
}
}
</pre>
</pre>
__NOTOC__
__NOTOC__

Latest revision as of 17:35, 24 October 2017

Common scripts[edit | edit source]

Changing categories[edit | edit source]

using System;
using DotNetWikiBot;
class MyBot : Bot 
{ 
	public static void Main() 
	{
		Site site = new Site("http://tolkiengateway.net", "KingAragorn Bot", "PASSWORD"); 

		PageList p = new PageList(site);
		p.FillAllFromCategory("Vinyar Tengwar issues");
		p.Load();
		p.RemoveFromCategory("Vinyar Tengwar issues");
		p.AddToCategory("Vinyar Tengwar");
		p.SaveSmoothly(1, "Bot comment: changed category.", true);
} 

}

Re-linking[edit | edit source]

using System;
using DotNetWikiBot;
class MyBot : Bot 
{ 
	public static void Main() 
	{ 
		Site site = new Site("http://tolkiengateway.net", "KingAragorn Bot", "PASSWORD"); 

		PageList p = new PageList(site);
 		p.FillFromLinksToPage("Erebor");
		p.Load();
		foreach (Page i in p) {
			i.text = i.text.Replace("[[Erebor]]", "[[Lonely Mountain|Erebor]]");
		}

		foreach (Page j in p) {
			j.text = j.text.Replace("[[Erebor|Lonely Mountain]]", "[[Lonely Mountain]]");
		}

		p.SaveSmoothly(1, "Bot message: re-linked.", true);
	} 

}

To avoid certain namespaces, add the following before p.Load():

		p.RemoveNamespaces(new int[] {1,3});

To include specific namespaces only, add the following before p.Load():

		p.FilterNamespaces(new int[] {1,3});

List of built-in namespaces and their corresponding integers:

  • 0 - Main
  • 1 - Talk
  • 2 - User
  • 3 - User talk
  • 4 - Tolkien Gateway
  • 5 - Tolkien Gateway talk
  • 6 - File
  • 7 - File talk
  • 8 - MediaWiki
  • 9 - MediaWiki talk
  • 10 - Template
  • 11 - Template talk
  • 12 - Help
  • 13 - Help talk
  • 14 - Category
  • 15 - Category talk

Irregular scripts[edit | edit source]

Change image categories and automatically insert proper sort key[edit | edit source]

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.Net;
using System.Web;

class MyBot : Bot 
{ 
	public static void Main() 
	{
		Site site = new Site("http://tolkiengateway.net", "KingAragorn Bot", "PASSWORD"); 
		
		PageList p = new PageList(site);
		p.FillSubsFromCategoryTree("Images");
		p.FilterNamespaces(new int[] {14});
		
		foreach(Page myPage in p) {
			String pagename1 = myPage.title;
			String pagename2 =  pagename1.Replace("Category:", "");
			String pagename3 =  pagename2.Replace("Maps of ", "");
			myPage.Load();
			myPage.RemoveTemplate("DEFAULTSORT");
			myPage.AddTemplate("{{DEFAULTSORT:" + pagename3 + "}}");			
			
		}
		p.SaveSmoothly(1, "Bot comment: fixed DEFAULTSORT.", true);
} 

}

Mass re-linking[edit | edit source]

using System;
using DotNetWikiBot;
class MyBot : Bot 
{ 
        public static void Main() 
        { 
                Site site = new Site("http://tolkiengateway.net", "KingAragorn Bot", "PASSWORD"); 

                PageList b = new PageList(site);
                b.FillFromFile("PageList.txt");
                foreach (Page c in b) {
                PageList i = new PageList(site);
                i.FillFromLinksToPage(c.title);
            i.LoadEx3();    
                foreach (Page p in i) {

                      p.text = p.text.Replace("[[Rankin/Bass' The Hobbit]]", "[[The Hobbit (1977 film)]]");
                      p.text = p.text.Replace("Rankin/Bass' The Hobbit|", "The Hobbit (1977 film)|");
                      p.text = p.text.Replace("[[Ralph Bakshi's The Lord of the Rings]]", "[[The Lord of the Rings (1978 film)]]");
                      p.text = p.text.Replace("Ralph Bakshi's The Lord of the Rings|", "The Lord of the Rings (1978 film)|");
                      p.text = p.text.Replace("[[Rankin/Bass' The Return of the King]]", "[[The Return of the King (1980 film)]]");
                      p.text = p.text.Replace("Rankin/Bass' The Return of the King|", "The Return of the King (1980 film)|");
                p.Save("Bot comment: fixing links", true);
}
}
        } 

}

Get list of article names and save to file[edit | edit source]

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

class MyBot : Bot
{
	public static void Main()
	{
		Site site = new Site("http://tolkiengateway.net", "KingAragorn Bot", "********"); 
		
		site.fetchRate = 5000;
		
		PageList p = new PageList(site);
		
		p.FillFromAllPages("The Tolkien Calendar 1993", 0 , false, 5000);
		
		p.SaveTitlesToFile("tg.txt");
	}
}