プロジェクト

全般

プロフィール

OwnTweetSearch » 履歴 » バージョン 1

kanata, 2025/04/13 13:59

1 1 kanata
# OwnTweetSearch
2
3
{{toc}}
4
5
## 自分の過去のTweetを検索する方法
6
7
Twitterしてると、たまに「あぁ~このタイムラインの話題、あの時つぶやいたあの話なんだよな~」と思う時があります
8
でも、後の祭りでなかなか検索できないんですよね
9
そんな訳で簡単に検索できるツールを作りました
10
11
# 前提
12
13
以下のコマンドが使える環境があること、仮想環境のLinuxとかWSLとか
14
Macは動かない気がするけど、中身をちょっと書き換えればいけると思う
15
16
* jq,sed,awk,grep,tail
17
18
# 準備(自分の全ツイート履歴のダウンロード)
19
20
Twitterは自分の過去ツイートの全データをダウンロードできます
21
以下の手順を基にダウンロードします
22
23
{{rawhtml(<div class="iframely-embed"><div class="iframely-responsive" style="height: 140px; padding-bottom: 0;"><a href="https://help.twitter.com/ja/managing-your-account/how-to-download-your-twitter-archive" data-iframely-url="//cdn.iframe.ly/HLSK3WV?iframe=card-small"></a></div></div><script async src="//cdn.iframe.ly/embed.js" charset="utf-8"></script>)}}
24
25
zipで提供されるはずですので、中身を展開して確認しましょう
26
この中の **tweet.js** だけが必要です
27
28
# OwnTweetSearch の導入
29
30
## install
31
32
ダウンロードして、実行権つけて、一箇所中身の設定項目を書き換えるだけ
33
34
```bash
35
$ wget https://raintrees.net/attachments/download/709/OwnTweetSearch.sh
36
$ chmod u+x ./OwnTweetSearch.sh
37
```
38
39
```
40
$ vi OwnTweetSearch.sh
41
- MY_NAME="own-twitter-id" #自身のTwitterのidを設定
42
+ MY_NAME="ここに自分のTwitterのidの@を抜いた形で設定してください" #自身のTwitterのidを設定
43
```
44
45
46
### 中身
47
48
中身シェルスクリプトなので、どうということはない
49
50
```bash
51
#!/bin/bash
52
53
IFS='
54
'
55
56
MY_NAME="own-twitter-id" #自身のTwitterのidを設定
57
HIT_LIST=$(cat tweet.js |sed -e 's/window.YTD.tweet.part0 = //'|jq -r -c '.[]'|grep "$1")
58
HIT_COUNT=$(echo "${HIT_LIST}"|wc -l)
59
COUNT="1"
60
61
for RECORD in $HIT_LIST
62
do
63
  echo $RECORD
64
  ID=$(echo $RECORD|grep -o "\"id\":\".*\""|tr ',' '\n'|grep "\"id\":\".*\""|tail -1|awk -F'"' '{print $4}')
65
  SCREEN_NAME=$(echo $RECORD|grep -o "screen_name\":\".*\","|awk -F'"' '{print $3}')
66
  if [ -z "$SCREEN_NAME" ]
67
  then
68
    SCREEN_NAME=$MY_NAME
69
  fi
70
  echo "https://twitter.com/${SCREEN_NAME}/status/${ID}"
71
  if [ "$HIT_COUNT" -gt 1 ]
72
  then
73
    echo "-------------------- [$HIT_COUNT] --------------------"
74
    : $((HIT_COUNT--))
75
  fi
76
done
77
```
78
79
## 使い方
80
81
tweet.jsが同じディレクトリにある状態で、引数に検索した文字を入れるだけ
82
複数件あると複数件ひっかかる
83
該当のJSONとURLを出力する
84
85
```bash
86
$ ./OwnTweetSearch.sh 私見
87
{"retweeted":false,"source":"<a href=\"http://www.twitpane.com\" rel=\"nofollow\">TwitPanePlus</a>","entities":{"hashtags":[],"symbols":[],"user_mentions":[],"urls":[]},"display_text_range":["0","161"],"favorite_count":"32","in_reply_to_status_id_str":"1121753187784876032","id_str":"1121772687645237249","in_reply_to_user_id":"983655865474363392","truncated":false,"retweet_count":"9","id":"1121772687645237249","in_reply_to_status_id":"1121753187784876032","created_at":"Fri Apr 26 13:47:04 +0000 2019","favorited":false,"full_text":"@urbsxb4mu 私見でございますが…以下を順番に読むといい気がしております\n\n入門UNIXシェルプログラミング―シェルの基礎から学ぶUNIXの世界  ブルース・ブリン  (著)\nシェルプログラミング実用テクニック Kindle版 上田隆一  (著)\n\nワイ、入門シェルプログラミングの本でシェルスクリプトを覚(文字数","lang":"ja","in_reply_to_screen_name":"44GG44KT44GT","in_reply_to_user_id_str":"983655865474363392"}
88
https://twitter.com/44GG44KT44GT/status/1121772687645237249
89
90
$ ./OwnTweetSearch.sh 変態だ
91
{"retweeted":false,"source":"<a href=\"https://about.twitter.com/products/tweetdeck\" rel=\"nofollow\">TweetDeck</a>","entities":{"hashtags":[],"symbols":[],"user_mentions":[{"name":"シェル芸bot","screen_name":"minyoruminyon","indices":["3","17"],"id_str":"3978626952","id":"3978626952"}],"urls":[{"url":"https://t.co/hgjICWlvJF","expanded_url":"https://youtu.be/3OSJlfPag4A","display_url":"youtu.be/3OSJlfPag4A","indices":["36","59"]},{"url":"https://t.co/L8UwU6aKef","expanded_url":"https://twitter.com/yami_buta/status/1121942576657879040","display_url":"twitter.com/yami_buta/stat…","indices":["64","87"]}]},"display_text_range":["0","87"],"favorite_count":"0","id_str":"1121942740575461376","truncated":false,"retweet_count":"0","id":"1121942740575461376","possibly_sensitive":false,"created_at":"Sat Apr 27 01:02:48 +0000 2019","favorited":false,"full_text":"RT @minyoruminyon: 仮に変態だとしても変態という名の https://t.co/hgjICWlvJF だよ! https://t.co/L8UwU6aKef","lang":"ja"}
92
https://twitter.com/minyoruminyon/status/1121942740575461376
93
-------------------- [5] --------------------
94
{"retweeted":false,"source":"<a href=\"http://www.twitpane.com\" rel=\"nofollow\">TwitPanePlus</a>","entities":{"hashtags":[],"symbols":[],"user_mentions":[],"urls":[]},"display_text_range":["0","5"],"favorite_count":"1","id_str":"1012308409654231042","truncated":false,"retweet_count":"0","id":"1012308409654231042","created_at":"Thu Jun 28 12:15:07 +0000 2018","favorited":false,"full_text":"変態だった","lang":"ja"}
95
https://twitter.com/kanata201612/status/1012308409654231042
96
-------------------- [4] --------------------
97
{"retweeted":false,"source":"<a href=\"http://www.twitpane.com\" rel=\"nofollow\">TwitPanePlus</a>","entities":{"hashtags":[],"symbols":[],"user_mentions":[{"name":"","screen_name":"youkanyukari","indices":["3","16"],"id_str":"-1","id":"-1"}],"urls":[]},"display_text_range":["0","42"],"favorite_count":"0","id_str":"982603281049403394","truncated":false,"retweet_count":"0","id":"982603281049403394","created_at":"Sat Apr 07 12:57:32 +0000 2018","favorited":false,"full_text":"RT @youkanyukari: 超・記号オンリー難読化シェル芸 とか変態だ・・・","lang":"ja"}
98
https://twitter.com/youkanyukari/status/982603281049403394
99
-------------------- [3] --------------------
100
{"retweeted":false,"source":"<a href=\"http://www.twitpane.com\" rel=\"nofollow\">TwitPanePlus</a>","entities":{"hashtags":[],"symbols":[],"user_mentions":[{"name":"上田 隆一","screen_name":"ryuichiueda","indices":["3","15"],"id_str":"372780389","id":"372780389"}],"urls":[]},"display_text_range":["0","80"],"favorite_count":"0","id_str":"908140256137854976","truncated":false,"retweet_count":"0","id":"908140256137854976","created_at":"Thu Sep 14 01:28:04 +0000 2017","favorited":false,"full_text":"RT @ryuichiueda: 創造し続けようと思う人間には、変態しかあり得ない。人生は変態であり、変態だ。('シェル芸おじさん自叙伝 Ⅱ' p114514)","lang":"ja"}
101
https://twitter.com/ryuichiueda/status/908140256137854976
102
-------------------- [2] --------------------
103
{"retweeted":false,"source":"<a href=\"https://mobile.twitter.com\" rel=\"nofollow\">Twitter Web App</a>","entities":{"hashtags":[],"symbols":[],"user_mentions":[],"urls":[]},"display_text_range":["0","66"],"favorite_count":"6","id_str":"1183283246122946560","truncated":false,"retweet_count":"0","id":"1183283246122946560","created_at":"Sun Oct 13 07:28:04 +0000 2019","favorited":false,"full_text":"着圧ソックスが低気圧時の頭痛に効くっていうから物色してたんだけど、女性用しかないな。これを着ける…?変態じゃないか…(変態だけど…)","lang":"ja"}
104
https://twitter.com/kanata201612/status/1183283246122946560
105
```