pepoとネットワークを語ろう

40年前からこれまでとこれからのネットワークを語る

Linuxスクリプトでpodcastのmp3ファイルをゲット!

2010-12-06 07:48:28 | Linux

itunesを使っていると、podcastで無料のニュースをMP3で聞けると知って、暫く前からゲットMP3プレヤーで聞いている
しかし、毎回itunesを立ち上げるのも邪魔くさいし、たまの休みにはゲットし損ねるので、linuxで少し手抜き気味ではあるが頭の
体操としてスクリプトを書いてみたよ、全然しらんかったがitunesのpodcastのURLをコピーしてwgetするとこんな中身

[root@~]# wget http://www3.nhk.or.jp/rj/podcast/rss/english.xml
--2010-12-03 12:26:16--  http://www3.nhk.or.jp/rj/podcast/rss/english.xml
Resolving www3.nhk.or.jp... 60.254.185.158, 60.254.185.184
Connecting to www3.nhk.or.jp|60.254.185.158|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1503 (1.5K) [text/xml]
Saving to: `english.xml'

100%[================================================>] 1,503       --.-K/s   in 0.002s

2010-12-03 12:26:16 (730 KB/s) - `english.xml' saved [1503/1503]

wgetでセーブしたenglish.xmlの内容を確認
[root@~]# cat english.xml
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:itunes="http://www.itunes.com/DTDs/Podcast-1.0.dtd" version="2.0">
 <channel>
  <copyright>NHK (Japan Broadcasting Corporation)</copyright>
  <description>This is the latest news in English from NHK WORLD RADIO JAPAN. This service is updated twice a day (3 times on weekends). For more information, please go to http://www.nhk.or.jp/english/.</description>
  <itunes:subtitle>This is the latest news in English from NHK WORLD RADIO JAPAN. This service is updated twice a day (3 times on weekends). For more information, please go to http://www.nhk.or.jp/english/.</itunes:subtitle>
  <itunes:author>NHK (Japan Broadcasting Corporation)</itunes:author>
  <itunes:image href="http://www.nhk.or.jp/rj/images/english.jpg"></itunes:image>
  <language>en</language>
  <link>http://www.nhk.or.jp/english/</link>
  <lastBuildDate>Fri, 03 Dec 2010 19:22:09 +0900</lastBuildDate>
  <title>NW English News - NHK WORLD RADIO JAPAN</title>
  <itunes:category text="News"></itunes:category>
  <item>
   <enclosure url="http://www9.nhk.or.jp/rj/podcast/mp3/20101203190100_1_1_english.mp3" length="7202442" type="audio/mpeg"></enclosure>
   <title>NHK WORLD RADIO JAPAN - English News at 19:01 (JST), December 3</title>
   <itunes:author>NHK (Japan Broadcasting Corporation)</itunes:author>
   <itunes:duration>15:00</itunes:duration>
   <itunes:category text="News"></itunes:category>
   <pubDate>Fri, 03 Dec 2010 19:22:09 +0900</pubDate>
  </item>
 </channel>
</rss>

       

[root@~]#
必要な行は、enclosure url="http://www9.nhk.or.jp/rj/podcast/mp3/20101202210100_1_1_english.mp3" だけなので、これをwgetすれば良い
crontabに登録すれば定期的に更新されるニュースなどのmp3ファイルを自動で取得できるよ
[root@~]# cat cnn.sh
#!/bin/sh
# licence GPLv2 ; this scripts designed by IZAMUKARERA 2010.12.3
# get podcast mp3
#XML=http://www3.nhk.or.jp/rj/podcast/rss/english.xml
XML=http://www.cnn.com/services/podcasting/newscast/rss.xml
DIR=/tmp/music
RAND=`echo -e $RANDOM`
TMP=/$DIR/${RAND}.xml
[ ! -e $DIR ] && mkdir -p $DIR
wget $XML -O $TMP

URL=`cat $TMP |awk '/enclosure url=/{split($0,I,"\"");{printf I[2]" "} }'`
for FIL in $URL
do
  MP3=`echo $FIL |awk '{s=split($0,I,"/"); printf I[s]}'`
  [ -f $DIR/$MP3 ] && continue
  wget $FIL -O $DIR/$MP3
  [ ! -s $DIR/$MP3 ] && rm -f $DIR/$MP3
