-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
177 lines (149 loc) Β· 4.48 KB
/
index.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
'use strict';
const getQuote = require('forbes-quote');
const core = require('@actions/core');
const fetch = require('isomorphic-unfetch');
const path = require('path');
const http = require('https');
const fs = require('fs');
const config = require('./src/config');
const { notBlankOrElse } = require('./src/utils');
async function getPlots(input) {
if (!input) {
return Promise.resolve({ options: [] });
}
const urlToFetch = `https://api.plot.ly/v2/search?q=${input}`;
const data = await fetch(urlToFetch);
const json = await data.json();
return {
options: json.files.map(o => {
return {
label: `${o.filename} by ${o.owner}, ${o.views} views`,
value: `${o.web_url.replace(/\/$/, '')}.json`,
};
}),
};
}
async function getTagsByCode(tags) {
return tags.reduce((acc, tag) => {
return { ...acc, [tag.code]: { ...tag, id: tag.code } };
}, {});
}
async function getData(url) {
const data = await fetch(url);
const json = await data.json();
return {
options: json.map(o => {
return {
label: o.name,
name: o.full_name,
description: o.description,
stars: o.stars,
trends: o.trends,
url: o.url,
npm: o.npm,
icon: o.icon,
downloads: o.downloads,
};
}),
};
}
async function getQuotesData(url) {
const data = await fetch(url);
const json = await data.json();
return json.map(o => {
return {
quote: o.quoteText,
author: o.quoteAuthor,
};
});
}
require('https').globalAgent.options.rejectUnauthorized = false;
async function fetchAsync(url) {
// await response of fetch call
try {
const response = await fetch(url);
// only proceed once promise is resolved
return await response.json();
} catch (e) {
console.error(e);
return null;
}
}
async function createData(url, filePath, fileName, fileExtension) {
try {
const imagePath = path.join(filePath, `${fileName}.${fileExtension}`);
console.log(`Generating screenshot with parameters: url=${url}, file=${imagePath}\n`);
if (!fs.existsSync(filePath)) {
fs.mkdirSync(filePath);
}
const image = fs.createWriteStream(imagePath);
await http.get(url, resp => {
resp.pipe(image);
});
return imagePath;
} catch (e) {
console.error(e);
}
}
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
const escape = str => {
return (
str
.replace(/\\'/g, "\\'")
.replace(/\\"/g, '\\"')
.replace(/\\&/g, '\\&')
.replace(/\\r/g, '\\r')
.replace(/\\t/g, '\\t')
.replace(/\\b/g, '\\b')
.replace(/\\f/g, '\\f')
// remove new lines
.replace(/\r?\n|\r/g, '')
// eslint-disable-next-line no-control-regex
.replace(/[\u0000-\u0019]+/g, '')
.trim()
);
};
const random = max => {
return Math.floor(Math.random() * max);
};
async function run() {
// const url = core.getInput('url');
// const width = notBlankOrElse(core.getInput('width'), config.width);
// const height = notBlankOrElse(core.getInput('height'), config.height);
// const fileName = notBlankOrElse(core.getInput('name'), config.name);
// const filePath = notBlankOrElse(core.getInput('path'), config.path);
// const fileExtension = notBlankOrElse(core.getInput('extension'), config.extension);
// const data = getData(config.routes.get_weekly_newsletter);
for (let i = 0; i < 1; i++) {
//await delay(1000);
try {
const data = await fetchAsync('https://www.forbes.com/forbesapi/thought/uri.json?enrich=true&query=1');
console.log(data);
for (const item in data) {
const result = JSON.stringify({
quote: escape(item.thought.quote),
author: escape(item.thought.thoughtAuthor.name),
});
console.log(`${result},`);
}
} catch (e) {
// empty
}
// const data = await fetchAsync('https://swquotesapi.digitaljedi.dk/api/SWQuote/RandomStarWarsQuote');
// if (data) {
// const index = data.content.lastIndexOf('β ');
// const result = JSON.stringify({
// quote: escape(data.content.substring(0, index)),
// author: escape(data.content.substring(index + 1)),
// });
// console.log(`${result},`);
// }
}
//const target = `${config.url}?url=${url}&width=${width}&height=${height}`;
//const imagePath = await createData(target, filePath, fileName, fileExtension);
//core.setOutput('image', imagePath);
}
module.exports = run;
if (require.main === module) {
run();
}