#!/usr/bin/perl use FindBin; BEGIN { require "$FindBin::RealBin/../../../lib/Modules.pm"; use lib "$FindBin::RealBin/../lib"; } use Contenido::Globals; use Contenido::Init; use ErrorTee; use PidFile; use Convert::UU qw(uudecode); use POSIX qw(strftime); use IIR_Parser; use Data::Dumper; use Image::Size; use vars qw($DEBUG); $DEBUG = 0; # begin Contenido::Init->init(); my $news_section = $project->s_alias()->{news}; my $sources_section = $project->s_alias()->{sources}; my $discovery_section = 260004783; my $temp_images_directory = "~/Contenido/var/projects/promosuite/loader/images"; my %inter = ( ); my %SPECATTRS = ( Image => [], SECT_ID => [], IMG_COMMENT => [], ShortAbstr => [], LongAbstr => [], Command => [], itype_id => [], ); $SPECATTRS{LentaImage} = $SPECATTRS{Image}; my %STATUSMAP = ( 0 => 0, #default ); my %fields_map = ( 'Title' => 'name', 'Body' => 'body', 'Body_Preview' => 'abstr', 'Date' => 'dtime', # 'SECT_ID' => 'sections' ); my $err = ErrorLog->new(); # Инициализация парсера IIR my $parser = IIR_Parser->new(err => $err, debug=>$DEBUG); # Читаем IIR в хэш {поле}=>{значение} my %obj; while (! $parser->eof ) { my $fn = $parser -> GetFieldName; my $fv = $parser -> GetFieldValue(mode => 'return'); $fv=[uudecode($fv)] if ($fv=~/^begin/); if (exists($SPECATTRS{$fn})){ push (@{$SPECATTRS{$fn}},$fv) }else{ $obj{$fn}=$fv if ($fn && defined($fv)); } } if (my ($cmd) = grep { /change_author/ } @{$SPECATTRS{Command}}) { ($obj{AUTH_ID}) = ($cmd =~ /change_author\s+(\d+)/); } #print Dumper(\%obj); #print Dumper(\%SPECATTRS); ##### warn Dumper \%SPECATTRS; ##### warn Dumper \%obj; ## Если новость не в теме "Олимпиада", то, если у нее есть картинки, отложим их в сторонку ## А вдруг пригодятся ## Потом выйдем, потому как соленые бананы мы не едим if ($obj{MTYPE_ID} != -4 ) { if (@{$SPECATTRS{Image}}) { my $i = 0; foreach my $image (@{$SPECATTRS{Image}}) { $i++; my ($img, $file) = @$image; my ($ext) = ($file =~ /\.(\w+)$/); my $real_msg_id = $obj{Real_Msg_Id}; $real_msg_id =~ s/^0+//; my $dir = "$temp_images_directory/$real_msg_id"; system("mkdir -p $dir"); if (my $fh = new IO::File ">$dir/image$i.$ext") { $fh->autoflush(1); $fh->print($img); $fh->close(); } else { warn "Can\'t open $dir/image$i.$ext: $!"; return undef; } } } ##### exit(); ##### } elsif (!(grep { $_ == $discovery_section } @{$SPECATTRS{SECT_ID}})) { ##### exit(); } my %src_map = map { $_->auth_id() => $_->name } $keeper->get_documents( s => $sources_section, class => 'promosuite::Source', status => 1, ); my $news = new promosuite::Article $keeper; foreach my $key (keys %obj) { next unless exists $fields_map{$key}; $news->{$fields_map{$key}} = $obj{$key}; } #warn Dumper(\%obj); #warn Dumper($news); my $body = $news->body(); $body =~ s/\n{2,}/
/gi; $body =~ s/\n/\ /gi; $body =~ s/
/\n\n/gi; $news->body($body); ## Аннотация, если надо if ( !$news->{abstr} || $news->{abstr} !~ /\S/) { my $text = substr($news->body(), 0, 300); $text =~ s/\s\S+$//; $news->{abstr} = $text; } my $images = {}; if (@{$SPECATTRS{Image}}) { my $i = 0; foreach my $image (@{$SPECATTRS{Image}}) { $i++; my $ind = $i - 1; my $key = "pictures_$i"; my ($img, $file); if (ref $image eq 'ARRAY') { ($img, $file) = @$image; } elsif ($obj{MTYPE_ID} == -4) { my $old_msg_id = $obj{Msg_Id}; my $dir = "$temp_images_directory/$old_msg_id"; opendir(D, $dir); ($file) = grep { /^image$i/ } readdir D; closedir D; if ($file) { if (my $fh = new IO::File "$dir/$file") { local $/ = undef; $img = <$fh>; } else { warn "Can\'t open $dir/$file: $!"; next; } } else { next; } } my ($ext) = ($file =~ /\.(\w+)$/); $ext = 'gif' unless $ext; my $ii = post_image($keeper->images_directory(), $ext, $img); next unless defined $ii; $ii->{alt}=$SPECATTRS{IMG_COMMENT}[$ind] if (exists($SPECATTRS{IMG_COMMENT}) && $SPECATTRS{IMG_COMMENT}[$ind]); $ii->{number} = $i; $images->{$key} = $ii; } $images->{maxnumber} = $i; local $Data::Dumper::Indent = 0; $news->images(Dumper($images)); } if (my ($cmd) = grep { /(?:ex_)?photobanner/ } @{$SPECATTRS{itype_id}}) { exit; } if (my ($cmd) = grep { /interface_lifetime/ } @{$SPECATTRS{Command}}) { my ($interface, $time) = ($cmd =~ /interface_lifetime\s+(.[^\s]+)\s+(.[^\s]+)$/); $news->{status} = 0; } else { $news->{status} = 0; ##### было 2 } ##### $news->{body} =~ tr/\202\204\213\221\222\223\224\225\226\227\233\246\253\271\273/\,\"\<\'\'\"\"\-\-\-\>\|\"\N\"/; ##### $news->{abstr} =~ tr/\202\204\213\221\222\223\224\225\226\227\233\246\253\271\273/\,\"\<\'\'\"\"\-\-\-\>\|\"\N\"/; #if (scalar @{$SPECATTRS{SECT_ID}}) { # $news->sections(@{ $SPECATTRS{SECT_ID} }); #}else{ $news->sections($news_section); #} if (exists $src_map{$obj{AUTH_ID}}) { $news->source($src_map{$obj{AUTH_ID}}); } else { $news->source("Источник медии # ".$obj{AUTH_ID}); } $news->store(); exit; sub post_image { my ($dest_dir, $ext, $img) = @_; my $uniq = time().'_'.int(rand(10**5)); my $fname="$uniq.$ext"; my $imgfile="$dest_dir/$fname"; if (my $fh = new IO::File ">$imgfile"){ $fh->autoflush(1); $fh->print($img); $fh->close(); }else{ warn "Can\'t open $imgfile: $!"; return undef; } my ($width, $height) = imgsize($imgfile); my $ii = { filename => $fname, width => $width, height => $height, }; $keeper->minimize_image($ii, '100x100'); return $ii; } 1; package ErrorLog; sub new { my ($class, %opts) = @_; $opts{_FH}=*STDERR unless ($opts{_FH}); my $self = \%opts; bless ($self,$class); return $self; } sub handle{ my ($self,$level,$msg)=@_; my $fh=$$self{'_FH'}; print $fh time()."\t$$\t$level\t$msg\n"; exit(1) if ($level eq 'fatal'); return 1; } 1;