#!/bin/csh -f # twitter # ------------------------------------------------------------------------- # Shell script to open Twitter or search for specific tweets. # ------------------------------------------------------------------------- # Revision History: # $Log$ # ------------------------------------------------------------------------- set browser = "open" # If $1 is a script in the browsers folder, use it as the browser. if ($#argv > 0 && -x "$0:h/browsers/$1") then set browser = $1:q shift endif # Collect command line options set option_from_string = "" set option_before_string = "" set option_after_string = "" set search_twitter = "false" # Special case: # If there's exactly one argument and it's a Twitter alias, show tweets # from that person. if ($#argv == 1) then set twitter_alias = "`echo $1 | twitter_aliases`" if ("$twitter_alias" != "$1") then $0:t -f $twitter_alias:q exit $status endif endif while ($#argv > 0) if ("$1" == "-h" || "$1" == "--help") then echo "Usage: $0:t [options] [arguments]" echo " $0:t from" echo "Help options:" echo " -h = Show this help text" echo " --help = Show this help text" echo "Search options:" echo " -f from_handle = Twitter handle that tweeted" echo " --from from_handle = Twitter handle that tweeted" echo " -b before_date = Tweeted before specified date" echo " --before before_date = Tweeted before specified date" echo " -a after_date = Tweeted on or after specified date" echo " --after after_date = Tweeted on or after specified date" echo "Option arguments:" echo " - from_handle can be specified with or without leading @." echo " - Dates are in format: YYYY-MM-DD" echo "Arguments:" echo " All arguments are treated as strings to search for. Can be" echo " single words or quoted phrases. Phrases can contain words" echo " separated by the special keyword OR so any of the words" echo " cause a match, not all of the words. Words or phrases can" echo " start with - to mean NOT found in the tweet. Hashtags start" echo " with #. Mentions start with @." echo "Notes:" echo " - The short form '$0:t from' is the same as the long form" echo " '$0:t --from from', but only if from is an alias expanded" echo " by twitter_aliases. Don't prefix from with @ or it loses" echo " this special meaning and is just a search for any mention" echo " of @from, not for tweets by from." echo " - All -f options are OR'd together. Finds tweets from any." echo " - Only one -b/--before and one -a/--after is allowed." echo " - All arguments are AND'd together. All must apply." exit 1 # From option else if ("$1" == "-f" || "$1" == "--from") then shift set search_twitter = "true" if ($#argv == 0) then beep "Error: Missing argument for option -f or --from at end of command" $0:t --help exit 1 endif set from = $1:q shift if ("$from" == "") then beep "Error: Empty argument for option -f or --from." $0:t --help exit 1 endif set char1 = "`echo $from | cut -c 1`" if ("$char1" == "-") then beep "Error: Missing argument for option -f or --from before '$from'." $0:t --help exit 1 endif # Strip optional leading "@" if ("$char1" == "@") then set from = "`echo $from | cut -c 2-`" endif # Expand common aliases set from = "`echo $from | twitter_aliases`" # Build required Twitter syntax if ("$option_from_string" == "") then set option_from_string = "from:$from" else set option_from_string = "$option_from_string OR from:$from" endif # Before option else if ("$1" == "-b" || "$1" == "--before") then shift set search_twitter = "true" if ("$option_before_string" != "") then beep "Error: Multiple -b or --before options specified." $0:t --help exit 1 endif if ($#argv == 0) then beep "Error: Missing argument for option -b or --before at end of command" $0:t --help exit 1 endif set before = $1:q shift if ("$before" == "") then beep "Error: Empty argument for option -b or --before." $0:t --help exit 1 endif set char1 = "`echo $before | cut -c 1`" if ("$char1" == "-") then beep "Error: Missing argument for option -b or --before before '$before'." $0:t --help exit 1 endif # Check date format @ numchars = `echo -n "$before" | wc -m` if ($numchars != 10) then beep "Error: Date argument for option -b or --before must be exactly 10 chars long." $0:t --help exit 1 endif set char5 = "`echo $before | cut -c 5`" set char8 = "`echo $before | cut -c 8`" if ("$char5" != "-" || "$char8" != "-") then beep "Error: Date argument format for option -b or --before must be YYYY-MM-DD." $0:t --help exit 1 endif # Build required Twitter syntax set option_before_string = "until:$before" # After option else if ("$1" == "-a" || "$1" == "--after") then shift set search_twitter = "true" if ("$option_after_string" != "") then beep "Error: Multiple -a or --after options specified." $0:t --help exit 1 endif if ($#argv == 0) then beep "Error: Missing argument for option -a or --after at end of command" $0:t --help exit 1 endif set after = $1:q shift if ("$after" == "") then beep "Error: Empty argument for option -a or --after." $0:t --help exit 1 endif set char1 = "`echo $after | cut -c 1`" if ("$char1" == "-") then beep "Error: Missing argument for option -a or --after before '$after'." $0:t --help exit 1 endif # Check date format @ numchars = `echo -n "$after" | wc -m` if ($numchars != 10) then beep "Error: Date argument for option -a or --after must be exactly 10 chars long." $0:t --help exit 1 endif set char5 = "`echo $after | cut -c 5`" set char8 = "`echo $after | cut -c 8`" if ("$char5" != "-" || "$char8" != "-") then beep "Error: Date argument format for option -a or --after must be YYYY-MM-DD." $0:t --help exit 1 endif # Build required Twitter syntax set option_after_string = "since:$after" # Skip options and arguments that are empty strings else if ("$1" == "") then shift # Invalid option else if ("-" == "`echo $1:q | cut -c 1`") then beep "Error: Invalid option: '$1:q'" $0 --help exit 1 else # Doesn't start with "-". Assume it's the first argument. set search_twitter = "true" break endif end if ("$option_from_string" != "") then set option_from_string = "(${option_from_string})" endif # Build URL query params set displayable_params = "" set params = "" if ("$search_twitter" == "true") then if ("$option_from_string" != "") then set params = "${params}${option_from_string} " endif if ("$option_before_string" != "") then set params = "${params}${option_before_string} " endif if ("$option_after_string" != "") then set params = "${params}${option_after_string} " endif while ($#argv > 0) # Skip empty strings if ("$1" != "") then # Accumulate params echo "$1" | grep -i " " >& /dev/null set no_space = $status echo "$1" | grep -i "@" >& /dev/null set no_at_sign = $status echo "$1" | grep -i "#" >& /dev/null set no_hash = $status echo "$1" | grep -i " OR " >& /dev/null set no_or = $status if (! $no_space && $no_at_sign && $no_hash && $no_or) then # Put quotes around any quoted phrase to cause Twitter to # search for the entire phrase, not just the words. # But no quotes if it contains an @mention or a #hashtag # because that would cause them to not be recognized. set displayable_params = "${params}('$1') " set params = "${params}(%22$1%22) " else # Put parentheses around each word (harmless) or phrase # (useful), so the user can do things like: # - "mountain OR hill" # - "@fred OR @joe" # - "#hashtag1 OR #hashtag2 OR #hashtag3" # - "@fred OR #hashtag1" set params = "${params}($1) " endif endif shift end if ("$displayable_params" == "") then set displayable_params = "${params}" endif @ numchars = `echo -n "$params" | wc -m` set params = `echo "$params" | cut -c 1-$numchars` endif set url = "https://twitter.com" if ("$search_twitter" == "true") then set url = "${url}/search?src=typed_query&f=live&q=" echo "$browser ${url}${displayable_params}" set params = `echo "$params" | urlencode --skip_percent` endif (set echo; $browser "${url}${params}")