-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.py
executable file
·86 lines (68 loc) · 2.53 KB
/
run.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
82
83
84
85
86
#!/data/data/com.termux/files/usr/bin/python3
import argparse
import json
from subprocess import run
from typing import List, Dict
def load_contacts(file_path: str) -> List[Dict]:
"""
Load contacts from a JSON file.
Args:
file_path (str): The path to the contacts JSON file.
Returns:
List[Dict]: A list of contacts.
Raises:
FileNotFoundError: If the specified file does not exist.
json.JSONDecodeError: If the file is not a valid JSON.
"""
with open(file_path, "r") as file:
return json.load(file)
def load_message(file_path: str) -> str:
"""
Load a message from a text file.
Args:
file_path (str): The path to the message text file.
Returns:
str: The message loaded from the file.
Raises:
FileNotFoundError: If the specified file does not exist.
"""
with open(file_path, "r") as file:
return file.read()
def concat_numbers(contacts: List[Dict]) -> str:
"""
Concatenate all numbers.
Args:
contacts (List[Dict]): A list of contacts.
Returns:
str: A comma-separated string of contact numbers.
"""
return ",".join(contact["number"] for contact in contacts)
def send_message(numbers: str, message: str, sim_card_slot: int) -> None:
"""
Send a message to the specified numbers using termux-sms-send.
Args:
numbers (str): A comma-separated string of contact numbers.
message (str): The message to be sent.
sim_card_slot (int): The SIM card slot to use for sending the message.
"""
run(["termux-sms-send", "-n", numbers, "-s", str(sim_card_slot), message])
def main() -> None:
"""
Main function to process command line arguments and send a message.
"""
parser = argparse.ArgumentParser(description="Send messages to contacts.")
parser.add_argument("contacts_path", type=str, help="Path to the contacts.json file.")
parser.add_argument("message_path", type=str, help="Path to the message.txt file.")
parser.add_argument("--sim", type=int, default=1, help="SIM card slot to use (default: 1).")
args = parser.parse_args()
try:
contacts = load_contacts(args.contacts_path)
message = load_message(args.message_path)
numbers = concat_numbers(contacts)
send_message(numbers, message, args.sim)
except FileNotFoundError as e:
parser.error(f"File not found: {e.filename}")
except json.JSONDecodeError:
parser.error("Invalid JSON file format.")
if __name__ == "__main__":
main()