Skip to content

Commit

Permalink
Implement working zdf downloader
Browse files Browse the repository at this point in the history
  • Loading branch information
Christine Emrich committed Mar 12, 2017
1 parent ecebd0c commit 8f88b70
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 52 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
30 changes: 18 additions & 12 deletions downloader/show-downloader.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
const channels = require('./../models/channels');
const downloaders = [
require('./zdf-downloader')
];
const channelIds = downloaders.reduce((ids, downloader) =>
ids.concat(downloader.channelIds), []
);

exports.getShows = function() {
return channels.ids.map(function (channelId) {
return exports.getShow(channelId);
});
return Promise.all(channelIds.map(channelId => {
return exports.getShow(channelId).catch(e => {
console.error(e.message);
return null;
});
}));
};

exports.getShow = function(channelId) {
return {
title: "The Title",
subtitle: "The Subtitle",
description: "description",
channel: channelId,
startTime: new Date().getTime(),
endTime: new Date().getTime()
};
for (let downloader of downloaders) {
if (downloader.channelIds.includes(channelId)) {
return downloader.getShow(channelId);
}
}
return Promise.reject('no downloader available');
};
53 changes: 53 additions & 0 deletions downloader/zdf-downloader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const request = require('request');
const moment = require('moment-timezone');

const baseUrl = 'https://api.zdf.de/cmdm/epg/broadcasts?to=2017-03-11T12%3A58%3A42Z&limit=1&page=1&order=desc';
const headers = {
"Host": "api.zdf.de",
"Accept": "application/vnd.de.zdf.v1.0+json",
"Api-Auth": "Bearer d2726b6c8c655e42b68b0db26131b15b22bd1a32",
"Origin": "https://www.zdf.de"
};
const channelIdMap = {
"zdf": "zdf",
"dreisat": "3sat",
"kika": "ki.ka",
"phoenix": "phoenix",
"zdf_info": "zdfinfo",
"zdf_neo": "ZDFneo",
"arte": "arte",
};

exports.channelIds = ["zdf", "dreisat", "kika", "phoenix", "zdf_info", "zdf_neo", "arte"];

function getShow(json, channelId) {
let broadcast = json['http://zdf.de/rels/cmdm/broadcasts'][0];

return {
title: broadcast.title,
subtitle: broadcast.subtitle,
channel: channelId,
startTime: moment(broadcast.airtimeBegin),
endTime: moment(broadcast.airtimeEnd)
};
}

exports.getShow = function (channelId) {
let urlChannel = channelIdMap[channelId];
let url = `${baseUrl}&tvServices=${urlChannel}`;
return new Promise((resolve, reject) => {
request.get({
url: url,
json: true,
headers: headers
}, (err, res, data) => {
if (err) {
reject(err);
} else if (res.statusCode !== 200) {
return reject('wrong status code: ' + res.statusCode);
} else {
return resolve(getShow(data, channelId));
}
});
});
};
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ const app = express();
app.get('/shows', shows.findAll);
app.get('/shows/:channelId', shows.findByChannel);

app.listen(3000);
app.listen(4000);
console.log('Listening on port 3000...');
36 changes: 0 additions & 36 deletions models/channels.js

This file was deleted.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"author": "Christine Coenen",
"license": "MIT",
"dependencies": {
"express": "^4.15.2"
"express": "^4.15.2",
"moment-timezone": "^0.5.11",
"request": "^2.81.0"
}
}
22 changes: 20 additions & 2 deletions routes/shows.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
const downloader = require('./../downloader/show-downloader');

function sendErrorResponse(e, res) {
let message = e.message;
if (typeof(message) === 'undefined') {
message = e;
}

console.error(message);
res.send({error: message});
}

function sendShowsResponse(shows, res) {
res.send({shows: shows });
}

exports.findAll = function(req, res) {
res.send(downloader.getShows());
downloader.getShows()
.then(shows => sendShowsResponse(shows.filter(show => show !== null), res))
.catch(e => sendErrorResponse(e, res));
};

exports.findByChannel = function(req, res) {
res.send([downloader.getShow(req.params.channelId)]);
downloader.getShow(req.params.channelId)
.then(show => sendShowsResponse(show === null ? [] : [show], res))
.catch(e => sendErrorResponse(e, res));
};

0 comments on commit 8f88b70

Please sign in to comment.