Wikipedia:Projekt DotNetWikiBot Framework/Innocent bot/Projekt Internet i Sverige

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.Collections.Generic;

public class TableRow
{
        public String cell1;
        public int cell2;
		public int cell3;
		public int cell4;
		public int cell5;
		public int cell6;
		public int cell7;
		public int cell8;
		public int cell9;
		public int cell10;
		public int cell11;
		public int cell12;
		public int cell13;
		public int cell14;
		public int cell15;
        
        public TableRow(String c1, int c2, int c3, int c4, int c5, int c6, int c7, int c8, int c9, int c10, int c11, int c12, int c13, int c14, int c15)
        {
                cell1 = c1;
                cell2 = c2;
				cell3 = c3;
				cell4 = c4;
				cell5 = c5;
				cell6 = c6;
				cell7 = c7;
				cell8 = c8;
				cell9 = c9;
				cell10 = c10;
				cell11 = c11;
				cell12 = c12;
				cell13 = c13;
				cell14 = c14;
				cell15 = c15;
        }
}

class MyBot : Bot
{
        static StringBuilder sb  = new StringBuilder();
        static byte[]        buf = new byte[8192];
        static int count;
        static string tempString;
        
        private static string getNumberOfVisits(string inStr)
        {
                int pos  = inStr.IndexOf(" has been viewed ");
                int pos2 = inStr.Substring(pos + 17).IndexOf(" times in ");
                if(pos > 0 && pos2 > 0)
                        return inStr.Substring(pos + 17, pos2);
                return "";
        }
        
        private static string getPage(string url)
        {
                HttpWebRequest  request  = (HttpWebRequest)WebRequest.Create(url);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream resStream = response.GetResponseStream();
                sb.Length = 0;
                tempString = null;
                count      = 0;
                do
                {
                        count = resStream.Read(buf, 0, buf.Length);
                        if (count != 0)
                        {
                                tempString = Encoding.UTF8.GetString(buf, 0, count);
                                sb.Append(tempString);
                        }
                }
                while (count > 0);
                return sb.ToString();
        }
        
