1
0

More than 1 year has passed since last update.

【Twitter】自分の全ツイートをいいね数の多い順に並べたい!

Last updated at Posted at 2021-12-11

「Twitter ツイート いいね 多い順 並べる」でググっても良いやり方が出てこなかったので自分でやってみました。

自分の全ツイートをダウンロード

まずはこちらを参考に自分の全ツイートをダウンロードします。

Python3をインストール

ターミナルにpython3と打ってpython3: command not foundが出た場合はPython3をインストールします。

Pythonコードを実行

2022/12/30追記

既存コードを改良しました。
以下のPythonコードはどこに配置しても使用可能です。
Twitterのアカウント名、アーカイブデータディレクトリのパスは自分のものに変更します。

実行環境は以下です。

  • Mac OS 10.15.7
  • Python 3.9.1
tweet_log.py
# coding: UTF-8

import json
from dateutil.parser import parse
from pytz import timezone
import codecs
import subprocess

# Twitterのアカウント名
account_name = "自分のTwitterアカウント名"
# アーカイブデータディレクトリのパス
dir_path = "自分のアーカイブデータのディレクトリパス/data"
# ツイートデータのパス
tweet_file_path = dir_path + "/tweets.js"

# 出力結果のファイル名
all_tweet_file_name = "tweet.tsv"
result_file_name = "tweet_result.tsv"
# フィルタするキーワード
search_filter_word = []
# フィルタする日付
filter_date = "2022"

encode = 'utf-8'

# jsonファイルを開いて読み込む
with codecs.open(tweet_file_path, 'r' , encode, 'ignore') as f:
    data = f.read()
tweet = json.loads(data[data.find('['):])

# 書き出すファイルを開く
f = open(all_tweet_file_name, 'wb')

# 見たいデータを出力する
for t in tweet:
    s = t['tweet']['full_text']
    e = t['tweet']['entities']
    if 'urls' in e:
        for u in e['urls']:
            s = s.replace(u['url'], u['expanded_url'])
    timestamp = parse(t['tweet']['created_at']).astimezone(timezone('Asia/Tokyo')).strftime("%Y-%m-%d %H:%M:%S")
    s = s.replace("\n", " ")
    s = s.replace("\r", " ")
    s = "https://twitter.com/" + account_name + "/status/" + t['tweet']['id']+ "\t" + timestamp + "\t" + t['tweet']['retweet_count'] + "\t" + t['tweet']['favorite_count'] + "\t" + s + '\n'
    f.write(s.encode(encode))
f.close()

# いいね数の多い順にソート
subprocess.run("sort -t$'\t' -k4rn " + all_tweet_file_name + " -o " + all_tweet_file_name, shell=True)
# 日付フィルタ
subprocess.run("cat " + all_tweet_file_name + " | awk 'BEGIN{FS=\"\t\"} $2 ~ /" + filter_date + "/ {print $0}' > \"" + result_file_name + "\"", shell=True)
# キーワードフィルタ
# subprocess.run("cat " + all_tweet_file_name + " | awk 'BEGIN{FS=\"\t\"} $5 ~ /" + '|'.join(search_filter_word) + "/ {print $0}' > \"" +  result_file_name + "\"", shell=True)

日付フィルタを使用すると、例えば「2022年でいいねを一番多くもらえたツイート」がすぐにわかります。
上記のようにフィルタする日付に2022を指定すると自分の全ツイートのうち2022年のツイートのみフィルタされます。
日付については、2022-12-30の形式のため、2022-12とすれば2022年12月のツイートのみがフィルタされます。

(追記ここまで)

以下のPythonコードをダウンロードしたアーカイブデータのdataディレクトリ内に配置します。
Twitterのアカウント名、アーカイブデータディレクトリのパスは自分のものに変更しておきます。
実行する際はdataディレクトリまで移動してから実行します。

自分の実行環境は以下です。

  • Mac OS 10.15.7
  • Python 3.9.1
tweet_log.py
import json
from dateutil.parser import parse
from pytz import timezone
import codecs
import subprocess

