#!/usr/bin/env python3 import requests import json import hmac import hashlib import re import sys import time import os import signal configfile = __file__ + ".conf" if not os.path.exists(configfile): if os.path.exists("/usr/local/etc/" + os.path.basename(configfile)): configfile = "/usr/local/etc/" + os.path.basename(configfile) elif os.path.exists("/etc/" + os.path.basename(configfile)): configfile = "/etc/" + os.path.basename(configfile) else: print("no config file found !") exit(1) exec(open(configfile).read()) def eprint(*args, **kwargs): print(*args, file=sys.stderr, **kwargs) def stoppe(number, frame): sys.exit(0) def connexion_post(method, data=None, headers={}): url = FREEBOX_URL + "/api/" + API_VERSION + method + "/" if data: data = json.dumps(data) return json.loads(requests.post(url, data=data, headers=headers).text) def connexion_get(method, headers={}): url = FREEBOX_URL + "/api/" + API_VERSION + method + "/" return json.loads(requests.get(url, headers=headers).text) def connexion_put(method, data=None, headers={}): url = FREEBOX_URL + "/api/" + API_VERSION + method + "/" if data: data = json.dumps(data) return json.loads(requests.put(url, data=data, headers=headers).text) def mksession(): challenge = connexion_get("/login")["result"]["challenge"] data={ "app_id": APP_ID, "password": hmac.new(APP_TOKEN, challenge.encode('utf-8'), hashlib.sha1).hexdigest() } return connexion_post("/login/session", data)["result"]["session_token"] def envoi_sms(number): url="https://smsapi.free-mobile.fr/sendmsg?" \ + "user=" + user \ + "&pass=" + password \ + "&msg=" + re.sub(r' ', '%20', "sms pour " + number + ':' + msg) requests.get(url) def marque_comme_lu(id_appel, number): method = "/call/log/" + str(id_appel) resultat = connexion_put(method, { "new": False }, headers={"X-Fbx-App-Auth": session_token}) if resultat.get('success'): eprint("appel de " + number + "(id: " + str(id_appel) + ") marqué comme lu") else: eprint(resultat) def appels(session_token): method = "/call/log" resultat = connexion_get(method, headers={"X-Fbx-App-Auth": session_token}) for appel in resultat["result"]: if appel.get('new') and appel.get('type') == 'missed' and appel_depuis_portable.match(appel.get('number')): envoi_sms(appel.get('number')) marque_comme_lu(appel.get('id'), appel.get('number')) time.sleep(5) signal.signal(signal.SIGTERM, stoppe) appel_depuis_portable = re.compile('^(0|\\+33)[67]') while True: try: session_token = mksession() except: time.sleep(10) else: appels(session_token) time.sleep(60)