-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetTweets.sh
executable file
·110 lines (95 loc) · 3.37 KB
/
getTweets.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env bash
#
# Fetch tweets from a specific user
#
#/ Usage:
#/ ./getTweets.sh -u <twitter_handle> [-m <max_num>] [-d]
#/
#/ Options:
#/ -u <handle> Mandatory, set twitter handle
#/ -d Optional, direct output without saving to json file
#/ -m Optional, max tweets number to download
#/ -h | --help Display this help message
set -e
set -u
usage() {
printf "%b\n" "$(grep '^#/' "$0" | cut -c4-)" && exit 1
}
set_var() {
_CURL=$(command -v curl)
_JQ=$(command -v jq)
_HOST_URL="https://twitter.com"
_API_URL="https://api.twitter.com"
[[ -z "${_MAX_TWEETS:-}" ]] && _MAX_TWEETS="4000" # API limit ~3200
_AUTH_TOKEN="AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA"
_TIMESTAMP="$(date +%s)"
}
set_args() {
expr "$*" : ".*--help" > /dev/null && usage
while getopts ":hdm:u:" opt; do
case $opt in
u)
_USER_HANDLE="$OPTARG"
;;
d)
_DIRECT_OUTPUT=true
;;
m)
_MAX_TWEETS="$OPTARG"
;;
h)
usage
;;
\?)
echo "[ERROR] Invalid option: -$OPTARG" >&2
usage
;;
esac
done
}
check_var() {
if [[ -z "${_USER_HANDLE:-}" ]]; then
echo "[ERROR] Missing twitter handle!" & usage
fi
}
fetch_guest_token() {
# $1: twitter handle
$_CURL -sS "$_HOST_URL/$1" \
-H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:75.0) Gecko/20100101 Firefox/73.0' \
| grep 'decodeURIComponent("gt=' \
| sed -E 's/.*gt=//' \
| awk -F ';' '{print $1}'
}
get_user_id() {
# $1: twitter handle
# $2: auth token
$_CURL -sS "$_API_URL/graphql/P8ph10GzBbdMqWZxulqCfA/UserByScreenName?variables=%7B%22screen_name%22%3A%22$1%22%2C%22withHighlightedLabel%22%3Atrue%7D" \
--header "authorization: Bearer $2" \
| $_JQ -r '.data.user.rest_id'
}
fetch_tweets() {
# $1: user id
# $2: auth token
# $3: guest token
$_CURL -sS "$_API_URL/2/timeline/profile/$1.json?include_profile_interstitial_type=1&include_blocking=1&include_blocked_by=1&include_followed_by=1&include_want_retweets=1&include_mute_edge=1&include_can_dm=1&include_can_media_tag=1&skip_status=1&cards_platform=Web-12&include_cards=1&include_composer_source=true&include_ext_alt_text=true&include_reply_count=1&tweet_mode=extended&include_entities=true&include_user_entities=true&include_ext_media_color=true&include_ext_media_availability=true&send_error_codes=true&simple_quoted_tweets=true&include_tweet_replies=true&count=$_MAX_TWEETS&ext=mediaStats%2ChighlightedLabel%2CcameraMoment" \
--header "authorization: Bearer $2" \
--header "x-guest-token: $3" \
| $_JQ '.globalObjects.tweets | .[] | select(.user_id_str==$id)' --arg id "$1" \
| $_JQ -s 'sort_by(.id_str)'
}
main() {
set_args "$@"
set_var
check_var
local i t
i=$(get_user_id "$_USER_HANDLE" "$_AUTH_TOKEN")
t=$(fetch_guest_token "$_USER_HANDLE")
if [[ -z "${_DIRECT_OUTPUT:-}" ]]; then
fetch_tweets "$i" "$_AUTH_TOKEN" "$t" > "${_USER_HANDLE}_${_TIMESTAMP}.json"
else
fetch_tweets "$i" "$_AUTH_TOKEN" "$t"
fi
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi