Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- #
- # sqwoker - the sqwok reader
- # ----------------------------------------------------------------
- # This software is still in alpha stage. Future releases may break
- # compatibility. The output of this script should not be treated
- # as consistent with the content located at sqwok.im
- #
- # Sqwoker uses a custom user agent. I believe this may help
- # troubleshoot any server-side issues caused by this client.
- #
- # Sqwoker was tested with bash and dash. Should theoretically work
- # with some other shells too.
- #
- # Sqwoker relies on curl for networking and jq for json parsing.
- # ----------------------------------------------------------------
- # Example usage
- #
- # Get post listing for "new" and save to file new.json
- #
- # $ ./sqwoker.sh --get new > new.json
- #
- # Parse the post listing from file new.json
- #
- # $ ./sqwoker.sh -p < new.json
- set -e
- # version string
- sqwoker_version="0.1.0"
- # help message
- sqwoker_help="sqwoker $sqwoker_version - help
- usage:
- $ $0 <flag> [options]
- flags:
- -g, --get <sort> download post listing and print to stdout
- -p, --parse parse json post listing from stdin
- -b, --brief like -p but briefer
- -d, --default execute the default function (-g hot | -b)
- -h, --help print a help message and exit
- -v, --version print version string and exit
- valid sort options:
- new
- hot
- "
- # the default function - called with -d
- sqwokerDefaultFunc () {
- getListing "hot" | parseListingBrief
- }
- # curl request to fetch post listing (output to stdout)
- # authorization header token required for guest authorization
- getListing () {
- curl -XGET "https://api.sqwok.im/v1/posts/$1" \
- -H "User-Agent: sqwoker/$sqwoker_version" \
- -H "Authorization: uskoa eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1ODc2MzA3OTYsIm9yaWdfaWF0IjoxNTg3NjI5ODk2LCJhdHRhY2tTaGlwc09uTWFycyI6dHJ1ZSwidGFubkhhdXNlckdhdGUiOnRydWV9.dpf5d0OKdFbas0F7IAis1ACoyikj1-LIMIfNxBFBXKo"
- }
- # jq filter to parse post listing (from stdin)
- parseListing () {
- jq -r '.data[] | "[=] \(.attributes.text)\n\(.relationships.nest.data.id) | \(.attributes.created_at) | messages: \(.attributes.message_count) | id: \(.id) | sqwok_id: \(.attributes.sqwok_id)\n"'
- }
- # like parseListing, but without some meta data
- parseListingBrief () {
- jq -r '.data[] | "[=] \(.attributes.text)\n\(.relationships.nest.data.id) | \(.attributes.created_at) | messages: \(.attributes.message_count) | id: \(.id)\n"'
- }
- # process command-line parametres
- # see if any parametres present
- if [ ! -z "$1" ]; then
- # get - download post listing
- if [ "$1" = "-g" ] || [ "$1" = "--get" ]; then
- # requires one additional parametre (sort option)
- if [ ! -z "$2" ]; then
- # filter valid sort options
- if [ "$2" = "new" ] || [ "$2" = "hot" ]; then
- getListing "$2"
- else
- printf "%s> ERROR: Invalid sort option (%s). Try -h for help\n" "$0" "$2" 1>&2
- fi
- else
- printf "%s> ERROR: No parametre specified for flag (%s). Try -h for help\n" "$0" "$1" 1>&2
- fi
- # parse - parse json post listing
- elif [ "$1" = "-p" ] || [ "$1" = "--parse" ]; then
- parseListing
- # brief - briefer version of -p
- elif [ "$1" = "-b" ] || [ "$1" = "--brief" ]; then
- parseListingBrief
- # default - execute the default function
- elif [ "$1" = "-d" ] || [ "$1" = "--default" ]; then
- sqwokerDefaultFunc
- # print help message
- elif [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
- printf "%s" "$sqwoker_help"
- # print version string
- elif [ "$1" = "-v" ] || [ "$1" = "--version" ]; then
- printf "sqwoker version %s\n" "$sqwoker_version"
- else
- printf "%s> ERROR: Invalid parametre (%s). Try -h for help\n" "$0" "$1" 1>&2
- fi
- else
- printf "%s> ERROR: No command given. Try -h for help\n" "$0" 1>&2
- fi
Advertisement
Add Comment
Please, Sign In to add comment