When does the next bus pass?
I work in a remote bussiness center downtown Paris. Last weeks I spend nearly 3 hour a days comuting.
Every evening, I have to wait for a bus and one time out of two it rains cats and dog.
So every evening I check the bus company's web site in order to known at what time the next bus pass to my station.
Late in the evening at Office I have no time to loose! So I decide to speed up this repetitive task:
A few times ago(lol) I wrote a C# code to perform a web crawling. I used the same technology to code the following extractor. As usual this has been made with VS studio but, use csc.exe (.Net 1.1 framework) to compile it as well. (see previous posts for exact command line)
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
namespace bus
{
///
/// Summary description for Form1.
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.RichTextBox richTextBox2;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
///
/// Use a regular expression to extract informations from a string
/// all matching results are stored in a string array.
///
///
///
///
private static string [] extractReg(string strHTML,Regex r)
{
Match m;
String[] results = new String[101];
// Use the Matches method to find all matches in the input string.
// Loop through the match collection to retrieve all
// matches.
int i=0;
m = r.Match(strHTML);
for (i=0; (m.Success &&amp; i<100); m =" m.NextMatch())">
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.richTextBox2 = new System.Windows.Forms.RichTextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(0, 0);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(344, 40);
this.button1.TabIndex = 0;
this.button1.Text = "check it ";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(0, 48);
this.richTextBox1.Name = "-";
this.richTextBox1.Size = new System.Drawing.Size(160, 96);
this.richTextBox1.TabIndex = 1;
this.richTextBox1.Text = "-";
//
// richTextBox2
//
this.richTextBox2.Location = new System.Drawing.Point(168, 48);
this.richTextBox2.Name = "-";
this.richTextBox2.Size = new System.Drawing.Size(176, 96);
this.richTextBox2.TabIndex = 1;
this.richTextBox2.Text = "-";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(352, 150);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.richTextBox2);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
///
/// This is the main method here we call the web pages and extract method
///
///
///
private void button1_Click(object sender, System.EventArgs e)
{
System.Net.HttpWebRequest r ;
string strRes;
int count =0;
string [] nbmin ;
//I found this regular expression which work well for RATP's real time bus position web site
// You will have to change it regarding the information you want to check.
Regex re = new Regex(">(?'1'[0-9]+ mn).*<", RegexOptions.IgnoreCaseRegexOptions.Compiled); // URL with rich querystrings RATP users will pay attention to "ligne" , "dir", standig for direction , "arr" standing for "arret" (los "libdir" and "lib arr") string URL1 ="http://www.ratp.info/horaires/index.php?reseau=BUS&etape=prec_bustram_ssotr&ligne=179&libligne=GARE+DE+ROBINSON+%2F+PONT+DE+SEVRES&dem=dem3&libarr=LA+BOURSIDIERE&libdir=GARE+DE+ROBINSON&act=act_valid&amp;arr=179_5206_5436&dir=A"; string URL2 = "http://www.ratp.info/horaires/index.php?reseau=BUS&etape=prec_bustram_ssotr&ligne=179&libligne=GARE+DE+ROBINSON+%2F+PONT+DE+SEVRES&dem=dem3&libarr=LA+BOURSIDIERE&libdir=PONT+DE+SEVRES&act=act_valid&amp;arr=179_5206_5436&dir=R"; r =(HttpWebRequest)WebRequest.Create(URL1); r.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials ; System.Net.HttpWebResponse rp = (HttpWebResponse)r.GetResponse(); // Gets the stream associated with the response. Stream receiveStream = rp.GetResponseStream(); Encoding encode = System.Text.Encoding.GetEncoding("utf-8"); // Pipes the stream to a higher level stream reader with the required encoding format. StreamReader readStream = new StreamReader( receiveStream, encode ); Char[] read = new Char[256]; // Reads 256 characters at a time. count = readStream.Read( read, 0, 256 ); strRes =""; while (count > 0)
{
// Dumps the 256 characters on a string .
String str = new String(read,0, count);
strRes +=str;
count = readStream.Read(read, 0, 256);
}
// Releases the resources of the response.
rp.Close();
// Releases the resources of the Stream.
readStream.Close();
//write the result of the extraction
this.richTextBox1.Text = "Bus 179 en direction de Robinson" + String.Join("\n", extractReg(strRes,re));
strRes ="";
r.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials ;
r =(HttpWebRequest)WebRequest.Create(URL2);
r.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials ;
rp = (HttpWebResponse)r.GetResponse();
count =0;
// Gets the stream associated with the response.
receiveStream = rp.GetResponseStream();
encode = System.Text.Encoding.GetEncoding("utf-8");
// Pipes the stream to a higher level stream reader with the required encoding format.
readStream = new StreamReader( receiveStream, encode );
read = new Char[256];
// Reads 256 characters at a time.
count = readStream.Read( read, 0, 256 );
while (count > 0)
{
// Dumps the 256 characters on a string .
String str = new String(read,0, count);
strRes +=str;
count = readStream.Read(read, 0, 256);
}
// Releases the resources of the response.
rp.Close();
// Releases the resources of the Stream.
readStream.Close();
re = new Regex(">(?'1'[0-9]+ mn).*<", RegexOptions.IgnoreCaseRegexOptions.Compiled); //write the result this.richTextBox2.Text = "Bus 179 en direction de pont de Sèvres " + String.Join("\n", extractReg(strRes,re)); } } }