Skip to content

Instantly share code, notes, and snippets.

@criztovyl
Last active August 29, 2015 14:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save criztovyl/0f7aa6cf28db3f0e905f to your computer and use it in GitHub Desktop.
Save criztovyl/0f7aa6cf28db3f0e905f to your computer and use it in GitHub Desktop.
Calling YouTube API from commandline (requires jsonCLI.py)
#!/bin/bash
# Call YouTube API from command line.
# Copyright (C) 2015 Christoph "criztovyl" Schulz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
###
# Requires jsonCLI.py: https://github.com/criztovyl/jsonCLI/blob/master/jsonCLI.py
###
###
# ToDO
# - different behaviour if sourced
# - cache
# + cache the result, as ~/.call_yt_api/$1_$2.json
# + should give the user access to the file without redownloading
# + should be removed if the user gets his result (handle via "end" arg? Backwards compatible.)
###
# Script
# Do not edit below this line.
#
###
# Read in the help
#
# The text
readarray help <<-API_KEY_HELP
# How to get your API Key
- got to https://console.developers.google.com/project
- select your project (if have no project, see "Create Project" and continue below afterwards)
- select APIs & auth on left navigation
- select Credentials
- your key is the "API Key" from "Public API access" (see "Create API Key" if you have none)
# Create a Project
- click on "Create Project"
- give the project a name and click on "Create"
- wait until Google created the project, it will open as soon its ready
# Create API Key
- click on "Create new Key" below "Public API access"
- select "Browser Key"
- enter nothing and click "Create"
- your key will appear as soon as its created.
# The key file
- the key is stored in the first line of ~/.google_api_key
API_KEY_HELP
##
# Store help into variable
# Clear variable
helpvar=
# Iterate over help lines; join by newline
for i in "${help[@]}"; do
# Append line and newline to variable
helpvar=$helpvar$i"\n"
done
# Remove last newline
helpvar=${helpvar%\\n}
# Finally replace help-lines array with joined lines
help=$helpvar
###
# The API key
#
# The keyfile
keyfile=$HOME"/.google_api_key"
# read key from keyfile
[ -f $keyfile ] && read key <$keyfile
# Loop for key if no key was found.
while true;
do
if [ -z "$key" ]; then
#Ask for key on stdin
#Variables
yes_lc="y"
question="The API Key is empty, set now? [Y]es [N]o [H]elp [Q]uit"
action="read -p \"Enter your API Key: \" key"
# Read single yes/no character
read -n 1 -p "$question " char
# To lower case
char=${char,,}
# Determine answer and executes action on yes (y) or on nothing (newline)
# Also determines if need to add a newline (if entered single character, not newline)
nl="\n"
[ -z "$char" ] && nl="" || [ "$char" == "y" ] && echo -ne "$nl" && eval "$action" || { \
echo -ne "$nl"; \
[ "$char" == "h" ] && echo -e $help && exit 0 || \
[ "$char" == "n" ] && echo "Continue without API key." && break || \
[ "$char" == "q" ] && exit 1
}
else
break;
fi
done
# Save key to keyfile
echo $key > $keyfile
###
# Usage: call_yt_api action data jsonPath
# resource: e.g. channels, see https://developers.google.com/youtube/v3/docs/#resource_types
# (method is currently "list" only)
# parameters: e.g. part=snippet&id=... (name1=value1&name2=value2&...&nameN=valueN)
# You can leave out "part=snippet", it will be added by default, if no other "part=" is given.
# jsonPath: arguments for jsonCLI, normally wich JSON Object should be returned.
# I.e. "items 0 snippet id" (you can put here other jsonCLI args also)
function call_yt_api()
{
# Variables
resource=$1
params=$2
jsonPath=${@:3}
# Add default part=snippet if no part is given
[ "$params" != *"part="* ] && params=$params"&part=snippet"
# If available, append API key to params
[ "$key" ] && params=$params"&key="$key
# Remove & from beginning of params (if there)
params=${params/#\&/}
# Insert into URL
url="https://www.googleapis.com/youtube/v3/$resource?$params"
# Download and get json data
echo `curl -s $url | jsonCLI $jsonPath`
}
call_yt_api $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment