1 | import chalk from 'chalk';
|
2 | const utils = require('../lib/utils');
|
3 | const debug = utils.debug;
|
4 | const fetch = require('node-fetch');
|
5 |
|
6 | const fetchStats = function(collectiveUrl) {
|
7 | const url = `${collectiveUrl}.json`;
|
8 | return fetch(url, { timeout: 1500 })
|
9 | .then(function(res) {
|
10 | return res.json();
|
11 | })
|
12 | .then(function(json) {
|
13 | return {
|
14 | currency: json.currency,
|
15 | balance: json.balance,
|
16 | yearlyIncome: json.yearlyIncome,
|
17 | backersCount: json.backersCount,
|
18 | contributorsCount: json.contributorsCount
|
19 | };
|
20 | })
|
21 | .catch(function(e) {
|
22 | const collectiveSlug = collectiveUrl.substr(collectiveUrl.lastIndexOf('/')+1);
|
23 | console.error(`${chalk.red(`[server error]`)} Cannot load the stats for ${collectiveSlug} – please try again later`);
|
24 | debug("Error while fetching ", url);
|
25 | });
|
26 | }
|
27 |
|
28 | const fetchBanner = function(slug) {
|
29 | const url = `https://opencollective.com/${slug}/banner.md`;
|
30 | return fetch(url)
|
31 | .then(function(res) {
|
32 | return res.text();
|
33 | })
|
34 | .catch(function(e) {
|
35 | debug("Error while fetching ", url);
|
36 | });
|
37 | }
|
38 |
|
39 | const fetchLogo = function(logoUrl) {
|
40 | if (!logoUrl.match(/^https?:\/\//)) {
|
41 | return "";
|
42 | }
|
43 | return fetch(logoUrl, { timeout: 1500 })
|
44 | .then(function(res) {
|
45 | if (res.status === 200 && res.headers.get('content-type').match(/^text\/plain/)) return res.text();
|
46 | else return "";
|
47 | })
|
48 | .catch(function(e) {
|
49 | debug("Error while fetching ", logoUrl);
|
50 | });
|
51 | }
|
52 |
|
53 | module.exports = {
|
54 | fetchLogo, fetchStats, fetchBanner
|
55 | }; |
\ | No newline at end of file |