-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f01d1b
commit 1ce0299
Showing
6 changed files
with
1,106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
/node_modules | ||
/.pnp | ||
.pnp.js | ||
|
||
# testing | ||
/coverage | ||
|
||
# production | ||
/build | ||
|
||
# misc | ||
.DS_Store | ||
*.pem | ||
|
||
# debug | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# local env files | ||
.env*.local | ||
|
||
# vercel | ||
.vercel | ||
|
||
# typescript | ||
*.tsbuildinfo | ||
next-env.d.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Instagram Messages Visualizer - Server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const JSZip = require("jszip"); | ||
|
||
/** | ||
* Get the users, based on the list of folders inside the messages/inbox folder | ||
* @param {String} fileID | ||
*/ | ||
const getUsers = (fileID) => { | ||
return new Promise((resolve, reject) => { | ||
const filePath = path.join(__dirname, 'uploads', `${fileID}.zip`); | ||
|
||
fs.readFile(filePath, (err, data) => { | ||
if (err) reject(err); | ||
JSZip.loadAsync(data).then(zip => { | ||
const users = []; | ||
|
||
zip.folder("messages/inbox").forEach((relativePath, file) => { | ||
if (file.dir && (relativePath.match(/\//g) || []).length === 1) { | ||
users.push(file.name); | ||
} | ||
}); | ||
|
||
resolve(users); | ||
}).catch(err => reject(err) ); | ||
}); | ||
}); | ||
}; | ||
|
||
/** | ||
* Get the messages and the participants of the conversion based on the .json files contained in the users folder | ||
* @param {String} fileID | ||
* @param {String} user | ||
* @returns | ||
*/ | ||
const getMessages = (fileID, user) => { | ||
return new Promise((resolve, reject) => { | ||
const filePath = path.join(__dirname, 'uploads', `${fileID}.zip`); | ||
|
||
fs.readFile(filePath, (err, data) => { | ||
if (err) reject(err); | ||
|
||
JSZip.loadAsync(data).then(zip => { | ||
const messages = []; | ||
const participants = []; | ||
const promises = []; | ||
|
||
zip.folder(user).forEach((relativePath, file) => { | ||
if (file.name.endsWith('.json')) { | ||
promises.push( | ||
file.async("text").then(text => { | ||
const data = JSON.parse(text); | ||
|
||
const sortedMessages = data.messages.slice().sort((a, b) => a.timestamp_ms - b.timestamp_ms); | ||
|
||
const getMessageType = (message) => { | ||
if (message.content) return { ...message, type: 'text', sender_name: decodeURIComponent(escape(message.sender_name)), content: decodeURIComponent(escape(message.content)) }; | ||
if (message.audio_files) return { ...message, type: 'audio', sender_name: decodeURIComponent(escape(message.sender_name)), content: "has sent an audio file" }; | ||
if (message.photos) return { ...message, type: 'photo', sender_name: decodeURIComponent(escape(message.sender_name)), content: "has sent a photo" }; | ||
return null; | ||
}; | ||
|
||
const filteredMessages = sortedMessages.map(getMessageType).filter(Boolean); | ||
const filteredMessagesWithoutLikes = filteredMessages.filter(message => message.content !== "A aimé un message"); | ||
|
||
messages.push(...filteredMessagesWithoutLikes); | ||
participants.splice(0, participants.length, ...data.participants.map(participant => decodeURIComponent(escape(participant.name)))); | ||
}) | ||
); | ||
} | ||
}); | ||
|
||
Promise.all(promises) | ||
.then(() => { | ||
if (messages.length && participants.length) { | ||
resolve({ messages, participants }); | ||
} else { | ||
throw new Error('The user does not exist'); | ||
} | ||
}) | ||
.catch(err => reject(err) ); | ||
}).catch(err => reject(err) ); | ||
}); | ||
}); | ||
}; | ||
|
||
module.exports = {getUsers, getMessages}; |
Oops, something went wrong.