You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.4 KiB
60 lines
1.4 KiB
// gab-tv.js
|
|
// Copyright (C) 2021 Digital Telepresence, LLC
|
|
// License: Apache-2.0
|
|
|
|
'use strict';
|
|
|
|
const fetch = require('node-fetch'); // jshint ignore:line
|
|
|
|
const CACHE_DURATION = 1000 * 60 * 5;
|
|
|
|
const { SiteService } = require('../../lib/site-lib');
|
|
|
|
class GabTVService extends SiteService {
|
|
|
|
constructor (dtp) {
|
|
super(dtp, module.exports);
|
|
}
|
|
|
|
channelMiddleware (channelSlug) {
|
|
return async (req, res, next) => {
|
|
try {
|
|
res.locals.gabTvChannel = await this.getChannelEpisodes(channelSlug, { allowCache: true });
|
|
return next();
|
|
} catch (error) {
|
|
this.log.error('failed to populdate Gab TV channel', { channelSlug, error });
|
|
return next();
|
|
}
|
|
};
|
|
}
|
|
|
|
async getChannelEpisodes (channelSlug, options) {
|
|
const { cache: cacheService } = this.dtp.services;
|
|
const cacheKey = `gabtv:ch:${channelSlug}`;
|
|
|
|
options = Object.assign({
|
|
allowCache: true,
|
|
}, options);
|
|
|
|
let json;
|
|
if (options.allowCache) {
|
|
json = await cacheService.getObject(cacheKey);
|
|
if (json) {
|
|
return json;
|
|
}
|
|
}
|
|
|
|
const response = await fetch(`https://tv.gab.com/channel/${channelSlug}/feed/json`);
|
|
json = await response.json();
|
|
|
|
await cacheService.setObjectEx(cacheKey, CACHE_DURATION, json);
|
|
|
|
return json;
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
slug: 'gab-tv',
|
|
name: 'gabTV',
|
|
create: (dtp) => { return new GabTVService(dtp); },
|
|
};
|