This repository has been archived by the owner on Aug 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_media_to_comments_and_replies.js
157 lines (146 loc) · 5.81 KB
/
extract_media_to_comments_and_replies.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
var fs = require('fs');
const { sanitazeMessage , sanitazeAttachment } = require('./sanitaze')
var crc32 = require('js-crc').crc32;
const axios = require('axios')
var username_uid = require('./username_uid.json')
const api_keys_by_uid = require('./api_keys_by_uid.json')
function extractFacebookUrl(u) {
//console.log(u)
u = u.substr(u.indexOf('l.php?u=') + 8); // remove before ?u=
u = u.substr(0, u.indexOf('&')); // remove after &
return decodeURIComponent(u);
}
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
function compare( a, b ) {
if ( a.utime < b.utime ){
return -1;
}
if ( a.utime > b.utime ){
return 1;
}
return 0;
}
let filenames = fs.readdirSync('./scrapped_v2/');
found=false;
asyncForEach(filenames, async filename => {
if(!found)
if(filename != "903863886392401.json")
return;
else{
found=true
}
console.log(filename)
let fbpost = require('./scrapped_v2/'+filename)
let temp_forum = require('./forum_posts/'+filename)
console.log(temp_forum.data.id)
await asyncForEach(fbpost.comments, async (comment, commentI) => {
let permacleaned = cleanUserPermalink(comment.author.permalink);
let hashed = crc32(permacleaned);
let g = sanitazeMessage(comment.html);
let userContentHTML = g.messageHTML;
let imgs1 = g.imgs;
g = sanitazeAttachment(comment.attachsHTML);
let attachsHTML = g.attachmentHTML
let imgs2 = g.imgs;
let imgs = imgs1.concat(imgs2);
let raw = userContentHTML
+ (attachsHTML != "" || imgs.length ? "\r\n **Adjuntos:** \r\n"+attachsHTML + imgs.join("\r\n") : "" )
raw = raw.trim() != "" ? raw : "*Contenido no disponible*";
//let fb_commentid = extractUrlValue("comment_id",comment.permalink);
let res;
try{
res = await axios({
method: 'post',
url: 'https://1btcarg.com/posts.json',
data: {
raw,
//+ ("\r\n **Fecha original:** \r\n"+ timeConverter(parseInt(comment.utime)))
//+ ("\r\n *fb_commentid:* \r\n"+ fb_commentid),
topic_id: temp_forum.data.topic_id,
created_at: new Date(comment.utime*1000).toJSON()
},
headers: {
'Api-Key': api_keys_by_uid[username_uid[hashed]],
'Api-Username': hashed
}
});
}catch(e){
console.log(e.toJSON())
process.exit()
}
fs.writeFileSync(`./forum_replies/${fbpost.id}.${commentI}.json`,JSON.stringify({fbPostId: fbpost.id, commentI, data: res.data}));
console.log("reply to topic id: "+temp_forum.data.id )
let post_number = res.data.post_number;
await asyncForEach(comment.replies, async (reply,replyI) => {
let permacleaned = cleanUserPermalink(reply.author.permalink);
let hashed = crc32(permacleaned);
//let fb_commentid = extractUrlValue("comment_id",comment.permalink);
let g = sanitazeMessage(reply.html);
let userContentHTML = g.messageHTML;
let imgs1 = g.imgs;
g = sanitazeAttachment(reply.attachsHTML);
let attachsHTML = g.attachmentHTML
let imgs2 = g.imgs;
let imgs = imgs1.concat(imgs2);
let raw = userContentHTML
+ (attachsHTML != "" || imgs.length ? "\r\n **Adjuntos:** \r\n"+attachsHTML + imgs.join("\r\n") : "" );
raw = raw.trim() != "" ? raw : "*Contenido no disponible*"
let res;
try {
res = await axios({
method: 'post',
url: 'https://1btcarg.com/posts.json',
data: {
topic_id: temp_forum.data.topic_id,
reply_to_post_number: post_number,
raw,
//+ ("\r\n **Fecha original:** \r\n"+ timeConverter(parseInt(comment.utime)))
//+ ("\r\n *fb_commentid:* \r\n"+ fb_commentid),
created_at: new Date(reply.utime*1000).toJSON()
},
headers: {
'Api-Key': api_keys_by_uid[username_uid[hashed]],
'Api-Username': hashed
}
});
}catch(e){
console.log(e.toJSON())
process.exit()
}
fs.writeFileSync(`./forum_replies/${fbpost.id}.${commentI}.${replyI}.json`,JSON.stringify({fbPostId: fbpost.id, commentI, replyI, data: res.data}));
console.log("reply to reply id: "+post_number )
});
});
});
function timeConverter(UNIX_timestamp){
var a = new Date(UNIX_timestamp * 1000);
var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var year = a.getFullYear();
var month = months[a.getMonth()];
var date = a.getDate();
var hour = a.getHours();
var min = a.getMinutes();
var sec = a.getSeconds();
var time = date + ' ' + month + ' ' + year + ' ' + hour + ':' + min + ':' + sec ;
return time;
}
function cleanUserPermalink(permalink){
permalink = permalink.replace(/https:\/\/www.facebook.com\//, ``);
permalink = permalink.replace(/profile.php\?id=/, ``);
permalink = permalink.replace(/\&[^]*$/, ``);
permalink = permalink.replace(/\?[^]*$/, ``);
permalink = permalink.replace(/\/$/, ``);
permalink = permalink.replace(/file:\/\/\/F:\//, ``);
return permalink;
}
function extractUrlValue(key, url)
{
if (typeof(url) === 'undefined')
url = window.location.href;
var match = url.match('[?&]' + key + '=([^&]+)');
return match ? match[1] : null;
}