Created
January 22, 2018 00:13
-
-
Save drench/3279b38a60feee2384a6ee592cc6e7eb to your computer and use it in GitHub Desktop.
A shell script to fetch metadata for Y*uTube videos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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