forked from TylerB260/PlutoXML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.js
185 lines (138 loc) · 4.68 KB
/
init.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
var request = require("request");
var j2x = require("jsontoxml");
var moment = require("moment");
var fs = require("fs-extra");
var paste = require("better-pastebin");
var config = fs.readJSONSync("config.json");
paste.setDevKey(config.pastebin.apikey)
var plutoxml = {
grabJSON: function(callback){
callback = callback || function(){};
console.log("[INFO] Grabbing EPG...");
// check for cache
if(fs.existsSync("cache.json")){
var stat = fs.statSync("cache.json");
var now = new Date() / 1000;
var mtime = new Date(stat.mtime) / 1000;
// it's under 30 mins old
if(now - mtime <= 1800){
console.log("[DEBUG] Using cache.json, it's under 30 minutes old.");
callback(false, fs.readJSONSync("cache.json"));
return
}
}
// 2019-05-20 00:00:00.000-0500
var url = "http://api.pluto.tv/v2/channels"+
"?start="+moment().format("YYYY-MM-DD HH:00:00 ZZ")+
"&stop="+moment().add(8, "hours").format("YYYY-MM-DD HH:00:00 ZZ")+
"&deviceId=thisisatest"+
"&deviceMake=PlutoXML"+
"&deviceVersion="+config.version+
"&deviceType=web"+
"&deviceDNT=1"+
"&sid=thisisatest"+
"&appName=PlutoXML"+
"&appVersion="+config.version;
console.log(url);
request(url, function(err, code, raw){
console.log("[DEBUG] Using api.pluto.tv, writing cache.json.");
fs.writeFileSync("cache.json", raw);
callback(err || false, JSON.parse(raw));
return;
});
}
}
module.exports = plutoxml;
paste.login(config.pastebin.user, config.pastebin.pass, function(success, data) {
plutoxml.grabJSON(function(err, json){
/////////////////////////
// XMLTV Program Guide //
/////////////////////////
var tv = [];
//////////////
// Channels //
//////////////
for(var i = 0; i < json.length; i++){
var channel = json[i];
tv.push({
name: "channel",
attrs: {id: channel.slug},
children: [
{name: "display-name", text: channel.name},
{name: "display-name", text: channel.number},
{name: "desc", text: channel.summary},
{name: "icon", attrs: {src: channel.logo.path}}
]
});
//////////////
// Episodes //
//////////////
for(var i2 = 0; i2 < channel.timelines.length; i2++){
var program = channel.timelines[i2];
console.log("[INFO] Adding instance of "+program.title+" to channel "+channel.name+".");
if(!program.episode){console.log(program);}
tv.push({
name: "programme",
attrs: {
start: moment(program.start).format("YYYYMMDDHHmmss ZZ"),
stop: moment(program.stop).format("YYYYMMDDHHmmss ZZ"),
channel: channel.slug
},
children: [
{name: "title", attrs: {lang: "en"}, text: program.title},
{name: "sub-title", attrs: {lang: "en"}, text: (program.title == program.episode.name ? "" : program.episode.name)},
{name: "desc", attrs: {lang: "en"}, text: program.episode.description},
{name: "date", text: moment(program.episode.firstAired).format("YYYYMMDD")},
{name: "category", attrs: {lang: "en"}, text: program.episode.genre},
{name: "category", attrs: {lang: "en"}, text: program.episode.subGenre},
{name: "episode-num", attrs: {system: "onscreen"}, text: program.episode.number},
]
});
}
}
var epg = j2x({
tv: tv
}, {
prettyPrint: true,
escape: true
});
fs.writeFileSync("epg.xml", epg)
console.log("[SUCCESS] Wrote the EPG to epg.xml!");
////////////////
// M3U8 Tuner //
////////////////
var m3u8 = "";
for(var i = 0; i < json.length; i++){
var channel = json[i];
if(channel.isStitched){
m3u8 = m3u8 + '#EXTINF:0, channel-id="'+channel.slug+'" tvg-logo="'+channel.logo.path+'" group-title="'+channel.category+'",'+channel.name+'\n'
m3u8 = m3u8 + channel.stitched.urls[0].url + "\n\n";
console.log("[INFO] Adding "+channel.name+" channel.")
}else{
console.log("[DEBUG] Skipping 'fake' channel "+channel.name+".");
}
}
fs.writeFileSync("tuner.m3u8", m3u8)
console.log("[SUCCESS] Wrote the M3U8 tuner to tuner.m3u8!");
//////////////////////////
// Pastebin Integration //
//////////////////////////
// update the EPG.
paste.edit(config.pastebin.epg, epg, function(success) {
if(!success){
console.log("[ERROR] Couldn't update the EPG on pastebin!");
process.exit();
}
console.log("[SUCCESS] Updated the EPG on pastebin!");
// update the m3u8 tuner.
paste.edit(config.pastebin.m3u8, m3u8, function(success) {
if(!success){
console.log("[ERROR] Couldn't update the M3U8 tuner on pastebin!");
process.exit();
}
console.log("[SUCCESS] Updated the M3U8 tuner on pastebin!");
console.log("[SUCCESS] All done!");
});
});
})
})