-
Notifications
You must be signed in to change notification settings - Fork 0
/
motley_fool_content.py
78 lines (73 loc) · 2.87 KB
/
motley_fool_content.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
import pdb
import requests
from bs4 import BeautifulSoup as bs
from urllib.parse import urlparse, parse_qs
import yaml
import json
import re
def get_tickers(service, credentials):
email = credentials['email']
password = credentials['password']
#with open('config.yml') as file:
# creds = yaml.load(file, Loader=yaml.FullLoader)
# email = creds[service]['email']
# password = creds[service]['password']
# Set global session user agent once
s = requests.Session()
s.headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36"}
r = s.head('https://www.fool.com/premium/auth/authenticate', allow_redirects=True)
site = s.get(r.url)
csrf_token = site.cookies['_csrf']
parsed_url = urlparse(r.url)
parsed_query = parse_qs(parsed_url.query)
client_id = parsed_query['client'][0]
nonce = parsed_query['nonce'][0]
state = parsed_query['state'][0]
data = {
"client_id":client_id,
"redirect_uri":"https://www.fool.com/premium/auth/callback/",
"tenant":"fool",
"response_type":"code",
"scope":"openid email profile",
"_csrf":csrf_token,
"state":state,
"_intstate":"deprecated",
"nonce":nonce,
"password":password,
"connection":"TMF-Reg-API",
"username":email
}
login_response = s.post("https://auth.fool.com/usernamepassword/login",data=data)
bs_content = bs(login_response.text, "html.parser")
wresult_token = bs_content.find("input",{"name":"wresult"})["value"]
wctx_token = bs_content.find("input",{"name":"wctx"})["value"]
headers = {
"Origin": "https://auth.fool.com",
"Referer":r.url
}
data = {
"wa":"wsignin1.0",
"wresult":wresult_token,
"wctx":wctx_token
}
login_response = s.post("https://auth.fool.com/login/callback",data=data, headers=headers, allow_redirects = True)
bs_content = bs(login_response.text, "html.parser")
recommendations = bs_content.find_all("div",{"class":"watch-state-button"})
tickers = []
for recommendation in recommendations:
tickers.append(json.dumps(recommendation.text).split()[1])
tickers = set(tickers)
new_rec_tickers = []
try:
new_recs = bs_content.find_all("th",{"class":"primary"})
for new_rec in new_recs:
if "New Stock" in new_rec.text:
z = re.search("(?=\/).*(?<=.png)", str(new_rec))
if z is not None:
new_rec_tickers.append(z.group(0).split('/')[-1].split('.')[0])
except Exception as e:
print("Failed to get new recommendations")
print(e)
print("new recommendations")
print(new_rec_tickers)
return list(tickers), new_rec_tickers