-
Notifications
You must be signed in to change notification settings - Fork 1
/
post_generator.py
298 lines (245 loc) · 7.4 KB
/
post_generator.py
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
"""Script to generate a new post"""
import datetime as dt
import json
import os
import random
import re
from functools import cache
from pathlib import Path
from typing import TypedDict
import requests
import yfinance as yf
AUTHOR = "MarketBot"
TICKERS: dict[str, str] = {
# description, symbol
"NASDAQ": "^IXIC",
"Dow": "^DJI",
"S&P 500": "^GSPC",
"Nikkei": "^N225",
"FTSE": "^FTSE",
"Capital One Stock": "COF",
}
UPS: list[str] = [
"soars",
"skyrockets",
"catapults",
"zooms",
"jumps",
"shoots up",
"goes wild",
]
DOWNS: list[str] = [
"plummets",
"tanks",
"in free fall",
"dives",
"plunges",
"scrambling",
"tumbles",
]
EXCLUDED_SECTIONS: list[str] = [
"About",
"Better Business",
"Business",
"Business to business",
"Opinion",
"Community",
"Crosswords",
"Global development",
"Help",
"Inequality",
"Info",
"Jobs",
"Membership",
"Money",
"News",
"Politics",
"Search",
"From the Guardian",
"From the Observer",
"Guardian holiday offers",
"World news",
]
"""Cite: https://college.harvard.edu/financial-aid/how-aid-works accessed on 2024-02-23,
using "Total billed and unbilled costs," plus the cost of health insurance, for the
23-24 academic year, multiplied by four. In USD."""
COST_OF_FULL_RIDE_AT_HARVARD: float = 366280
@cache
def get_gbp_to_usd() -> float:
"""Grab the current GBP to USD conversion rate from Yahoo Finance
Returns
-------
float
The value in US dollars equivalent to one British Pound
"""
# The symbol for GBP to USD on Yahoo Finance is "GBPUSD=X"
gbp_usd = yf.Ticker("GBPUSD=X")
# Fetch the latest price
hist = gbp_usd.history(period="1d")
# Return the last closing price, which represents the latest exchange rate
return hist["Close"].iloc[-1]
def _clean_headline(headline: str) -> str:
"""Get to the relevant "sound byte" of a headline"""
return headline.split("–")[0].split(":")[-1].split(";")[0].split("|")[0].strip()
def get_market_movement(ticker: str, date: dt.date) -> bool:
"""Ping Yahoo Finance and determine whether the
given ticker closed higher or lower than it opened
on the specified date.
Parameters
----------
ticker: str
The symbol of the stock or index to query
date : date
The date to query
Returns
-------
bool
True if the stock closed higher than it opened, False otherwise.
"""
stonk = (
yf.Ticker(ticker)
.history(period="1d", start=date, end=date + dt.timedelta(days=1))
.reset_index()
)
try:
return stonk.at[0, "Close"] > stonk.at[0, "Open"]
except KeyError as no_data:
raise ValueError(
f"No data found for {ticker} on {date.isoformat()}. Is it a weekend or a holiday?"
)
class _Article(TypedDict):
"""Everything important we're extracting from an article"""
headline: str
lede: str
url: str
tags: list[str]
def get_random_article(api_key: str, date: dt.date) -> _Article:
"""Grab a bunch of random articles from The Guardian
from a given date and select one to use as the basis
of this post
Parameters
----------
api_key : str
An access key for The Guardian's API
date : date
The date to query
Returns
-------
dict
The relevant parts of the article
"""
response = requests.get(
"https://content.guardianapis.com/search",
params={
"api-key": api_key,
"from-date": date.isoformat(),
"to-date": (date + dt.timedelta(days=1)).isoformat(),
"page-size": "50",
"section": ",".join([f"-{section}" for section in EXCLUDED_SECTIONS]),
},
)
if response.status_code != 200:
raise RuntimeError(
f"Couldn't hit The Guardian API. Recieved error code: {response.status_code}\n{response.text}"
)
articles = json.loads(response.text)["response"]["results"]
response = requests.get(
random.choice(articles)["apiUrl"],
params={
"api-key": api_key,
"show-fields": ["body"],
},
)
if response.status_code != 200:
raise RuntimeError(
f"Couldn't hit The Guardian API. Recieved error code: {response.status_code}\n{response.text}"
)
copyright_notice = "© The Guardian—"
article = json.loads(response.text)["response"]["content"]
return {
"headline": _clean_headline(article["webTitle"]),
"lede": copyright_notice
+ article["fields"]["body"].split("</p>")[0].split("<p>")[-1],
"url": article["webUrl"],
"tags": [article["pillarName"]],
}
def express_values_in_scholarships(raw_content: str) -> str:
"""Express any monetary values in a string in terms of full rides to Harvard,
a la https://www.smbc-comics.com/comic/2014-09-28
Parameters
----------
raw_content : str
The original content
Returns
-------
str
The converted content
"""
values = re.findall(r"([\$|£])([0-9\.,]*)(m|bn|)", raw_content)
converted = raw_content
for currency, figure_str, multiplier in values:
figure = float(figure_str.replace(",", ""))
if currency == "£":
figure *= get_gbp_to_usd()
match multiplier:
case "m":
figure *= 1e6
case "bn":
figure *= 1e9
case _:
pass
n_scholarships = figure / COST_OF_FULL_RIDE_AT_HARVARD
if n_scholarships < 1:
substitution = f"{n_scholarships:.1g}"
else:
substitution = f"{n_scholarships:.1f}"
substitution += " full rides to Harvard"
converted = converted.replace(
f"{currency}{figure_str}{multiplier}", substitution
)
return converted
def generate_post(guardian_api_key: str, date: dt.date | None = None) -> str:
"""Create a new post
Parameters
----------
guardian_api_key : str
An access key for The Guardian's API
date : date, optional
The date to generate the post for. If None is given, yesterday will be used.
Returns
-------
str
Hugo-style markdown post
"""
date = date or dt.date.today() - dt.timedelta(days=1)
indicator, symbol = random.choice(list(TICKERS.items()))
is_up = get_market_movement(symbol, date)
status = random.choice(UPS if is_up else DOWNS)
while True:
article = get_random_article(guardian_api_key, date)
if "<" in (article["headline"] + article["lede"]):
continue
break
article["headline"] = express_values_in_scholarships(article["headline"])
article["lede"] = express_values_in_scholarships(article["lede"])
headline = " ".join([indicator, status, "as", article["headline"]])
tags = ", ".join([f'"{tag}"' for tag in article["tags"] + [indicator, symbol]])
return f"""---
author: {AUTHOR}
title: >
{headline}
summary: >
{article["lede"]}
image: {"up" if is_up else "down"}1.png
tags: [{tags}]
date: {date.isoformat()}
redirect_to: {article["url"]}
redirect_enabled: true
---
"""
if __name__ == "__main__":
random.seed()
date = dt.date.today() - dt.timedelta(days=1)
Path(f"content/redirect/{date.isoformat()}.md").write_text(
generate_post(os.environ["GUARDIAN_API_KEY"], date)
)