Skip to content

Instantly share code, notes, and snippets.

@kerel-fs
Created October 19, 2015 20:13
Show Gist options
  • Save kerel-fs/d64f12c5a23577ca2b9a to your computer and use it in GitHub Desktop.
Save kerel-fs/d64f12c5a23577ca2b9a to your computer and use it in GitHub Desktop.
Parse yowsup-cli message output, see https://github.com/tgalal/yowsup/issues/450
#!/usr/bin/env python3
import sys
import re
from datetime import datetime
import sqlite3
CLIDUMP = './your-cli.dump'
test_str = """[1234567890123/123456789012-1234567898@g.us(10-10-2042 11:30)]:[AC103CD12DER5] Your Messages goes here
Message AC103CD12DER5: Sent delivered receipt
[connected]:
[1234567890123/123456789012-1234567898@g.us(10-10-2042 11:30)]:[AC103CD12DER5] Another message.
Message AC103CD12DER5: Sent delivered receipt
[connected]:"""
if __name__ == "__main__":
messageRE = re.compile('\[(?P<a>[0-9]*)\/(?P<b>[0-9]*-[0-9]*)@g.us\((?P<c>(?:[0-9]{2}\-){2}2015 [0-9]{2}:[0-9]{2})\)\]:\[(?P<d>[A-Z0-9a-z\/]{13}|.*)\]\t (?P<e>.*)\nMessage (?P=d): Sent delivered receipt\n\[connected]:')
typeRE = re.compile('\[Media Type:image, Size:[0-9]*, URL:(?P<url>.*)\]')
messages = []
# It's your memory, deal with it ;P
with open(CLIDUMP) as f:
raw = f.read()
# raw = test_str
messagesraw = raw.split('\n\n')
i = 0
for m in messagesraw:
match = re.search(messageRE, m)
message ={'senderid': match.group('a'),
'groupid': match.group('b'),
'time': int(datetime.strptime(match.group('c'),'%d-%m-%Y %H:%M').timestamp()) + i,
'id': match.group('d'),
'body': match.group('e'),
'type':'text'}
# Create unique timestamps for closely succeeding messages by adding 0-59 seconds
i = (i + 1) % 60
# Parse messages of type image
typeMatch = re.search(typeRE, message['body'])
if typeMatch:
message.update({'body': typeMatch.group('url'),
'type': 'image'})
messages.append(message)
timestamps = list(m['time'] for m in messages)
if len(timestamps) > len(set(timestamps)):
sys.exit("Timestamps aren't uniue, we failed!")
for message in messages:
print(message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment