Python3.10で任意(他人)のアカウントのTweet分析(時間帯、曜日ごとのTweet頻度等)

Twitter Tweet分析 whotwi Python Tweet頻度 廃人 無職


whotwiは他人のアカウントも含めて、時間帯、曜日ごとのTweet頻度などを見ることができるが、
他人のアカウントを入力してみると、

「このユーザーは、whotwi には表示しない設定にしています」

と、公開アカウントでもwhotwiの設定で非表示にしている人がいる。

ja.whotwi.com


現在、whotwi代用のWebサービスは無いように思われる。

TwitterAPIは任意のアカウントのtweetを取得できるので、自力で収集すれば同等の情報を取得できるはずである。
GitHubで探したが、Pythonで使えそうなプログラムは以下のものくらいだった。

github.com

4年前が最終更新日で、Python3.10に対応していない箇所があるようなので(Python3.7までのようだ)、
以下に対応策を書いておく。

tweets_analyzer の準備

github.com
tweets_analyzerのプロジェクトファイルをダウンロードした後、

pip install -r requirements.txt

上記のとおり、必要なパッケージをインストールして、
secrets.py に自分のTwitterAPIキー等を書き込む。

Python3.10 への対応

secrets.pyというファイル名がNumpyのものと競合するようなので、ファイル名を任意のものと変更する。
stackoverflow.com

例えば、

mv secrets.py ssecrets.py

とし、tweets_analyzer.pyの該当部分を上記の名前で書き換える。

#vim tweets_analyzer.py


from ssecrets import consumer_key, consumer_secret, access_token, access_token_secret

 

AttributeError: module 'collections' has no attribute 'Iterable' エラーについて

github.com


実行すると、

AttributeError: module 'collections' has no attribute 'Iterable'

というエラーが出るので、
下記のようにモジュールをimportするよう追記する。

#vim tweets_analyzer.py

import collections
collections.Iterable = collections.abc.Iterable

 

タイムゾーンの問題

TwitterAPIの変更によるものか、タイムゾーンを取得できなくなっているようなので、
プログラム実行時に、下記のコマンドを追加(日本時間)。

#UTC +9時間(32400秒)

--utc-offset 32400

 

実行例

#-n [任意のスクリーンネーム]

python3 tweets_analyzer.py --utc-offset 32400 -n pantiliner



[+] Detected languages (top 5)
- ja     814 (81%)
- zxx    150 (15%)
- en      16 (1%)
- qme      9 (0%)
- und      6 (0%)

[+] Detected sources (top 10)
- Twitter for Android      878 (87%)
- Twitter Web App           78 (7%)
- 読書メーター                    22 (2%)
- TweetDeck                 19 (1%)
- Nintendo Switch Share      3 (0%)

[+] There are 16 geo enabled tweet(s)
[+] Detected places (top 10)
- 千代田区     11 (68%)
- 沼津市       4 (25%)
- 中央区       1 (6%)

[+] Top 10 hashtags
- #Yahooニュース              26 (11%)
- #bookmeter              22 (9%)
- #ちいかわファンアート             11 (4%)
- #ちいかわ                   11 (4%)
- #Togetter                5 (2%)
- #プリマジ                    4 (1%)
- #青山ひかる                   4 (1%)
- #NintendoSwitch2022      4 (1%)
- #野生児猫娘                   3 (1%)
- #イマドキ撮影会                 3 (1%)

[+] @pantiliner did 414 RTs out of 1000 tweets (41.4%)
[+] Top 5 most retweeted users
- @hikaru06kon      20 (4%)
- @dq10_lilith       9 (2%)
- @jednact           8 (1%)
- @chikaiwa1111      8 (1%)
- @mondaiji_con      7 (1%)

[+] Top 5 most mentioned users
- @hikaru06kon        24 (4%)
- @dq10_lilith        14 (2%)
- @ngnchiikawa         9 (1%)
- @jednact             8 (1%)
- @AUTOMATONJapan      8 (1%)

[+] Most referenced domains (from URLs)
- youtu.be                58 (20%)
- news.yahoo.co.jp        32 (11%)
- greta.5ch.net           25 (8%)
- bookmeter.com           23 (8%)
- youtube.com             12 (4%)
- automaton-media.com      8 (2%)