UNPKG

tvguide

Version:

Node module that auto-gets current (Dutch) tv show information

119 lines (106 loc) 4.41 kB
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Node.js example script to use the tvguide module * * * * The main purpose of this script is to show how to interface between the tvguide module * * and a HTML-client (in this case a web-app designed for smartphone) * * The included web folder contains an example web app, reachable at localhost:PORT * * Be sure to manually npm-install all includes! * * * * All relevant variables can be set in the User settings section below * * * * Lirc (Linux Infrared Remote Control) is also implemented to provide IR-control of your TV * * If you don't have Lirc, its sections are labeled with @OPTIONAL, so feel free to remove * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /* User settings * * To customize the channels, keep debug true and check the console for the available channels * The requests and triggers are socket.emit commands */ var PORT = 8080; var debug = true; var channels = [1, 2, 3, 4, 31, 36, 46, 92, 34, 37, 89, 25, 91, 18, 29]; var updateTrigger = 'ack:update'; var progressTrigger = 'ack:progresses'; var initRequest = 'req:init'; var initAcknowledge = 'ack:init'; /* @OPTIONAL Lirc user settings */ var lircRemote = 'LGRemote'; var lircOkCommand = 'OK'; var lircCommandRequest = 'req:command'; var lircChannelRequest = 'req:channel'; /* Includes */ var http = require('http') , express = require('express') , app = express() , server = http.Server(app) , io = require('socket.io').listen(server) , lirc = require('lirc_node') , tvguide = require('tvguide') ; /* Setup web server */ app.use(express.static(__dirname + '/web')); server.listen(PORT, null); /* @OPTIONAL setup Lirc */ lirc.init(); /* Setup and start tvguide */ tvguide.SetDebug(debug); tvguide.SetGuideTimezoneOffset(0); tvguide.SetChannels(channels, function () { /* Start auto-updating guide */ tvguide.Start(function (err, channels, guide) { if (err) throw new Error(err); /* Will be called each time there is/are new show(s) * @channels channels that have new shows * @guide the entire guide */ io.sockets.emit(updateTrigger, channels, guide); }); /* Start auto-updating show progress getter * Called every 60 s, since seconds are not calculated */ tvguide.StartProgress(60000, function (progresses) { /* Will be called every minute, @callback(updated progresses) */ io.sockets.emit(progressTrigger, progresses); }); }); /* Setup socket.io */ io.sockets.on('connection', function (socket) { /* On client request, send initialization data */ socket.on(initRequest, function () { var guide = tvguide.GetGuide(); var channels = tvguide.GetChannels(); socket.emit(initAcknowledge, channels, guide); var progresses = tvguide.GetProgresses(); socket.emit(progressTrigger, progresses); }); /* @OPTIONAL On client request, send lirc command */ socket.on(lircCommandRequest, function (command) { lirc.irsend.send_once(lircRemote, command); }); /* @OPTIONAL On client request, send lirc sequence to start a channel */ socket.on(lircChannelRequest, function (channel) { var channelNumber = parseInt(channel) + 1; var digits = channelNumber.toString().split(''); lirc.irsend.send_once(lircRemote, digits[0], function () { if (channelNumber >= 10) { lirc.irsend.send_once(lircRemote, digits[1], function () { lirc.irsend.send_once(lircRemote, lircOkCommand); }); } else { lirc.irsend.send_once(lircRemote, lircOkCommand); } }); }); });