Skip to content

Instantly share code, notes, and snippets.

@tdmalone
Created October 26, 2022 07:35
Show Gist options
  • Select an option

  • Save tdmalone/a374e31a2da658ab9591d24c9d74a682 to your computer and use it in GitHub Desktop.

Select an option

Save tdmalone/a374e31a2da658ab9591d24c9d74a682 to your computer and use it in GitHub Desktop.

Revisions

  1. tdmalone created this gist Oct 26, 2022.
    76 changes: 76 additions & 0 deletions pocket.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,76 @@
    #!/usr/bin/env bash

    set -euo pipefail

    #################################################

    # Create an app at https://getpocket.com/developer/apps/new
    # and enter the consumer key below
    # Then you're good to go!

    app_consumer_key="XXXXXXXXXXXXX"

    #################################################

    redirect_uri="https://getpocket.com/" # We don't really have a suitable URI to redirect to, this is just a CLI!

    api_base="https://getpocket.com/"
    read_base="https://getpocket.com/read/"
    token_filename="${HOME}/.pocket_access_token"
    result_filename="${HOME}/.pocket_last_result"

    bold="$(tput bold)"
    underline="$(tput smul)"
    normal="$(tput sgr0)"

    # @see https://getpocket.com/developer/docs/authentication
    function authenticate(){
    if [[ -f "${token_filename}" ]]; then
    access_token="$(cat "${token_filename}")"
    else
    echo "Authenticating with Pocket, please authorise once the browser opens..."
    request_token="$(curl --silent --fail "${api_base}v3/oauth/request" --request POST --data "consumer_key=${app_consumer_key}&redirect_uri=${redirect_uri}" | sed 's/code=//')"
    open "${api_base}auth/authorize?request_token=${request_token}&redirect_uri=${redirect_uri}"
    # shellcheck disable=SC2034
    echo "Press enter once you have authorized Pocket" && read -r answer
    echo "Retrieving access token..."
    access_token="$(curl --silent --fail "${api_base}v3/oauth/authorize" --request POST --data "consumer_key=${app_consumer_key}&code=${request_token}" | sed 's/access_token=\([^&]*\)&.*/\1/')"
    echo "${access_token}" > "${token_filename}"
    echo
    fi
    }

    function handle_auth_errors(){
    echo && echo "ERROR: Authentication failed. Did you click 'Authorize' on the Pocket site?"
    }

    function api(){
    endpoint="${1}"
    params="${2}"
    auth_params='{"consumer_key": "'"${app_consumer_key}"'", "access_token": "'"${access_token}"'"}'
    payload="$(echo "${auth_params} ${params}" | jq --slurp add)"
    curl --silent "${api_base}${endpoint}" --header "Content-type: application/json" --data "${payload}" | tee "${result_filename}"
    }

    # @see https://getpocket.com/developer/docs/v3/retrieve
    function retrieve_untagged_article(){
    count="1" # TODO: Support more than 1 item at a time.
    result="$(api v3/get '{"count": '"${count}"', "detailType": "complete", "tag": "_untagged_"}')"
    echo "Most recently saved, untagged article:" && echo
    echo -n "${normal}${bold}" && echo -n "" && echo "${result}" | jq --join-output ".list[].resolved_title" && echo
    echo -n "${normal}${underline}" && echo -n "${read_base}" && echo "${result}" | jq --join-output ".list[].item_id" && echo
    echo -n "${normal}" && echo -n "" && echo "${result}" | jq --join-output ".list[].excerpt" && echo

    echo -n "${normal}" \
    && echo -n "(" && echo "${result}" | jq --join-output ".list[].time_to_read" && echo -n " min read, " \
    && echo -n "added " && echo -n "$(date -r "$(echo "${result}" | jq --raw-output ".list[].time_added")")" && echo -n ")" \
    && echo
    }

    # Authentication
    # TODO: Handle expired (or otherwise invalid) auth token
    trap handle_auth_errors EXIT
    authenticate
    trap - EXIT

    retrieve_untagged_article