-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
158 lines (130 loc) · 5.41 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
const dotenv = require("dotenv");
const { TwitterApi } = require("twitter-api-v2");
const jimp = require("jimp");
const axios = require("axios");
const twemoji = require('twemoji');
const mergeImg = require('merge-img');
const fs = require('fs');
dotenv.config();
const emojis = require('./resources/emoji-compact.json');
const client = new TwitterApi({
appKey: process.env.APP_KEY,
appSecret: process.env.APP_SECRET,
accessToken: process.env.ACCESS_TOKEN,
accessSecret: process.env.ACCESS_SECRET,
});
let followersList = [];
let userId = '';
let latestBlogHdl = '';
const getLatestTweetReaction = async () => {
let emoijString = [];
function getEmojis(input) {
return emojis
.filter((e) => input.indexOf(e) > -1)
.map((e) => twemoji.convert.toCodePoint(e));
}
const userReplyMention = await client.v2.userMentionTimeline(userId, { max_results: 50 });
userReplyMention.data.data.forEach((i) => {
i.text && emoijString.unshift(...getEmojis(i.text));
});
let emojiImg = emoijString.map((name) => `./resources/emojiList_72x72/${name}.png`)
.filter((path) => fs.existsSync(path)).reverse();
emojiImg.length > 10 && emojiImg.splice(10);
await mergeImg(emojiImg, { direction: true })
.then(img => {
img.write("./resources/chirp/emoji.png", () => {
console.log("Emoji Image Created.");
});
});
return "Done with Emojis";
}
const getLatestArticleHeadline = async () => {
let title = "Sorry! Can't fetch Latest title";
let result = '';
await axios.post('https://api.hashnode.com/', {
headers: {
'Content-Type': 'application/json',
},
query: `query GetUserArticles($page: Int!) {
user(username: "${process.env.HN_USR_NAME}") {
publication {
posts(page: $page) {
title
}
}
}
}`,
variables: { page: 0 }
}).then(response => {
(result = response.data.data.user.publication.posts[0].title).length > 35 ? title = `${result.substring(0, 35)}...` : title = result;
});
return title;
}
const getFollowerDetails = async () => {
const followers = await client.v2.followers(userId, { max_results: 3 });
return followers.data.map((follower) => follower.username);
}
const drawBanner = async () => {
// const font = await jimp.loadFont(jimp.FONT_SANS_32_WHITE);
// const fontSmall = await jimp.loadFont(jimp.FONT_SANS_16_WHITE);
const font = await jimp.loadFont(jimp.FONT_SANS_32_BLACK);
//const fontSmall = await jimp.loadFont(jimp.FONT_SANS_16_BLACK);
const drawImageInBanner = (type, mask, drawPos_X, drawPos_Y) => {
type === 'follower' ? (mask.resize(60, 60), mask.circle()) : mask.resize(45, 385);
banner.composite(mask, drawPos_X, drawPos_Y);
}
const writeInBanner = (font, writePos_X, writePos_Y, text) => {
banner.print(font, writePos_X, writePos_Y, text);
}
const deleteImages = () => {
try {
console.log('Removing Images.');
fs.unlinkSync(`./resources/chirp/${followersList[0]}.png`);
fs.unlinkSync(`./resources/chirp/${followersList[1]}.png`);
fs.unlinkSync(`./resources/chirp/${followersList[2]}.png`);
// fs.unlinkSync(`./resources/chirp/emoji.png`);
// fs.unlinkSync(`./resources/chirp/finalBanner.png`);
} catch (e) {
console.log(e);
}
}
const banner = await jimp.read("./resources/banner.png");
banner.resize(1500, 500);
// Recent Followers Picture update to banner
drawImageInBanner('follower', await jimp.read(`./resources/chirp/${followersList[0]}.png`), 70, 80);
drawImageInBanner('follower', await jimp.read(`./resources/chirp/${followersList[1]}.png`), 70, 145);
drawImageInBanner('follower', await jimp.read(`./resources/chirp/${followersList[2]}.png`), 70, 210);
// followersList.map((item, i) => {
// let x = 135,
// y = 100;
// writeInBanner(fontSmall, x, y + (i * 65), item);
// });
// Update the Latest Blog Headline
writeInBanner(font, 425, 440, latestBlogHdl);
// Update the Latest Tweet Reactions
drawImageInBanner('emoji', await jimp.read(`./resources/chirp/emoji.png`), 1450, 40);
banner.write("./resources/chirp/finalBanner.png", async () => {
// Comment the below statement while testing. Uncomment to make your profile banner LIVE!
await client.v1.updateAccountProfileBanner("./resources/chirp/finalBanner.png");
console.log("Banner Uploaded.");
deleteImages();
});
}
const init = async () => {
//const me = await client.v2.userByUsername('sangyk_dev');
const me = await client.v2.userByUsername(process.env.TWITTER_HANDLE);
userId = me.data.id;
//Getting Latest Tweet Reactions from twitter
const latestTweetReaction = await getLatestTweetReaction();
//Getting Latest Blog post from hashnode
latestBlogHdl = await getLatestArticleHeadline()
//Getting Latest Followers List from Twitter
followersList = await getFollowerDetails();
const savePic = async (username) => {
const URL = `https://unavatar.io/twitter/${username}`;
const image = await jimp.read(URL);
image.write(`./resources/chirp/${username}.png`);
};
Promise.all(followersList.map(savePic)).then(drawBanner)
}
init();