0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

IFTTTのレシピの節約方法(1つのレシピでPCの音楽、Youtube音楽、youtube動画を操作)

Last updated at Posted at 2022-02-14

はじめに

「WSLのUbuntuでPC内の音楽データをGoogleHomeで操作する」
「WSLのUbuntuでyoutube音楽をGoogleHomeで操作する」
「WSLのUbuntuでyoutube動画をGoogleHomeで操作する」
を1つのIFTTTのレシピで操作する方法です。
IFTTTが有料になり、IFTTTのレシピ数を節約するのが目的です。
●GoogleHomeで下記の音声入力を行う。
音楽 あいみょん:PC内の音楽ファイルを再生する
音楽 YT あいみょん:youtubeの音楽を再生する
音楽 動画 あいみょん:youtubeの動画を再生する
音楽がIFTTTのトリガーコマンドです。
その後の入力文字にYT、動画があった場合、プログラムで処理を分けています。

プログラム(node.js)

●firebaseとyoutubeのKeyは自分のKeyに置き換えてください

    var firebase = require("firebase");
    var iconv = require('iconv-lite');

    //firebase config
    var config = {
        apiKey: "XXXXXXXXXXXXXXXXXXXXXXXX",
        authDomain: "XXXXXXXXXX.firebaseapp.com",
        databaseURL: "https://XXXXXXXX.firebaseio.com",
        projectId: "XXXXXXXX",
        storageBucket: "XXXXXXXX.appspot.com",
        messagingSenderId: "XXXXXXXXXXXX"
    };

    firebase.initializeApp(config);

    const Youtube = require('youtube-node');
    const youtube = new Youtube();
    youtube.setKey('XXXXXXXXXXXXXXXXXXXXXXXXXX');

    //jsonからvalueに一致する値取得
    const getJsonData = (value, json) => {
      for (var word in json)  if (value == word) return json[word]
      return json["default"]
    }

    //database更新時
    const path = "/googlehome"
    const key = "word"
    const db = firebase.database()
    db.ref(path).on("value", function(changedSnapshot) {
    //値取得
    let value = changedSnapshot.child(key).val()
    if (!value) return
    console.log(value)
    var keyword = value.replace(/ /g,"");
    const keyword2 = value.split(" ");

    flag=0;
    if(keyword2[0] === "動画") {
      flag=1;
      console.log("動画を再生します");
      var keyword = keyword.replace("動画","");
    }
    if(keyword2[0] === "YT") {
      flag=2;
      console.log("youtubeを再生します");
      var keyword = keyword.replace("YT","");
    }

    //コマンド
    const STOP = "killall mplayer mpv";

    if(keyword === "停止"){
      command = STOP
    } else if(keyword === "止め て"){
      command = STOP
    } else {
      command = "未定義"
    }
    console.log(command)

    //コマンド実行
    var exec = require('child_process').exec;
    exec(STOP);

    if(command === '未定義' && flag==1) {
      var exec = require('child_process').exec;
      exec(STOP);
      play_youtube(keyword);
    }
    if(command === '未定義' && flag==2) {
      var exec = require('child_process').exec;
      exec(STOP);
      command = "mpv --no-video --ytdl-format=bestaudio ytdl://ytsearch10:"
      var exec = require('child_process').exec;
      exec(command.concat(keyword),(err, stdout, stderr) => {
        if (err) {
          console.log(`stderr: ${stderr}`)
          return
        }
        console.log(`stdout: ${stdout}`)
      }
      )
      console.log(command.concat(keyword));
    }
    if(command === "未定義" && flag==0) {
      var exec = require('child_process').exec;
      exec(STOP);
      command = 'cat /mnt/j/music/htdocs/music/list.txt | grep "'  + keyword + '"';
      var exec = require('child_process').exec;
      exec(command, {maxBuffer: 40000*1024}, function(error, stdout, stderr) {
        if (error !== null) {
          console.log('指定された曲はありません');
          command = '/home/XXXXXX/speak.sh 指定された曲はありません'
          exec(command);
        } else {
          console.log(stdout);
        }
      });

      command = 'cat /mnt/j/music/htdocs/music/list.txt | grep "'  + keyword + '">/home/XXXXXX/playlist';
      console.log(command)
      var exec = require('child_process').exec;
      exec(command, {maxBuffer: 40000*1024}, function(error, stdout, stderr) {
        if (error !== null) {
          console.log('Exec error: ' + error);
        }
      });
      function mplay(){
      command = 'mplayer -shuffle -playlist /home/XXXXXX/playlist';
      console.log(command)
      var exec = require('child_process').exec;
      exec(command, {maxBuffer: 40000*1024}, function(error, stdout, stderr) {
        if (error !== null) {
          console.log('Exec error: ' + error);
        }
      });
      }
      setTimeout(mplay,3000);
    }
    //firebase clear
    db.ref(path).set({[key]: ""});
    })

    function play_youtube(keyword) {
      youtube.search(keyword, 1, {'type':'video'} , function(error, result) {
        if (error) {
          console.log(error);
          return ;
        }
        for (const item of result.items) {
          if (item.id.videoId) {
            var exec = require('child_process').exec;
            command = "'/mnt/c/Program Files/BraveSoftware/Brave-Browser/Application/brave.exe' --new-window 'https://www.youtube.com/watch?v="+item.id.videoId+"'"
            console.log(command);
            exec(command);
          }
        }
      });
    }

まとめ

●IFTTのレシピがいままで3つ必要だったのが1つでできました。

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?