        public static void Main()
        {
				
                Site site   = new Site("http://sv.wikipedia.org", "Innocent bot", "********");
                PageList pl = new PageList(site);
                //date to check against (normally last month), remember to edit the text in the header as well!
                String date = "201109";
                //Header for the page
                String[] headersStr  = new String[1];
                String[] footersStr  = new String[1];
                String[] catscanStr  = new String[1];
                String[] articlesStr = new String[1];
				//String[] datenr = new String[12];
				
				String[] datenr = {"201110", "201111", "201112", "201201", "201202", "201203", "201204", "201205", "201206", "201207", "201208", "201209"};
                
                headersStr[0] = "Denna sida innehåller statistik över antalet visningar av respektive artikel i [[:Kategori:Internet i Sverige]] under september 2012.";
                
                footersStr[0] = "[[Kategori:Wikipedia:Projekt Internet i Sverige]]";
                
                catscanStr[0] = "http://toolserver.org/~daniel/WikiSense/CategoryIntersect.php?wikilang=sv&wikifam=.wikipedia.org&basecat=" + "Internet_i_Sverige" + "&basedeep=20&templates=&mode=al&go=Sök&format=wiki&userlang=sv";
                
                articlesStr[0] = "Wikipedia:Projekt Internet i Sverige/Mest lästa artiklar/Senaste året";
                
                String tmpStr;
				
                for(int stat = 0; stat < 1; stat++)
                {
						
                        Page myPage2 = new Page(site);
                        myPage2.title = articlesStr[stat];
                        //Add header
                        myPage2.text += headersStr[stat] + "\n";
                        myPage2.text += "{| class=\"sortable wikitable\"\n! Pos !! Artikel !! "+datenr[0]+" !! "+datenr[1]+" !! "+datenr[2]+" !! "+datenr[3]+" !! "+datenr[4]+" !! "+datenr[5]+" !! "+datenr[6]+" !! "+datenr[7]+" !! "+datenr[8]+" !! "+datenr[9]+" !! "+datenr[10]+" !! "+datenr[11]+" !! Pos\n|-\n";
                        //Catscan search
                        tmpStr = getPage(catscanStr[stat]);
                        string[] lines = Regex.Split(tmpStr, "\n");
                        string title;
                        int pos;
                        int pos2;
                        List<TableRow> tableRow = new List<TableRow>();
						
                        foreach (string line in lines)
                        {
								
                                pos  = line.IndexOf("[[");
                                pos2 = line.IndexOf("]]");
                                if(pos > 0 && pos2 > 0)
                                {
                                        title = line.Substring(pos + 2, pos2 - 4);
                                        if(!title.StartsWith(":Kategori:"))
                                        {
												Console.WriteLine(title);
												string[] temp = new String[12];
												for(int h = 0; h < 12; h++)
												{
													tmpStr = getPage("http://stats.grok.se/sv/" + datenr[h] + "/" + title);
													temp[h] = getNumberOfVisits(tmpStr);
												}
	                                            if(temp[11].Length > 0)
                                                        tableRow.Add(new TableRow(title.Replace("_", " "), int.Parse(temp[0]), int.Parse(temp[1]), int.Parse(temp[2]), int.Parse(temp[3]), int.Parse(temp[4]), int.Parse(temp[5]), int.Parse(temp[6]), int.Parse(temp[7]), int.Parse(temp[8]), int.Parse(temp[9]), int.Parse(temp[10]), int.Parse(temp[11]), 0, 0));
                                        }
                                }
                        }
                        tableRow.Sort(delegate(TableRow r1, TableRow r2)
                                {
                                        return r1.cell12.CompareTo(r2.cell12);
                                }
                        );
						tableRow.Reverse();
						tableRow[0].cell14 = 1;
						Console.WriteLine("Sort1");
						for(int i = 1; i<tableRow.Count; i++)
						{
								if(tableRow[i].cell13 == tableRow[i-1].cell13)
										tableRow[i].cell14 = tableRow[i-1].cell14;
								else
										tableRow[i].cell14 = i + 1;
						}
						Console.WriteLine("Sort2");
                        tableRow.Sort(delegate(TableRow r1, TableRow r2)
                                {
                                        return r1.cell13.CompareTo(r2.cell13);
                                }
                        );
						Console.WriteLine("Sort3");
                        //descending order
                        tableRow.Reverse();
						tableRow[0].cell15= 1;
						for(int i = 1; i<tableRow.Count; i++)
						{
								if(tableRow[i].cell13 == tableRow[i-1].cell13)
										tableRow[i].cell15 = tableRow[i-1].cell15;
								else
										tableRow[i].cell15 = i + 1;
						}
						Console.WriteLine("Sort4");
                        tableRow.ForEach(delegate(TableRow r)
                                {
                                        myPage2.text += "|-\n| "+r.cell15.ToString();
										if(r.cell15 < r.cell14)
											if(r.cell12 == 0)
												myPage2.text += " [[Fil:Plus blue.svg|10px]]";
											else
												myPage2.text += " [[Fil:Fairytale up.png|10px]]";
										if(r.cell15 > r.cell14)
											myPage2.text += " [[Fil:RedDownArrow.svg|10px]]";
										if(r.cell15 == r.cell14)
											if(r.cell12 == 0)
												myPage2.text += " [[Fil:Plus blue.svg|10px]]";
											else
												myPage2.text += " [[Fil:Go-next-grey.svg|10px]]";
										myPage2.text += "\n| [[" + r.cell1 + "]]\n| " + r.cell2.ToString() +"\n| "+ r.cell3.ToString() +"\n| "+ r.cell4.ToString() +"\n| "+ r.cell5.ToString() +"\n| "+ r.cell6.ToString() +"\n| "+ r.cell7.ToString() +"\n| "+ r.cell8.ToString() +"\n| "+ r.cell9.ToString() +"\n| "+ r.cell10.ToString() +"\n| "+ r.cell11.ToString() +"\n| "+ r.cell12.ToString();
										if(r.cell12 != 0)
										{
											myPage2.text += " <small>("+r.cell14.ToString()+")</small>";
										}
										myPage2.text += "\n| "+ r.cell13.ToString();
										if(r.cell13 > r.cell12)
											if(r.cell12 != 0)
												myPage2.text += " [[Fil:Fairytale up.png|10px]]";
											else
												myPage2.text += " [[Fil:Plus blue.svg|10px]]";
										if(r.cell13 < r.cell12)
											myPage2.text += " [[Fil:RedDownArrow.svg|10px]]";
										if(r.cell13 == r.cell12)
											myPage2.text += " [[Fil:Fairytale right.png|10px]]";
										myPage2.text += "\n| "+r.cell15.ToString();
										if(r.cell15 < r.cell14)
											if(r.cell12 == 0)
												myPage2.text += " [[Fil:Plus blue.svg|10px]]";
											else
												myPage2.text += " [[Fil:Fairytale up.png|10px]]";
										if(r.cell15 > r.cell14)
											myPage2.text += " [[Fil:RedDownArrow.svg|10px]]";
										if(r.cell15 == r.cell14)
											if(r.cell12 == 0)
												myPage2.text += " [[Fil:Plus blue.svg|10px]]";
											else
												myPage2.text += " [[Fil:Go-next-grey.svg|10px]]";
										myPage2.text += "\n";
                                }
                        );
						Console.WriteLine("Sort5");
                        myPage2.text += "|}\n";
                        //Add footer
                        myPage2.text += footersStr[stat] + "\n";
                        //Save page
						//Console.WriteLine(myPage2.text);
                        myPage2.Save("Uppdaterar statistiken", true);
						
                }
				/**/
        }
}