再生時間を取得したい
先日作ったcitypopをたれ流してくれるツールに再生時間をつけたい。
(あわよくばorderで降順か昇順にしたい。)
Videosというメソッドを使えばできそう。
videos().list()を使ってデータを取得する。
part="contentDetails"を設定しないとdurationが取得できないところが苦労した。
def youtube_search(options): youtube = build( YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY ) video_list_res = youtube.videos().list( part="contentDetails", id="GOVPpLtJUIg" ).execute() videos_duraition = "" # videos_url = [] for video_result in video_list_res.get("items", []): videos_duraition = video_result['contentDetails']['duration'] print(videos_duraition)
取得してきた結果
PT45M18S
これで時間が取得できた。
ISO 8601 に従った記載方式なのでHH:MM:SSの形にしたい。
簡単そうだったのでparserを自作した。
#! /usr/bin/env python class iosPraser: def iso_parser(target_str): target_str = target_str.replace('PT', '') target_str = target_str.replace('H', ':') target_str = target_str.replace('M', ':') result_str = target_str.replace('S', '') return result_str
試しにpythonコンソールで打ってテストする。
>>> import iosparser >>> a="PT1H23M31S" >>> str=iosparser.iosPraser.iso_parser(a) >>> print(str) 1:23:31
おぉちゃんと変換されたので感動!
本モジュールに移して結果を見てみるとapiから返ってくるdiurationにSがつかないものもあるらしい。
PT32M
ちょっとprserの中身を使いしてSがない場合は文字列の最後に「00」をつけるようにする。
#! /usr/bin/env python import re class iosParser: def iso_parser(target_str): result_str = target_str.replace('PT', '') result_str = result_str.replace('H', ':') result_str = result_str.replace('M', ':') result_str = result_str.replace('S', '') if not re.search('S', target_str): result_str = result_str + "00" return result_str
pythonコンソールで打ってテストする。
>>> import iosparser >>> parse = iosparser.iosParser >>> str = "35M" >>> atr = parse.iso_parser(str) >>> print(atr) 35:00
OKだったので本モジュールに組み込んで実行してみる。
NIGHT CITY CityPop シティポップ 80s Japanese Mix 44:25 Japanese 80s City Pop Compilation 32:00 warm nights in tokyo [ city pop/ シティポップ ] 45:18
print結果はOKだったので実際再生できるようにさせる。
再生できて再生時間を付けられた!
特に大幅な改良はしていないけどメインの処理のソースコード
def video_duration_search(videoid): youtube = build( YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY ) video_list_res = youtube.videos().list( part="contentDetails", id=videoid ).execute() videos_duraition = "" for video_result in video_list_res.get("items", []): videos_duraition = video_result['contentDetails']['duration'] videotime = iosparser.iosParser.iso_parser(videos_duraition) return videotime def youtube_search(options): youtube = build( YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY ) search_response = youtube.search().list( q=options.q, part="id,snippet", maxResults=options.max_results, type="video", videoDuration="long", ).execute() videos_ids = [] videos_titles = [] videos_url = [] for search_result in search_response.get("items", []): if search_result["id"]["kind"] == "youtube#video": videos_titles.append("%s" % search_result["snippet"]["title"]) videos_url.append("https://www.youtube.com/watch?v=%s" % search_result["id"]["videoId"]) videos_ids.append("%s" % search_result["id"]["videoId"]) videotime_arr = [] for videoid in videos_ids: videotime = video_duration_search(videoid) videotime_arr.append(videotime) df = pd.DataFrame(index=[], columns=[]) df["title"] = videos_titles df["url"] = videos_url df["video_time"] = videotime_arr print(test_color.Color.GREEN + "♫ Let's Play CITYPOP ♫" + test_color.Color.END) for title, url, video_time in zip(df['title'], df['url'], df['video_time']): print(test_color.Color.PURPLE + "Now Playing is ♫♫" + test_color.Color.END) print(test_color.Color.PURPLE + ("[%s] %s" % (title, video_time)) + test_color.Color.END) webbrowser.open(url, new=1) print(test_color.Color.RED + ("Press enter to listen to the next song!") + test_color.Color.END) get_key = input() print(test_color.Color.BLUE + ("Finished! See you later Zzz...") + test_color.Color.END)
次はリストを表示できるようにしたいな。