Skip to content

Instantly share code, notes, and snippets.

@MawKKe
Last active June 6, 2020 23:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MawKKe/3049287614c4de5bf163 to your computer and use it in GitHub Desktop.
Save MawKKe/3049287614c4de5bf163 to your computer and use it in GitHub Desktop.
Python CLI program to show (unread) SMS's on Jolla/Sailfish OS
#!/usr/bin/env python3
#
# Author: Markus (MawKKe) ekkwam@gmail.com | 2016-01-27 02:52
#
# ---
# CLI program to show (unread) SMS's on Jolla/Sailfish OS
#
# Reads directly form an sqlite3 database, so standard Python libraries suffice.
#
# The database file is typically located in ~/.local/share/commhistory/commhistory.db
# ---
#
# See 'python3 smsread.py -h' for usage
#
import sqlite3
import time
import sys
import argparse
default_path = "/home/nemo/.local/share/commhistory/commhistory.db"
# fields in "Events" schema:
# type: 2 = SMS msg's
# direction: 1 = received, 2 = sent
# isRead: 0 = unread, 1 = read
# startTime ??? weird, its a text message
# Open as read-only
def smsread(db_path, read_all=False):
with sqlite3.connect("file:{0}?mode=ro".format(db_path), uri=True) as conn:
# Return rows as dicts
conn.row_factory = sqlite3.Row
# Note: Select all fields, it's simpler and we can just pick whatever fields we want via the row dict
# Note: "isRead in (0,0)" -> Only unread msgs. "isRead in (0,1)" -> Both unread and read msgs :)
if not read_all:
print("Unread SMS's:\n-------------")
for row in conn.execute("select * from Events where type = 2 and isRead in (0,?) order by startTime", (int(read_all),)):
timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(row["startTime"]))
direction = {1: "Received", 2: "Sent"}[row["direction"]]
remote = row["remoteUid"]
msg = row["freeText"].replace('\n', ' ')
print("{0} {1: <8} [{2}] {3}".format(timestamp, direction, remote, msg))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Show (unread) SMS's on Jolla/Sailfish OS")
parser.add_argument("--all", help="Show all SMS's, not just unread ones.", action='store_true')
parser.add_argument("--path", help="Database path (default: {0})".format(default_path), type=str, default=default_path)
args = parser.parse_args(sys.argv[1:])
smsread(args.path, args.all)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment