1 |
#!/usr/bin/perl |
---|
2 |
# |
---|
3 |
# Anime recording system foltia |
---|
4 |
# http://www.dcc-jpl.com/soft/foltia/ |
---|
5 |
# |
---|
6 |
# usage :getxml2db [long] |
---|
7 |
#しょぼいカレンダー<http://cal.syoboi.jp/>から番組データXMLを取得しfoltia DBにインポートする |
---|
8 |
# |
---|
9 |
#オプション |
---|
10 |
#long:2週間分取り込む。このモードで一日一回回せばよいでしょう。 |
---|
11 |
# |
---|
12 |
# DCC-JPL Japan/foltia project |
---|
13 |
# |
---|
14 |
# |
---|
15 |
|
---|
16 |
use LWP::Simple; |
---|
17 |
use Jcode; |
---|
18 |
use Time::Local; |
---|
19 |
use DBI; |
---|
20 |
use DBD::Pg; |
---|
21 |
use DBD::SQLite; |
---|
22 |
use Digest::MD5 qw(md5_hex); |
---|
23 |
|
---|
24 |
$path = $0; |
---|
25 |
$path =~ s/getxml2db.pl$//i; |
---|
26 |
if ($path ne "./"){ |
---|
27 |
push( @INC, "$path"); |
---|
28 |
} |
---|
29 |
require "foltialib.pl"; |
---|
30 |
|
---|
31 |
|
---|
32 |
$processes = &processfind("getxml2db.pl"); |
---|
33 |
if ($processes > 1 ){ |
---|
34 |
#print "process found:$processes\n"; |
---|
35 |
&writelog("getxml2db processes exist. exit:"); |
---|
36 |
exit; |
---|
37 |
}else{ |
---|
38 |
#print "process NOT found:$processes\n"; |
---|
39 |
&writelog("getxml2db Normal launch."); |
---|
40 |
} |
---|
41 |
|
---|
42 |
# http://sites.google.com/site/syobocal/spec/cal_chk-php |
---|
43 |
#if ($ARGV[0] eq "long"){ |
---|
44 |
# $uri="http://cal.syoboi.jp/cal_chk.php"; |
---|
45 |
# #$uri="http://syobocal.orz.hm/cal_chk.php"; |
---|
46 |
# &writelog("getxml2db use long mode."); |
---|
47 |
#}else{ |
---|
48 |
# $uri="http://cal.syoboi.jp/cal_chk.xml"; |
---|
49 |
# #$uri="http://syobocal.orz.hm/cal_chk.xml"; |
---|
50 |
#} |
---|
51 |
$uri = "http://cal.syoboi.jp/cal_chk.php?days="; |
---|
52 |
$uri .= ($ARGV[0] eq "long")? 14: 7; |
---|
53 |
|
---|
54 |
$dbh = DBI->connect($DSN,$DBUser,$DBPass) ||die $DBI::error;; |
---|
55 |
|
---|
56 |
$dbh->{AutoCommit} = 0; |
---|
57 |
|
---|
58 |
# If-Modified-Since使うように変更#2008/11/14 |
---|
59 |
my $CacheDir = '/tmp/shobocal'; |
---|
60 |
if (! -e $CacheDir) { |
---|
61 |
mkdir $CacheDir or die "cannot create $CacheDir: $!"; |
---|
62 |
} |
---|
63 |
my $cache = sprintf("%s/%s.xml", $CacheDir, Digest::MD5::md5_hex($uri)); |
---|
64 |
LWP::Simple::mirror($uri, $cache) or die "cannot get content from $uri"; |
---|
65 |
open(SHOBO, "<$cache"); |
---|
66 |
my (@line) = <SHOBO>; |
---|
67 |
close(SHOBO); |
---|
68 |
#my ($content) = get("$uri"); |
---|
69 |
#if ($content eq ""){ |
---|
70 |
#&writelog("getxml2db no responce from $uri, exit:"); |
---|
71 |
# exit;#しょぼかるが落ちているなど |
---|
72 |
#} |
---|
73 |
#my (@line) = split(/\n/, $content); |
---|
74 |
|
---|
75 |
foreach(@line){ |
---|
76 |
s/\xef\xbd\x9e/\xe3\x80\x9c/g; #wavedash |
---|
77 |
s/\xef\xbc\x8d/\xe2\x88\x92/g; #hyphenminus |
---|
78 |
s/&#([0-9A-Fa-f]{2,6});/(chr($1))/eg; #'遊戯王5D's'とかの数値参照対応を |
---|
79 |
|
---|
80 |
Jcode::convert(\$_,'euc','utf8'); |
---|
81 |
|
---|
82 |
#<ProgItem PID="21543" TID="528" StTime="20041114213000" EdTime="20041114220000" ChName="AT-X" Count="4" StOffset="0" SubTitle="いやだよ、サヨナラ…" Title="おとぎストーリー 天使のしっぽ" ProgComment=""/> |
---|
83 |
if (/^<ProgItem /){ |
---|
84 |
s/<ProgItem //i; |
---|
85 |
s/\"\/>/\" /i; |
---|
86 |
s/\"[\s]/\";\n/gio; |
---|
87 |
s/\'/\\'/gio; |
---|
88 |
s/\"/\'/gio; |
---|
89 |
#s/[\w]*=/\$item{$&}=/gio; |
---|
90 |
#s/\=}=/}=/gio; |
---|
91 |
s/(\w+)=/\$item{$1}=/gio;#by foltiaBBS |
---|
92 |
|
---|
93 |
#$item{PID}='21543'; |
---|
94 |
#$item{TID}='528'; |
---|
95 |
#$item{StTime}='20041114213000'; |
---|
96 |
#$item{EdTime}='20041114220000'; |
---|
97 |
#$item{ChName}='AT-X'; |
---|
98 |
#$item{Count}='4'; |
---|
99 |
#$item{StOffset}='0'; |
---|
100 |
#$item{SubTitle}='いやだよ、サヨナラ…'; |
---|
101 |
#$item{Title}='おとぎストーリー 天使のしっぽ'; |
---|
102 |
#$item{ProgComment}=''; |
---|
103 |
eval("$_"); |
---|
104 |
#Jcode::convert(\$item{Title},'euc'); |
---|
105 |
|
---|
106 |
$programtitlename = $item{Title}; |
---|
107 |
$programtitlename =~ s/\<\;/</gi; |
---|
108 |
$programtitlename =~ s/\>\;/>/gi; |
---|
109 |
$programtitlename =~ s/\&\;/\&/gi; |
---|
110 |
# $programtitle = $dbh->quote($programtitlename); |
---|
111 |
$programtitle = $programtitlename; |
---|
112 |
|
---|
113 |
#Jcode::convert(\$item{ChName},'euc'); |
---|
114 |
#Jcode::convert(\$item{SubTitle},'euc'); |
---|
115 |
|
---|
116 |
#$programSubTitle = $dbh->quote($item{SubTitle}); |
---|
117 |
$programSubTitle = $item{SubTitle}; |
---|
118 |
$programSubTitle =~ s/\<\;/</gi; |
---|
119 |
$programSubTitle =~ s/\>\;/>/gi; |
---|
120 |
$programSubTitle =~ s/\&\;/\&/gi; |
---|
121 |
# $programSubTitle = $dbh->quote($programSubTitle); |
---|
122 |
|
---|
123 |
$offsetmin = $item{StOffset}/60; |
---|
124 |
$edtime = &syobocaldate2foltiadate($item{EdTime}); |
---|
125 |
$sttime = &syobocaldate2foltiadate($item{StTime}); |
---|
126 |
$length = &calclength($sttime,$edtime); |
---|
127 |
$recstartdate = &calcoffsetdate($sttime ,$offsetmin ); |
---|
128 |
$recenddate = &calcoffsetdate($edtime ,$offsetmin ); |
---|
129 |
|
---|
130 |
$stationid = &getstationid($item{ChName}); |
---|
131 |
#サブタイトル追加------------------------------------------------- |
---|
132 |
#番組があるか確認 |
---|
133 |
$sth = $dbh->prepare($stmt{'getxml2db.1'}); |
---|
134 |
$sth->execute($item{TID}); |
---|
135 |
@titlecount= $sth->fetchrow_array; |
---|
136 |
|
---|
137 |
if ($titlecount[0] == 0){ |
---|
138 |
#なければ追加 |
---|
139 |
|
---|
140 |
#200412012359 |
---|
141 |
$nomalstarttime = substr($sttime,8,4); |
---|
142 |
|
---|
143 |
$sth = $dbh->prepare($stmt{'getxml2db.2'}); |
---|
144 |
$oserr = $sth->execute($item{TID}, $programtitle, '', $nomalstarttime, $length, '', '', 3, 1, '', ''); |
---|
145 |
&writelog("getxml2db ADD TV Progtam:$item{TID}:$programtitle"); |
---|
146 |
}else{ |
---|
147 |
#2006/2/26 |
---|
148 |
#あったら、タイトル確認して |
---|
149 |
$sth = $dbh->prepare($stmt{'getxml2db.3'}); |
---|
150 |
$sth->execute($item{TID}); |
---|
151 |
@titlearray = $sth->fetchrow_array; |
---|
152 |
#更新などされてたらupdate |
---|
153 |
#print "$titlearray[0] / $programtitle\n"; |
---|
154 |
if ($titlearray[0] ne "$programtitlename" ){ |
---|
155 |
$sth = $dbh->prepare($stmt{'getxml2db.4'}); |
---|
156 |
$oserr = $sth->execute($programtitle, $item{TID}); |
---|
157 |
&writelog("getxml2db UPDATE TV Progtam:$item{TID}:$programtitle"); |
---|
158 |
}#end if update |
---|
159 |
}# end if TID |
---|
160 |
|
---|
161 |
|
---|
162 |
#PIDがあるか確認 |
---|
163 |
$sth = $dbh->prepare($stmt{'getxml2db.5'}); |
---|
164 |
$sth->execute($item{'TID'}, $item{'PID'}); |
---|
165 |
@subticount= $sth->fetchrow_array; |
---|
166 |
if ($subticount[0] >= 1){ |
---|
167 |
#PIDあったら上書き更新 |
---|
168 |
#ここでこんなエラー出てる |
---|
169 |
# DBD::Pg::st execute failed: ERROR: invalid input syntax for type bigint: "" at /home/foltia/perl/getxml2db.pl line 147. |
---|
170 |
#UPDATE foltia_subtitle SET stationid = '42',countno = '8',subtitle = '京都行きます' ,startdatetime = '200503010035' ,enddatetime = '200503010050',startoffset = '0' ,lengthmin = '15' WHERE tid = '550' AND pid = '26000' |
---|
171 |
if ($item{Count} == ""){ |
---|
172 |
$sth = $dbh->prepare($stmt{'getxml2db.6'}); |
---|
173 |
$oserr = $sth->execute($stationid, undef, $programSubTitle, $recstartdate, $recenddate, $offsetmin, $length, $item{'TID'}, $item{'PID'}); |
---|
174 |
}else{ |
---|
175 |
$sth = $dbh->prepare($stmt{'getxml2db.7'}); |
---|
176 |
$oserr = $sth->execute($stationid, $item{'Count'}, $programSubTitle, $recstartdate, $recenddate, $offsetmin, $length, $item{'TID'}, $item{'PID'}); |
---|
177 |
} |
---|
178 |
}else{ |
---|
179 |
#なければ追加 |
---|
180 |
|
---|
181 |
#こっちに入る時刻はオフセットされた時刻! |
---|
182 |
#そのままキューに入る形で |
---|
183 |
if ($item{Count} eq ""){ |
---|
184 |
$sth = $dbh->prepare($stmt{'getxml2db.8'}); |
---|
185 |
$oserr = $sth->execute($item{'PID'}, $item{'TID'}, $stationid, undef, $programSubTitle, $recstartdate, $recenddate, $offsetmin, $length); |
---|
186 |
}else{ |
---|
187 |
$sth = $dbh->prepare($stmt{'getxml2db.9'}); |
---|
188 |
$oserr = $sth->execute($item{'PID'}, $item{'TID'}, $stationid, $item{'Count'}, $programSubTitle, $recstartdate, $recenddate, $offsetmin, $length); |
---|
189 |
} |
---|
190 |
} |
---|
191 |
|
---|
192 |
#print "$DBQuery\n\n\n"; |
---|
193 |
#debug 20050803 |
---|
194 |
#&writelog("getxml2db $DBQuery"); |
---|
195 |
|
---|
196 |
|
---|
197 |
}#if |
---|
198 |
}#foreach |
---|
199 |
|
---|
200 |
$oserr = $dbh->commit; |
---|
201 |
## $dbh->disconnect(); |
---|