Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Google Position Monitor

, 19 Jul 2004
To find position of a website in Google search results for given keywords.
google_position_monitor_demo.zip
obj
Debug
temp
TempPE
bin
Debug
data.ydb
GoolgeMonitor.exe
App.ico
GoolgeMonitor.csproj.user
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Collections.Specialized;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;


namespace GoolgeMonitor
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Button button5;
		private System.Windows.Forms.NumericUpDown NoOfPages;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Button button4;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.ListView listView1;
		private System.Windows.Forms.ProgressBar progressBar1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Label label1;
		private System.Threading.Thread myThread;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.Label label4;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			listView1.Columns.Add("Keyword", 150, HorizontalAlignment.Left);
			listView1.Columns.Add("Position", 150, HorizontalAlignment.Left);
			listView1.Columns.Add("Date", 200, HorizontalAlignment.Left);
		
			FillList();
			
			
			

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.button5 = new System.Windows.Forms.Button();
			this.NoOfPages = new System.Windows.Forms.NumericUpDown();
			this.label3 = new System.Windows.Forms.Label();
			this.button4 = new System.Windows.Forms.Button();
			this.button3 = new System.Windows.Forms.Button();
			this.listView1 = new System.Windows.Forms.ListView();
			this.progressBar1 = new System.Windows.Forms.ProgressBar();
			this.label2 = new System.Windows.Forms.Label();
			this.button2 = new System.Windows.Forms.Button();
			this.button1 = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.label4 = new System.Windows.Forms.Label();
			((System.ComponentModel.ISupportInitialize)(this.NoOfPages)).BeginInit();
			this.SuspendLayout();
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(16, 304);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(216, 20);
			this.textBox2.TabIndex = 22;
			this.textBox2.Text = "Key word";
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(16, 32);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(368, 20);
			this.textBox1.TabIndex = 15;
			this.textBox1.Text = "YourUrl";
			// 
			// button5
			// 
			this.button5.Location = new System.Drawing.Point(344, 304);
			this.button5.Name = "button5";
			this.button5.Size = new System.Drawing.Size(80, 23);
			this.button5.TabIndex = 27;
			this.button5.Text = "Delete";
			this.button5.Click += new System.EventHandler(this.button5_Click);
			// 
			// NoOfPages
			// 
			this.NoOfPages.Location = new System.Drawing.Point(200, 344);
			this.NoOfPages.Name = "NoOfPages";
			this.NoOfPages.Size = new System.Drawing.Size(32, 20);
			this.NoOfPages.TabIndex = 26;
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(16, 344);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(152, 16);
			this.label3.TabIndex = 25;
			this.label3.Text = "No of Google Pages to check";
			// 
			// button4
			// 
			this.button4.Location = new System.Drawing.Point(440, 304);
			this.button4.Name = "button4";
			this.button4.Size = new System.Drawing.Size(80, 23);
			this.button4.TabIndex = 24;
			this.button4.Text = "Delete all";
			this.button4.Click += new System.EventHandler(this.button4_Click);
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(248, 304);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(80, 24);
			this.button3.TabIndex = 23;
			this.button3.Text = "Add";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// listView1
			// 
			this.listView1.FullRowSelect = true;
			this.listView1.GridLines = true;
			this.listView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
			this.listView1.Location = new System.Drawing.Point(16, 64);
			this.listView1.Name = "listView1";
			this.listView1.Size = new System.Drawing.Size(504, 224);
			this.listView1.TabIndex = 21;
			this.listView1.View = System.Windows.Forms.View.Details;
			// 
			// progressBar1
			// 
			this.progressBar1.Location = new System.Drawing.Point(16, 408);
			this.progressBar1.Name = "progressBar1";
			this.progressBar1.Size = new System.Drawing.Size(504, 8);
			this.progressBar1.TabIndex = 20;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(16, 376);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(100, 16);
			this.label2.TabIndex = 19;
			this.label2.Text = "Processing";
			// 
			// button2
			// 
			this.button2.Enabled = false;
			this.button2.Location = new System.Drawing.Point(472, 32);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(48, 24);
			this.button2.TabIndex = 18;
			this.button2.Text = "Stop";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(400, 32);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(48, 24);
			this.button1.TabIndex = 17;
			this.button1.Text = "Start";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(72, 16);
			this.label1.TabIndex = 16;
			this.label1.Text = "URL:";
			// 
			// textBox3
			// 
			this.textBox3.Location = new System.Drawing.Point(352, 344);
			this.textBox3.Name = "textBox3";
			this.textBox3.Size = new System.Drawing.Size(168, 20);
			this.textBox3.TabIndex = 28;
			this.textBox3.Text = "servername.com:port";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(248, 344);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(96, 23);
			this.label4.TabIndex = 29;
			this.label4.Text = "Proxy (URL:port ):";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(528, 429);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.textBox3);
			this.Controls.Add(this.textBox2);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.button5);
			this.Controls.Add(this.NoOfPages);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.button4);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.listView1);
			this.Controls.Add(this.progressBar1);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.label1);
			this.Name = "Form1";
			this.Text = "Google Monitor";
			this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
			this.Load += new System.EventHandler(this.Form1_Load);
			((System.ComponentModel.ISupportInitialize)(this.NoOfPages)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
		
		}

		private void FillList()
		{
			try
			{
				System.IO.StreamReader ssW =  System.IO.File.OpenText("data.ydb");
				string sStr = ssW.ReadLine();
				textBox1.Text = sStr;
				sStr = "";
				sStr = ssW.ReadLine();
				NoOfPages.Value = Int32.Parse(sStr);
				sStr = "";
				sStr = ssW.ReadLine();
				textBox3.Text = sStr;
				sStr = "";
				while(true)
				{
					string sKW = "";
					string sPos = "";
					string sDate = "";
					
					sStr = ssW.ReadLine();
					if(sStr == "")
					{
						break;
					}
					else if(sStr == null)
					{
						break;
					}
					int nPos = -1;
					nPos = sStr.IndexOf("##");
					if(nPos != -1)
					{
						sKW = sStr.Substring(0,nPos);
						sStr = sStr.Substring(nPos+2,sStr.Length-(nPos+2));
						nPos = -1;
					
						nPos = sStr.IndexOf("##");
					
						if(nPos != -1)
						{
							sPos = sStr.Substring(0,nPos);
							sStr = sStr.Substring(nPos+2,sStr.Length-(nPos+2));
							nPos = -1;
							nPos = sStr.IndexOf("##");
							if(nPos != -1)
							{
								sDate = sStr.Substring(0,nPos);
								sStr = sStr.Substring(nPos+2,sStr.Length-(nPos+2));
								nPos = -1;
							}

						}
					

					}
					ListViewItem item1 = new ListViewItem(sKW,0);
						
					item1.SubItems.Add(sPos);
					item1.SubItems.Add(sDate);
					
					listView1.Items.AddRange(new ListViewItem[]{item1});
				}
				ssW.Close();
			}
			catch(Exception e)
			{
				return;
			}

		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			myThread = null;
			myThread = new System.Threading.Thread(new System.Threading.ThreadStart(StartSearch));								
			
			myThread.Start();
			button1.Enabled = false;


			button3.Enabled = false;
			button4.Enabled = false;
			button5.Enabled = false;



			button2.Enabled = true;
			label2.Text = "Processing";
		//	StartSearch();

		}
		private void StartSearch()
		{
			progressBar1.Minimum = 0;
			progressBar1.Maximum = listView1.Items.Count;
			//	progressBar1.Step = 1;
			

			for(int i = 0 ; i < listView1.Items.Count;i++)
			{
				
				string sKW = listView1.Items[i].SubItems[0].Text;
				SearchKWinGoogle(sKW,i);
				progressBar1.Value = i+1;
				
				
				
			}
			button1.Enabled = true;
			button2.Enabled = false;


			button3.Enabled = true;
			button4.Enabled = true;
			button5.Enabled = true;


			label2.Text = "Completed";
		}

		public string GetPage(string url)
		{
			
			WebResponse response = null;
			Stream stream = null;
			StreamReader
				reader = null;

			try
			{
				HttpWebRequest request =
					(HttpWebRequest)WebRequest.Create(url);

			
				if(textBox3.Text != "")
				{

					WebProxy myProxy = new WebProxy(textBox3.Text);
					request.Proxy = myProxy;
				}




				response = request.GetResponse();
				stream = response.GetResponseStream();

				if( !response.ContentType.ToLower().StartsWith("text/") )
					return null;

				string buffer = "",line;

				reader = new StreamReader(stream);

				while( (line = reader.ReadLine())!=null )
				{
					buffer+=line+"\r\n";
				}

				return buffer;
			}
			catch(WebException e)
			{
				System.Console.WriteLine("Can't download:" + e);
				return null;
			}
			catch(IOException e)
			{
				System.Console.WriteLine("Can't download:" + e);
				return null;
			}
			finally
			{
				if( reader!=null )
					reader.Close();

				if( stream!=null )
					stream.Close();

				if( response!=null )
					response.Close();
			}
		}
		private void SearchKWinGoogle(string sKeyWord,int index)
		{
			int nCount = 0;
			bool bBreak = false;

			StringCollection strCollection = new StringCollection();

			while(true)
			{
				if(bBreak)
				{
					break;
				}
				if(NoOfPages.Value != 0 && (NoOfPages.Value- 1) < nCount)
				{
					break;
				}
				
				string url = "http://www.google.com/search?q=";
				url += sKeyWord;
				url+= "&hl=en&lr=&ie=UTF-8&start=";		

				url += (nCount*10).ToString();
				
				url += "&sa=N";
				string sPage = GetPage(url);
				nCount++;
				if(sPage == null)
				{
					continue;
				}	
				bBreak = ExtractHrefFromSearchPages(sPage , strCollection);
			}			

			
			int position = -1;
			for(int i = 0; i < strCollection.Count;i++)
			{
				string sTmp = strCollection[i];
				if(sTmp.IndexOf(textBox1.Text) > -1)
				{
					position = i;
				}
			}
		
			if(position == -1)
			{
				listView1.Items[index].SubItems[1].Text = "Not Found";
				listView1.Items[index].SubItems[2].Text = DateTime.Now.ToString();
			}
			else
			{
				listView1.Items[index].SubItems[1].Text = (position+1).ToString();
				listView1.Items[index].SubItems[2].Text = DateTime.Now.ToString();
			}

		}
		public bool ExtractHrefFromSearchPages(string str, StringCollection strCollection)
		{
			
			Regex r;
			Match m;
			bool bBreak = false;

			r = new Regex("href\\s*=\\s*(?:\"(?<1>[^\"]*)\"|(?<1>\\S+))",
				RegexOptions.IgnoreCase|RegexOptions.Compiled);
			for (m = r.Match(str); m.Success; m = m.NextMatch()) 
			{
				//	MessageBox.Show("Found href " + m.Groups[1].Value + " at " + m.Groups[1].Index.ToString());
							
				if(!strCollection.Contains(m.Groups[1].Value))
				{
					string sFound = m.Groups[1].Value;	
				
					if(sFound.IndexOf("&filter=0") >= 0)
					{
						bBreak = true;
					}
					
					if(sFound.IndexOf("oi=news") >= 0)
					{
						continue;
					}

					if(sFound.IndexOf("google.com") >= 0)
					{
						continue;
					}
					if(sFound.IndexOf("&spell=1") >= 0)
					{
						continue;
					}
					if(sFound.IndexOf("&q=+site") >= 0)
					{
						continue;
					}
					if(sFound.IndexOf("/search") >= 0)
					{
						continue;
					}
					if(sFound.IndexOf("www.googleadservices.com") >= 0)
					{
						continue;
					}
					if(sFound.IndexOf("dictionary.reference.com")>= 0)
					{
						continue;
					}
					if(sFound.IndexOf("language_tools") >= 0)
					{
						continue;
					}
					if(sFound.IndexOf("/swr?q=") >= 0)
					{
						continue;
					}
					if(sFound.IndexOf("/help/") >= 0)
					{
						continue;
					}
					if(sFound.IndexOf("search?q=cache") >= 0)
					{
						continue;
					}
					if(sFound.IndexOf("q=related:") >= 0)
					{
						continue;
					}
					if(sFound.IndexOf("/advanced_search?q=") >= 0)
					{
						continue;
					}
					if(sFound.IndexOf("/preferences?q=") >= 0)
					{
						continue;
					}
					if(sFound.IndexOf("/quality_form?q=") >= 0)
					{
						continue;
					}
					if(sFound.IndexOf("/ads/") >= 0)
					{
						continue;
					}
					if(sFound.IndexOf("/services/") >= 0)
					{
						continue;
					}
					if(sFound.IndexOf("/about.html") >= 0)
					{
						continue;
					}
					if(sFound == "/options/")
					{
						continue;
					}
					if(sFound.IndexOf("/url?q=") >= 0)
					{
						continue;
					}
					int nPos = -1;
					nPos = sFound.IndexOf(">");
					if(nPos != -1)
					{
						sFound = sFound.Substring(0,nPos);
					}
					strCollection.Add(sFound);
					
					if(sFound.IndexOf(textBox1.Text) > -1)
					{
						bBreak = true;
						break;
					}
					
				}
			
			}
			return bBreak;	
		}

		private void button3_Click(object sender, System.EventArgs e)
		{
			ListViewItem item1 = new ListViewItem(textBox2.Text,0);
			item1.SubItems.Add("");
			item1.SubItems.Add("");
			item1.SubItems.Add("");
			item1.SubItems.Add("");

			listView1.Items.AddRange(new ListViewItem[]{item1});

		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			myThread.Abort();
			myThread.Join();

			button1.Enabled = true;
			button2.Enabled = false;


			button3.Enabled = true;
			button4.Enabled = true;
			button5.Enabled = true;

		}

		private void button5_Click(object sender, System.EventArgs e)
		{
			foreach(System.Windows.Forms.ListViewItem item in listView1.SelectedItems)
			{
				item.Remove();
			}
		}

		private void button4_Click(object sender, System.EventArgs e)
		{
			while(true)
			{
				if (listView1.TopItem == null)
				{
					break;
				}
				else
				{
					listView1.TopItem.Remove();
				}
					
			}
		}

		private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			WriteToFile();
		}
		private void WriteToFile()
		{
			System.IO.StreamWriter ssW = System.IO.File.CreateText("data.ydb");
			
			ssW.WriteLine(textBox1.Text);
			ssW.WriteLine(NoOfPages.Value);
			ssW.WriteLine(textBox3.Text);
			for(int i=0;i<listView1.Items.Count;i++)
			{
				string sWrite = "";
				
				sWrite += listView1.Items[i].SubItems[0].Text;
				sWrite += "##";
				sWrite += listView1.Items[i].SubItems[1].Text;
				sWrite += "##";
				sWrite += listView1.Items[i].SubItems[2].Text;
				sWrite += "##";
				ssW.WriteLine(sWrite);

			}

			
			ssW.Close();
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author


| Advertise | Privacy | Mobile
Web04 | 2.8.140921.1 | Last Updated 20 Jul 2004
Article Copyright 2004 by Haja
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid