application pour envoyer un ...
|
1 |
#!/usr/bin/env python3 |
2 | ||
3 |
import requests |
|
4 |
import json |
|
5 |
import hmac |
|
6 |
import hashlib |
|
7 |
import re |
|
8 |
import sys |
|
9 |
import time |
|
10 |
import os |
|
11 |
import signal |
|
12 | ||
13 |
configfile = __file__ + ".conf" |
|
14 |
if not os.path.exists(configfile): |
|
15 |
if os.path.exists("/usr/local/etc/" + os.path.basename(configfile)): |
|
16 |
configfile = "/usr/local/etc/" + os.path.basename(configfile) |
|
17 |
elif os.path.exists("/etc/" + os.path.basename(configfile)): |
|
18 |
configfile = "/etc/" + os.path.basename(configfile) |
|
19 |
else: |
|
20 |
print("no config file found !") |
|
21 |
exit(1) |
|
22 |
exec(open(configfile).read()) |
|
23 | ||
24 |
def eprint(*args, **kwargs): |
|
25 |
print(*args, file=sys.stderr, **kwargs) |
|
26 | ||
27 |
def stoppe(number, frame): |
|
28 |
sys.exit(0) |
|
29 | ||
30 |
def connexion_post(method, data=None, headers={}): |
|
31 |
url = FREEBOX_URL + "/api/" + API_VERSION + method + "/" |
|
32 |
if data: data = json.dumps(data) |
|
33 |
return json.loads(requests.post(url, data=data, headers=headers).text) |
|
34 | ||
35 |
def connexion_get(method, headers={}): |
|
36 |
url = FREEBOX_URL + "/api/" + API_VERSION + method + "/" |
|
37 |
return json.loads(requests.get(url, headers=headers).text) |
|
38 | ||
39 |
def connexion_put(method, data=None, headers={}): |
|
40 |
url = FREEBOX_URL + "/api/" + API_VERSION + method + "/" |
|
41 |
if data: data = json.dumps(data) |
|
42 |
return json.loads(requests.put(url, data=data, headers=headers).text) |
|
43 | ||
44 |
def mksession(): |
|
45 |
challenge = connexion_get("/login")["result"]["challenge"] |
|
46 |
data={ |
|
47 |
"app_id": APP_ID, |
|
48 |
"password": hmac.new(APP_TOKEN, challenge.encode('utf-8'), hashlib.sha1).hexdigest() |
|
49 |
} |
|
50 |
return connexion_post("/login/session", data)["result"]["session_token"] |
|
51 | ||
52 |
def envoi_sms(number): |
|
53 |
url="https://smsapi.free-mobile.fr/sendmsg?" \ |
|
54 |
+ "user=" + user \ |
|
55 |
+ "&pass=" + password \ |
|
56 |
+ "&msg=" + re.sub(r' ', '%20', "sms pour " + number + ':' + msg) |
|
57 |
requests.get(url) |
|
58 | ||
59 |
def marque_comme_lu(id_appel, number): |
|
60 |
method = "/call/log/" + str(id_appel) |
|
61 |
resultat = connexion_put(method, { "new": False }, headers={"X-Fbx-App-Auth": session_token}) |
|
62 |
if resultat.get('success'): |
|
63 |
eprint("appel de " + number + "(id: " + str(id_appel) + ") marqué comme lu") |
|
64 |
else: |
|
65 |
eprint(resultat) |
|
66 | ||
67 |
def appels(session_token): |
|
68 |
method = "/call/log" |
|
69 |
resultat = connexion_get(method, headers={"X-Fbx-App-Auth": session_token}) |
|
70 |
appel_depuis_portable = re.compile('^(0|\\+33)[67]') |
|
71 |
for appel in resultat["result"]: |
|
72 |
if appel.get('new') and appel.get('type') == 'missed' and appel_depuis_portable.match(appel.get('number')): |
|
73 |
envoi_sms(appel.get('number')) |
|
74 |
marque_comme_lu(appel.get('id'), appel.get('number')) |
|
75 |
time.sleep(5) |
|
76 | ||
77 |
signal.signal(signal.SIGTERM, stoppe) |
|
78 |
while True: |
|
79 |
try: |
|
80 |
session_token = mksession() |
|
81 |
except: |
|
82 |
time.sleep(10) |
|
83 |
else: |
|
84 |
appels(session_token) |
|
85 |
time.sleep(60) |