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;
public class TableRow
{
public List<string> cell1;
public string cell2;
public TableRow(List<string> c1, string c2)
{
cell1 = c1;
cell2 = c2;
}
}
class MyBot : Bot
{
public static TableRow Gängavsidor(Site site, string next, string from)
{
List<string> r = new List<string>();
XmlDocument doc = new XmlDocument();
string url = "action=query&list=allpages&aplimit=5000&apfilterredir=nonredirects&format=xml";
if(next != "")
url += "&apcontinue="+HttpUtility.UrlEncode(next);
if(from != "")
url += "&apfrom="+HttpUtility.UrlEncode(from);
string tmpStr = site.PostDataAndGetResultHTM(site.site+"/w/api.php", url);
tmpStr = tmpStr.Replace("allpages apcontinue", "bllpages apcontinue");
File.WriteAllText("hitra.xml", tmpStr);
//Console.WriteLine(tmpStr);
doc.LoadXml(tmpStr);
for(int i = 0 ;i< doc.GetElementsByTagName("p").Count ;i++)
{
string title = doc.GetElementsByTagName("p")[i].Attributes.GetNamedItem("title").Value;
r.Add(title);
}
string nästa = "";
//Console.WriteLine(doc.GetElementsByTagName("bllpages").Count);
if(doc.GetElementsByTagName("bllpages").Count != 0)
nästa = doc.GetElementsByTagName("bllpages")[0].Attributes.GetNamedItem("apcontinue").Value;
TableRow t = new TableRow(r, nästa);
//Console.WriteLine(nästa);
return t;
}
public static List<string> Interwiki(Site site, string title)
{
List<string> r = new List<string>();
XmlDocument doc = new XmlDocument();
string url = "action=wbgetentities&sites=svwiki&titles="+HttpUtility.UrlEncode(title)+"&languages=sv&format=xml";
//string tmpStr = site.PostDataAndGetResultHTM(site.site+"/w/api.php", url);
string tmpStr = site.PostDataAndGetResultHTM(site.site+"/w/api.php", url);
doc.LoadXml(tmpStr);
for(int i = 0 ;i< doc.GetElementsByTagName("sitelink").Count ;i++)
{
string s = doc.GetElementsByTagName("sitelink")[i].Attributes.GetNamedItem("site").Value;
string t = doc.GetElementsByTagName("sitelink")[i].Attributes.GetNamedItem("title").Value;
s = s.Replace("_","-");
string t2 = s.Substring(0,s.Length-4)+":"+t;
//Console.WriteLine(t2);
r.Add(t2);
}
return r;
}
private static string getURLPost(string uri, string parameters)
{
WebRequest webRequest = WebRequest.Create (uri);
byte[] bytes = Encoding.ASCII.GetBytes (parameters);
Stream outputStream = null;
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
try
{
webRequest.ContentLength = bytes.Length;
outputStream = webRequest.GetRequestStream();
outputStream.Write(bytes, 0, bytes.Length);
}
catch (WebException ex)
{
Console.WriteLine(ex.Message + "Error with request!");
}
finally
{
if (outputStream != null)
outputStream.Close();
}
try
{
WebResponse webResponse = webRequest.GetResponse();
if (webResponse == null)
return null;
StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream());
return myStreamReader.ReadToEnd().Trim();
}
catch (WebException ex)
{
Console.WriteLine(ex.Message + "Error with response!");
}
return null;
}
public static void Main()
{
string Password = File.ReadAllText("koden.txt");
Site site = new Site("http://sv.wikipedia.org", "Innocent bot", Password);
Site site2 = new Site("http://wikidata.org", "Innocent bot", Password);
string next = "";
bool hake = true;
string from = "Dubiaranea amoena";
while(hake)
{
List<string> r = new List<string>();
TableRow t = new TableRow(r, "");
t = Gängavsidor(site, next, from);
from = "";
r = t.cell1;
Console.WriteLine(r.Count);
next = t.cell2;
Console.WriteLine(next);
if(next == "")
hake = false;
int h1 = 0;
int h2 = 0;
int h3 = 0;
int h4 = 0;
foreach(string artikel in r)
{
try
{
h1++;
List<string> r2 = new List<string>();
r2 = Interwiki(site2, artikel);
if(r2.Count > 0)
{
Page p = new Page(site, artikel);
p.Load();
string orginal = p.text;
int a = 0;
foreach(string s in r2)
{
string org = p.text;
p.text = p.text.Replace("[["+s+"]]\n", "");
p.text = p.text.Replace("[["+s+"]]", "");
if(p.text != org)
a++;
}
if(orginal != p.text)
{
string txet = "Bot tar bort "+a.ToString()+" gamla iw-länkar som är dubbletter av de som numer finns på [[d:Special:ItemByTitle/svwiki/"+artikel+"|Wikidata]]";
p.Save(p.text, txet, true);
Bot.Wait(1);
h2++;
}
h3 += a;
}
}
catch (WebException)
{
Bot.Wait(120);
site = new Site("http://sv.wikipedia.org", "Innocent bot", Password);
site2 = new Site("http://wikidata.org", "Innocent bot", Password);
}
}
Page pr = new Page(site, "Användare:Innocent bot/IW-Rapport");
pr.Load();
pr.text = "I urval av "+h1.ToString()+" artiklar togs "+h3.ToString()+" iw-länkar bort i "+h2.ToString()+" av dessa artiklar.";
pr.Save(pr.text, "Rapport", false);
}
}
}