DmitryPukhov / pytrade

22 stars 15 forks source link

Read sms message from Android #8

Open euvgub opened 3 years ago

euvgub commented 3 years ago

`from datetime import datetime import os import pipes import subprocess

ADB_BINARY = "adb" SMS_DB = "/data/data/com.android.providers.telephony/databases/mmssms.db" SELECT_SMS_SQL = 'SELECT _id, address, date, body FROM sms WHERE read=0;' UPDATE_READ_SQL = 'UPDATE sms SET read=1 WHERE _id in (%d);'

def get_unread_messages(): cmd = 'adb shell "su -c sqlite3 %s %s"' % (SMS_DB, pipes.quote(SELECT_SMS_SQL)) print cmd p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True) (out, err) = p.communicate() result = [] for msg in out.replace('\r', '').strip('\n').split('\n'): msg = msg.split('|') result.append(dict(id=msg[0], sender=msg[1], date_time=str(datetime.fromtimestamp(int(int(msg[2])/1000))), message=msg[3])) return result def set_read_message(msg_id): """ Sets a message identified by msg_id to read. :param msg_id: :return: """ cmd = 'adb shell "su -c sqlite3 %s %s"' % (SMS_DB, pipes.quote(UPDATE_READ_SQL % msg_id)) os.system(cmd) def main(): result = get_unread_messages() for r in result: print r if name == 'main': main()`