Skip to content

Commit

Permalink
Add a new subcommand fetch-tweets
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Aug 21, 2018
1 parent b537e0c commit 5bbfce9
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.md
Expand Up @@ -55,6 +55,7 @@ Available commands are:
* `fetch` (`get`, `show`): fetches a JSON string of a tweet.
* `search`: searches tweets with queries.
* `fetch-favorites` (`fetch-fav`): fetches favorite tweets.
* `fetch-tweets` (`fetch-posts`): fetches tweets of a user.
* `watch-mentions` (`watch`): watches mentions, retweets, DMs, etc., and executes handlers for each event.
* `type`: detects the type of the given input.
* `body`: extracts the body of a tweet.
Expand Down Expand Up @@ -156,6 +157,26 @@ Some commands require URL of a tweet, and they accept shortened URLs like `http:
$ ./tweet.sh fetch-fav -c 10 -s 0123456789
~~~

### `fetch-tweets` (`fetch-posts`): fetches tweets of a user.

* Parameters
* `-u`: the screen name of the owner of tweets to be fetched from. Yourself by default.
* `-c`: maximum number of tweets to be fetched. 10 by default.
* `-s`: the id of the last tweet already known. (optional)
If you specify this option, only tweets newer than the given tweet will be returned.
* `-m`: the id of the tweet you are searching tweets older than it. (optional)
If you specify this option, only tweets older than the given tweet will be returned.
* `-a`: include replies.
* `-r`: include retweets.
* Standard output
* [A JSON string of a user timeline](https://developer.twitter.com/en/docs/tweets/timelines/api-reference/get-statuses-user_timeline.html).
* Example

~~~
$ ./tweet.sh fetch-tweets -u screen_name -c 20
$ ./tweet.sh fetch-posts -u screen_name -c 10 -s 0123456789
~~~

#### Streaming

Basically this command provides ability to get search result based on the given query.
Expand Down
93 changes: 93 additions & 0 deletions tweet.sh
Expand Up @@ -227,6 +227,8 @@ Available commands:
search : searches tweets.
fetch-favorites(fetch-fav)
: fetches favorite tweets.
fetch-tweets(fetch-posts)
: fetches tweets of a user.
watch-mentions(watch)
: watches mentions, retweets, DMs, etc.
type : detects the type of the given input.
Expand Down Expand Up @@ -288,6 +290,13 @@ FIN
Usage:
./tweet.sh fetch-fav -c 10
./tweet.sh fetch-favorites -c 100 -s 0123456
FIN
;;
fetch-tweet*|fetch-post* )
cat << FIN
Usage:
./tweet.sh fetch-tweets -u screen_name -c 10
./tweet.sh fetch-posts -u screen_name -c 100 -s 0123456
FIN
;;
watch|watch-mentions )
Expand Down Expand Up @@ -714,6 +723,87 @@ FIN
handle_mentions "$user_screen_name" "$@"
}

fetch_tweets() {
ensure_available
local count=10
local since_id=''
local max_id=''
local user_screen_name="$(self_screen_name)"
local exclude_replies='exclude_replies 1'
local include_rts='include_rts 0'

local OPTIND OPTARG OPT
while getopts c:s:m:u:ar OPT
do
case $OPT in
c )
count="$OPTARG"
;;
s )
since_id="$(echo "$OPTARG" | extract_tweet_id)"
[ "$since_id" != '' ] && since_id="since_id $since_id"
;;
m )
max_id="$(echo "$OPTARG" | extract_tweet_id)"
[ "$max_id" != '' ] && max_id="max_id $max_id"
;;
u )
user_screen_name="$OPTARG"
;;
a )
exclude_replies='exclude_replies 0'
;;
r )
include_rts='include_rts 1'
;;
esac
done

local params="$(cat << FIN
screen_name $user_screen_name
count $count
$since_id
$max_id
$exclude_replies
$include_rts
FIN
)"
local result="$(echo "$params" |
call_api GET https://api.twitter.com/1.1/statuses/user_timeline.json)"
echo "$result"
check_errors "$result"
}

watch_mentions() {
ensure_available

local extra_keywords=''
local OPTIND OPTARG OPT
while getopts k:m:r:q:f:d:s: OPT
do
case $OPT in
k )
extra_keywords="$OPTARG"
;;
esac
done

local user_screen_name="$(self_screen_name)"
local tracking_keywords="$user_screen_name"
[ "$extra_keywords" != '' ] && tracking_keywords="$tracking_keywords,$extra_keywords"

echo "Tracking mentions for $tracking_keywords..." 1>&2

local params="$(cat << FIN
replies all
track $tracking_keywords
FIN
)"
echo "$params" |
call_api GET https://userstream.twitter.com/1.1/user.json |
handle_mentions "$user_screen_name" "$@"
}

handle_mentions() {
local user_screen_name=$1
shift
Expand Down Expand Up @@ -1655,6 +1745,9 @@ then
fetch-fav|fetch-favorites )
fetch_favorites "$@"
;;
fetch-tweet*|fetch-post* )
fetch_tweets "$@"
;;
watch|watch-mentions )
watch_mentions "$@"
;;
Expand Down

0 comments on commit 5bbfce9

Please sign in to comment.