Last active
September 10, 2019 02:02
-
-
Save eduardomazolini/69eee2f02d7a89bb17cb66a752e648d7 to your computer and use it in GitHub Desktop.
Usei para baixar os arquivos fora do navegado do WhatsApp Web, quando usava https://github.com/mukulhase/WebWhatsapp-Wrapper
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
import os, sys, time, json | |
from base64 import b64decode, b64encode | |
from io import BytesIO | |
from axolotl.kdf.hkdfv3 import HKDFv3 | |
from axolotl.util.byteutil import ByteUtil | |
import binascii | |
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes | |
from cryptography.hazmat.backends import default_backend | |
import mimetypes | |
import requests | |
class media_msg: pass | |
media_msg.content = '' | |
media_msg.client_url = 'https://mmg.whatsapp.net/d/f/Akj16wBZXU4Lz7ptWZwK-w4MD4CCnYp1dQJEHqgXrpC5.enc' | |
media_msg.media_key = 'B59MDEx062iyLoYYGCvVeNWcwILD2m+FFkCaYLgfkuc=' | |
media_msg.crypt_keys = {'document': '576861747341707020446f63756d656e74204b657973', | |
'image' : '576861747341707020496d616765204b657973', | |
'video' : '576861747341707020566964656f204b657973', | |
'ptt' : '576861747341707020417564696f204b657973', | |
'audio' : '576861747341707020417564696f204b657973'} | |
media_msg.type = 'ptt' | |
media_msg.filename = '' | |
media_msg.mime = 'audio/ogg; codecs=opus' | |
if (media_msg.mime.find(";")==-1): | |
extension = mimetypes.guess_extension(media_msg.mime) | |
else: | |
extension = mimetypes.guess_extension(media_msg.mime[:media_msg.mime.find(";")]) | |
media_msg.filename = ''.join([str(id(media_msg)), extension or '']) | |
def download_file(url): | |
return requests.get(url).content | |
def download_media(media_msg, force_download=False): | |
if not force_download: | |
try: | |
if media_msg.content: | |
return BytesIO(b64decode(media_msg.content)) | |
except AttributeError: | |
pass | |
file_data = download_file(media_msg.client_url) | |
if not file_data: | |
raise Exception('Impossible to download file') | |
media_key = b64decode(media_msg.media_key) | |
derivative = HKDFv3().deriveSecrets(media_key, | |
binascii.unhexlify(media_msg.crypt_keys[media_msg.type]), | |
112) | |
parts = ByteUtil.split(derivative, 16, 32) | |
iv = parts[0] | |
cipher_key = parts[1] | |
e_file = file_data[:-10] | |
cr_obj = Cipher(algorithms.AES(cipher_key), modes.CBC(iv), backend=default_backend()) | |
decryptor = cr_obj.decryptor() | |
return BytesIO( decryptor.update(e_file) + decryptor.finalize()) | |
def save_media(media_msg, path, force_download=False): | |
# gets full media | |
filename = os.path.join(path, media_msg.filename) | |
ioobj = download_media(media_msg, force_download) | |
with open(filename, "wb") as f: | |
f.write(ioobj.getvalue()) | |
return filename | |
print(save_media(media_msg,'.',force_download=True)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment