Source

shlomi-fish-homepage / lib / Shlomif / Homepage / Amazon.pm

package Shlomif::Homepage::Amazon;

use strict;
use warnings;



sub import
{
    my ($module, $args) = @_;
    return Shlomif::Homepage::Amazon::Obj->new($args)->process;
}

package Shlomif::Homepage::Amazon::Obj;

use Moose;

use JSON qw(decode_json);

use XML::Grammar::ProductsSyndication;
use XML::LibXML::XPathContext;
use Term::ReadPassword;

has 'wml_dir' => (isa => 'Str', is => 'ro');
has 'lib_dir' => (isa => 'Str', is => 'ro');
has 'xml_basename' => (isa => 'Str', is => 'ro');
has 'ps' => (isa => 'XML::Grammar::ProductsSyndication', is => 'rw');

sub BUILD
{
    my $self = shift;

    $self->ps(XML::Grammar::ProductsSyndication->new(
        {
            'source' =>
            {
                'file' => sprintf("%s/%s", $self->wml_dir, $self->xml_basename),
            }
        },
    ));

}

sub _utf8_slurp
{
    my $filename = shift;

    open my $in, '<', $filename
        or die "Cannot open '$filename' for slurping - $!";

    binmode $in, ':encoding(utf8)';

    local $/;
    my $contents = <$in>;

    close($in);

    return $contents;
}

sub process
{
    my ($self) = @_;

    my $config = decode_json(_utf8_slurp($ENV{HOME}.'/.shlomifish-amazon-sak.json'));

    if (!$self->ps->is_valid())
    {
        die "Not valid.";
    }
    my $xml = $self->ps->transform_into_html({ 'output' => "xml" });

    my $xc = XML::LibXML::XPathContext->new($xml);
    $xc->registerNs('html' => "http://www.w3.org/1999/xhtml");

    open my $out, ">", $self->lib_dir() . "/include-me.html";
    binmode $out, ":utf8";
    print {$out} $xc->findnodes('/html:html/html:body/html:div')->[0]->toString(0);
    close ($out);

    $self->ps->update_cover_images(
        {
            'size' => "l",
            'resize_to' => { 'width' => 150, 'height' => 250 },
            'name_cb' =>
            sub
            {
                my $args = shift;
                return $self->wml_dir() . "/images/$args->{id}.jpg";
            },
            'amazon_token' => "0VRRHTFJECHSKYNYD282",
            'amazon_associate' => "shlomifishhom-20",
            'amazon_sak' => $config->{'amazon_sak'},
        }
    );

    return;
}

1;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.