Created
March 26, 2018 03:34
-
-
Save drbh/e12aa0da998218ecd2d903674b8ca6d9 to your computer and use it in GitHub Desktop.
Extract that pesky iMessages data
This file contains hidden or 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 | |
| import sys | |
| import sqlite3 | |
| import datetime | |
| USERNAME = os.popen('whoami').read().strip() | |
| class DataGrabber(object): | |
| """docstring for ClassName""" | |
| def __init__(self): | |
| print '/Users/'+USERNAME+'/Library/Messages/chat.db' | |
| self.conn = sqlite3.connect('/Users/'+USERNAME+'/Library/Messages/chat.db', check_same_thread=False) | |
| self.OSX_EPOCH = 978307200 | |
| def get_table_names(self): | |
| c = self.conn.cursor() | |
| c.execute("SELECT name FROM sqlite_master WHERE type='table';") | |
| print(c.fetchall()) | |
| def get_messages(self, handle_id): | |
| c = self.conn.cursor() | |
| c.execute("SELECT * FROM `message` WHERE handle_id=" + str(handle_id) + " ORDER BY date DESC LIMIT 20" ) | |
| all_messages = c.fetchall()[::-1] | |
| payload = [] | |
| for message in all_messages: | |
| fro = "ME" if message[21] == 1 else "THEM" | |
| payload += [ { "message":message[2], "time":str(datetime.datetime.fromtimestamp(message[15] + self.OSX_EPOCH)), "from":fro } ] | |
| c.close() | |
| return payload | |
| def get_handles_like(self, search): | |
| c = self.conn.cursor() | |
| c.execute("SELECT * FROM `handle` WHERE id LIKE '%"+search+"%'") | |
| all_handles = c.fetchall() | |
| c.close() | |
| return all_handles | |
| def get_all_conversations(self, handle_id): | |
| c = self.conn.cursor() | |
| c.execute("SELECT * FROM `chat_handle_join` WHERE handle_id=" + str(handle_id)) | |
| all_convs = c.fetchall() | |
| c.close() | |
| return all_convs | |
| def find_get_messages(self, search): | |
| payload = [] | |
| for hand in self.get_handles_like(search): | |
| # print hand | |
| payload += [self.get_messages(hand[0])] | |
| return payload |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment