-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·170 lines (149 loc) · 6.54 KB
/
index.js
File metadata and controls
executable file
·170 lines (149 loc) · 6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env node
// npm modules
var DirecTV = require('directv-remote');
var program = require('commander');
// node api modules
var fs = require('fs');
var path = require('path');
// declare constants
var HOME_DIR = process.env.HOME || process.env.USERPROFILE;
var CONFIG_FILE = path.join(HOME_DIR,'.directvrc');
// declare global vars
var ipAddr,
clientAddr;
// parse command line arguments (commander)
program
.version('0.0.1')
.option('-i, --ip [address]', 'IP address for the STB [required]')
.option('-c, --client [address]', 'Client address of networked STB')
.option('-l, --locations', 'Lists the networked STBs by name and client address')
.option('-s, --system', 'Lists serial number(s) and version information')
.option('-w, --watching', 'Lists program info for show currently being watched')
.option('-k, --key [value]', 'Sends key input to the specified STB')
.option('-g, --guide [channel]', 'Lists program info for specified channel and time (-d)')
.option('-d, --date [string]', 'Specify a time to use alongside -g option (default: now)')
.option('-t, --tune [channel]', 'Tunes the STB to the specified channel')
.parse(process.argv);
// read the config file
fs.readFile(CONFIG_FILE, 'utf8', function (err, data) {
config = {};
// bail if the config file cannot be read
if (err) {
if (err.code === 'ENOENT') {
// if the config file doesn't exist, continue on
} else {
// bail if there is a weird file error
return console.error(err);
}
} else {
try {
config = JSON.parse(data);
} catch (err) {
// again, bail if the config file cannot be read
return console.log(new Error('Invalid config file: ' + CONFIG_FILE));
}
}
// hoist these vars to the global scope
ipAddr = program.ip || config.ipAddr;
clientAddr = program.client || config.clientAddr;
DirecTV.validateIP(ipAddr, runCommands);
});
function runCommands(err) {
// if the ipAddr is not that of a valid STB, exit now
if (err) return console.error(err);
var Remote = new DirecTV.Remote(ipAddr);
if (program.locations) {
Remote.getLocations(1,function(err,response) {
if (err) return console.error(err);
if (response.locations.length === 0) {
console.log('0 Set Top Boxes found');
} else {
console.log('Found', response.locations.length, 'Set Top Boxes:');
for (var stb in response.locations) {
console.log(response.locations[stb].locationName + ':', response.locations[stb].clientAddr.toUpperCase());
}
}
});
}
if (program.system) {
console.log('System information:')
Remote.getVersion(function(err,response) {
if (err) return console.error(err);
for (var property in response) {
console.log(property + ':', response[property]);
}
});
Remote.getSerialNum(undefined, function(err, response) {
if (err) return console.error(err);
for (var property in response) {
console.log(property + ':', response[property]);
}
});
}
if (program.watching) {
Remote.getTuned(clientAddr, function(err, response) {
if (err) return console.error(err);
console.log('Show:', response.title, '('+response.rating+')');
if (response.episodeTitle) console.log('Episode:', response.episodeTitle);
console.log('Channel:', response.callsign, '('+response.major+')');
var startTime = new Date(response.startTime * 1000);
var endTime = new Date((response.startTime + response.duration) * 1000);
console.log('Began:', startTime.toTimeString());
console.log('Ends:', endTime.toTimeString());
if (response.rating) console.log('Rating:', response.rating);
});
}
if (program.key) {
Remote.processKey(program.key, clientAddr, function(err, response) {
if (err) return console.error(err);
console.log('Successfully sent', response.key);
});
}
if (program.guide) {
var channel = program.guide;
var startTime;
// if a date/time was provided, parse it
if (program.date) {
// processing dates in the format epoch (seconds)
startTime = (typeof program.date === 'string') ? program.date : undefined;
// processing dates in the format 2359 === 11:59pm
if (program.date.length === 4) {
var startDate = new Date();
var hours = program.date.substring(0,2);
var minutes = program.date.substring(2,4);
startDate.setHours(hours, minutes, '00');
startTime = parseInt(startDate.getTime() / 1000)
}
// processing dates in the format 201412312359 === Dec. 31, 2014 11:59 pm
if (program.date.length === 12) {
var startDate = new Date();
var year = program.date.substring(0,4);
var month = parseInt(program.date.substring(4,6))-1;
var day = program.date.substring(6,8);
startDate.setFullYear(year, month, day);
var hours = program.date.substring(8,10);
var minutes = program.date.substring(10,12);
startDate.setHours(hours, minutes, '00');
startTime = parseInt(startDate.getTime() / 1000);
}
}
Remote.getProgInfo(channel, startTime, clientAddr, function(err, response) {
if (err) return console.err(err);
console.log('Show:', response.title, '('+response.rating+')');
if (response.episodeTitle) console.log('Episode:', response.episodeTitle);
console.log('Channel:', response.callsign, '('+response.major+')');
var startTime = new Date(response.startTime * 1000);
var endTime = new Date((response.startTime + response.duration) * 1000);
console.log('Begins:', startTime.toLocaleString());
console.log('Ends:', endTime.toTimeString());
if (response.rating) console.log('Rating:', response.rating);
});
}
if (program.tune) {
var channel = program.tune;
Remote.tune(channel, clientAddr, function(err, response) {
if (err) return console.error(err);
console.log('Successfully changed channel to', channel);
});
}
}