# Twitterのアカウント名
account_name = "自分のTwitterアカウント名"
# アーカイブデータディレクトリのパス
dir_path = "/自分のアーカイブデータのディレクトリパス/data"
# 出力結果のファイル名
all_tweet_file_name = "tweet.tsv"

encode = 'utf-8'

# jsonファイルを開いて読み込む
with codecs.open('tweet.js', 'r' , encode, 'ignore') as f:
    data = f.read()
tweet = json.loads(data[data.find('['):])

# 書き出すファイルを開く
f = open(all_tweet_file_name, 'wb')

# 見たいデータを出力する
for t in tweet:
    s = t['tweet']['full_text']
    e = t['tweet']['entities']
    if 'urls' in e:
        for u in e['urls']:
            s = s.replace(u['url'], u['expanded_url'])
    timestamp = parse(t['tweet']['created_at']).astimezone(timezone('Asia/Tokyo')).strftime("%Y-%m-%d %H:%M:%S")
    s = s.replace("\n", " ")
    s = s.replace("\r", " ")
    s = "https://twitter.com/" + account_name + "/status/" + t['tweet']['id']+ "\t" + timestamp + "\t" + t['tweet']['retweet_count'] + "\t" + t['tweet']['favorite_count'] + "\t" + s + '\n'
    f.write(s.encode(encode))
f.close()

# いいね数の多い順にソート
subprocess.run("sort -t$'\t' -k4rn " + all_tweet_file_name + " -o " + all_tweet_file_name, shell=True, cwd=dir_path)

出力されたTSVファイルをExcelやNumbersなどで開くといいね数の多い順にツイートが並べられていると思います。
以下は自分のアーカイブデータで試したものです。
表の左から4番目の列がいいね数です。
スクリーンショット 2021-12-11 14.57.40.png

これで自分の全ツイートをいいね数の多い順に並べることができました!

指定したキーワードを含むツイートでいいね数の多い順に並べる

search_filter_wordにキーワードを指定するとそれを含むツイートの中でいいね数の多い順に並べられます。

tweet_log.py
import json
from dateutil.parser import parse
from pytz import timezone
import codecs
import subprocess

# Twitterのアカウント名
account_name = "自分のTwitterアカウント名"
# アーカイブデータディレクトリのパス
dir_path = "/自分のアーカイブデータのディレクトリパス/data"
# 出力結果のファイル名
all_tweet_file_name = "tweet.tsv"
search_file_name = "tweet_result.tsv"
# フィルタするキーワード
search_filter_word = ["hogehoge", "fugafuga"]

encode = 'utf-8'

# jsonファイルを開いて読み込む
with codecs.open('tweet.js', 'r' , encode, 'ignore') as f:
    data = f.read()
tweet = json.loads(data[data.find('['):])

# 書き出すファイルを開く
f = open(all_tweet_file_name, 'wb')

# 見たいデータを出力する
for t in tweet:
    s = t['tweet']['full_text']
    e = t['tweet']['entities']
    if 'urls' in e:
        for u in e['urls']:
            s = s.replace(u['url'], u['expanded_url'])
    timestamp = parse(t['tweet']['created_at']).astimezone(timezone('Asia/Tokyo')).strftime("%Y-%m-%d %H:%M:%S")
    s = s.replace("\n", " ")
    s = s.replace("\r", " ")
    s = "https://twitter.com/" + account_name + "/status/" + t['tweet']['id']+ "\t" + timestamp + "\t" + t['tweet']['retweet_count'] + "\t" + t['tweet']['favorite_count'] + "\t" + s + '\n'
    f.write(s.encode(encode))
f.close()

# いいね数の多い順にソート
subprocess.run("sort -t$'\t' -k4rn " + all_tweet_file_name + " -o " + all_tweet_file_name, shell=True, cwd=dir_path)
# キーワードフィルタ
subprocess.run("cat " + all_tweet_file_name + " | awk 'BEGIN{FS=\"\t\"} $5 ~ /" + '|'.join(search_filter_word) + "/ {print $0}' > \"" +  search_file_name + "\"", shell=True, cwd=dir_path)

参考:Twitter自分の全ツイート履歴をダウンロード――オンラインではできないDM検索もやってみた

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0