From 3c4741942d4ad5858c2932dc172a1e3b7ba6adc3 Mon Sep 17 00:00:00 2001 From: Thomas Lange Date: Sun, 6 Nov 2022 15:09:31 +0100 Subject: Optimize code and comments --- package/bin/android-sms-extractor | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) (limited to 'package') diff --git a/package/bin/android-sms-extractor b/package/bin/android-sms-extractor index f8872ed..f1bf259 100755 --- a/package/bin/android-sms-extractor +++ b/package/bin/android-sms-extractor @@ -4,7 +4,7 @@ ##%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%## # # # Extract all SMS from the SQLite database of the com.android.messaging app as # -# simple plain text sorted by conversation to stdout or a file. # +# simple plain text sorted by conversation to stdout. # # # # USAGE INSTRUCTIONS (tested with SMS app in LineageOS 14.1 and 17.1): # # 1. Enable USB debugging in the developer options of your Android. # @@ -19,9 +19,7 @@ ##%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%## import os -import sys import sqlite3 - from datetime import datetime from argparse import ArgumentParser @@ -31,9 +29,6 @@ from argparse import ArgumentParser parser = ArgumentParser(prog="android-sms-extractor", description="Android SMS Extractor") -#=============================================================================== -# Define arguments -#=============================================================================== parser.add_argument('db_file', action='store', help="Path to the SQLite database file") @@ -46,16 +41,25 @@ parser.add_argument('--datetime-format', action='store', dest='dt_format', #=============================================================================== args = parser.parse_args() +#=============================================================================== +# Check if db_file argument is a file +#=============================================================================== if not os.path.isfile(args.db_file): exit("Could not open file »{}«".format(args.db_file)) -# Establish connection to local SQLite database -connection = sqlite3.connect(args.db_file) +#=============================================================================== +# Establish connection to SQLite database +#=============================================================================== +db_conn = sqlite3.connect(args.db_file) # Get a cursor to the SQLite database connection -c = connection.cursor(); +c = db_conn.cursor() -c.execute("SELECT conversations._id, conversations.name, conversations.participant_normalized_destination \ +#=============================================================================== +# Query and loop through all SMS conversations +#=============================================================================== +c.execute("SELECT conversations._id, conversations.name, \ + conversations.participant_normalized_destination \ FROM conversations WHERE latest_message_id NOT NULL ORDER BY name") for conversation in c.fetchall(): @@ -67,8 +71,11 @@ for conversation in c.fetchall(): print("{} [{}]".format(data_name, data_dest)) print("==================================================") - c.execute("SELECT messages._id, parts.text, parts.timestamp, participants.sim_slot_id FROM messages \ - LEFT JOIN parts ON messages._id = parts.message_id LEFT JOIN participants ON messages.sender_id = participants._id \ + # Query and loop through all messages in this conversation + c.execute("SELECT messages._id, parts.text, parts.timestamp, \ + participants.sim_slot_id FROM messages \ + LEFT JOIN parts ON messages._id = parts.message_id \ + LEFT JOIN participants ON messages.sender_id = participants._id \ WHERE messages.conversation_id = ? ORDER BY parts.timestamp", (data_id,)) for result in c.fetchall(): @@ -88,4 +95,5 @@ for conversation in c.fetchall(): else: msg_direction = "<---" - print("[{} {}]:\n{}\n".format(msg_time_formatted, msg_direction, msg_text)) + print("[{} {}]:\n{}\n".format( + msg_time_formatted, msg_direction, msg_text)) -- cgit v1.2.3