User:KingAragorn Bot/Scripts: Difference between revisions
From Tolkien Gateway
KingAragorn (talk | contribs) |
KingAragorn (talk | contribs) |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
==Common scripts== | |||
= | ===Changing categories=== | ||
== | |||
<pre> | <pre> | ||
using System; | using System; | ||
Line 7: | Line 6: | ||
class MyBot : Bot | 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); | |||
} | |||
} | } | ||
</pre> | </pre> | ||
==Re-linking== | ===Re-linking=== | ||
<pre> | <pre> | ||
using System; | using System; | ||
Line 68: | Line 27: | ||
class MyBot : Bot | 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); | |||
} | |||
} | } | ||
</pre> | </pre> | ||
To avoid certain namespaces, add the following before p.Load(): | |||
<pre> | <pre> | ||
p.RemoveNamespaces(new int[] {1,3}); | |||
</pre> | |||
{ | |||
To include specific namespaces only, add the following before p.Load(): | |||
<pre> | |||
p.FilterNamespaces(new int[] {1,3}); | |||
</pre> | </pre> | ||
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== | ||
===Change image categories and automatically insert proper sort key=== | |||
== | |||
<pre> | <pre> | ||
using System; | using System; | ||
using System.IO; | |||
using System.Text; | |||
using System.Text.RegularExpressions; | |||
using System.Collections; | |||
using System.Xml; | |||
using DotNetWikiBot; | using DotNetWikiBot; | ||
using System.Threading; | |||
using System.Net; | |||
using System.Web; | |||
class MyBot : Bot | class MyBot : Bot | ||
{ | { | ||
public static void Main() | public static void Main() | ||
{ | { | ||
Site site = new Site("http://tolkiengateway.net", "KingAragorn Bot", "PASSWORD"); | 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); | |||
} | |||
} | } | ||
</pre> | </pre> | ||
== | ===Mass re-linking=== | ||
<pre> | <pre> | ||
using System; | using System; | ||
Line 142: | Line 120: | ||
class MyBot : Bot | class MyBot : Bot | ||
{ | { | ||
public static void Main() | public static void Main() | ||
{ | { | ||
Site site = new Site("http:// | 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); | |||
} | } | ||
} | |||
} | |||
} | } | ||
</pre> | </pre> | ||
__NOTOC__ |
Revision as of 19:17, 16 June 2012
Common scripts
Changing categories
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
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
Change image categories and automatically insert proper sort key
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
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); } } } }