-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
265 lines (254 loc) Β· 7.8 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
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
// this is a microservice that will read the db, via the api, and then share on linkedin via the linkedin api
const axios = require("axios");
const dayjs = require("dayjs");
// extend the dayjs library to add a custom format utc
const utc = require("dayjs/plugin/utc");
dayjs.extend(utc);
const FormData = require("form-data");
const API_PORT = "5000";
console.log(process.env.NODE_ENV);
const VISIBILITY = ["PUBLIC", "CONNECTIONS"];
(async () => {
const baseURL =
process.env.NODE_ENV === "development"
? `http://localhost:${API_PORT}`
: `https://pigeon-api.onrender.com`;
console.log("running a task every 5 minutes");
const usersUrl = `${baseURL}/api/users/linkedin/users`;
console.log("π ~ file: index.js ~ line 19 ~ usersUrl", usersUrl);
// get the data from the api async/await
const getData = async (url) => {
try {
const response = await axios.get(url);
const { data } = response;
// console.log(data);
return data;
} catch (error) {
console.log("π ~ file: index.js ~ line 32 ~ getData ~ error", error);
}
};
let users = await getData(usersUrl);
console.log("π ~ file: index.js ~ line 33 ~ users", users);
// get all posts for the users
const usersIds = users.map((user) => user.id);
// loop over the users and get the posts for each user
await usersIds.forEach(async (userId, index, array) => {
const getPosts = async () => {
const options = {
method: "GET",
url: `${baseURL}/api/posts`,
params: { userId: userId },
};
try {
const res = await axios.request(options);
console.log("π ~ file: index.js ~ line 49 ~ getPosts ~ res", res.data);
return res.data;
} catch (err) {
console.log("π ~ file: index.js ~ line 52 ~ getPosts ~ err", err);
}
};
let { posts } = await getPosts();
console.log(
"π ~ file: index.js ~ line 57 ~ await usersIds.forEach ~ posts",
posts
);
posts.forEach(async (post) => {
const user = users.find((user) => user.id === post.id);
const { accessToken, id } = user;
const { content, _id, date, isPosted, image } = post;
// get image from cloudinary link in image
const getImage = async () => {
const options = {
method: "GET",
url: image,
};
try {
const res = await axios.request(options);
console.log(
"π ~ file: index.js ~ line 71 ~ getImage ~ res",
res.data
);
return res.data;
} catch (err) {
console.log("π ~ file: index.js ~ line 74 ~ getImage ~ err", err);
}
};
let imageBuffer = await getImage();
// check if date is past, using dayjs, if yes, post to linkedin, and then delete from db
if (!isPosted && dayjs(date).isBefore(dayjs())) {
const status = await postToLinkedin(
content,
accessToken,
id,
imageBuffer
);
console.log(
"π ~ file: index.js ~ line 54 ~ posts.forEach ~ status",
status
);
if (status === 201) {
// update the post from the db with isPosted: true
const updateUrl = `${baseURL}/api/posts/${_id}`;
const updatePost = async (url) => {
try {
const body = {
isPosted: true,
id: id,
content: content,
date: date,
image: image,
};
const response = await axios.put(url, body);
const { data } = response;
console.log(
"π ~ file: index.js ~ line 62 ~ deletePost ~ data",
data
);
return data;
} catch (error) {
console.log(
"π ~ file: index.js ~ line 88 ~ updatePost ~ error",
error
);
}
};
updatePost(updateUrl);
}
}
console.log(
"π ~ file: index.js ~ line 113 ~ posts.forEach ~ image",
image
);
});
});
})();
// function to post to linkedin via the api, this will be called in the cron job, and will post to linkedin, and then delete the post from the db
const postToLinkedin = async (content, accessToken, id, image = null) => {
console.log("π ~ file: index.js ~ line 118 ~ postToLinkedin ~ image", image);
const shareUrl = "https://api.linkedin.com/v2/ugcPosts";
const headers = {
"X-Restli-Protocol-Version": "2.0.0",
Authorization: `Bearer ${accessToken}`,
};
let shareBody = {};
if (image) {
let uploadUrl = "";
let asset = "";
// Register your image to be uploaded.
const registerUploadUrl =
"https://api.linkedin.com/v2/assets?action=registerUpload";
const body = {
registerUploadRequest: {
recipes: ["urn:li:digitalmediaRecipe:feedshare-image"],
owner: `urn:li:person:${id}`,
serviceRelationships: [
{
relationshipType: "OWNER",
identifier: "urn:li:userGeneratedContent",
},
],
},
};
try {
const registerUploadResponse = await axios.post(registerUploadUrl, body, {
headers,
});
const { data } = registerUploadResponse;
uploadUrl =
data.value.uploadMechanism[
"com.linkedin.digitalmedia.uploading.MediaUploadHttpRequest"
].uploadUrl;
console.log(
"π ~ file: index.js ~ line 149 ~ postToLinkedin ~ uploadUrl",
uploadUrl
);
asset = data.value.asset;
} catch (error) {
console.log(
"π ~ file: index.js ~ line 154 ~ postToLinkedin ~ error",
error
);
}
// Upload your image to LinkedIn.
try {
// const imageBuffer = Buffer.from(image, "base64");
// create image binary from image url
const formData = new FormData();
formData.append("fileupload", image);
const uploadResponse = await axios.put(uploadUrl, formData, {
...headers,
...formData.getHeaders(),
});
const { data } = uploadResponse;
console.log(
"π ~ file: index.js ~ line 180 ~ postToLinkedin ~ data",
data
);
} catch (error) {
console.log(
"π ~ file: index.js ~ line 185 ~ postToLinkedin ~ error",
error
);
}
// Create the image share.
shareBody = {
author: `urn:li:person:${id}`,
lifecycleState: "PUBLISHED",
specificContent: {
"com.linkedin.ugc.ShareContent": {
shareCommentary: {
text: content,
},
shareMediaCategory: "IMAGE",
media: [
{
status: "READY",
description: {
text: "Center stage!",
},
media: asset,
title: {
text: "LinkedIn Talent Connect 2021",
},
},
],
},
},
visibility: {
"com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC",
},
};
} else {
shareBody = {
author: `urn:li:person:${id}`,
lifecycleState: "PUBLISHED",
specificContent: {
"com.linkedin.ugc.ShareContent": {
shareCommentary: {
text: content,
},
shareMediaCategory: "NONE",
},
},
visibility: {
"com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC",
},
};
}
try {
const response = await axios.post(shareUrl, shareBody, {
headers: headers,
});
console.log(
"π ~ file: index.js ~ line 239 ~ postToLinkedin ~ response",
response
);
return response.status;
} catch (error) {
console.log(
"π ~ file: index.js ~ line 243 ~ postToLinkedin ~ error",
error
);
return error;
}
};