Skip to content

Instantly share code, notes, and snippets.

@angelworm
Created September 22, 2018 13:40
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angelworm/79c1ce27770480f413fb414d3dd168c0 to your computer and use it in GitHub Desktop.
Save angelworm/79c1ce27770480f413fb414d3dd168c0 to your computer and use it in GitHub Desktop.
Twitterで検索してくる奴
#!/bin/bash -u
### search.sh
# searches specified words on Twitter.
#
# useage: ./search.sh word
# requirements: jq, curl, perl
# example: ./search.sh hoge | cut -f 1 | sort -u > blocklist.csv
QUERY=$(echo ${1:?"search.sh query"} | jq -R '@uri')
AUTH_TOKEN='AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA'
GUEST_TOKEN=$(curl -sSL "https://mobile.twitter.com/search?q=${QUERY}" -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:62.0) Gecko/20100101 Firefox/62.0'| perl -nl -e '/gt=(\d+)/ and print $1' | head -n 1)
TEMP_FILE=$(mktemp -t '.json')
trap "rm -f $TEMP_FILE" EXIT
cursor=
while :
do
curl -sSL "https://api.twitter.com/2/search/adaptive.json?&include_want_retweets=1&include_ext_alt_text=true&count=200&query_source=typed_query&tweet_search_mode=live&q=${QUERY}"${cursor:+"&cursor=$cursor"} \
-H "authorization: Bearer ${AUTH_TOKEN}" \
-H "x-guest-token: $GUEST_TOKEN" \
> $TEMP_FILE
cat $TEMP_FILE |\
jq -e -r '.globalObjects.tweets[] | [.user_id_str, .id_str, .created_at, .text] | @tsv'
[ $? != 0 ] && exit 0
cursor=$(cat $TEMP_FILE | jq -r '.timeline.instructions[] | .addEntries.entries[]? // .replaceEntry.entry? | select(.entryId? == "sq-cursor-bottom") | .content?.operation?.cursor?.value? | @uri')
sleep 30
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment