-
Notifications
You must be signed in to change notification settings - Fork 0
/
show.py
executable file
·82 lines (62 loc) · 2.44 KB
/
show.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/python
import argparse
import asyncio
import codecs
import pulsate
import sys
import magic
async def main(argv):
parser = argparse.ArgumentParser()
parser.add_argument('--filter')
parser.add_argument('--total', type=int)
args = parser.parse_args()
total = args.total if args.total is not None else 40
signal = pulsate.SignalCli()
await signal.connect()
config = pulsate.load_config()
database_filename = config["database"]
signal_db = pulsate.SignalMessageDatabase(database_filename)
my_telephone = config["my_telephone"]
for message in signal_db.fetch()[-total:]:
display_prefix = ""
if message.group_id:
group_name = await signal.get_group_name(message.group_id)
if group_name is None:
group_name = "(Unnamed group)"
if args.filter is not None and group_name != args.filter:
continue
group_id = codecs.encode(message.group_id, 'hex').decode('ascii')
group_name += " %s" % group_id
else:
group_name = None
if message.destination is None:
if group_name:
display_prefix += "%s - " % group_name
if message.source == my_telephone:
contact_name = "Me"
else:
contact_name = await signal.get_contact_name(message.source)
if args.filter is not None and contact_name != args.filter:
continue
if contact_name is None:
display_prefix += "%s" % message.source
else:
display_prefix += "%s (%s)" % (
contact_name, message.source)
else:
destination = await signal.get_contact_name(message.destination)
if args.filter is not None and destination != args.filter:
continue
display_prefix += "Me to %s (%s)" % (
destination, message.destination)
if message.text:
print("%s: %s" % (display_prefix, message.text))
for attachment in message.attachments:
try:
file_type = magic.from_file(attachment, mime=True)
except FileNotFoundError:
file_type = "<deleted>"
print("%s: Attachment: %s %s" %
(display_prefix, file_type, attachment))
if __name__ == "__main__":
asyncio.get_event_loop().run_until_complete(main(sys.argv))