File size: 5,305 Bytes
a6e70b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"""Tavily community-discovery fallback.

Uses Tavily search to find additional Reddit subreddits when the static
COMMUNITY_MAP yields fewer than ``threshold`` comments.  **This module never
extracts pain points from Tavily snippets** — it only returns subreddit names
that should be scraped by the Reddit JSON scraper.

Required env: ``TAVILY_API_KEY``
"""
from __future__ import annotations

import logging
import re
import time
from typing import Any

import diskcache
import requests

from src.config import settings

logger = logging.getLogger(__name__)

# Disk-backed cache for Tavily community discovery
_CACHE = diskcache.Cache(settings.cache_dir)
_TTL_S: int = settings.cache_ttl_hours * 3600
_MISSING = object()

# ------------------------------------------------------------------
# Constants
# ------------------------------------------------------------------
_MAX_RESULTS: int = 5
_REQUEST_TIMEOUT: int = 20
_REQUEST_DELAY_S: float = 0.5

# Subreddits we never want to scrape (meta, huge, off-topic)
_SUBREDDIT_DENYLIST: set[str] = {
    "all", "popular", "askreddit", "announcements", "blog",
    "pics", "funny", "memes", "aww", "gifs", "videos", "news",
    "worldnews", "politics", "science", "IAmA", "bestof",
    "lifeprotips", "personalfinance", "amitheasshole", "tifu",
    "todayilearned",
}


def _url_to_subreddit(url: str) -> str | None:
    """Extract ``subreddit_name`` from a Reddit URL or return ``None``."""
    # Match /r/subreddit/... or /r/subreddit (trailing slash optional)
    m = re.search(r"reddit\.com/r/([A-Za-z0-9_]+)", url)
    if m:
        name = m.group(1).lower()
        return name
    return None


def _snippet_to_subreddits(text: str) -> set[str]:
    """Extract r/name references from raw text."""
    found: set[str] = set()
    for m in re.finditer(r"/?r/([A-Za-z0-9_]+)", text):
        name = m.group(1).lower()
        if name not in _SUBREDDIT_DENYLIST:
            found.add(name)
    return found


def _is_valid_subreddit(name: str) -> bool:
    """HEAD-check whether ``r/name`` actually exists and is accessible."""
    url = f"https://www.reddit.com/r/{name}.json"
    try:
        time.sleep(_REQUEST_DELAY_S)
        r = requests.head(
            url,
            headers={"User-Agent": "ventureforge/0.1.0 (academic research)"},
            timeout=10,
            allow_redirects=True,
        )
        return r.status_code == 200
    except Exception as e:
        logger.debug(f"HEAD check failed for r/{name}: {e}")
        return False


def search_communities(domain: str) -> list[str]:
    """Ask Tavily for Reddit communities related to *domain* complaints.

    Returns a deduplicated, validated list of subreddit names (lowercased)
    sorted by confidence.  Empty list if Tavily is mis-configured, rate-
    limited, or returns no Reddit results.
    """
    if not settings.tavily_enabled:
        logger.info("[tavily] fallback skipped — TAVILY_API_KEY not set")
        return []

    cache_key = ("tavily_communities", domain.strip().lower())
    cached = _CACHE.get(cache_key, default=_MISSING)
    if cached is not _MISSING:
        logger.info(f"[tavily] cache hit for domain='{domain}'")
        return list(cached)

    query = f'site:reddit.com "{domain}" frustration OR complaint OR problem OR hate community subreddit'
    payload: dict[str, Any] = {
        "api_key": settings.tavily_api_key,
        "query": query,
        "search_depth": "basic",
        "max_results": _MAX_RESULTS,
        "domain": "reddit.com",
    }

    try:
        time.sleep(_REQUEST_DELAY_S)
        r = requests.post(
            "https://api.tavily.com/search",
            json=payload,
            timeout=_REQUEST_TIMEOUT,
        )
        r.raise_for_status()
        data = r.json()
    except requests.HTTPError as e:
        logger.warning(f"[tavily] HTTP error {r.status_code}: {e}")
        return []
    except Exception as e:
        logger.warning(f"[tavily] request error: {e}")
        return []

    results = data.get("results", [])
    if not results:
        logger.info("[tavily] no results returned")
        return []

    # ---- Extract candidate subreddits from URLs and snippets ----
    candidates: set[str] = set()
    for item in results:
        url = item.get("url", "")
        snippet = item.get("content", "")
        if "reddit.com" in url:
            sr = _url_to_subreddit(url)
            if sr:
                candidates.add(sr)
        candidates.update(_snippet_to_subreddits(snippet))

    # Remove denylisted and already-known subreddits
    known = set()
    from src.tools.reddit_scraper import COMMUNITY_MAP
    for subs in COMMUNITY_MAP.values():
        known.update(s.lower() for s in subs)
    candidates -= known
    candidates -= _SUBREDDIT_DENYLIST

    if not candidates:
        logger.info("[tavily] no new subreddit candidates found")
        return []

    # Validate existence via HEAD request
    valid: list[str] = []
    for name in sorted(candidates):
        if _is_valid_subreddit(name):
            valid.append(name)
            logger.info(f"[tavily] validated r/{name}")
        else:
            logger.debug(f"[tavily] r/{name} rejected (HEAD check failed)")

    logger.info(f"[tavily] discovered {len(valid)} new subreddits: {valid}")
    return valid