Skip to content

Instantly share code, notes, and snippets.

@paulp
Created January 24, 2018 02:28
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save paulp/91d657489b617dbb7de6db8b7c4fe4e4 to your computer and use it in GitHub Desktop.
Save paulp/91d657489b617dbb7de6db8b7c4fe4e4 to your computer and use it in GitHub Desktop.
Use google's verbatim search and date range restriction simultaneously
#!/usr/bin/env bash
#
# Use google's date range and "verbatim" simultaneously
# Paul Phillips <psp@brb.fyi>
#
NAME="$(basename "$0")"
read -r -d '' USAGE << EOM
usage: $NAME [-s days] [-e days] [search terms]
Performs verbatim google search with date range.
Warning: after you use this a couple times in a short
period, google will decide you are a robot and assault
you with captchas.
Examples:
$NAME -s 14 "jordan peterson" # Searches most recent 14 day range
$NAME -s 60 -e 30 suffuse talk # Searches between 60 and 30 days ago
$NAME scala unsound # Searches 0-30 days ago (default)
EOM
case "$1" in
"") echo >&2 "$USAGE" && exit 1 ;;
-h) echo >&2 "$USAGE" && exit 0 ;;
esac
# Convert a year month and day into a julian day.
# https://en.wikipedia.org/wiki/Julian_day
julian_day() {
local YYYY MM DD;
read -r YYYY MM DD < <( date "+%Y %m %d" )
local MM1=$(( 12 * YYYY + 10#$MM - 3 ))
local YY1=$(( MM1 / 12 ))
local MM2=$(( 734 * MM1 + 15 ))
local RES=$(( MM2/24 - YY1*2 + YY1/4 - YY1/100 + YY1/400 + 10#$DD + 1721119 ))
printf "%s\n" "$RES"
}
verbatim_search_url () {
local today="$(julian-day)"
local start="$(( today - 30 ))"
local end="$today"
while getopts :s:e: opt; do
case $opt in
s) start=$(( today - $OPTARG )) ;;
e) end=$(( today - $OPTARG )) ;;
esac
done
shift $(( OPTIND-1 ))
local params="nfpr=1&tbs=li:1" # this part means 'verbatim'
local query="$(xs of "$@" "daterange:$start-$end" | xs join:'+')"
echo "https://www.google.com/search?q=$query&$params"
}
open "$(verbatim_search_url "$@")"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment