File size: 2,286 Bytes
61cfd01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
import traceback
from loguru import logger
from .analysis_helpers import analyze_url, analyze_text
from .orchestrator import ORCH

def on_analyze(news_text: str = "", news_url: str = "", image_url: str = "", run_serp: bool = False):
    """
    Analyze either a text or a URL (mutually exclusive).
    """
    try:
        if news_url and news_text:
            return {"error": "Please provide either a URL or text — not both."}, "", [], {}

        if news_url:
            article_text, headline, qa_fallback_note = analyze_url(news_url, run_serp)
            url = news_url
        elif news_text:
            article_text, headline, qa_fallback_note = analyze_text(news_text)
            url = None
        else:
            return {"error": "No input provided."}, "", [], {}

        claim = ""
        report = ORCH.run(
            claim_text=claim,
            article_text=article_text,
            url=url,
            image_url=image_url or None,
            run_serpapi=run_serp,
        )

        extracted_claims = [r.get("claim") for r in report.get("reports", [])]
        qa_text = ""
        if report.get("reports"):
            qa_text = report["reports"][0].get("qa_summary", "") or qa_fallback_note

        phishing_tag = extract_phishing_tag(report)
        if report.get("reports"):
            report["reports"][0]["phishing_tag"] = phishing_tag

        phish = report.get("reports", [{}])[0].get("phishing_analysis", {}) or {}
        phish["phishing_tag"] = phishing_tag

        return report, qa_text, extracted_claims, phish

    except Exception:
        logger.exception("on_analyze failed")
        return {"error": traceback.format_exc()}, "", [], {}


def extract_phishing_tag(report):
    summary_phish_flag = report.get("summary", {}).get("phishing_flag")
    if summary_phish_flag is True:
        return "Unsafe"
    elif summary_phish_flag is False:
        return "Safe"

    first_phish = report.get("reports", [{}])[0].get("phishing_analysis", {}) or {}
    sb = (first_phish.get("safe_browsing") or {})
    vt = (first_phish.get("virustotal") or {})
    if sb.get("safe") is False or vt.get("safe") is False:
        return "Unsafe"
    elif sb.get("safe") is True and vt.get("safe") is True:
        return "Safe"
    return "Unknown"