Skip to content

Instantly share code, notes, and snippets.

@drench
Created January 22, 2018 00:13
Show Gist options
  • Save drench/3279b38a60feee2384a6ee592cc6e7eb to your computer and use it in GitHub Desktop.
Save drench/3279b38a60feee2384a6ee592cc6e7eb to your computer and use it in GitHub Desktop.
A shell script to fetch metadata for Y*uTube videos
#!/bin/sh
#
# Given a Y*uTube video ID, this grabs JSON metadata from publically available
# HTML. No API!
#
# Requires:
# * curl
# * node
# * pup https://github.com/ericchiang/pup
#
# It sends JSON to standard out, so with jq you can do things like:
#
# % ./ytjson XaDfGFW6MZo | jq '.args | .title, .length_seconds, .view_count'
# Gastr del Sol - Ursus Arctos Wonderfilis (1995/09/23)
# 31476
# 580
usage() {
>&2 echo "usage: $0 videoid"
exit 111
}
videoid=$1
if [ -z "$videoid" ]; then
usage
fi
tmp_script=$(mktemp)
cat <<EOJS > $tmp_script
window = {};
yt = {
setConfig: function() {},
setMsg: function() {},
pushConfigArray: function() {}
};
EOJS
curl --silent "https://www.youtube.com/watch?v=$videoid" |
pup 'script:contains("length_seconds") text{}' >> $tmp_script
echo 'console.log(JSON.stringify(ytplayer.config));' >> $tmp_script
node $tmp_script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment