From 053ecd3296df4b297a5c3bb7a00e5f3eb58ff7d7 Mon Sep 17 00:00:00 2001 From: Thomas Lange Date: Sun, 6 Nov 2022 14:24:01 +0100 Subject: Use argparse for parsing command-line arguments --- package/bin/android-sms-extractor | 58 +++++++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 18 deletions(-) diff --git a/package/bin/android-sms-extractor b/package/bin/android-sms-extractor index 3f05911..601ff8c 100755 --- a/package/bin/android-sms-extractor +++ b/package/bin/android-sms-extractor @@ -1,21 +1,46 @@ #!/usr/bin/env python3 -# Export all SMS from the SQLite database of the com.android.messaging -# app as simple plaintext sorted by conversation to stdout. -# -# Usage instructions for LineageOS 14.1 (tested on OnePlus X [onyx]): -# 1. Enable USB debugging in the developer options -# 2. Allow root access from ADB in the developer options -# 3. Connect the device and restart the adb daemon in root mode: -# $ adb root -# 4. Pull the corresponding database file: -# $ adb pull /data/data/com.android.messaging/databases/bugle_db . -# 5. Pass the name of the database file to this script: -# $ ./{this_script} bugle_db +##%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%## +# Android SMS Extractor [Thomas Lange ] # +##%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%## +# # +# 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. # +# # +# USAGE INSTRUCTIONS (tested with SMS app in LineageOS 14.1 and 17.1): # +# 1. Enable USB debugging in the developer options of your Android. # +# 2. Enable "Root debugging" in the developer options of your Android. # +# 3. Connect your Android to your PC and (re)start ADB in root mode: # +# $ adb root # +# 4. Pull the corresponding database file to your PC: # +# $ adb pull /data/data/com.android.messaging/databases/bugle_db . # +# 5. Pass the path to the extracted database file to this script: # +# $ android-sms-extractor bugle_db # +# # +##%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%## import os import sys import sqlite3 + from datetime import datetime +from argparse import ArgumentParser + +#=============================================================================== +# Initialize argument parser +#=============================================================================== +parser = ArgumentParser(prog="android-sms-extractor", + description="Android SMS Extractor") + +#=============================================================================== +# Define positional arguments +#=============================================================================== +parser.add_argument('db_file', action='store', + help="Path to the SQLite database file") + +#=============================================================================== +# Parse arguments +#=============================================================================== +args = parser.parse_args() # Default date_format_string = "%Y-%m-%d %H:%M:%S" @@ -23,14 +48,11 @@ date_format_string = "%Y-%m-%d %H:%M:%S" # German version # date_format_string = "am %d.%m.%Y um %H:%M:%S" -if len(sys.argv) < 2: - exit("Usage: {} database_file > formatted.txt".format(sys.argv[0])) - -if not os.path.isfile(sys.argv[1]): - exit("Could not open file {}".format(sys.argv[1])) +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(sys.argv[1]) +connection = sqlite3.connect(args.db_file) # Get a cursor to the SQLite database connection c = connection.cursor(); -- cgit v1.2.3