Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Slashdot Headline Grabber for Win32

by httptech (Chaplain)
on May 03, 2000 at 04:33 UTC ( [id://10011]=sourcecode: print w/replies, xml ) Need Help??
Category: Win32/GUI
Author/Contact Info Joe Stewart a.k.a. httptech joe@NO.httptech.SPAM.com
Description: A Win32 GUI program to download Slashdot headlines every 30 minutes and display them in a small window.
#!c:\\perl\\bin\\perl.exe
# Slashdot Headline Grabber for Win32
# by Joe Stewart

use Win32::GUI;
use Win32::Shell;
use Socket;

my $count;
my %stories;
my @keys;

my $maxsize = ([400,42]);
my $minsize = ([200,42]);
my $main = Win32::GUI::Window->new(-name => 'Main',
-text => 'Slashdot Headlines',
-width => 275,
-height => 42,
-maxsize => $maxsize,
-minsize => $minsize);


my $label = new Win32::GUI::Label($main,
-text => "Downloading Headlines...",
-name => "Url", 
-width => 350,
-height => 42,
-left => 40,
-notify => 1);


my $button_back = new Win32::GUI::Button($main,
-text => "<",
-name => "Back",
-width => 15,
-left => 0,
-height => 15);

my $button_fwd = new Win32::GUI::Button($main,
-text => ">",
-name => "Fwd",
-width => 15,
-left => 15,
-height => 15);

$main->AddButton($button_back);
$main->AddButton($button_fwd);
$main->AddLabel($label);

my $Cycle = $main->AddTimer("Cycle", 10000);
my $Reload = $main->AddTimer("Reload", 1800000);

$main->Show();

GUI::Update($main);

my $inet = inet_aton('www.slashdot.org');
my $proto=getprotobyname('tcp');

&fetch_headlines;

$main->Url->Text($stories{$keys[0]});
$Cycle->Interval(10000);

Win32::GUI::Dialog();

sub Main_Terminate {
-1;
}

sub Fwd_Click {
$count++;
$count = 0 if $count == scalar(@keys);
$main->Url->Text($stories{$keys[$count]});
$Cycle->Interval(10000);
}

sub Back_Click {
$count--;
$count = scalar(@keys) - 1 if $count == -1;
$main->Url->Text($stories{$keys[$count]});
$Cycle->Interval(10000);
}


sub Url_DblClick {
my $url = $keys[$count];
Win32::Shell::Execute("open", $url, undef, undef, "SW_SHOWNORMAL");
return 1;
}

sub fetch_headlines {
my @D;
socket(S,PF_INET,SOCK_STREAM,$proto) || return 0;
if(connect(S,pack "SnA4x8",2,80,$inet)) {
select(S);      
$|=1;
print "GET /slashdot.xml HTTP/1.0\n\n";    
@D=<S>;
select(STDOUT); 
close(S);
} else { return 0 } 

my ($title, $url);

for (@D) {
$title = $1 if /\<title\>(.*)\<\/title\>/;
$url = $1 if /\<url\>(.*)\<\/url\>/;

if (/<\/story>/) { 
$stories{$url} = $title;
push(@keys, $url);
$title = "";
$url = "";
}
}
return 1;
}

sub Cycle_Timer {
&Fwd_Click;
return 1;
}

sub Reload_Timer {
&fetch_headlines;
$Reload->Interval(1800000);
return 1;
}
Replies are listed 'Best First'.
RE: Slashdot Headline Grabber for Win32
by marcos (Scribe) on May 04, 2000 at 14:11 UTC
    I think the Slashdot Headline Grabber is a good idea. I only have one question: why not using LWP to get the /. xml page? I think it's better, and using LWP it is possible to set up a proxy (very useful if you are beyond a firewall). I hacked your code a bit, and here are my suggestions:
    use LWP; use HTTP::Request::Common; # # .... # sub fetch_headlines { my @D; my $ua = new LWP::UserAgent; return 0 unless ($ua); $ua->proxy('http', 'http://myproxy.mynet.org:8080'); #set up your +proxy here my $url = "http://www.slashdot.org/slashdot.xml"; my $res = $ua->request(GET $url); if ($res->is_success) { @D = split /\n/, $res->content; } else { return 0; } my ($title, $url); for (@D) { $title = $1 if /\<title\>(.*)\<\/title\>/; $url = $1 if /\<url\>(.*)\<\/url\>/; if (/<\/story>/) { $stories{$url} = $title; push(@keys, $url); $title = ""; $url = ""; } } return 1; }

    It may also be a good idea to use XML::DOM to parse the XML downloaded from /. but that is probably too much for the Headline Grabber: the for loop is quicker.
    Any comment is highly appreciated.
    marcos
      I actually started out using LWP; however I wrote this app to be compiled by perl2exe. The LWP version was 1.2 megs after compilation, and the Socket.pm version was just under a meg. I went with Socket.pm to save the 200K and also because it was a nice learning exercise for me in Socket.pm

      Good point about the proxy though; I hadn't thought about that.

        I never tried to use perl2exe: does it work well? I also have one question: where can I find Win32::GUI is there a ppm for Activestate perl?
        TIA
        marcos
      Well, my first post on perlmonks.org...
      ( and I FSK it up.... sheesh! )

      I am working from behind a corporate firewall, so I tried the LWP example above with my proxy info, and am getting:

      Protocol scheme: 'http://www.slashdot.org/slashdot.xml' is not supported.
      along with what looks like a 501 response in the debugger.

      I tried this with a couple of different URLs and got the same result, so I suspect it may be the proxy setup. Any ideas?

      Just to confirm:

      My proxy settings in Netscape show

      pset.tgw.canon.co.jp/proxy.pac

      so I used:
      #set up your proxy here
      $ua->proxy('http', 'http://pset.tgw.canon.co.jp:80');

      I tried both 80 and 8080 for ports, with the same result.

      Any advice appreciated.

        I think that the problem is that your company uses a proxy configuration script.
        If you check your Netscape proxy settings you should have the 'Automatic proxy configuration' option enabled and the 'Configuration Location (URL)' set to 'pset.tgw.canon.co.jp/proxy.pac'. If so my guess is correct: your are using an automatic proxy configuration script.
        You may download the proxy configuration script with a browser going to the URL http://pset.tgw.canon.co.jp/proxy.pac - or with a simple perl script that uses LWP and gets that URL :-).
        The script should not be too complicated to read: there should be a function that returns a proxy, more or less like this:
        return "PROXY 151.92.12.112:8080";
        the return value may be different if you are trying to get corporate intranet URLs or Internet URLs (for corporate intranet URLs you may have something like return "DIRECT").
        So all you have to do is find out in the proxy.pac the IP address (or the name), and the port of the real proxy your company is using to access the Internet, and then use this same proxy and port in the perl script.
        I hope this works. If you have problems, ask me, please.
        marcos
Re: Slashdot Headline Grabber for Win32
by Anonymous Monk on Jul 15, 2001 at 19:19 UTC
    i had the same idea in my head, but only for an html. Put it in marquee tags. I donīt knopw perl but i though it was a good idea

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://10011]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-03-19 09:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found