-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
59 lines (44 loc) · 1.53 KB
/
Copy pathcli.py
File metadata and controls
59 lines (44 loc) · 1.53 KB
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
"""
Check a message from the command line.
python cli.py "Your parcel is held, pay a 1.99 fee at royal-mail.top/redeliver"
With no message it reads from standard input, so you can also do:
Get-Content message.txt | python cli.py (PowerShell)
"""
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
if hasattr(sys.stdout, "reconfigure"):
sys.stdout.reconfigure(encoding="utf-8")
from scamshield import ScamShield
# A coloured risk bar if the terminal supports it; plain text otherwise.
_ICON = {"danger": "[!]", "warning": "[?]", "safe": "[OK]"}
def render(result):
bar_len = 28
filled = round(result["risk"] / 100 * bar_len)
bar = "#" * filled + "-" * (bar_len - filled)
print()
print(f' {_ICON[result["level"]]} {result["verdict"]} '
f'(risk {result["risk"]}/100, confidence: {result["confidence"]})')
print(f' [{bar}]')
print()
print(f' Message: {result["text"]}')
print()
if result["reasons"]:
print(" Why:")
for reason in result["reasons"]:
arrow = "scam ->" if reason["direction"] == "scam" else "genuine ->"
print(f' {arrow:<10} {reason["text"]}')
print()
print(f' Advice: {result["advice"]}')
print()
def main():
message = " ".join(sys.argv[1:]).strip()
if not message:
message = sys.stdin.read().strip()
if not message:
print(__doc__)
return
shield = ScamShield()
render(shield.classify(message))
if __name__ == "__main__":
main()