見出し画像

#TW-12 Twitterに自動翻訳 後編 (Tweet translation app2)

(English follows)
前回の記事では、ツイッターで異文化理解(?)をテーマにタイムラインを全翻訳するというアプリを作ってみたという話をしました(ここ)。そしてあまりWEBプログラミングをやったことがないので、それに挑戦したという話も。。。無惨なデザインになりましたがギリ、自分で使うには十分な感じになりました。現在ちょこちょこ見てはほうほう、インドネシアの方々はこんなことをツイートしているのか、ふむふむと。なんというか生活(?)を覗き見ている感じです。さて今回は何しろ素人がまたまた苦労したという話を。。。

目次
1. Twitter API のバージョン
2. Tweepyの"extended"オプション
3. Tweetpyのデータ解析
4. 引用元の情報

さて今回ツイッターのデータを取得するにはTweepy というPythonのライブラリを使ったのですが、基本的にTweepyの仕様を理解するのに時間がかかり、結局見つからなかったデータは自分で取りに行くというところで時間がかかったという感じでした(素人感強し)。なかなか簡単にはいかないですね。。。

実は最初はTwitterのAPIを直接使おうとしていたのですが、自分が普段使っていたTwitter API2の方はなんと、、、フォローしているかたのツイートをタイムラインに表示するための機能が見つからないのです。そう、僕は古いAPIには入っているその機能が新APIではまだ作られていないことを知らなかったのです。ここで時間を消費。結局古い方を使うならライブラリを使おうとTweepyを選択しました。ふー。

次に、最初はいくら探しても、画像や動画などツイートに添付されているもののURLが見当たらず、あれあれと探していました。すぐに公式ドキュメントに戻ればよかったのもの、ライブラリが返してくるデータが多くて、どこかに入っているのではないかとデータの中身を漁るというしまつ。どうもこのライブラリには extendedというオプションがあってそれを使わないと、URLが返ってこないと分かるまでにまでに時間を消費。。。使いたい皆様はお気をつけて。

次に、リツイートなのか、引用があるのかなどなどを示す情報があったりなかったりで、その整理にも苦労しました。引用にはフラグみたいなものがあったのですが、リツイートにはなかったりします。結局テキストの頭に「RT」とついていたら、リツイート情報を探しに行くというアナログな方法も使いました。

最後に引用されているツイートの場合、引用元の情報(サムネイルとか、記事のタイトルとか)をツイッターアプリは表示しているのですが、その情報はextendedにしても取れなそうでした(もし見逃していたら教えてください。。。)なので、引用元のページにOGPタグ、なんというかページを作った方がSNS表示用(?)に付け加えてくれている情報を拾いに行って、引用情報として表示するということを行いました。

ちなみに、最後の最後に一番苦労したのは、サーバーの設定ファイルを間違えていて(.htaccess1ファイルの改行コードがcrlfになっていた)いたことに気が付かずページがサーバー上で動かなかった部分であることは内緒。開発者のみなさまいつも本当にご苦労さまです。。。

I mentioned in the last post (Here) that I wanted to use Twitter to understand other cultures and develop a tool to show auto translated timeline. Also I told that one of my intention is to learn Web programming which I haven't tried much. Well although I'm not satisfied with the design, it is now enough to comfortably read through timelines with languages which I don't understand at all. Now I'm enjoying tweets which Indonesian people tweeting every day with it. Thanks google translation… Ok, today's topic is to explain where I actually got confused about this development… (A lot) 

Contents
1. Twitter API version issue
2. Tweepy "extended" option
3. Analyzing Tweepy data
4. Information about quoted source

I used python library "Tweepy" for this but initially I tried to use Twitter API which (I thought) I'm familiar with. But I didn't know that Twitter API2 which I normally use still does not have a function to pull out timeline data. I didn't know that it has not been developed yet. So it took me soo long time to look through API2 documents to look for the function. Yes, I wasted long time here. Then after finding out that I have to use the old API, I found using Tweepy is much easier for it. Ummmm.

Secondary, I couldn't find few information which I need to show tweets like how they are shown on the twitter app. They are URLs for attached videos and photos. I was supposed to go back to the official documents but the data library provide is huge and I looked at them one by one, believing that they are there… In the end, I found that I have to use "extended" options to call the library to get those URLs. Wasted time again (Stupid me).

Thirdly, It really took me long time to organize data which Tweepy provides to form tweet structure. For example, I couldn't find any data which shows that this tweet is the "retweet" (Please let me know if there is any…). In the end, I judged it by get first few digit of tweet if that starts from "RT" or not. It doesn't sound so smart…

Last of all, I found that there are no information about quoted article in Tweepy data. When a tweet quotes an article, twitter app shows thumbnails, title etc under the tweet. Even the extended option doesn't provide those information so I had to open the quoted page one by one to get OGP tag which is … basically Information which publishers prepare for social network. 

Again coding itself didn't take as long time as analyzing data which I'm not sure it is the right thing or not… By the way, I want to keep this secret that the worst thing I struggled this time was the server setting. I uploaded a wrong setting file (.htaccess file had "crlf" not "LF" for line feeding code) and it was the reason why my python script didn't work when I uploaded them to the server… Full of my respect to software engineers who do this everyday.



この記事が気に入ったらサポートをしてみませんか?