File size: 2,779 Bytes
6c5248d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os, yaml, requests, re, yfinance as yf
from datetime import datetime, timezone, timedelta
from telegram import Bot

# ── load secrets ─────────────────────────────────────────────────────────
GNEWS_KEY  = os.getenv("GNEWS_KEY")
TG_BOT     = Bot(os.getenv("TELEGRAM_TOKEN"))
CHAT_ID    = os.getenv("TELEGRAM_CHAT_ID")

# ── helper constants ──────────────────────────────────────────────────────
BASE_MAP = "sector_map.yaml"
DYN_MAP  = "sector_map_dynamic.yaml"

# ── time helper ────────────────────────────────────────────────────────────
def ist_now():
    return datetime.now(timezone.utc) + timedelta(hours=5, minutes=30)

# ── yaml loaders ──────────────────────────────────────────────────────────
def load_yaml(path):
    if not os.path.exists(path):
        return {}
    with open(path) as f:
        return yaml.safe_load(f) or {}

SECTOR = load_yaml(BASE_MAP)
DYN    = load_yaml(DYN_MAP)

def save_dynamic(store):
    with open(DYN_MAP, "w") as f:
        yaml.safe_dump(store, f)

# ── news & keyword helpers ─────────────────────────────────────────────────
def fetch_news(max_items=250):
    today = datetime.utcnow().strftime("%Y-%m-%d")
    url = (
        f"https://gnews.io/api/v4/search"
        f"?q=India&lang=en&from={today}&token={GNEWS_KEY}&max={max_items}"
    )
    return requests.get(url, timeout=20).json().get("articles", [])

def keyword_hit(text, keywords):
    txt = text.lower()
    return any(k.lower() in txt for k in keywords)

# ── ticker validation ──────────────────────────────────────────────────────
def validate_ticker(tkr, cutoff=5e12):
    if not tkr.endswith(".NS"):
        tkr += ".NS"
    try:
        info = yf.Ticker(tkr).info
        cap = info.get("marketCap") or 0
        return tkr if cap > cutoff else None
    except:
        return None

# ── telegram sender ────────────────────────────────────────────────────────
def send_tg(md):
    TG_BOT.send_message(chat_id=CHAT_ID, text=md, parse_mode="MarkdownV2")