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
Showing
15 changed files
with
328 additions
and
47 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
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,38 @@ | ||
## Downloading Files | ||
Puppeteer takes care of the file downloading. The decryption is being done as fast as possible (outruns native methods). Supports big files! | ||
|
||
```javascript | ||
const hydraBot = require('hydra-bot'); | ||
const fs = require('fs'); | ||
const mime = require('mime-types'); | ||
|
||
(async () => { | ||
let client; | ||
// start bot service | ||
const ev = await hydraBot.initServer(); | ||
// return connection information | ||
ev.on('connection', async (conn) => { | ||
// Was connected to whatsapp chat | ||
if (conn.connect) { | ||
client = conn.client; | ||
} | ||
}); | ||
ev.on('newMessage', async (newMsg) => { | ||
// when is received | ||
if (!newMsg.result.isSentByMe) { | ||
// message received! | ||
console.log('NewMessageReceived: ', newMsg.result); | ||
// dowload files | ||
if (newMsg.result.isMedia === true || newMsg.result.isMMS === true) { | ||
const buffer = await client.decryptFile(newMsg.result); | ||
// At this point you can do whatever you want with the buffer | ||
// Most likely you want to write it into a file | ||
const fileName = `some-file-name.${mime.extension(newMsg.result.mimetype)}`; | ||
fs.writeFile(fileName, buffer, (err) => { | ||
console.log(err); | ||
}); | ||
} | ||
} | ||
}); | ||
})(); | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1 +1,5 @@ | ||
{} | ||
{ | ||
"devDependencies": { | ||
"@types/atob": "^2.1.2" | ||
} | ||
} |
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
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
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
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,54 @@ | ||
import { scraping } from './scraping.layes'; | ||
import { magix, makeOptions, puppeteerConfig, sleep } from '../../help'; | ||
import axios from 'axios'; | ||
import { Page, Browser } from 'puppeteer'; | ||
import { CreateOptions } from '../../model/interface'; | ||
|
||
export class Whatsapp extends scraping { | ||
constructor( | ||
public page: Page, | ||
public browser: Browser, | ||
public options: CreateOptions, | ||
public ev: any | ||
) { | ||
super(page, browser, options, ev); | ||
} | ||
|
||
/** | ||
* Decrypts message file | ||
* @param message Message object | ||
* @returns Decrypted file buffer (null otherwise) | ||
*/ | ||
public async decryptFile(message:any) { | ||
const options = makeOptions(puppeteerConfig.useragentOverride); | ||
message.clientUrl = | ||
message.clientUrl !== undefined | ||
? message.clientUrl | ||
: message.deprecatedMms3Url; | ||
|
||
if (!message.clientUrl) { | ||
throw new Error( | ||
'message is missing critical data needed to download the file.' | ||
); | ||
} | ||
|
||
let haventGottenImageYet: boolean = true, | ||
res: any; | ||
try { | ||
while (haventGottenImageYet) { | ||
res = await axios.get(message.clientUrl.trim(), options); | ||
if (res.status == 200) { | ||
haventGottenImageYet = false; | ||
} else { | ||
await sleep(2000); | ||
} | ||
} | ||
} catch (error) { | ||
console.error(error); | ||
throw 'Error trying to download the file.'; | ||
} | ||
const buff = Buffer.from(res.data, 'binary'); | ||
return magix(buff, message.mediaKey, message.type.toUpperCase(), message.size); | ||
} | ||
|
||
} |
Oops, something went wrong.