TEDの動画と台本をダウンロードする
作ってみた。
pythonでプログラム組むのは初めてなので、python的におかしなコードになってると思います。
普段使ってるjavaの癖で、やりたいことはとにかくメソッド分割してしまいたくなる…。
import sys import re import urllib2 url = r"http://www.ted.com/talks/lang/ja/damon_horowitz_philosophy_in_prison.html" def get_htmlsource(url): fp = urllib2.urlopen(url) html = fp.read() fp.close() return html def get_title(url): html = get_htmlsource(url) return re.search(r'<title>([^<]+)</title>',html).group(1) def get_transcript(url): html = get_htmlsource(url) p = re.compile(r'<a href="[0-9]*" class="transcriptLink">([^<]+)</a>') return p.findall(html) def make_transcript_data(url): t_list_ja = get_transcript(url) url_en = re.sub(r'/ja/', r'/en/', url) t_list_en = get_transcript(url_en) t_list = [] for i in range(len(t_list_ja)): t_list.append(t_list_ja[i] + '\n') t_list.append(t_list_en[i] + '\n') t_list.append('\n') return t_list def write(title,t_list,media): write_transcript(title,t_list) write_media(title,media) def write_transcript(title,t_list): f = open(r'/Users/******/Desktop/' + title + r'.txt','w') for i in range(len(t_list)): t_list[i] = t_list[i] + '\n' f.writelines(t_list) f.close() def write_media(title,media): f = open(r'/Volumes/******/iTunes/Movies/TED/' + title + r'.mp4', 'w') f.write(media) f.close() def get_media_url(html): return re.search(r'<meta property="og:video" content="([^"]+)" />',html).group(1) def get_media(url): media_url = get_media_url(urllib2.urlopen(url).read()) return urllib2.urlopen(media_url).read() t_list = make_transcript_data(url) media = get_media(url) write(get_title(url),t_list,media) print r'***** finish *****'
これでダウンロードしたファイルをiTunesに登録するコード組みたいんだけど、色々ググッてみてもそれらしい情報は見つからず…。
VBScriptだけど、これはいけるか?
vbscript - Programmatically add converted video to iTunes using SDK - Stack Overflow