Last active
December 6, 2016 18:38
-
-
Save mfakane/d9b97f2cb8f635eec6a893e2156616e3 to your computer and use it in GitHub Desktop.
Download video from nicovideo
This file contains 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 | |
PROGNAME=$(basename $0) | |
#urldecode="nkf --url-input" | |
urldecode='eval echo -e $(sed "s/+/ /g; s/%/\\\\x/g")' | |
account="" | |
password="" | |
cookie="" | |
savecookie="" | |
outdir="." | |
ids="" | |
usage() { | |
echo "Usage: $PROGNAME [OPTION]... ID..." | |
echo "Download video(s) specified by IDs" | |
echo | |
echo "Options:" | |
echo " -c, --cookie COOKIEFILE Save current login info to COOKIEFILE" | |
echo " -s, --save-cookie COOKIEFILE Load login info from COOKIEFILE" | |
echo " -u, --username USERNAME Set nicovideo account user name" | |
echo " -p, --password PASSWORD Set nicovideo account password" | |
echo " -o, --outdir DIRNAME Save videos to another directory instead of current directory" | |
exit 1 | |
} | |
if (( $# == 0 )); then | |
usage | |
fi | |
while (( $# > 0 )); do | |
case "$1" in | |
--help) | |
usage | |
;; | |
--cookie) | |
cookie=$2 | |
shift 2 | |
;; | |
--save-cookie) | |
savecookie=$2 | |
shift 2 | |
;; | |
--username) | |
account=$2 | |
shift 2 | |
;; | |
--password) | |
password=$2 | |
shift 2 | |
;; | |
--outdir) | |
outdir=$2 | |
shift 2 | |
;; | |
-*) | |
opts=$1 | |
shift | |
if [[ $opts =~ "h" ]]; then | |
usage | |
fi | |
if [[ $opts =~ "c" ]]; then | |
cookie=$1 | |
shift | |
fi | |
if [[ $opts =~ "s" ]]; then | |
savecookie=$1 | |
shift | |
fi | |
if [[ $opts =~ "u" ]]; then | |
account=$1 | |
shift | |
fi | |
if [[ $opts =~ "p" ]]; then | |
password=$1 | |
shift | |
fi | |
if [[ $opts =~ "o" ]]; then | |
outdir=$1 | |
shift | |
fi | |
;; | |
*) | |
ids="$ids $1" | |
shift | |
;; | |
esac | |
done | |
if [[ -z $account ]] && [[ -z $cookie ]]; then | |
echo "$PROGNAME: account user name must be specified" | |
exit 1 | |
fi | |
if [[ -z $password ]] && [[ -z $cookie ]]; then | |
echo "$PROGNAME: account password must be specified" | |
exit 1 | |
fi | |
tmpdir=`mktemp --tmpdir -d $PROGNAME.$$.XXXXXX` | |
tmpcookie="$tmpdir/tmpcookie.txt" | |
onExit() { | |
[[ -n $tmpdir ]] && rm -R $tmpdir | |
} | |
trap onExit EXIT | |
trap "trap - EXIT; onExit; exit -1" INT PIPE TERM | |
if [[ -z $cookie ]]; then | |
curl -s -c "$tmpcookie" -d mail="$account" -d password="$password" "https://secure.nicovideo.jp/secure/login?site=niconico" > /dev/null | |
if ! [[ -z $savecookie ]]; then | |
cp "$tmpcookie" "$savecookie" | |
fi | |
else | |
cp "$cookie" "$tmpcookie" | |
fi | |
for id in $ids; do | |
id=$(echo $id | grep -oE "[a-z0-9]+$") | |
echo -n "$id - " | |
thumb_info=$(curl -s -b "$tmpcookie" "http://ext.nicovideo.jp/api/getthumbinfo/$id") | |
res=$(echo $thumb_info | grep -oE "<nicovideo_thumb_response status=\"[^\"]+" | cut -c 35-) | |
if [[ $res == "fail" ]]; then | |
echo "video not found." | |
continue | |
fi | |
title=$(echo $thumb_info | grep -oE "<title>[^<]+" | cut -c 8-) | |
movie_type=$(echo $thumb_info | grep -oE "<movie_type>[^<]+" | cut -c 13-) | |
curl -s -b "$tmpcookie" -c "$tmpcookie" "http://www.nicovideo.jp/watch/$id" > /dev/null | |
video_info=`curl -s -b "$tmpcookie" "http://flapi.nicovideo.jp/api/getflv?v=$id&as3=1"` | |
if [[ $video_info == "closed=1&done=true" ]]; then | |
echo "access denied." | |
continue | |
fi | |
video_url=$(echo $video_info | grep -oE "url=[^&]+" | cut -c 5- | $urldecode) | |
filename="$id - $title.$movie_type" | |
echo "$title.$movie_type" | |
curl -b "$tmpcookie" -o "$outdir/$filename" "$video_url" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment