-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.py
58 lines (45 loc) · 1.46 KB
/
utils.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
from tenacity import retry, stop_after_attempt, wait_random_exponential
import requests
import json
from datetime import datetime
import re
from typing import List
def correct_date(yr, dt):
"""Some transcripts have incorrect date, correcting it
Args:
yr (int): actual
dt (datetime): given date
Returns:
datetime: corrected date
"""
dt = datetime.strptime(dt, "%Y-%m-%d %H:%M:%S")
if dt.year != yr:
dt = dt.replace(year=yr)
return dt.strftime("%Y-%m-%d %H:%M:%S")
def extract_speakers(cont: str) -> List[str]:
"""Extract the list of speakers
Args:
cont (str): transcript content
Returns:
List[str]: list of speakers
"""
pattern = re.compile(r"\n(.*?):")
matches = pattern.findall(cont)
return list(set(matches))
@retry(wait=wait_random_exponential(min=1, max=5), stop=stop_after_attempt(2))
def get_earnings_transcript(quarter: str, ticker: str, year: int):
"""Get the earnings transcripts
Args:
quarter (str)
ticker (str)
year (int)
"""
response = requests.get(
f"https://discountingcashflows.com/api/transcript/{ticker}/{quarter}/{year}/",
auth=("user", "pass"),
)
resp_text = json.loads(response.text)
# speakers_list = extract_speakers(resp_text[0]["content"])
corrected_date = correct_date(resp_text[0]["year"], resp_text[0]["date"])
resp_text[0]["date"] = corrected_date
return resp_text[0]