Skip to content

Commit b8b2db9

Browse files
committed
fix: Giving profile pic as Placehoder when thumbnail not present
1 parent e946237 commit b8b2db9

4 files changed

Lines changed: 27 additions & 9 deletions

File tree

index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@ http.createServer(async (req, res) => {
1111
const timestamp = Math.floor(Date.now() / 1000);
1212
let articles = [];
1313

14-
1514
if (!username) {
1615
res.write(JSON.stringify({ error: 'Add your medium username as query string' }));
1716
res.end();
1817

1918
return;
2019
}
21-
const responseArticles = await userArticles(`${username}?t=${timestamp}`);
20+
const {articles: responseArticles, profileImgUrl} = await userArticles(`${username}?t=${timestamp}`);
2221

2322
if (!responseArticles || responseArticles.length === 0) {
2423

@@ -44,7 +43,7 @@ http.createServer(async (req, res) => {
4443
let result = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="390px" version="1.2" height="${articles.length * 120}">`;
4544

4645
await asyncForEach(articles, async (article, index) => {
47-
const articleCard = await ArticleCard(article, colors);
46+
const articleCard = await ArticleCard(article, colors,profileImgUrl);
4847
result += `<g transform="translate(0, ${index * 120})">${articleCard}</g>`;
4948
});
5049

src/ArticleCard.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
const { readingTimeCalc, imgToDataURL } = require('./utils');
22

3-
const ArticleCard = async (data, colors) => {
4-
const thumbnailBase64 = await imgToDataURL(data.thumbnail);
3+
function trimText(text, threshold) {
4+
if (text.length <= threshold) return text;
5+
return text.substr(0, threshold).concat("...");
6+
}
7+
8+
const ArticleCard = async (data, colors, placeHolderImgUrl) => {
9+
const thumbnailBase64 = await imgToDataURL(data.thumbnail,placeHolderImgUrl);
510
const articleDate = new Date(data.pubDate);
611
const readingTime = readingTimeCalc(data.content);
712
const re = /[0-9A-Fa-f]{6}/g; //hex code format
@@ -31,7 +36,7 @@ const ArticleCard = async (data, colors) => {
3136
3237
<g fill="#000000" fill-opacity="1" stroke="#000000" stroke-opacity="1" stroke-width="1" stroke-linecap="square" stroke-linejoin="bevel" transform="matrix(1,0,0,1,0,0)">
3338
34-
<text fill="${hexText ? hexText : colors.text}" fill-opacity="1" stroke="none" xml:space="preserve" x="110" y="20" font-family="Arial" font-size="15" font-weight="700" font-style="normal" >${data.title.replace(/&(?!#?[a-z0-9]+;)/g, '&amp;')}</text>
39+
<text fill="${hexText ? hexText : colors.text}" fill-opacity="1" stroke="none" xml:space="preserve" x="110" y="20" font-family="Arial" font-size="15" font-weight="700" font-style="normal" text-overflow ="ellipsis">${trimText(data.title.replace(/&(?!#?[a-z0-9]+;)/g, '&amp;'),33)}</text>
3540
</g>
3641
3742
<g fill="#000000" fill-opacity="1" stroke="#000000" stroke-opacity="1" stroke-width="1" stroke-linecap="square" stroke-linejoin="bevel" transform="matrix(1,0,0,1,0,0)">

src/mediumAPI.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const userArticles = async (username) => {
77

88
const feed = await parser.parseURL(`https://medium.com/feed/@${username}`);
99
let result = [];
10+
const imgUrl = feed.image.url;
1011

1112
feed.items.forEach((item) => {
1213
const imageObj = imageRegex.exec(item['content:encoded']);
@@ -18,7 +19,7 @@ const userArticles = async (username) => {
1819
result = [...result, item];
1920
});
2021

21-
return result;
22+
return {profileImgUrl: imgUrl, articles: result};
2223

2324
};
2425

src/utils.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,21 @@ const readingTimeCalc = (text) => {
3131
return `${displayed} min read`;
3232
}
3333

34-
const imgToDataURL = url => {
35-
return axios.get(url, { responseType: 'arraybuffer' }).then(({ data }) => sharp(data).resize(200).toBuffer()).then(data => `data:image/png;base64,${data.toString('base64')}`);
34+
const imgToDataURL = async (url,placeHolder) => {
35+
async function processImage(url) {
36+
let {data} = await axios.get(url, { responseType: 'arraybuffer' });
37+
let sharpedData = await sharp(data).resize(200).toBuffer();
38+
return `data:image/png;base64,${sharpedData.toString('base64')}`
39+
}
40+
try{
41+
return await processImage(url);
42+
}catch(e){
43+
try{
44+
return await processImage(placeHolder);
45+
}catch(e){
46+
return await processImage("https://lippianfamilydentistry.net/wp-content/uploads/2015/11/user-default.png");
47+
}
48+
}
3649
};
3750

3851
const asyncForEach = async (array, callback) => {

0 commit comments

Comments
 (0)