-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtvguide.js
213 lines (192 loc) · 7.01 KB
/
tvguide.js
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*jslint node: true */
"use strict";
// will be a "module" eventually, but this code grabs the json stream
// This module should ONLY gather raw data from tvguide, and preferrably cache it to about 30 minutes. If you want to translate the data we should have a "filter" file/function that does that.
var http = require("http");
var sanitize = require("sanitize-filename");
var CONFIG = require('./config');
var Firebase = require('firebase');
var myRootRef = new Firebase(CONFIG.FB_URL);
var filter = require('./filter');
var tuner = require('./tuner');
var local_cache_lineup = [];
var get = function (date, length, callback) {
var start = date;
var a;
var options = {
host: 'mobilelistings.tvguide.com',
port: 80,
path: '/Listingsweb/ws/rest/schedules/' + CONFIG.ANTENNA + '/start/' + start + '/duration/' + length + '?ChannelFields=Name%2CFullName%2CNumber%2CSourceId&ScheduleFields=ProgramId%2CEndTime%2CStartTime%2CTitle%2CAiringAttrib%2CCatId&formattype=json&disableChannels=music%2Cppv%2C24hr&inclchTypeMask=16'
};
http.get(options, function (res) {
var str = "";
res.on('data', function (chunk) {
str += chunk;
});
res.on('end', function () {
try {
a = JSON.parse(str);
} catch (e) {
console.log(str); //error in the above string(in this case,yes)!
a = [];
}
callback(a);
});
});
};
var lineup = function (duration, refresh, res) {
if (refresh === true) {
get(Math.floor(new Date().getTime()) / 1000, duration, function (result) {
var chan = tuner.cached_channel();
var temp1 = filter.channels(chan, result);
var temp2 = filter.shows(temp1);
myRootRef.update({
"tvguide": temp2
});
console.log('refreshed cache');
local_cache_lineup = temp2;
res(temp2);
});
} else {
console.log("returned cache");
res(local_cache_lineup);
}
};
var start = function (ret) {
setInterval(function () {
lineup(CONFIG.UPDATE_FREQUENCY.duration, true, function (res) {
console.log("Updated Lineup");
});
}, CONFIG.UPDATE_FREQUENCY.interval);
lineup(CONFIG.UPDATE_FREQUENCY.duration, true, function (res) {
console.log("Updated Lineup");
ret("Success");
});
};
/*
var provider = function(zip, callback){
var options = {
host: 'mobilelistings.tvguide.com',
port: 80,
path: '/Listingsweb/ws/rest/serviceproviders/zipcode/' + zip + '?formattype=json'
};
http.get(options, function(res) {
var str = "";
res.on('data', function(chunk) {
str += chunk;
});
res.on('end', function() {
var temp = JSON.parse(str);
for (element in temp)
{
if temp[element].indexOf("broadcast") > -1){
console.log(temp[element]);
}
}
callback();
});
});
}
*/
var cached_lineup = function () {
return local_cache_lineup;
};
// Need to remove duplicates
var find = function (entire_listing, show) {
var res = [];
console.log("Searching for show" + show);
for (var key in entire_listing) {
// grab the index.
for (var item in entire_listing[key]["ProgramSchedules"]) {
if (entire_listing[key]["ProgramSchedules"][item]["Title"].indexOf(show) > -1) {
console.log(entire_listing[key]["ProgramSchedules"][item]["Title"]);
var startTime = entire_listing[key]["ProgramSchedules"][item]["StartTime"];
var endTime = entire_listing[key]["ProgramSchedules"][item]["EndTime"];
res.push({
// Firebase Workaround for not allowed to have .'s in paths
channel: entire_listing[key]["Channel"]["Number"].replace('.', '-'),
date: startTime,
length: (endTime - startTime),
startTime: startTime,
endTime: endTime,
id: entire_listing[key]["ProgramSchedules"][item]["ProgramId"],
title: entire_listing[key]["ProgramSchedules"][item]["Title"]
});
}
}
}
return res;
};
// http://mobilelistings.tvguide.com/Listingsweb/ws/rest/airings/20385.268435456/start/1422793800/duration/20160/search?search=Big%20Bang%20Theory&formattype=json
var search = function (program_name, callback) {
var options = {
host: 'mobilelistings.tvguide.com',
port: 80,
path: '/Listingsweb/ws/rest/airings/20385.268435456/start/1422793800/duration/20160/search?search=' + program_name
};
http.get(options, function (res) {
var str = "";
res.on('data', function (chunk) {
str += chunk;
});
res.on('end', function () {
var result = JSON.parse(str);
callback(result);
});
});
};
var name = function (id, res) {
// http://mapi.tvguide.com/listings/details?program=
var options = {
host: 'mapi.tvguide.com',
port: 80,
path: '/listings/details?program=' + id
};
http.get(options, function (result) {
var str = "";
result.on('data', function (chunk) {
str += chunk;
});
result.on('end', function () {
var result = JSON.parse(str);
console.log({program : result["program"]});
sanatize_name(result["program"], function (locresult) {
console.log(locresult);
res(locresult);
});
});
});
};
var sanatize_name = function (program, res) {
if (program != null) {
if (program["season"] != null && program["episode"] != null && program["episode_title"] != null) {
res(sanitize("S" + program["season"] + ".E" + program["episode"] + "." + program["title"] + "." + program["episode_title"]).replace(/ /g, "_"));
return;
}
if (program["episode_title"] != null) {
res(sanitize(program["episode_title"]).replace(/ /g, "_"));
return;
} else {
res(sanitize(program["title"]).replace(/ /g, "_") + "." + new Date().getTime());
return;
}
}
};
// prints all the data in a lineup, previously the program schedules was just an array of objects when printed.
var print = function() {
for (element in cached_lineup){
console.log(cached_lineup[element].Channel);
for (show in cached_lineup[element].ProgramSchedules){
console.log(cached_lineup[element].ProgramSchedules[show]);
}
}
}
module.exports = {
start: start,
lineup: lineup,
find: find,
name: name,
cached_lineup: cached_lineup,
print : print,
get: get
}