37,720
社区成员
发帖
与我相关
我的任务
分享
#!/usr/bin/perl -w
use strict;
use warnings;
use DBI;
use XML::Smart;
use Data::Dumper;
use Encode qw/encode decode/;
use Digest::MD5 qw(md5 md5_hex md5_base64);
use MIME::Base64 qw(encode_base64 decode_base64);
use LWP::UserAgent;
require "db.pl";
my $dbh = &get_joy_db;
#my $ii = 0;
#my $jj = 0;
my %db_joyid;
my %all_urls = ("电视剧"=>"http://xxxxx/get_list.php?channelid=58",
"电影"=>"http://xxxxx/get_list.php?channelid=51",
"播客"=>"http://xxxxx/get_video_list.php?channelid=1",
"娱乐"=>"http://xxxxx/get_video_list.php?channelid=3");
while(my ($cat, $url) = each %all_urls){
my @content_url = return_url_update($url);
foreach my $u (@content_url) {
print $cat,$u."\n";
my $XML = XML::Smart->new( $u, 'XML::Smart::Parser' );
my @items = reverse(@{ $XML->{root}{videolist}{video}});
foreach my $item (@items) {
print "\n\n\n";
my $title = $dbh->quote(&encode("GBK",$item->{title}));
#print $title."\n";
my $id = &encode("GBK",$item->{id});
my $intro = $dbh->quote(&encode("GBK",$item->{desc}));
my $tags = $dbh->quote(&encode("GBK",$item->{tag}));
my $play_url = &encode("GBK",$item->{play_url});
my $src_image = &encode("GBK",$item->{img_url});
my $time = &encode("GBK",$item->{totaltime});
my $image = handleJoyPic($src_image,"joy");
$image = "http://cnimg.realnetworks.com.cn/guide/mdb/".$image;
my ($curdate,$curtime)=get_time();
print $cat."===>".$title."=>".$id."=>".$image."\n";
my $sql_chk = "select joyid from joy where joyid = $id";
my $sth_chk = &execsql($dbh, $sql_chk);
while ( my ($joyid) = $sth_chk->fetchrow() ) {
$db_joyid{$joyid} = $joyid;
}
if ($db_joyid{$id}) {
print "\n\t"."数据库数据重复:".$title."\n";
#$jj++;
}else{
my $sql_insert = "insert into joy (`title`,`intro`,`tags`,`image_1`,`src_image`,`joyid`,`category`,`length`,`createdate`,`createtime`,`playurl`) values ($title,$intro,$tags,'$image','$src_image','$id','$cat','$time','$curdate','$curtime','$play_url');";
my $sth_insert = &execsql($dbh, $sql_insert);
print "\n\t"."数据插入成功:".$title."\n";
$sth_insert->finish();
#$ii++;
}
$sth_chk->finish();
undef %db_joyid;
}
}
}
#print "总共插入$ii条数据,另有$jj条数据重复!";
sub get_time {
my ($sec,$min,$hour,$day,$mon,$year)=localtime(time);
$sec = ($sec < 10)?"0$sec":$sec;
$min = ($min < 10)?"0$min":$min;
$hour = ($hour < 10)?"0$hour":$hour;
$day = ($day < 10)?"0$day":$day;
$mon = ($mon < 9)?"0".($mon+1):($mon+1);
$year += 1900;
return "$year-$mon-$day", "$hour:$min:$sec";
}
sub get_joy_db {
my ( $db_host, $db_name, $db_user, $db_pwd, $db_port, $db_timeout ) =
( "xxxxxxxxxxxxxxx", "movie", "root", "xxxxxxxxx", 9002, 0 );
my $dbh =
&db_open( $db_host, $db_name, $db_user, $db_pwd, $db_port, $db_timeout );
return $dbh;
}
sub file_name {
my ($filepath) = @_;
my $pos = rindex( $filepath, "/" );
if ( $pos == -1 ) {
return $filepath;
}
else {
return substr( $filepath, $pos + 1 );
}
}
sub md5_filename{
my ($filename) = @_;
#print $filename;
my $pos = rindex($filename,".");
#print $pos."\n";
my $prename = substr($filename,0,$pos);
my $extname = substr($filename,$pos+1);
return md5_hex($prename).".".$extname;
}
sub handleJoyPic {
my ($arg,$partner) = @_;
if ( $arg !~ /http:\/\/.*/isg ) {
return "";
}
my $img_filename = file_name($arg);
my $md5_filename = md5_filename($img_filename);
my $hash = md5_hex($arg);
my $len = length($hash);
my $ascii = 0;
for ( my $i = 0 ; $i < $len ; $i++ ) {
$ascii += ord( substr( $hash, $i, 1 ) );
}
my $path = sprintf( "%d/%d", $ascii % 128, $ascii % 53 );
my $fixed_img_path = "$partner/$path/$md5_filename";
my $cmd = "mkdir -p $partner/$path/;wget $arg;mv $img_filename $partner/$path/$md5_filename";
$cmd .=";/usr/local/bin/rsync -vtp --timeout=120 -R $fixed_img_path xxxxxxxxx::img_mdb";
#print $cmd."\n";
system("$cmd");
#print $fixed_img_path."\n";
return $fixed_img_path;
}
sub return_url_update{
my ($url,$num) = @_;
if (!$num) {
$num = 50;
}
$url = $url."&start=0&num=".$num;
return $url;
}