done

rm -f $TMP

でわ、実際に起動してみると
[root@~]# ./cnn.sh
--2010-12-03 12:36:02--  http://www.cnn.com/services/podcasting/newscast/rss.xml
Resolving www.cnn.com... 157.166.224.25, 157.166.224.26, 157.166.226.25, ...
Connecting to www.cnn.com|157.166.224.25|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 6564 (6.4K) [application/xml]
Saving to: `//tmp/music/21468.xml'

100%[===============================================================>] 6,564       34.1K/s   in 0.2s

2010-12-03 12:36:03 (34.1 KB/s) - `//tmp/music/21468.xml' saved [6564/6564]

--2010-12-03 12:36:03--  http://www.podtrac.com/pts/redirect.mp3/podcasts.cnn.net/cnn/services/podcasting/newscast/audio/2010/12/02/CNN-News-12-02-10-10PM.mp3
Resolving www.podtrac.com... 69.16.232.36
Connecting to www.podtrac.com|69.16.232.36|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://podcasts.cnn.net/cnn/services/podcasting/newscast/audio/2010/12/02/CNN-News-12-02-10-10PM.mp3 [following]
--2010-12-03 12:36:04--  http://podcasts.cnn.net/cnn/services/podcasting/newscast/audio/2010/12/02/CNN-News-12-02-10-10PM.mp3
Resolving podcasts.cnn.net... 202.229.2.98, 202.229.2.82
Connecting to podcasts.cnn.net|202.229.2.98|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1044773 (1020K) [audio/mpeg]
Saving to: `/tmp/music/CNN-News-12-02-10-10PM.mp3'

100%[===============================================================>] 1,044,773    359K/s   in 2.8s

2010-12-03 12:36:07 (359 KB/s) - `/tmp/music/CNN-News-12-02-10-10PM.mp3' saved [1044773/1044773]

--2010-12-03 12:36:07--  http://www.podtrac.com/pts/redirect.mp3/podcasts.cnn.net/cnn/services/podcasting/newscast/audio/2010/12/02/CNN-News-12-02-10-9PM.mp3
Resolving www.podtrac.com... 69.16.232.36
Connecting to www.podtrac.com|69.16.232.36|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://podcasts.cnn.net/cnn/services/podcasting/newscast/audio/2010/12/02/CNN-News-12-02-10-9PM.mp3 [following]
--2010-12-03 12:36:07--  http://podcasts.cnn.net/cnn/services/podcasting/newscast/audio/2010/12/02/CNN-News-12-02-10-9PM.mp3
Resolving podcasts.cnn.net... 202.229.2.98
Connecting to podcasts.cnn.net|202.229.2.98|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1044145 (1020K) [audio/mpeg]
Saving to: `/tmp/music/CNN-News-12-02-10-9PM.mp3'

100%[===============================================================>] 1,044,145    361K/s   in 2.8s

2010-12-03 12:36:10 (361 KB/s) - `/tmp/music/CNN-News-12-02-10-9PM.mp3' saved [1044145/1044145]

--2010-12-03 12:36:10--  http://www.podtrac.com/pts/redirect.mp3/podcasts.cnn.net/cnn/services/podcasting/newscast/audio/2010/12/02/CNN-News-12-02-10-8PM.mp3
Resolving www.podtrac.com... 69.16.232.36
Connecting to www.podtrac.com|69.16.232.36|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://podcasts.cnn.net/cnn/services/podcasting/newscast/audio/2010/12/02/CNN-News-12-02-10-8PM.mp3 [following]
--2010-12-03 12:36:11--  http://podcasts.cnn.net/cnn/services/podcasting/newscast/audio/2010/12/02/CNN-News-12-02-10-8PM.mp3
Resolving podcasts.cnn.net... 202.229.2.98
Connecting to podcasts.cnn.net|202.229.2.98|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1043727 (1019K) [audio/mpeg]
Saving to: `/tmp/music/CNN-News-12-02-10-8PM.mp3'

100%[===============================================================>] 1,043,727    361K/s   in 2.8s

2010-12-03 12:36:13 (361 KB/s) - `/tmp/music/CNN-News-12-02-10-8PM.mp3' saved [1043727/1043727]

