|
@@ -6,20 +6,31 @@ import contextlib
|
|
|
import glob
|
|
|
import io
|
|
|
import os
|
|
|
+import string
|
|
|
import subprocess
|
|
|
import syslog
|
|
|
import threading
|
|
|
-import time
|
|
|
+import urllib2
|
|
|
import wave
|
|
|
from datetime import datetime
|
|
|
|
|
|
import linphone
|
|
|
from enum import Enum
|
|
|
-import threading
|
|
|
|
|
|
VOLUME_THRESHOLD = 100
|
|
|
|
|
|
|
|
|
+def slugify(s):
|
|
|
+ """
|
|
|
+ Normalizes string, converts to lowercase, removes non-alpha characters,
|
|
|
+ and converts spaces to hyphens, wich is url/filename friendly.
|
|
|
+ """
|
|
|
+ valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits)
|
|
|
+ filename = ''.join(c for c in s if c in valid_chars)
|
|
|
+ filename = filename.replace(' ', '_') # I don't like spaces in filenames.
|
|
|
+ return filename
|
|
|
+
|
|
|
+
|
|
|
class ConversationStatus(Enum):
|
|
|
READY_TO_TALK = 0
|
|
|
IMTALKING = 1
|
|
@@ -37,7 +48,6 @@ class Conversation(object):
|
|
|
@status.setter
|
|
|
def status(self, value):
|
|
|
if value != self._status:
|
|
|
- print(" New status : " + str(value))
|
|
|
self._status = value
|
|
|
|
|
|
|
|
@@ -56,6 +66,26 @@ replies_pos = 0
|
|
|
|
|
|
THREADS_MUST_QUIT = False
|
|
|
|
|
|
+KTIP_LOOKUP_URL = "https://www.ktipp.ch/service/warnlisten/detail/?warnliste_id=7&ajax=ajax-search-form&keyword={" \
|
|
|
+ "$number$}"
|
|
|
+
|
|
|
+
|
|
|
+def is_in_local_blacklist(a_number):
|
|
|
+ black_list = current_dir + "/blacklist.txt"
|
|
|
+ if os.path.isfile(black_list):
|
|
|
+ return a_number in open(current_dir + "/blacklist.txt").read()
|
|
|
+
|
|
|
+
|
|
|
+def is_in_ktipp_blacklist(a_number):
|
|
|
+ # On peut interroger le site ktipp:
|
|
|
+ # https://www.ktipp.ch/service/warnlisten/detail/?warnliste_id=7&ajax=ajax-search-form&keyword=0445510503
|
|
|
+ # Si argument keyword pas trouvé, ca donne ca dans la réponse :
|
|
|
+ # 0 Einträge
|
|
|
+
|
|
|
+ url = KTIP_LOOKUP_URL.replace("{$number$}", a_number)
|
|
|
+
|
|
|
+ return "0 Eintr" not in urllib2.urlopen(url).read()
|
|
|
+
|
|
|
|
|
|
def log(msg):
|
|
|
syslog.syslog(msg)
|
|
@@ -91,7 +121,7 @@ def say(core):
|
|
|
replies_pos = 1
|
|
|
|
|
|
duration = get_wav_duration(voice_filename)
|
|
|
- print("Saying : " + voice_filename + "(duration : " + str(duration) + ")")
|
|
|
+ log("Saying : " + voice_filename + "(duration : " + str(duration) + ")")
|
|
|
|
|
|
core.play_file = voice_filename
|
|
|
|
|
@@ -110,25 +140,27 @@ def call_state_changed(core, call, state, message):
|
|
|
if state == linphone.CallState.Released:
|
|
|
# Let's convert wav to mp3
|
|
|
log("Converting output from wav to mp3")
|
|
|
- subprocess.call('lame --quiet --preset insane %s' % call.current_params.record_file, shell=True)
|
|
|
- os.remove(call.current_params.record_file)
|
|
|
+ if os.path.isfile(call.current_params.record_file):
|
|
|
+ subprocess.call('lame --quiet --preset insane %s' % call.current_params.record_file, shell=True)
|
|
|
+ os.remove(call.current_params.record_file)
|
|
|
|
|
|
if state == linphone.CallState.IncomingReceived:
|
|
|
log("Incoming call : {}".format(call.remote_address.username))
|
|
|
|
|
|
replies_pos = 0
|
|
|
|
|
|
- if call.remote_address.username == "**620" or call.remote_address.username == "0765757624":
|
|
|
- print(" ... " + call.remote_address.username)
|
|
|
-
|
|
|
- #if call.remote_address.username == "0227570500":
|
|
|
- # print(" .. from Bernard !")
|
|
|
-
|
|
|
+ if is_in_local_blacklist(call.remote_address.username) or is_in_ktipp_blacklist(call.remote_address.username):
|
|
|
+ log("telemarketer calling : " + call.remote_address.username)
|
|
|
|
|
|
call_params = core.create_call_params(call)
|
|
|
- call_params.record_file = current_dir + "/out/call_" + datetime.now().strftime(
|
|
|
+ a_file = current_dir + "/out/call_from_" + slugify(call.remote_address.username) + \
|
|
|
+ "_" + datetime.now().strftime(
|
|
|
'%Y-%m-%d_%Hh%Mmn%Ss') + ".wav"
|
|
|
|
|
|
+ log(a_file)
|
|
|
+
|
|
|
+ call_params.record_file = a_file
|
|
|
+
|
|
|
# Let ring some time
|
|
|
sleep(5)
|
|
|
|
|
@@ -153,7 +185,7 @@ def registration_state_changed(core, call, state, message):
|
|
|
|
|
|
def incoming_stream_worker(core, call):
|
|
|
global conversation
|
|
|
- print("Worker is starting")
|
|
|
+ log("Worker is starting")
|
|
|
|
|
|
f = open(incoming_stream_file, "rb")
|
|
|
f.seek(0, io.SEEK_END)
|
|
@@ -179,7 +211,7 @@ def incoming_stream_worker(core, call):
|
|
|
if len(buf) >= 20000:
|
|
|
volume = audioop.rms(buf, 2)
|
|
|
print("Detected volume : " + str(volume))
|
|
|
- #print("State : " + str(conversation.status))
|
|
|
+ # print("State : " + str(conversation.status))
|
|
|
buf = ''
|
|
|
if volume < VOLUME_THRESHOLD:
|
|
|
if conversation.status is ConversationStatus.READY_TO_TALK:
|
|
@@ -191,8 +223,7 @@ def incoming_stream_worker(core, call):
|
|
|
sleep(0.01)
|
|
|
previous_status = conversation.status
|
|
|
|
|
|
-
|
|
|
- print("Worker is quitting")
|
|
|
+ log("Worker is quitting")
|
|
|
|
|
|
|
|
|
def main():
|
|
@@ -228,8 +259,6 @@ def main():
|
|
|
sleep(0.03)
|
|
|
core.iterate()
|
|
|
|
|
|
- log("callblocker quitting.")
|
|
|
-
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
@@ -238,27 +267,3 @@ if __name__ == "__main__":
|
|
|
except KeyboardInterrupt:
|
|
|
THREADS_MUST_QUIT = True
|
|
|
print "Bye"
|
|
|
-
|
|
|
-# - Hi its lenny
|
|
|
-# - Hmmm, sorry, I can barely here you there
|
|
|
-# - Yes, yes, yes
|
|
|
-# - Oh good, yes, yes, yes
|
|
|
-# - Hmm, mais il me semble que quelqu'un a appelé pour ça la semaine passée ... c'était vous ?
|
|
|
-# - Yeah, pouvez-vous me rappeler votre nom ?
|
|
|
-# - Ca tombe bien que vous m'appeliez, car justement ma nièce me parlait de la même chose l'autre jour.
|
|
|
-# j'écoute d'ailleurs toujours attentivement son avis, car vous savez, c'est la première de la famille
|
|
|
-# qui est allé à l'Université. Et elle l'a terminé avec mention, nous en sommes très fière.
|
|
|
-# et du coup elle m'a dit que je devrais jeter un oeil à ce dont vous me parlez.
|
|
|
-# - Hmm, désolé, j'ai pas tout à fait compris, vous pouvez répéter ?
|
|
|
-# - Heu, oui, vous pouvez me rappeler de la part de quelle entreprise vous appelez
|
|
|
-# - Oui, en fait y'a une chose ... heuu, car j'avais eu un appel comme le votre, j'ai eu pas mal
|
|
|
-# de problème avec le personnel ici, qui m'on fait la morale, car je me suis retrouvé avec des affaires
|
|
|
-# dont je n'avais pas besoin, et ma nièce justement m'a fait la morale également ... vous savez ce que c'est
|
|
|
-# la famille ....
|
|
|
-# - Could you say that again please ?
|
|
|
-
|
|
|
-# Idées :
|
|
|
-# - En tout cas vous me semblez une personne fort sympathique, c'est toujours agréable de discute
|
|
|
-# des personnes sympathique, quand on est une personne seule comme moi ... heuu, en fait on parlait
|
|
|
-# de quoi déjà ?
|
|
|
-# - Bon,
|