24.07.2017 Views

Hacking Gmail

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 13 — Building an API from the HTML-Only Version<br />

193<br />

There is a lot going on here. You have the entire message, and all of the associated<br />

metadata — the sender, the date, the subject line, and so forth — and you have a<br />

whole collection of actions to perform on the message, with (joy of joys) a seemingly<br />

easy-to-decipher system of URLs to set them going. Later on in this chapter, you<br />

return to this listing to work on these commands.<br />

Meanwhile, you need to get at the message contents. The technique is exactly the<br />

same as when you looked through the Inbox. Listing 13-6 shows the code that<br />

does this.<br />

Listing 13-6: Code to Parse an Individual Message Page<br />

#!/usr/bin/perl<br />

use warnings;<br />

use strict;<br />

use HTML::TokeParser;<br />

open( FILEIN, “<strong>Gmail</strong> - single message.html” );<br />

undef $/;<br />

my $filecontents = ;<br />

my $stream = HTML::TokeParser->new( \$filecontents );<br />

$stream->get_tag(“table”);<br />

$stream->get_tag(“table”);<br />

$stream->get_tag(“table”);<br />

$stream->get_tag(“table”);<br />

$stream->get_tag(“table”);<br />

$stream->get_tag(“table”);<br />

$stream->get_tag(“table”);<br />

$stream->get_tag(“table”);<br />

$stream->get_tag(“table”);<br />

$stream->get_tag(“table”);<br />

$stream->get_tag(“table”);<br />

$stream->get_tag(“b”);<br />

my $subject = $stream->get_trimmed_text(“/b”);<br />

$stream->get_tag(“b”);<br />

my $from_true_name = $stream->get_trimmed_text(“/b”);<br />

$stream->get_tag(“/font”);<br />

my $from_email_address = $stream->get_trimmed_text(“/td”);<br />

Continued

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!