-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFinPyDecorRequests.py
More file actions
59 lines (47 loc) · 1.62 KB
/
Copy pathFinPyDecorRequests.py
File metadata and controls
59 lines (47 loc) · 1.62 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
import numpy as np
import pandas as pd
from pprint import pprint
import requests
from functools import wraps, reduce
from datetime import date, datetime, timedelta, time
from bs4 import BeautifulSoup
import re
"""William Murphy
4/25/2018
use of decorators to
limit the need of classes
"""
# --- decorator with variable input
def ticker(ticker: str):
def decorate(f: callable):
@wraps(f)
def wrapper(url, period, t1, t2, *args, **kwargs):
atom = datetime(1970,1,1)
d = timedelta(days=1).total_seconds()
t1_diff = int(((t1-atom).days)*d)+104400
t2_diff = int(((t2-atom).days)*d)+104400
url_ = (url + '/' + ticker + '/history?period1='
+ str(t1_diff) + '&period2=' + str(t2_diff)
+ '&interval=' + period
+ '&filter=history&frequency=' + period)
print(url_)
F = f(url_, period, t1, t2, *args, **kwargs)
return F
return wrapper
return decorate
@ticker('AAPL')
def makeWebRequest(url, period, t1, t2):
r = requests.get(str(url))
if r.status_code == 200:
print(r.status_code)
print("Connection successful")
return r
else:
print("Failed to connect\n")
# --- test connection
yahoo = makeWebRequest(url="https://finance.yahoo.com/quote",
period='1d', t1=datetime(2018,4,22),
t2=datetime(2018,4,23))
#soup = BeautifulSoup(yahoo.content, 'html.parser')
#table = soup.find_all('table')
print(re.findall(r'\<span\s\bdata-reactid="\d{0,3}">\w+\<*', str(yahoo.content)))