Script pour récupérer par mail la dernière image d’un blog photo tumblr

#!/usr/bin/perl

use strict;
use warnings;
use LWP::UserAgent;
use XML::LibXML;
use HTML::TreeBuilder;
use XML::LibXML::XPathContext;

use MIME::Lite;
use Net::SMTP;

my $url="http://SITENAME.tumblr.com/api/read?type=photo&num=1&start=0";

my $ua = LWP::UserAgent->new;
my $resp = $ua->get($url);

my $parser = XML::LibXML->new();
my $doc = $parser->parse_string($resp->content);
my $root = $doc->documentElement();
my $xcont = XML::LibXML::XPathContext->new($root);
my $imgnodes = $xcont->findnodes('//photo-url[@max-width="1280"]');

foreach my $itemnode ($imgnodes->get_nodelist) {
        my $imagenode = $itemnode->firstChild;
        my $imglink = $imagenode->getValue;
        my $data = $ua->get($imglink);
        my $filename = "image.jpg";
        open (FH, ">$filename");
        binmode (FH);
        print FH $data->content;
        close (FH);

        my $from = "";
        my $to = "";
        my $mail_host = "";
        my $subject = "Une nouvelle fracture de l'oeil";
        my $msg = MIME::Lite->new (
                From => $from,
                To => $to,
                Subject => $subject,
                Type =>'multipart/mixed'
        ) or die "Error creating multipart container: $!n";
        $msg->attach (
                Type => 'image/jpg',
                Path => $filename,
                Filename => $filename,
                Disposition => 'attachment'
        ) or die "Error adding $filename: $!n";
        MIME::Lite->send('smtp', $mail_host, Timeout=>60);
        $msg->send;
}

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *