Skip to content

Instantly share code, notes, and snippets.

@abiyani
Last active August 29, 2015 14:01
Show Gist options
  • Save abiyani/bac0974e06ae5b8f939e to your computer and use it in GitHub Desktop.
Save abiyani/bac0974e06ae5b8f939e to your computer and use it in GitHub Desktop.
Perform multiple bing search from your account (by default assumes that you are logged into bing in Firefox, but you can supply a custom cookie file too)
#!/bin/bash
# Usage:
# ./bing_search.bash [max_random_wait_in_seconds=5] [cookie_file_path]
set -e
function ensureCookieFile() {
local mpath=~/.mozilla/firefox
cf="$(find "$mpath" -name "cookies.sqlite" -type f -print0 | xargs -0 stat --format '%Y :%y %n' | sort -nr | rev | head -n 1 | cut -f 1 -d ' ' | rev)"
if [[ -z "$cf" ]]; then
echo -e "\nUnable to find file 'cookies.sqlite' inside the source tree '${mpath}' -- will exit\n" >&2
exit 1
fi
}
function doIt() {
printf -- "- Max wait in seconds: '%s'\n- Cookie file: '%s'\n" "${1}" "${2}"
local tfile="$(mktemp -u)"
local i
local failure_regex='ERROR.*Failed to load cookies from'
for i in {1..100}; do
# Search for the string "<timestamp>-i" in each iteration
local out="$(aria2c --load-cookies "${2}" "https://www.bing.com/search?q=$(date +%s)-$i" -d "$(dirname "$tfile")" -o "$(basename "$tfile")" 2>&1)"
if [[ "$out" =~ $failure_regex ]]; then
echo -ne "\nERROR: Invalid cookie file '${2}'\n\n" >&2
exit 1
fi
rm "$tfile"
printf "Completed search #%s" "${i}"
if [[ ${1} -gt 0 ]]; then
local rn=$(( ( RANDOM % ${1} ) + 1 ))
printf " ... sleeping for %s seconds" "${rn}"
sleep $rn # Be human :D
fi
printf "\n"
done
}
if [[ -z "$1" ]]; then wait_limit=5; else wait_limit="$1"; fi
if [[ ! $wait_limit =~ [0-9]+ ]]; then
echo -ne "\nERROR: 'max_random_wait_in_seconds' must be a non-negative integer, but value is: '$wait_limit'\n\n" >&2
exit 1
fi
if [[ -z "$2" ]]; then
ensureCookieFile
else
cf="$2"
fi
set -u
doIt ${wait_limit} "${cf}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment