-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
267 lines (206 loc) · 8.58 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
266
267
/*
بِسْمِ اللهِ الرَّحْمٰنِ الرَّحِيْمِ ﷺ
InshaAllah, By his marcy I will Gain Success
*/
import path, { resolve } from 'path';
import { __dirname, FACEBOOK_APP_ID, FACEBOOK_CLIENT_SECRET, FACEBOOK_PAGE_ID, FB_USER_ID, IG_ID, REDIRECT_URI, require } from './Env.js'
import express from 'express'
import fetch, { FormData } from 'node-fetch';
import { error, log } from 'console';
import fs from 'fs'
import initializeVideoUplaod from './controlars/fb/initializeVideoUplaod.js';
import { breakJsonData, makeUrlWithParams } from 'string-player';
import Awaiter from 'awaiter.js';
import VideoUploader from './controlars/fb/videoUpload.js';
import Facebook from './Facebook.js';
import catchError, { namedErrorCatching } from './controlars/fb/catchError.js';
import Instagram from './Instagram.js';
const app = express();
app.use(express.static(path.resolve(__dirname, './public')))
let fb=new Facebook({
client_id :FACEBOOK_APP_ID,
client_secret :FACEBOOK_CLIENT_SECRET,
redirect_uri :REDIRECT_URI,
})
app.get('/', (req, res) => res.redirect('/Auth.html'));
app.get("/facebook/login", (req, res) => {
res.redirect(fb.getAuthUrl());
});
app.get('/facebook/callback', async function (req, res) {
try {
let { code } = req.query, writingObject = new Object();
if (!code) return res.status(400).send("Authorization code missing");
let access_token = await fb.getAccessToken(code);
writingObject.user_access_token = access_token;
let user_id=await fb.getUserID(access_token);
let page=await fb.getPages(user_id,access_token);
writingObject.user_id=user_id;
writingObject.page_id=page.id;
writingObject.page_access_token=page.access_token;
let P = new fb.Page({ pageid: writingObject.page_id, page_accessToken: writingObject.page_access_token });
writingObject['Instagram_id']=await P.getLinkedInstagramAccounts();
log(writingObject);
fs.writeFileSync(resolve(__dirname,'./fb.json'),breakJsonData(writingObject));
return res.status(200).send(breakJsonData(writingObject));
} catch (error) {
catchError(res,error)
}
})
app.get('/video',
//the idea of publishing video like this to facebook page was taken from stackOverflow
//that is the reason i have keep the name of function stackOverflowIdeaFacebookPageVideoUpload(){}
async function (req,res) {
try {
let page =new fb.Page({
page_accessToken :require('./fb.json').page_access_token,
pageid :require('./fb.json').page_id
});
let id= await page.uploadVideo({
file_url :'https://gojushinryu.com/video-for-download',
description :"test video for api practice"
})
return res.json({id});
} catch (error) {
catchError(res,error)
}
}
)
app.get('/ig/single/image', async function (req, res) {
try {
let ints=new Instagram({
accessToken :require('./fb.json').user_access_token,
id:require('./fb.json').Instagram_id
})
let creation_id=await ints.uploadSingleImage({
caption: 'Hello , I am testing instegram api',
image_url: 'https://gojushinryu.com/img/aboutusarticleImage.jpg'
})
let post_id=await ints.publishCreation(creation_id);
return res.json({post_id});
} catch (error) {
catchError(res,error);
}
})
app.get('/ig/images', async function (req, res) {
try {
let instagram=new Instagram({
accessToken :require('./fb.json').user_access_token, id : require('./fb.json').Instagram_id
});
let post_id= await instagram.createCarusel({
images: ['https://gojushinryu.com/img/IMG-20240907-WA0006.jpg', 'https://gojushinryu.com/img/IMG-20240907-WA0008.jpg'],
caption: "testing instagram api"
});
res.json({post_id});
return;
} catch (error) {
return catchError(res,error);
}
})
app.get('/ig/video', async function (req, res) {
try {
let ints=new Instagram({
accessToken :require('./fb.json').user_access_token,
id:require('./fb.json').Instagram_id
});
let creation_id=await ints.uploadReel({video_url:'https://gojushinryu.com/video-for-download',caption:'testing video upload'});
await ints.checkFinishUploadOrNot(creation_id);
let post_id=await ints.publishCreation(creation_id);
return res.json({post_id});
} catch (error) {
return catchError(res,error);
}
try {
let params = (new URLSearchParams({
video_url: 'https://gojushinryu.com/video-for-download',
media_type: 'REELS',
caption: 'Testing video upload',
access_token: require('./fb.json').access_token
})).toString();
let response = await fetch(`https://graph.facebook.com/v21.0/${IG_ID}/media?${params}`, { method: 'POST' });
response = await response.json();
console.log(response)
if (response.error) throw response.error
let containerID = response.id;
async function checkFinishReelsUpload(containerID) {
let params = (new URLSearchParams({
fields: 'status_code',
access_token: require('./fb.json').access_token
})).toString();
let response = await fetch(`https://graph.facebook.com/v21.0/${containerID}?${params}`);
response = await response.json();
console.log(response);
if (response.error) throw response.error
if (response.status_code !== "FINISHED") return false;
if (response.status_code === "FINISHED") return true;
}
if (containerID) {
for (let index = 0; index < 30; index++) {
let status = await checkFinishReelsUpload(containerID);
if (status) {
index = 31;
}
if (!status) {
await Awaiter(3000);
}
};
let params = (new URLSearchParams({
creation_id: containerID,
access_token: require('./fb.json').access_token
})).toString()
let response = await fetch(`https://graph.facebook.com/v21.0/${IG_ID}/media_publish?${params}`, { method: 'POST' });
response = await response.json();
console.log({
...response
});
if (response.error) throw response.error
if (!response.id) throw response
return res.status(201).json({
success: true,
publish_id: response.id
});
}
} catch (error) {
catchError(res, error);
}
})
app.get('/fb/image', async function (req, res) {
try {
let page =new fb.Page({
page_accessToken :require('./fb.json').page_access_token,
pageid :require('./fb.json').page_id
});
let url = [
'https://gojushinryu.com/img/IMG-20240907-WA0006.jpg',
'https://gojushinryu.com/img/IMG-20240907-WA0008.jpg'
];
let facebookImagesId = [];
for (let i = 0; (i < url.length && i <=9); i++) {
let media_fbid=await page.uploadPhoto(url[i]).then(id=> id.media_fbid).catch(error => namedErrorCatching('upload photo error', error));
facebookImagesId.push({media_fbid});
}
let id=await page.postWithImages(facebookImagesId,'testing images upload').catch(e => namedErrorCatching('post-images-error',e));
return res.status(201).json({ id })
} catch (error) {
catchError(res,error);
}
})
app.get('/my-info', async function (req, res) {
try {
let response = await fetch(`https://graph.facebook.com/v21.0/me/accounts?access_token=${require('./fb.json').user_access_token}`);
response = await response.json();
let access_token=response.data[0].access_token,
page_id=response.data[0].id;
response=await fetch(`https://graph.facebook.com/v21.0/${page_id}?fields=instagram_business_account&access_token=${access_token}`);
response=await response.json();
if (response.instagram_business_account?.id){
let ig_id=response.instagram_business_account.id;
log(ig_id);
}
else {
throw 'error , failed to gain intagram ig id'
}
} catch (error) {
catchError(res,error);
}
})
app.listen(3000)