Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Generic Media Decoder #143

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions backend/whatsapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
import hashlib;
import hmac;
import traceback;

import urllib2;
import mimetypes;
import websocket;
import curve25519;
import pyqrcode;
Expand Down Expand Up @@ -82,6 +83,7 @@ class WhatsAppWebClient:
onMessageCallback = None;
onCloseCallback = None;
activeWs = None;
location = r'''/usr/src/app/media/'''; ## Change Location of media as per requirement
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely need to set a sane default location for this to be safe to merge.

messageSentCount = 0;
websocketThread = None;
messageQueue = {}; # maps message tags (provided by WhatsApp) to more information (description and callback)
Expand Down Expand Up @@ -111,7 +113,35 @@ def __init__(self, onOpenCallback, onMessageCallback, onCloseCallback):
websocket.enableTrace(True);
self.connect();


def decodeMedia(self, mediaMessage):
info = media = ''
if 'imageMessage' in mediaMessage:
media = mediaMessage.get('imageMessage')
info = 'Image'
elif 'videoMessage' in mediaMessage:
media = mediaMessage.get('videoMessage')
info = 'Video'
elif 'audioMessage' in mediaMessage:
media = mediaMessage.get('audioMessage')
info = 'Audio'
elif 'documentMessage' in mediaMessage:
media = mediaMessage.get('documentMessage')
info = 'Document'
extension = mimetypes.guess_extension(type=media.get('mimetype'))
if extension is None or extension is '':
extension = '.ogg'
mediaData = urllib2.urlopen(media['url']).read()
mediaKeyExpanded = HKDF(base64.b64decode(media['mediaKey']), 112, "WhatsApp " + info + " Keys")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

file = mediaData[:-10]
iv = mediaKeyExpanded[:16]
cipherKey = mediaKeyExpanded[16:48]
decryptor = AES.new(cipherKey, AES.MODE_CBC, iv)
data = AESUnpad(decryptor.decrypt(file))

location = self.location + str(getTimestampMs()) + extension
with open(location, 'wb') as f:
f.write(data)
return location

def onOpen(self, ws):
try:
Expand Down Expand Up @@ -170,6 +200,7 @@ def onMessage(self, ws, message):
try:
processedData = whatsappReadBinary(decryptedMessage, True);
messageType = "binary";
##self.decodeMedia(mediaMessage=processedData[2][0]['message']) media decoder can be called here if message is media
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is the lynchpin, we'll need to enable this before safely merging.

except:
processedData = { "traceback": traceback.format_exc().splitlines() };
messageType = "error";
Expand Down