Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 123 lines (104 sloc) 3.3 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
#!/usr/bin/env perl
use strict;
use warnings;
use Flickr::API2;
use YAML qw/LoadFile DumpFile/;
use FindBin;
use Email::Stuff;
use LWP::Simple qw/get/;
use Fatal qw/rename/;
use Getopt::Long;
use XML::Atom::Client;
use DateTime::Format::Atom;

my $config_file = "$FindBin::Bin/.momrc";
GetOptions(
    'config=s' => \$config_file,
) or usage();

$| = 1;

my $momrc = LoadFile($config_file);

my $api = new Flickr::API2(
    { key => $momrc->{api_key}, secret => $momrc->{api_secret} });

my @to_send;
for my $person (@{ $momrc->{people} }) {
    my ($id, $username) = ($person->{id}, $person->{username});
    print "\nFetching photos for $username\n";
    my @photos = $api->people->getPublicPhotos($id, per_page => 25);

    for my $photo (@photos) {
        my $title = $photo->title;
        my $id = $photo->info->{photo}{id};
        my $p = $api->photos->by_id($id);
        my $date = $photo->info->{photo}{dates}{posted};

        if ($date > $momrc->{last_sent}) {
            print "Title is $title id is $id posted at $date\n";
        }
        else {
            print " Found an old photo - skipping.\n";
            last;
        }
        # Work around a bug in Flickr::API2::Photo::populate_size_urls
        %Flickr::API2::Photo::label_to_accessor = (Medium => 'url_m', Large => 'url_l');
        $p->url_m('');
        $p->url_l('');
        $p->populate_size_urls;

        my $url = $p->url_l || $p->url_m || return;
        my $filename = join('.', $id, $photo->info->{photo}{originalformat}||'jpg');
        push @to_send, {
            subject => "Photo from $username",
            url => $url,
            filename => $filename,
        };
    }
}

for my $feed_cfg (@{ $momrc->{feeds} }) {
    my $api = XML::Atom::Client->new;
    my $feed = $api->getFeed($feed_cfg->{url});

    my $f = DateTime::Format::Atom->new();
    my $dt = $f->parse_datetime( $feed->updated );
    if ($dt->epoch > $momrc->{last_sent}) {
        print " Feed has been updated - fetching new photos.\n";
    }
    else {
        print " Feed hasn't been updated - skipping.\n";
        next;
    }
    my @entries = $feed->entries;
    for my $entry (@entries) {
        my $body = $entry->content->body;
        while ($body =~ m/href="(http.+?\.(?:jpg|jpeg))"/ig) {
            my $url = $1;
            push @to_send, {
                subject => "Photo from $feed_cfg->{name}",
                url => $url,
            };
        }
    }

}

for my $img (@to_send) {
    unless ($img->{filename}) {
        ($img->{filename} = $img->{url}) =~ s/.+\///;
    }

    my $photo_content = get $img->{url};
    unless ($photo_content) {
        print "Couldn't fetch $img->{url}\n";
        next;
    }
    print "Fetched - size is " . length($photo_content) . "\n";

    print "Emailing as $img->{filename}\n";
    Email::Stuff->to($momrc->{email_to})
        ->from($momrc->{email_from})
        ->subject($img->{subject})
        ->text_body("Photo: $img->{filename} attached")
        ->attach($photo_content, filename => $img->{filename})
        ->send;
    print "Sent.\n";
}


$momrc->{last_sent} = time;
my $momrc_temp = "$config_file.tmp.$$";
DumpFile($momrc_temp, $momrc);
rename $momrc_temp => $config_file;

exit;


sub usage {
    die <<EOT
USAGE: momframe [--config=file]
EOT
}
Something went wrong with that request. Please try again.