| 1 |
296 |
ahitrov |
#!/usr/bin/perl |
| 2 |
|
|
|
| 3 |
|
|
use strict; |
| 4 |
|
|
use warnings "all"; |
| 5 |
|
|
use locale; |
| 6 |
|
|
use Data::Dumper; |
| 7 |
|
|
|
| 8 |
|
|
use FindBin; |
| 9 |
|
|
BEGIN { |
| 10 |
|
|
require "$FindBin::RealBin/../lib/Modules.pm"; |
| 11 |
|
|
} |
| 12 |
|
|
|
| 13 |
|
|
use Contenido::Globals; |
| 14 |
|
|
use Contenido::Init; |
| 15 |
|
|
use ErrorTee; |
| 16 |
|
|
use PidFile; |
| 17 |
|
|
use P::WebFetcher; |
| 18 |
|
|
|
| 19 |
|
|
|
| 20 |
|
|
# begin |
| 21 |
|
|
Contenido::Init->init(); |
| 22 |
|
|
|
| 23 |
|
|
my $keeper_module = $state->project.'::Keeper'; |
| 24 |
|
|
$keeper = $keeper_module->new($state); |
| 25 |
|
|
|
| 26 |
|
|
PidFile->new($keeper, compat=>1); # db-based locking (run only on one host) |
| 27 |
|
|
#PidFile->new($keeper, compat=>1, per_host=>1); # db-based locking (run on whole cluster) |
| 28 |
|
|
|
| 29 |
|
|
for my $plugin ( split(/\s+/, $state->plugins) ) { |
| 30 |
|
|
my $class = $plugin.'::Apache'; |
| 31 |
|
|
eval { $class->child_init(); }; |
| 32 |
|
|
if ( $@ ) { |
| 33 |
|
|
print("Не могу выполнить метод child_init плагина $plugin ($class) по причине '$@'"); |
| 34 |
|
|
} |
| 35 |
|
|
} |
| 36 |
|
|
|
| 37 |
|
|
############################################ |
| 38 |
|
|
# please use: |
| 39 |
|
|
# $state->{log_dir} for logging |
| 40 |
|
|
# $state->{tmp_dir} for temporary files |
| 41 |
|
|
########################################### |
| 42 |
|
|
my @prjSec = $keeper->get_documents ( |
| 43 |
|
|
class => 'promosuite::ProjectSection', |
| 44 |
|
|
ids => 1, |
| 45 |
|
|
); |
| 46 |
|
|
|
| 47 |
|
|
my $SOURCES = {}; |
| 48 |
|
|
my $SRC = $keeper->get_documents ( |
| 49 |
|
|
class => 'promosuite::Source', |
| 50 |
|
|
return_mode => 'array_ref', |
| 51 |
|
|
no_limit => 1, |
| 52 |
|
|
); |
| 53 |
|
|
map { $SOURCES->{$_->auth_id} = $_ } grep { $_->auth_id } @$SRC; |
| 54 |
|
|
|
| 55 |
|
|
my $MEDIASRC = $keeper->{media}->get_documents ( |
| 56 |
|
|
class => 'media::Source', |
| 57 |
|
|
return_mode => 'hash_ref', |
| 58 |
|
|
no_limit => 1, |
| 59 |
|
|
); |
| 60 |
|
|
|
| 61 |
|
|
foreach my $prjSecId (@prjSec) { |
| 62 |
|
|
my ($prjParam) = $keeper->get_documents ( |
| 63 |
|
|
s => $prjSecId, |
| 64 |
|
|
class => 'promosuite::Project', |
| 65 |
|
|
limit => 1, |
| 66 |
|
|
); |
| 67 |
|
|
|
| 68 |
|
|
if ( $prjParam->media_import ) { |
| 69 |
|
|
my $last_id = $prjParam->media_last; |
| 70 |
|
|
|
| 71 |
|
|
my @media_import_aliases = split( /\ +/, $prjParam->media_import ); |
| 72 |
|
|
|
| 73 |
|
|
my $sect = ""; |
| 74 |
|
|
my @sect_ids = (); |
| 75 |
|
|
|
| 76 |
|
|
foreach my $alias (@media_import_aliases) { |
| 77 |
|
|
$sect = $keeper->{media}->get_rubric($alias); |
| 78 |
|
|
@sect_ids = (); |
| 79 |
|
|
push @sect_ids, $sect->id if ref $sect; |
| 80 |
|
|
} |
| 81 |
|
|
|
| 82 |
|
|
my @news = $keeper->{media}->get_documents ( |
| 83 |
|
|
s => \@sect_ids, |
| 84 |
|
|
class => 'media::News', |
| 85 |
|
|
status => [5,8], |
| 86 |
|
|
limit => 30, |
| 87 |
|
|
order => [qw|date direct|], |
| 88 |
|
|
); |
| 89 |
|
|
my @media_ids = map { $_->id } @news; |
| 90 |
|
|
|
| 91 |
|
|
my ($section) = $keeper->get_sections ( |
| 92 |
|
|
s => $prjSecId, |
| 93 |
|
|
class => 'promosuite::NewsSection', |
| 94 |
|
|
status => 1, |
| 95 |
|
|
limit => 1, |
| 96 |
|
|
); |
| 97 |
|
|
next unless ref $section; |
| 98 |
|
|
|
| 99 |
|
|
my $loaded = @media_ids ? $keeper->get_documents ( |
| 100 |
|
|
s => $section->id, |
| 101 |
|
|
class => 'promosuite::News', |
| 102 |
|
|
ext_id => \@media_ids, |
| 103 |
|
|
light => 1, |
| 104 |
|
|
return_mode => 'hash_ref', |
| 105 |
|
|
hash_by => 'ext_id', |
| 106 |
|
|
) : {}; |
| 107 |
|
|
|
| 108 |
|
|
foreach my $msg (@news) { |
| 109 |
|
|
next if exists $loaded->{$msg->id}; |
| 110 |
|
|
|
| 111 |
|
|
my $message = promosuite::News->new ( $keeper ); |
| 112 |
|
|
$message->ext_id( $msg->id ); |
| 113 |
|
|
$message->dtime( $msg->dtime ); |
| 114 |
|
|
$message->name( Convert::Cyrillic::cstocs('KOI8', 'UTF8', $msg->name) ); |
| 115 |
|
|
$message->abstr( Convert::Cyrillic::cstocs('KOI8', 'UTF8', $msg->abstract) ); |
| 116 |
|
|
$message->body( Convert::Cyrillic::cstocs('KOI8', 'UTF8', $msg->body) ); |
| 117 |
|
|
$message->sections( $section->id ); |
| 118 |
|
|
### Get Source |
| 119 |
|
|
if ( exists $SOURCES->{$msg->source} ) { |
| 120 |
|
|
$message->source_id( $SOURCES->{$msg->source}->id ); |
| 121 |
|
|
} else { |
| 122 |
|
|
my $msrc = $MEDIASRC->{$msg->source}; |
| 123 |
|
|
my $src = promosuite::Source->new ($keeper); |
| 124 |
|
|
$src->name( Convert::Cyrillic::cstocs('KOI8', 'UTF8', $msrc->name) ); |
| 125 |
|
|
$src->auth_id( $msrc->id ); |
| 126 |
|
|
$src->url( $msrc->url ); |
| 127 |
|
|
$src->status(1); |
| 128 |
|
|
$src->sections( $project->s_alias->{sources} ); |
| 129 |
|
|
$src->store; |
| 130 |
|
|
$message->source_id( $src->id ); |
| 131 |
|
|
$SOURCES->{$msg->source} = $src; |
| 132 |
|
|
} |
| 133 |
|
|
### /Get Source |
| 134 |
|
|
### Store images |
| 135 |
|
|
my $image_hash; |
| 136 |
|
|
my $idx = 0; |
| 137 |
|
|
local $Data::Dumper::Indent = 0; |
| 138 |
|
|
foreach my $image ($msg->get_pics('image_set')) { |
| 139 |
|
|
my $source_id = $image->source; |
| 140 |
|
|
my $src; |
| 141 |
|
|
if ( $source_id ) { |
| 142 |
|
|
if ( exists $SOURCES->{$source_id} ) { |
| 143 |
|
|
$src = $SOURCES->{$source_id}; |
| 144 |
|
|
} else { |
| 145 |
|
|
my $msrc = $MEDIASRC->{$msg->source}; |
| 146 |
|
|
$src = promosuite::Source->new ($keeper); |
| 147 |
|
|
$src->name( Convert::Cyrillic::cstocs('KOI8', 'UTF8', $msrc->name) ); |
| 148 |
|
|
$src->auth_id( $msrc->id ); |
| 149 |
|
|
$src->url( $msrc->url ); |
| 150 |
|
|
$src->status(1); |
| 151 |
|
|
$src->sections( $project->s_alias->{sources} ); |
| 152 |
|
|
#$src->store; |
| 153 |
|
|
#$SOURCES->{$msg->source} = $src; |
| 154 |
|
|
} |
| 155 |
|
|
} |
| 156 |
|
|
my $image_data = store_image( |
| 157 |
|
|
$image->name(), |
| 158 |
|
|
"/images/".$message->get_file_name(), |
| 159 |
|
|
$message, |
| 160 |
|
|
"pictures", |
| 161 |
|
|
[qw(240x240)] |
| 162 |
|
|
); |
| 163 |
|
|
if ($image_data) { |
| 164 |
|
|
@{%{$image_data->{'mini'}}}{'filename', 'width', 'height'} = @{%{$image_data->{'mini'}{'240x240'}}}{'filename', 'width', 'height'}; |
| 165 |
|
|
$image_data->{number} = ++$idx; |
| 166 |
|
|
if ( ref $src ) { |
| 167 |
|
|
$image_data->{sid} = $src->id; |
| 168 |
|
|
$image_data->{url} = $src->url; |
| 169 |
|
|
$image_data->{copyright} = Convert::Cyrillic::cstocs('KOI8', 'UTF8', $src->name); |
| 170 |
|
|
} |
| 171 |
|
|
$image_hash->{"image_".($idx)} = $image_data; |
| 172 |
|
|
} |
| 173 |
|
|
} |
| 174 |
|
|
if ($image_hash) { |
| 175 |
|
|
$image_hash->{"maxnumber"} = $idx; |
| 176 |
|
|
local $Data::Dumper::Indent = 0; |
| 177 |
|
|
$message->pictures(Dumper($image_hash)); |
| 178 |
|
|
} |
| 179 |
|
|
### /Store images |
| 180 |
|
|
$message->url( $msg->url ); |
| 181 |
|
|
$message->status(3); |
| 182 |
|
|
$message->store; |
| 183 |
|
|
} |
| 184 |
|
|
$last_id = $news[0]->id; |
| 185 |
|
|
$prjParam->media_last( $last_id ); |
| 186 |
|
|
$prjParam->store; |
| 187 |
|
|
} |
| 188 |
|
|
} |
| 189 |
|
|
|
| 190 |
|
|
sub store_image { |
| 191 |
|
|
my $url = shift || return; |
| 192 |
|
|
my $filename = shift || return; |
| 193 |
|
|
my $object = shift || return; |
| 194 |
|
|
my $field = shift || return; |
| 195 |
|
|
my $resolutions = shift || return; |
| 196 |
|
|
|
| 197 |
|
|
my $f = P::WebFetcher->new(); |
| 198 |
|
|
|
| 199 |
|
|
my $buffer = $f->fetch("http://www.rambler.ru/news".$url); |
| 200 |
|
|
|
| 201 |
|
|
return unless $buffer; |
| 202 |
|
|
|
| 203 |
|
|
my $suffix = $url =~ /(jpe?g|gif|png)$/i ? lc $1 : "bin"; |
| 204 |
|
|
|
| 205 |
|
|
my $filename_tmp = $state->{"tmp_dir"}."/".join("_", split("/", $filename)); |
| 206 |
|
|
my $fh_tmp = IO::File->new(">".$filename_tmp.".".$suffix) || die "Can't open temporary file"; |
| 207 |
|
|
|
| 208 |
|
|
syswrite $fh_tmp, $buffer, length $buffer; |
| 209 |
|
|
undef $fh_tmp; |
| 210 |
|
|
|
| 211 |
|
|
my $image_hash; |
| 212 |
|
|
|
| 213 |
|
|
@{%{$image_hash}}{"filename", "width", "height"} = ( |
| 214 |
|
|
$filename.".".$suffix, |
| 215 |
|
|
Image::Size::imgsize($filename_tmp.".".$suffix), |
| 216 |
|
|
); |
| 217 |
|
|
|
| 218 |
|
|
unless (Contenido::File::store($filename.".".$suffix, $filename_tmp.".".$suffix)) { |
| 219 |
|
|
print "NEWS LOADER WARNING: Can't store file! (name: ".$filename.".".$suffix.")\n"; |
| 220 |
|
|
return; |
| 221 |
|
|
} |
| 222 |
|
|
|
| 223 |
|
|
for my $resolution (@$resolutions) { |
| 224 |
|
|
my $c_line = $state->{"convert_binary"}." -geometry '".$resolution.">' -quality 80 ".$filename_tmp.".".$suffix." ".$filename_tmp.".".$resolution.".".$suffix; |
| 225 |
|
|
my $result = `$c_line 2>&1`; |
| 226 |
|
|
|
| 227 |
|
|
if (length $result > 0) { |
| 228 |
|
|
print "Contenido Error: При вызове '$c_line' произошла ошибка '$result' ($@)\n"; |
| 229 |
|
|
} |
| 230 |
|
|
|
| 231 |
|
|
@{%{$image_hash->{"mini"}{$resolution}}}{"filename", "width", "height"} = ( |
| 232 |
|
|
$filename.".".$resolution.".".$suffix, |
| 233 |
|
|
Image::Size::imgsize($filename_tmp.".".$resolution.".".$suffix), |
| 234 |
|
|
); |
| 235 |
|
|
|
| 236 |
|
|
unless (Contenido::File::store($filename.".".$resolution.".".$suffix, $filename_tmp.".".$resolution.".".$suffix)) { |
| 237 |
|
|
print "NEWS LOADER WARNING: Can't store file! (name: ".$filename.".".$resolution.".".$suffix.")\n"; |
| 238 |
|
|
} |
| 239 |
|
|
|
| 240 |
|
|
unlink $filename_tmp.".".$resolution.".".$suffix if -e $filename_tmp.".".$resolution.".".$suffix; |
| 241 |
|
|
} |
| 242 |
|
|
|
| 243 |
|
|
unlink $filename_tmp.".".$suffix if -e $filename_tmp.".".$suffix; |
| 244 |
|
|
|
| 245 |
|
|
return $image_hash; |
| 246 |
|
|
} |