-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
101 lines (89 loc) · 2.23 KB
/
handler.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
'use strict';
const fetch = require('node-fetch');
const metascraper = require('metascraper');
const MBFC_JSON_URL = 'https://raw.githubusercontent.com/drmikecrowe/mbfcext/main/docs/v3/combined.json';
const reportingScale = {
VH: 'Very High',
H: 'High',
MF: 'Mostly Factual',
M: 'Mixed',
L: 'Low',
VL: 'Very Low',
};
const biasScale = {
L: 'Left Bias',
LC: 'Left-Center Bias',
C: 'Least Biased (Center)',
RC: 'Right-Center Bias',
R: 'Right Bias',
PS: 'Pro-Science',
CP: 'Conspiracy-Pseudoscience',
S: 'Satire',
FN: 'Questionable Sources',
};
// Re-used between invocations
let mbfcData;
let meta;
module.exports.hello = async (event) => {
// const url = event.
if (!mbfcData) {
mbfcData = await (await fetch(MBFC_JSON_URL)).json();
}
if (!meta) {
meta = metascraper([
require('metascraper-author')(),
require('metascraper-date')(),
require('metascraper-description')(),
require('metascraper-image')(),
require('metascraper-logo')(),
require('metascraper-publisher')(),
require('metascraper-title')(),
require('metascraper-url')(),
]);
}
if (!event.queryStringParameters || !event.queryStringParameters.url) {
return {
statusCode: 400,
body: {
error: 'URL not specified',
},
};
}
const { biases } = mbfcData;
const biasInfo = {
L: biases['left'],
LC: biases['left-center'],
C: biases['center'],
RC: biases['right-center'],
R: biases['right'],
PS: biases['pro-science'],
CP: biases['conspiracy'],
S: biases['satire'],
FN: biases['fake-news'],
};
const formatMbfc = (entry) => ({
siteName: entry.n,
mbfcUrl: `https://mediabiasfactcheck.com/${entry.u}`,
factualReporting: reportingScale[entry.r],
bias: biasScale[entry.b],
biasDescription: biasInfo[entry.b].description,
lastUpdated: mbfcData.date,
});
const url = event.queryStringParameters.url;
const siteUrl = url.split('/')[2].replace('www.', '');
const articleResponse = await fetch(url);
const html = await articleResponse.text();
const metadata = await meta({ html, url });
const mbfc = Object.values(mbfcData.sources).find((entry) => siteUrl === entry.d);
return {
statusCode: 200,
body: JSON.stringify(
{
...metadata,
mbfc: formatMbfc(mbfc, mbfcData.date),
},
null,
2
),
};
};