Repository navigation
Expand file tree
/
Copy pathweb_context.py
More file actions
96 lines (84 loc) · 3.06 KB
/
Copy pathweb_context.py
File metadata and controls
96 lines (84 loc) · 3.06 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
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
"""Public links for a corridor. They never become a trade value."""
import json
import re
import urllib.parse
import urllib.request
_MONEY = re.compile(
r"(?:\$\s?\d[\d.,]*|\b\d[\d.,]*\s*(?:trillion|billion|million))(?:\s*(?:trillion|billion|million|bn|mn))?",
re.IGNORECASE,
)
def _strip_money(text: str) -> str:
cleaned = _MONEY.sub("", text or "")
return " ".join(cleaned.split())
def _wikipedia(query: str) -> list[dict]:
params = urllib.parse.urlencode(
{
"action": "opensearch",
"search": query[:120],
"limit": 3,
"namespace": 0,
"format": "json",
}
)
request = urllib.request.Request(
"https://en.wikipedia.org/w/api.php?" + params,
headers={"User-Agent": "ASAN-Macro/1.0 (trade brief; context links only)"},
)
with urllib.request.urlopen(request, timeout=8) as response:
data = json.loads(response.read().decode())
titles = data[1] if len(data) > 1 else []
urls = data[3] if len(data) > 3 else []
return [{"title": title, "url": url} for title, url in zip(titles, urls)]
def _duckduckgo(query: str) -> list[dict]:
params = urllib.parse.urlencode(
{"q": query[:120], "format": "json", "no_html": "1", "skip_disambig": "1"}
)
request = urllib.request.Request(
"https://api.duckduckgo.com/?" + params,
headers={"User-Agent": "ASAN-Macro/1.0 (trade brief; context links only)"},
)
with urllib.request.urlopen(request, timeout=8) as response:
data = json.loads(response.read().decode())
links = []
if data.get("AbstractURL") and data.get("Heading"):
links.append({"title": data["Heading"], "url": data["AbstractURL"]})
for topic in data.get("RelatedTopics") or []:
if isinstance(topic, dict) and topic.get("FirstURL"):
links.append({"title": topic.get("Text") or "Public page", "url": topic["FirstURL"]})
if len(links) >= 3:
break
return links[:3]
def _search(query: str) -> list[dict]:
links: list[dict] = []
for finder in (_duckduckgo, _wikipedia):
try:
links.extend(finder(query) or [])
except Exception:
continue
if len(links) >= 3:
break
return links[:3]
def public_notes(query: str, search=None) -> dict:
"""Up to three links. Titles are prose, not a source of chart dollars."""
finder = search or _search
try:
raw = finder(query) or []
except Exception:
raw = []
links = []
for item in raw:
url = str(item.get("url") or "")
title = _strip_money(str(item.get("title") or "Public page"))
if url.startswith("http") and title:
links.append({"title": title, "url": url})
if len(links) == 3:
break
if not links:
return {"summary": "", "links": []}
return {
"summary": (
"What people are saying is context only. "
"These links are public pages. They are not the source of any figure in the chart."
),
"links": links,
}