こちらの記事を参照して詰まったところがあったのでメモ
Google Homeで好きなポッドキャストをスマートに再生する
環境
Raspberry pi 3 ModelB
$ lsb_release -a
No LSB modules are available.
Distributor ID: Raspbian
Description: Raspbian GNU/Linux 9.4 (n/a)
Release: 9.4
Codename: n/a
$ npm -v
5.6.0
$ node -v
v9.4.0
パッケージのバージョン
feedparser@2.2.9
firebase@5.5.1
google-home-notifier@1.2.0
request@2.88.0
記事を参照して●●.jsをつくりました
$ node ●●.js
を実行すると下記のような警告がでてきて、そのままの状態になる
*** WARNING *** The program 'node' uses the Apple Bonjour compatibility layer of Avahi.
*** WARNING *** Please fix your application to use the native API of Avahi!
*** WARNING *** For more information see <http://0pointer.de/avahi-compat?s=libdns_sd&e=node>
*** WARNING *** The program 'node' called 'DNSServiceRegister()' which is not supported (or only supported partially) in the Apple Bonjour compatibility layer of Avahi.
*** WARNING *** Please fix your application to use the native API of Avahi!
*** WARNING *** For more information see <http://0pointer.de/avahi-compat?s=libdns_sd&e=node&f=DNSServiceRegister>
GoolgeHomeでpodcastを流してと命令すると
FirebaseのRealtimeDatabase
のurl
の中身が更新されるが
下記のようなエラーがでてくる
@firebase/database: FIREBASE WARNING: Exception was thrown by user callback. TypeError: ref.child(...).val is not a function
コード
上記の文献から引用しました
ref.on('child_changed', function(snapshot) {
var url = ref.child('url').val();
if (url) {
playLatestPodcast(url);
}
ref.update({'url': ''}); // 変更をリセット
});
の var url = ref.child('url').val();
を下記のように書き換えるとpodcast
を再生できるようになった
ref.on('child_changed', function(snapshot) {
var url = snapshot.val()
if (url) {
playLatestPodcast(url);
}
ref.update({'url': ''}); // 変更をリセット
});
コード全体
var FeedParser = require('feedparser');
var firebase = require('firebase');
var googleHome = require('google-home-notifier');
var request = require('request');
const lang = 'ja';
const ip = '192.168.xx.xx'; //再生したいGoogle HomeのIPアドレス
googleHome.ip(ip, lang);
const config = {
apiKey: 'xxxxxxx_xxxxxxxxxxxxxxxxxxxxx_xxxxxxx',
authDomain: 'xxxxxxxxxxxxxx.firebaseapp.com',
databaseURL: 'https://xxxxxxxxxxxxxx.firebaseio.com',
projectId: 'xxxxxxx',
storageBucket: 'xxxxxxx.appspot.com',
messagingSenderId: 'xxxxxxx'
};
firebase.initializeApp(config);
var db = firebase.database();
var ref = db.ref('/');
ref.on('child_changed', function(snapshot) {
var url = snapshot.val()
if (url) {
playLatestPodcast(url);
}
ref.update({'url': ''}); // 変更をリセット
});
function playLatestPodcast(url) {
var req = request(url);
var parser = new FeedParser();
var items = [];
req.on('response', function(res) {
this.pipe(parser);
});
parser.on('readable', function() {
while (item = this.read()) {
items.push(item);
}
});
parser.on('end', function() {
googleHome.play(getLatestPodcastUrl(items), function(notifyRes) {});
});
}
function getLatestPodcastUrl(items) {
for (item of items) {
for (enclosure of item.enclosures) {
var url = enclosure['url'];
if (url) {
return url;
}
}
}
return "";
}
参考文献
Google Homeで好きなポッドキャストをスマートに再生する
ありがとうございました