Skip to content

Commit

Permalink
懂球帝路由逻辑复用,外链视频处理 (DIYgod#757)
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryQW authored and DIYgod committed Sep 21, 2018
1 parent ca1afee commit 40240ad
Showing 3 changed files with 64 additions and 98 deletions.
51 changes: 2 additions & 49 deletions routes/dongqiudi/player_news.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,9 @@
const axios = require('../../utils/axios');
const cheerio = require('cheerio');
const dayjs = require('dayjs');
const utils = require('./utils');

module.exports = async (ctx) => {
const id = ctx.params.id;
const link = `https://www.dongqiudi.com/player/${id}.html`;
const api = `https://www.dongqiudi.com/data/person/archive?person=${id}`;

const axios_ins = axios.create({
headers: {
'X-Requested-With': 'XMLHttpRequest',
Referer: link,
},
});

const response = await axios_ins.get(link);
const $ = cheerio.load(response.data);
const playerName = `${$('h1.name').text()} ${$('span.en_name').text()}`;

const list = (await axios_ins.get(api)).data.data;

const out = await Promise.all(
list.map(async (item) => {
const itemUrl = item.web_url;
const res = await axios_ins.get(itemUrl);
const content = cheerio.load(res.data);

if (itemUrl.includes('/video/')) {
content('div.video').each((i, v) => {
const link = v.attribs.src;
content('div.video').replaceWith(`<video width="100%" controls> <source src="${link}" type="video/mp4"> Your RSS reader does not support video playback. </video>`);
});
}

const serverOffset = new Date().getTimezoneOffset() / 60;
const single = {
title: content('h1').text(),
guid: itemUrl,
link: itemUrl,
description: content('#con > div.left > div.detail > div:nth-child(3)').html(),
pubDate: dayjs(content('#con h4 span.time').text())
.add(-8 - serverOffset, 'hour')
.toISOString(),
author: content('#con h4 span.name').text(),
};
return Promise.resolve(single);
})
);

ctx.state.data = {
title: `${playerName} - 相关新闻`,
link,
item: out,
};
await utils.ProcessFeed(ctx, link, api);
};
51 changes: 2 additions & 49 deletions routes/dongqiudi/team_news.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,9 @@
const axios = require('../../utils/axios');
const cheerio = require('cheerio');
const dayjs = require('dayjs');
const utils = require('./utils');

module.exports = async (ctx) => {
const team = ctx.params.team;
const link = `https://www.dongqiudi.com/team/${team}.html`;
const api = `https://www.dongqiudi.com/data/team/archive?team=${team}`;

const axios_ins = axios.create({
headers: {
'X-Requested-With': 'XMLHttpRequest',
Referer: link,
},
});

const response = await axios_ins.get(link);
const $ = cheerio.load(response.data);
const teamName = $('h1.name').text();

const list = (await axios_ins.get(api)).data.data;

const out = await Promise.all(
list.map(async (item) => {
const itemUrl = item.web_url;
const res = await axios_ins.get(itemUrl);
const content = cheerio.load(res.data);

if (itemUrl.includes('/video/')) {
content('div.video').each((i, v) => {
const link = v.attribs.src;
content('div.video').replaceWith(`<video width="100%" controls> <source src="${link}" type="video/mp4"> Your RSS reader does not support video playback. </video>`);
});
}

const serverOffset = new Date().getTimezoneOffset() / 60;
const single = {
title: content('h1').text(),
guid: itemUrl,
link: itemUrl,
description: content('#con > div.left > div.detail > div:nth-child(3)').html(),
pubDate: dayjs(content('#con h4 span.time').text())
.add(-8 - serverOffset, 'hour')
.toISOString(),
author: content('#con h4 span.name').text(),
};
return Promise.resolve(single);
})
);

ctx.state.data = {
title: `${teamName} - 相关新闻`,
link,
item: out,
};
await utils.ProcessFeed(ctx, link, api);
};
60 changes: 60 additions & 0 deletions routes/dongqiudi/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const dayjs = require('dayjs');
const cheerio = require('cheerio');
const axios = require('../../utils/axios');

module.exports = {
ProcessFeed: async (ctx, link, api) => {
const axios_ins = axios.create({
headers: {
'X-Requested-With': 'XMLHttpRequest',
Referer: link,
},
});

const response = await axios_ins.get(link);
const $ = cheerio.load(response.data);
const name = `${$('h1.name').text()} ${$('span.en_name').text()}`;

const list = (await axios_ins.get(api)).data.data;
const out = await Promise.all(
list.map(async (item) => {
const itemUrl = item.web_url;
const res = await axios_ins.get(itemUrl);
const content = cheerio.load(res.data);

content('div.video').each((i, v) => {
const link = v.attribs.src;
switch (v.attribs.site) {
case 'qiniu':
content('div.video').replaceWith(`<video width="100%" controls> <source src="${link}" type="video/mp4"> Your RSS reader does not support video playback. </video>`);
break;
case 'youku':
content('div.video').replaceWith(`<iframe height='100%' width='100%' src='${link}' frameborder=0 scrolling=no webkitallowfullscreen=true allowfullscreen=true></iframe>`);
break;
default:
break;
}
});

const serverOffset = new Date().getTimezoneOffset() / 60;
const single = {
title: content('h1').text(),
guid: itemUrl,
link: itemUrl,
description: content('#con > div.left > div.detail > div:nth-child(3)').html(),
pubDate: dayjs(content('#con h4 span.time').text())
.add(-8 - serverOffset, 'hour')
.toISOString(),
author: content('#con h4 span.name').text(),
};
return Promise.resolve(single);
})
);

ctx.state.data = {
title: `${name} - 相关新闻`,
link,
item: out,
};
},
};

0 comments on commit 40240ad

Please sign in to comment.