--2010-12-03 12:36:14--  http://www.podtrac.com/pts/redirect.mp3/podcasts.cnn.net/cnn/services/podcasting/newscast/audio/2010/12/02/CNN-News-12-02-10-7PM.mp3
Resolving www.podtrac.com... 69.16.232.36
Connecting to www.podtrac.com|69.16.232.36|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://podcasts.cnn.net/cnn/services/podcasting/newscast/audio/2010/12/02/CNN-News-12-02-10-7PM.mp3 [following]
--2010-12-03 12:36:14--  http://podcasts.cnn.net/cnn/services/podcasting/newscast/audio/2010/12/02/CNN-News-12-02-10-7PM.mp3
Resolving podcasts.cnn.net... 202.229.2.98
Connecting to podcasts.cnn.net|202.229.2.98|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1047280 (1023K) [audio/mpeg]
Saving to: `/tmp/music/CNN-News-12-02-10-7PM.mp3'

100%[===============================================================>] 1,047,280    360K/s   in 2.8s

2010-12-03 12:36:17 (360 KB/s) - `/tmp/music/CNN-News-12-02-10-7PM.mp3' saved [1047280/1047280]

--2010-12-03 12:36:17--  http://www.podtrac.com/pts/redirect.mp3/podcasts.cnn.net/cnn/services/podcasting/newscast/audio/2010/12/02/CNN-News-12-02-10-6PM.mp3
Resolving www.podtrac.com... 69.16.232.36
Connecting to www.podtrac.com|69.16.232.36|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://podcasts.cnn.net/cnn/services/podcasting/newscast/audio/2010/12/02/CNN-News-12-02-10-6PM.mp3 [following]
--2010-12-03 12:36:17--  http://podcasts.cnn.net/cnn/services/podcasting/newscast/audio/2010/12/02/CNN-News-12-02-10-6PM.mp3
Resolving podcasts.cnn.net... 202.229.2.98
Connecting to podcasts.cnn.net|202.229.2.98|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1043309 (1019K) [audio/mpeg]
Saving to: `/tmp/music/CNN-News-12-02-10-6PM.mp3'

100%[===============================================================>] 1,043,309    362K/s   in 2.8s

2010-12-03 12:36:20 (362 KB/s) - `/tmp/music/CNN-News-12-02-10-6PM.mp3' saved [1043309/1043309]

--2010-12-03 12:36:20--  http://www.podtrac.com/pts/redirect.mp3/podcasts.cnn.net/cnn/services/podcasting/newscast/audio/2010/12/02/CNN-News-12-02-10-5PM.mp3
Resolving www.podtrac.com... 69.16.232.36
Connecting to www.podtrac.com|69.16.232.36|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://podcasts.cnn.net/cnn/services/podcasting/newscast/audio/2010/12/02/CNN-News-12-02-10-5PM.mp3 [following]
--2010-12-03 12:36:21--  http://podcasts.cnn.net/cnn/services/podcasting/newscast/audio/2010/12/02/CNN-News-12-02-10-5PM.mp3
Resolving podcasts.cnn.net... 202.229.2.98
Connecting to podcasts.cnn.net|202.229.2.98|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1044981 (1020K) [audio/mpeg]
Saving to: `/tmp/music/CNN-News-12-02-10-5PM.mp3'

100%[===============================================================>] 1,044,981    361K/s   in 2.8s

2010-12-03 12:36:24 (361 KB/s) - `/tmp/music/CNN-News-12-02-10-5PM.mp3' saved [1044981/1044981]

[root@~]# ll /tmp/music/
total 6180
-rw-r--r-- 1 root root 1044773 12月  3 12:11 CNN-News-12-02-10-10PM.mp3
-rw-r--r-- 1 root root 1044981 12月  3 07:58 CNN-News-12-02-10-5PM.mp3
-rw-r--r-- 1 root root 1043309 12月  3 08:58 CNN-News-12-02-10-6PM.mp3
-rw-r--r-- 1 root root 1047280 12月  3 09:58 CNN-News-12-02-10-7PM.mp3
-rw-r--r-- 1 root root 1043727 12月  3 10:58 CNN-News-12-02-10-8PM.mp3
-rw-r--r-- 1 root root 1044145 12月  3 11:58 CNN-News-12-02-10-9PM.mp3
おーと、mp3ファイルがぎょうさんゲットできとるわ

pepo