-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
274 lines (259 loc) · 9.71 KB
/
app.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
var express = require('express');
var app = express();
app.use(express.json());
const { getDevData, getMediumData, getHashnodeBlog, getLatestHashnodeBlog } = require('./datafetcher')
const { blogCardH, blogCardV } = require('./MediumblogCard');
const { DblogCardH, DblogCardV } = require('./DevblogCard');
const { hashnodeBlogCard, hashnodeLastestBlogCard } = require('./hashnodeBlogCard');
const asyncForEach = async (array, callback) => {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array)
}
};
app.get('/', async (request, response) => {
response.writeHead(301, { Location: 'https://souravdey777.github.io/Github-Cards-External-Blogs/' });
response.end();
})
app.get('/getMediumBlogs', async (request, response) => {
try {
if (!request.query.username) {
response.write(JSON.stringify({ error: 'your medium username is require in the query string' }));
response.end();
return;
}
const username = request.query.username;
let limit = 5;
let type = 'vertical'
if (request.query.type) {
type = request.query.type;
}
if (request.query.limit) {
limit = request.query.limit;
}
const resultData = (await getMediumData(username));
let result = `<svg>`;
if (type === 'horizontal') {
result = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="${resultData.length * 200}" version="1.2" height="310">`;
await asyncForEach(resultData, async (blog, index) => {
if (index >= limit) {
return;
}
const blogCardObj = await blogCardH(blog);
result += `<g transform="translate(${index * 200}, 0)">${blogCardObj}</g>`;
});
} else {
result = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="400" version="1.2" height="${resultData.length * 160}">`;
await asyncForEach(resultData, async (blog, index) => {
if (index >= limit) {
return;
}
const blogCardObj = await blogCardV(blog);
result += `<g transform="translate(0, ${index * 160})">${blogCardObj}</g>`;
});
}
result += `</svg>`;
response.writeHead(200, { 'Content-Type': 'image/svg+xml' });
response.write(result);
response.end();
} catch (error) {
console.log(error);
response.send('Error while fetching the data' + error);
}
});
app.get('/getDevBlogs', async (request, response) => {
try {
if (!request.query.username) {
response.write(JSON.stringify({ error: 'your medium username is require in the query string' }));
response.end();
return;
}
const username = request.query.username;
let limit = 5;
let type = 'vertical'
if (request.query.type) {
type = request.query.type;
}
if (request.query.limit) {
limit = request.query.limit;
}
const resultData = (await getDevData(username));
let result = `<svg>`;
if (type === 'horizontal') {
result = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="${resultData.length * 200}" version="1.2" height="310">`;
await asyncForEach(resultData, async (blog, index) => {
if (index >= limit) {
return;
}
const blogCardObj = await DblogCardH(blog);
result += `<g transform="translate(${index * 200}, 0)">${blogCardObj}</g>`;
});
} else {
result = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="400" version="1.2" height="${resultData.length * 160}">`;
await asyncForEach(resultData, async (blog, index) => {
if (index >= limit) {
return;
}
const blogCardObj = await DblogCardV(blog);
result += `<g transform="translate(0, ${index * 160})">${blogCardObj}</g>`;
});
}
result += `</svg>`;
response.writeHead(200, { 'Content-Type': 'image/svg+xml' });
response.write(result);
response.end();
} catch (error) {
console.log(error);
response.send('Error while fetching the data' + error);
}
});
app.get('/getMediumBlogsByID', async (request, response) => {
try {
if (!request.query.username) {
response.write(JSON.stringify({ error: 'your medium username is require in the query string' }));
response.end();
return;
}
const username = request.query.username;
let limit = 5;
let type = 'vertical'
if (request.query.type) {
type = request.query.type;
}
if (request.query.limit) {
limit = request.query.limit;
}
let data = (await getMediumData(username));
const resultData = [];
resultData.push(data[request.query.id])
let result = `<svg>`;
if (type === 'horizontal') {
result = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="${resultData.length * 200}" version="1.2" height="310">`;
await asyncForEach(resultData, async (blog, index) => {
if (index >= limit) {
return;
}
const blogCardObj = await blogCardH(blog);
result += `<g transform="translate(${index * 200}, 0)">${blogCardObj}</g>`;
});
} else {
result = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="400" version="1.2" height="${resultData.length * 160}">`;
await asyncForEach(resultData, async (blog, index) => {
if (index >= limit) {
return;
}
const blogCardObj = await blogCardV(blog);
result += `<g transform="translate(0, ${index * 160})">${blogCardObj}</g>`;
});
}
result += `</svg>`;
response.writeHead(200, { 'Content-Type': 'image/svg+xml' });
response.write(result);
response.end();
} catch (error) {
console.log(error);
response.send('Error while fetching the data' + error);
}
});
// Hashnode Blogs
app.get('/getHashnodeBlog', async (request, response) => {
try {
if (!request.query.url) {
response.write(JSON.stringify({ error: 'URL parameters are missing!' }));
response.end();
return;
}
var { hostname, pathname } = new URL(request.query.url);
console.log(hostname, pathname)
const slug = pathname.replace("/", "");
const large = request.query.large === "true" ? true : false
const resultData = (await getHashnodeBlog(slug, hostname));
if (!resultData.data.post) {
response.write(JSON.stringify({ error: 'Post does not exits!' }));
response.end();
return;
}
const blogCardObj = await hashnodeBlogCard(resultData.data.post, hostname, large);
response.writeHead(200, { 'Content-Type': 'image/svg+xml' });
response.write(blogCardObj);
response.end();
} catch (error) {
console.log(error);
response.send('Error while fetching the data' + error);
}
});
app.get('/getLatestHashnodeBlog', async (request, response) => {
try {
if (!request.query.username) {
response.write(JSON.stringify({ error: 'Query parameters are missing!' }));
response.end();
return;
}
const { username } = request.query;
let limit = 3;
if (request.query.limit) {
limit = request.query.limit;
if (limit > 6) {
response.write(JSON.stringify({ error: 'limit parameters is more than 6!' }));
response.end();
}
}
const large = request.query.large === "true" ? true : false
const resultData = (await getLatestHashnodeBlog(username));
if (!resultData.data.user.publication.posts.length) {
response.write(JSON.stringify({ error: 'Post does not exits!' }));
response.end();
return;
}
let result = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="${limit * (large ? 304 : 176)}" version="1.2" height="${(large ? 530 : 310)}">`;
await asyncForEach(resultData.data.user.publication.posts, async (blog, index) => {
if (index >= limit) {
return;
}
const blogCardObj = await hashnodeBlogCard(blog, resultData.data.user.publicationDomain, large);
result += `<svg x="${index * (large ? 304 : 176)}" y="0">${blogCardObj}</svg>`;
});
result += `</svg>`;
response.writeHead(200, { 'Content-Type': 'image/svg+xml' });
response.write(result);
response.end();
} catch (error) {
console.log(error);
response.send('Error while fetching the data' + error);
}
});
app.get('/getHashnodeBlogBySequence', async (request, response) => {
try {
if (!request.query.username || !request.query.sequence) {
response.write(JSON.stringify({ error: 'Query parameters are missing!' }));
response.end();
return;
}
else if (request.query.sequence < 1) {
response.write(JSON.stringify({ error: 'Please enter a valid sequence!' }));
response.end();
return;
}
const large = request.query.large === "true" ? true : false
const { username, sequence } = request.query;
var sequenceBy6 = sequence % 6;
sequenceBy6 = (sequenceBy6 === 0 ? 6 : sequenceBy6)
const page = parseInt((sequence - 1) / 6)
const resultData = (await getLatestHashnodeBlog(username, page));
if (!resultData.data.user.publication.posts[sequenceBy6 - 1]) {
response.write(JSON.stringify({ error: 'Post does not exits!' }));
response.end();
return;
}
const blogCardObj = await hashnodeBlogCard(resultData.data.user.publication.posts[sequenceBy6 - 1], resultData.data.user.publicationDomain, large);
response.writeHead(200, { 'Content-Type': 'image/svg+xml' });
response.write(blogCardObj);
response.end();
} catch (error) {
console.log(error);
response.send('Error while fetching the data' + error);
}
});
var port = process.env.PORT || 3000;
app.listen(port, function () {
console.log('Server listening ' + port);
});