Archives

Archives / 2004 / October
  • Update: displaying the Gmail ATOM feed in your Web Browser

    Update: you can download the script here and the script engine here. If your Gmail account name contains a 'dot', you should use the /a command line switch e.g. axscript /a:myaccount,mypassword gmail.csx

    Transforming the Atom feed XML returned from Gmail to HTML and displaying the result in your web browser - makes the previous example somewhat more useful.

    One way is to create a small transformation function (using a XSLT document like that provided by manalang.com) say in C#, and save it as xml.cs ...

    // xml.cs
    using System;
    using System.IO;
    using System.Xml;
    using System.Xml.Xsl;

    public class XML
    {
        // an atom 0.3 transformation style sheet
        public static string AtomXsl =
                @"http://manalang.com/wp-content/atom03.xsl";
       
        // transform xml using a transformation document
        public static string Transform(string xmlDoc, string xslDoc)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xmlDoc);
           
            // transform
            XslTransform transform = new XslTransform();
            transform.Load(xslDoc);
           
            StringWriter writer = new StringWriter();
            transform.Transform(doc, null, writer, null);
           
            return writer.ToString();
        }  
    }

    Then replace the print(feedAsXML) line at the bottom of gmail.vb with the following code ...

    ' transform the atom xml feed to html
    feedAsHtml = XML.Transform(feedAsXml, XML.AtomXsl)

    ' write the html to a temporary file
    tempOutputFile = Path.GetTempFileName() & ".htm"
    writer = new StreamWriter(File.Open(tempOutputFile, FileMode.Create))
    writer.Write(feedAsHtml)
    writer.Close()

    ' display file in browser
    System.Diagnostics.Process.Start(tempOutputFile)

    Now if you add xml.cs to the command line, your unread gmail should be displayed in your web browser e.g. ...

    ax[w]script [username] [password] gmail.vb xml.cs

    For the record, here is the gmail.vb file in C#

    // gmail.cs - gets unread mail from a gmail account as an atom feed
    // for Alintex Script - www.alintex.com
    // usage: ax(w)script [username] [password] gmail.cs

    using System;
    using System.Diagnostics;
    using System.Net;
    using System.IO;
    using System.Text;

    #region Script

    // check commandline arguments
    if (args.Length < 2)
    {
       print("gmail.cs - gets unread gmail as an atom feed");
       print("Usage: ax(w)script [username] [password] gmail.cs");
       return;
    }

    WebClient client = new WebClient();
    byte[] bytes = Encoding.ASCII.GetBytes(args[0] + ":" + args[1]);
    client.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(bytes));
     
    const string gmailURI = @"https://gmail.google.com/gmail/feed/atom";
    StreamReader reader = new StreamReader(client.OpenRead(gmailURI));
    string feedAsXml = reader.ReadToEnd();
    reader.Close();
     
    // transform the atom xml feed to html
    string feedAsHtml = XML.Transform(feedAsXml, XML.AtomXsl);

    // write the html to a temporary file
    string tempOutputFile = Path.GetTempFileName() + ".htm";
    StreamWriter writer = new StreamWriter(File.Open(tempOutputFile, FileMode.Create));
    writer.Write(feedAsHtml);
    writer.Close();

    // display file in browser
    Process.Start(tempOutputFile);

    #endregion

    Read more...