Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Should-I-Answer screening service #146

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion callattendant/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"PHONE_DISPLAY_FORMAT": "###-###-####",

"BLOCK_ENABLED": True,
"BLOCK_SERVICE": "NOMOROBO",
"BLOCK_SERVICE": "SHOULDIANSWER",

"BLOCK_NAME_PATTERNS": {"V[0-9]{15}": "Telemarketer Caller ID", },
"BLOCK_NUMBER_PATTERNS": {},
Expand Down
12 changes: 11 additions & 1 deletion callattendant/screening/callscreener.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from screening.blacklist import Blacklist
from screening.whitelist import Whitelist
from screening.nomorobo import NomoroboService

from screening.shouldianswer import ShouldIAnswer

class CallScreener(object):
'''The CallScreener provides provides blacklist and whitelist checks'''
Expand Down Expand Up @@ -94,6 +94,15 @@ def is_blacklisted(self, callerid):
print(">>> {}".format(reason))
self.blacklist_caller(callerid, reason)
return True, reason
if block["service"].upper() == "SHOULDIANSWER":
print(">> Checking shouldianswer...")
result = self._shouldianswer.lookup_number(number)
if result["spam"]:
reason = "{} with score {}".format(result["reason"], result["score"])
if self.config["DEBUG"]:
print(">>> {}".format(reason))
self.blacklist_caller(callerid, reason)
return True, reason
print("Caller has been screened")
return False, "Not found"
finally:
Expand All @@ -113,6 +122,7 @@ def __init__(self, db, config):

self._blacklist = Blacklist(db, config)
self._whitelist = Whitelist(db, config)
self._shouldianswer = ShouldIAnswer()
self._nomorobo = NomoroboService()

if self.config["DEBUG"]:
Expand Down
95 changes: 95 additions & 0 deletions callattendant/screening/shouldianswer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#
# ShouldIAnswer.py
#
# Copyright 2021 Thomas BARDET <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


import urllib.request
from bs4 import BeautifulSoup


class ShouldIAnswer(object):

def lookup_number(self, number):
#number = '{}-{}-{}'.format(number[0:3], number[3:6], number[6:])
url = "https://www.doisjerepondre.fr/numero-de-telephone/%s" % number
# print(url)
headers = {}
allowed_codes = [404] # allow not found response
content = self.http_get(url, headers, allowed_codes)
soup = BeautifulSoup(content, "lxml") # lxml HTML parser: fast

score = 0 # = no spam

scoreContainer = soup.find(class_="scoreContainer")

#print(positions)
if len(scoreContainer) > 0:
divScore = scoreContainer.select("div > .negative")
#print(divScore)
if(divScore):
#print("Spammer!")
score = 2

reason = ""
numbers = soup.find(class_="number")
#print(numbers)
if len(numbers) > 0:
spanReason = numbers.select("div > span")
reason = spanReason[0].get_text()
reason = reason.replace("\n", "").strip(" ")
#print(reason)
#review = soup.find(class_="review reviewNew")
#print(review)
#if len(review) > 0:
#bouton = review.select("div > #nf1ButYes ")
#print(bouton)

spam = False if score < self.spam_threshold else True

result = {
"spam": spam,
"score": score,
"reason": reason
}
print(result)
return result

def http_get(self, url, add_headers={}, allowed_codes=[]):
data = ""
try:
request = urllib.request.Request(url, headers=add_headers)
response = urllib.request.urlopen(request, timeout=5)
data = response.read()
except urllib.error.HTTPError as e:
code = e.getcode()
if code not in allowed_codes:
raise
data = e.read()
return data

def __init__(self, spam_threshold=2):

self.spam_threshold = spam_threshold