LoginSignup
12
12

More than 5 years have passed since last update.

peco(percol)を使ってコマンドラインから作業用BGMを検索・再生する

Last updated at Posted at 2014-06-30

概要

前回の投稿の続きでニコニコから動画を検索してコマンドラインから再生します。
使ってみると思ったより実用的な感じだったので別に書きます。作業用BGMなのでコマンドラインのみで完結させてウィンドウも出さないようにします。

必要なもの

  • peco (or percol)
  • curl
  • ruby
  • mplayer

スクリプト

前回使ったnicovideo-dumpを少し修正したものを使います。
環境変数NICOVIDEO_DUMP_EMAILNICOVIDEO_DUMP_PASSWORDを事前に設定しておきます。

nicovideo-dump
#! /bin/sh

if [ $# -eq 0 ]; then
    TARGET=$(cat -)
else
    TARGET=$1
fi

if [ -z "$TARGET" ]; then
    exit 1
fi

SAVEDIR=/tmp
COOKIE=/tmp/nico_cookie
FILENAME=`echo $TARGET | ruby -e "print $<.read.chomp.split('/').pop"`

curl -s -c $COOKIE -d "mail=$NICOVIDEO_DUMP_EMAIL" -d "password=$NICOVIDEO_DUMP_PASSWORD" "https://secure.nicovideo.jp/secure/login?site=niconico" -3 -i > /dev/null
curl -s -b $COOKIE -c $COOKIE $TARGET -i > /dev/null
FLAPI_POSTFIX=`echo $FILENAME | ruby -e 'print $<.read.chomp[0,2] == "nm" ? "?as3=1" : ""'`
FLAPI_RES=`curl -s -b $COOKIE "http://flapi.nicovideo.jp/api/getflv/$FILENAME$FLAPI_POSTFIX"`
VIDEO_URL=`echo $FLAPI_RES | ruby -r cgi -e 'print CGI.unescape(Hash[*($<.read.chomp.split("&").map{|e| e.split("=")}.flatten)]["url"])'`
curl -s -b $COOKIE $VIDEO_URL
curl -s -b $COOKIE "https://secure.nicovideo.jp/secure/logout" -3 -i > /dev/null

exit 0

今回は作業用BGMを探したいのでpecoに渡す要素としてニコニコ動画から作業用BGM+αのタグで検索したものを使います。
以下のコマンドを実行すると引数なしの場合は「作業用BGM」タグ、引数を渡すと「作業用BGM」タグと引数名のタグで検索した結果がヒットします。
RSSの都合上32件しか候補が出ないですが、デフォルトではコメントが新しい順で検索されているので1時間おきに内容が適当に変わるはず。

mplayerのオプションに-novideoを指定しているので選択するとウィンドウが出ずに再生が始まります。

.zshrc
function peco-nico-bgm() {
    TAG=`echo "作業用BGM $*" | nkf -WwMQ | tr -d '\n' | tr = % | sed -e 's/%%/%/g'`
    ruby -r rss -e "RSS::Parser.parse(\"http://www.nicovideo.jp/tag/$TAG?rss=2.0\").channel.items.each {|item| puts item.link + \"\t\" + item.title}" | peco | while read line; do
        echo $line
        echo $line | awk '{print $1}' | nicovideo-dump | mplayer - -novideo
    done
}

タグ無し検索

スクリーンショット 2014-07-01 00.41.49.png

タグ追加検索

スクリーンショット 2014-07-01 00.42.08.png

便利

12
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
12