私の趣味の一つに映画鑑賞がありますが、視聴後はtwitterに映画情報を投稿して備忘としています。
情報の取得にはBash snippetsのmoviesコマンドを使っていたのですが、ある時取得できなくなってしまいました。
Bash-Snippets - Useful BASH Scripts For Heavy Commandline Users
masashi@PC-ubuntu:~$ movies sorry to bother you ================================================== | Title: | Year: | Runtime: | Genre: | Director: | Actors: ==================================================
この様に空の値が返ってきます。
ソースをみたところomdb apiというサイトのAPIを叩いているようですが、APIキーの期限が切れてしまったのでしょうか。
手動で投稿でもいいんですが、せっかく取得〜投稿までのツールを作ったので取得部分を自作したいと思います。
APIキー申請
一日1000アクセスまでなら無料の様です。
簡単に用途を書いて申請。
OMDb API - The Open Movie Database
取得ツール
映画のデータはjsonで取得できますが、moviesコマンドはある程度成形して出力してくれます。
どう加工しましょうかね。
- 取得データ
ちょうど昨日見たのですがBad boysのデータを使います。
{"Title":"Bad Boys","Year":"1995","Rated":"R","Released":"07 Apr 1995","Runtime":"119 min","Genre":"Action, Comedy, Crime","Director":"Michael Bay","Writer":"George Gallo, Michael Barrie, Jim Mulholland","Actors":"Will Smith, Martin Lawrence, Lisa Boyle","Plot":"Two hip detectives protect a witness to a murder while investigating a case of stolen heroin from the evidence storage room from their police precinct.","Language":"English","Country":"United States","Awards":"4 nominations","Poster":"https://m.media-amazon.com/images/M/MV5BMGE1ZTQ0ZTEtZTEwZS00NWE0LTlmMDUtMTE1ZWJiZTYzZTQ2XkEyXkFqcGdeQXVyNTAyODkwOQ@@._V1_SX300.jpg","Ratings":[{"Source":"Internet Movie Database","Value":"6.9/10"},{"Source":"Rotten Tomatoes","Value":"42%"},{"Source":"Metacritic","Value":"41/100"}],"Metascore":"41","imdbRating":"6.9","imdbVotes":"246,119","imdbID":"tt0112442","Type":"movie","DVD":"12 Feb 2014","BoxOffice":"$65,807,024","Production":"Don Simpson/Jerry Bruckheimer Films","Website":"N/A","Response":"True"}
投稿ツールではmoviesの結果をさらにgrepしているのでキー毎に改行できれば行けそうですね。
といわけでコードはこちら
投稿のツールはpythonで作っているので取得ツールもpythonで作ります。
import requests import sys import json import configure import re KEY = configure.APIKEY url = 'http://www.omdbapi.com/' gettitle = '+'.join(sys.argv[1:]) getinfo = ''.join([url, '?apikey=', KEY, '&t=', gettitle]) def main(): movieinfo = requests.get(getinfo) dataload = json.loads(movieinfo.text) datadump = json.dumps(dataload, indent=0) shapedata = re.sub('"|,', "", datadump) print(shapedata) if __name__ == '__main__': main()
よくあるrequestsを使った取得です。
クエリ用引数
映画タイトルを引数で渡しますが数単語あるのがほとんどなのでjoinを咬ませてstringにしています。
gettitle = '+'.join(sys.argv[1:])
APIキー無しのクエリはこんな感じです。
http://www.omdbapi.com/?t=bad+boys
jsonの成形
最終的に改行して出力したいのですが、dumpsのindent=0でできました。
でも一回loadsで辞書型にしないと改行されませんでした。
こんな感じに出力されます。
ここまで来たらほぼmoviesコマンドの結果と同じですね。
(movies) masashi@PC-ubuntu:~/movietweet$ python getinfo.py bad boys | head { Title: Bad Boys Year: 1995 Rated: R Released: 07 Apr 1995 Runtime: 119 min Genre: Action Comedy Crime Director: Michael Bay Writer: George Gallo Michael Barrie Jim Mulholland Actors: Will Smith Martin Lawrence Lisa Boyle
最後に投稿用のシェルスクリプトを修正して完成です。
これは上記の結果をgrepしているだけなので割愛します。
動かしてみる
実行してみましょう。
(movies) masashi@PC-ubuntu:~/movietweet$ bash twmovieinfo.sh bad boys 2021-07-24に「Bad Boys」を視聴しました。 Infomation --- Year: 1995 Genre: Action Comedy Crime Director: Michael Bay Actors: Will Smith Martin Lawrence Lisa Boyle Production: Don Simpson/Jerry Bruckheimer Films --- 投稿しますか?: [y/n]y 投稿を完了しました。
2021-07-24に「Bad Boys」を視聴しました。
— masashi (@palomax69) 2021年7月24日
Infomation
---
Year: 1995
Genre: Action Comedy Crime
Director: Michael Bay
Actors: Will Smith Martin Lawrence Lisa Boyle
Production: Don Simpson/Jerry Bruckheimer Films
---
OK!
いいですね。
今までと同じ出力です。
ここだけ見たら中身が変わったなんてわからないですね。
まとめ
というわけで自作ツールでのAPI取得でした。
いままで使っていたツールが使えなくなったのは残念ですが仕方ないですね。
今回は自作なので突然エラーが起きても原因追求や修正はそんなに難しくないと思います。
あとはエラー判定等の処理を入れておかないとですね。
おまけ
本シリーズ
2020年上半期見た映画メモ & twitter取得ツール改修 - paloma blog
映画視聴ロガーを使って今年見た映画を振り返る - paloma blog