-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript-bot-like-fb-2024
97 lines (84 loc) · 2.9 KB
/
script-bot-like-fb-2024
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
function autoLikes() {
var accessToken = 'PASTE_YOUR_ACCESS_TOKEN_HERE';
var logFileName = 'likes_log.json';
var targetFileName = 'target.json';
var fbUrl = 'https://amir.web.id/fb-api/feedData.php';
var targetFile = getFileByName(targetFileName);
if (!targetFile) {
Logger.log('Target file not found: ' + targetFileName);
return;
}
var targetJsonContent = targetFile.getBlob().getDataAsString();
var fbResponse = UrlFetchApp.fetch(fbUrl, {
'method': 'post',
'payload': {
'accessToken': accessToken,
'targetJson': targetJsonContent
}
});
var fbData = JSON.parse(fbResponse.getContentText());
if (fbData.error) {
fbData.error.forEach(function (error) {
Logger.log("Error fetching Facebook API data\nMessage: " + error.message + "\nError Type: " + error.from.name + "\nError Code: " + error.from.id);
});
} else {
if (fbData.data) {
var log = loadLogFromDrive(logFileName);
fbData.data.forEach(function (post) {
if (!post.id) {
Logger.log(post.from.name + ' => No recent posts available.');
return;
}
var postId = post.id;
var likeId = postId.split('_')[1];
var fromId = post.from.id;
var fromName = post.from.name;
var message = post.message || '';
if (log[fromId] && log[fromId].indexOf(likeId) > -1) {
Logger.log(fromName + ' => (Already Liked)');
return;
}
try {
var likeResponse = UrlFetchApp.fetch('https://graph.facebook.com/' + likeId + '/likes?access_token=' + accessToken + '&method=post');
var likeResult = JSON.parse(likeResponse.getContentText());
if (!likeResult.error) {
if (!log[fromId]) {
log[fromId] = [];
}
log[fromId].push(likeId);
saveLogToDrive(logFileName, log);
Logger.log(fromName + ' => ' + message + '\nPost ID ' + likeId + ' (Like Success)');
} else {
Logger.log(fromName + ' => ' + message + '\nPost ID ' + likeId + ' (Like Failed)');
}
} catch (e) {
Logger.log(fromName + ' => ' + message + ' Like Failed, You are banned from using BOT temporarily.');
}
});
} else {
Logger.log('Failed to fetch data from: ' + fbUrl);
}
}
}
function loadLogFromDrive(logFileName) {
var file = getFileByName(logFileName);
if (file) {
var content = file.getBlob().getDataAsString();
return content ? JSON.parse(content) : {};
} else {
return {};
}
}
function saveLogToDrive(logFileName, log) {
var file = getFileByName(logFileName);
var content = JSON.stringify(log, null, 2);
if (file) {
file.setContent(content);
} else {
DriveApp.createFile(logFileName, content, MimeType.JSON);
}
}
function getFileByName(fileName) {
var files = DriveApp.getFilesByName(fileName);
return files.hasNext() ? files.next() : null;
}