-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
69 lines (56 loc) · 2.21 KB
/
test.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
import requests
import datetime
import pyEX as p
import os
import psycopg2
c = p.Client(api_token='pk_13bea402dd284dd994c2a87b076d4d9f')
if not os.environ.get('DATABASE_URL'):
raise RuntimeError("DATABASE_URL NOT SET")
DATABASE_URL = os.environ['DATABASE_URL']
conn = psycopg2.connect(DATABASE_URL, sslmode='require')
cur = conn.cursor()
def news(symbol):
articles = c.news(symbol)[:3]
return [
{
"Date": datetime.datetime.fromtimestamp(articles[0]["datetime"]/1000).strftime("%B %d"),
"ImageUrl": articles[0]["image"],
"Headline": articles[0]["headline"],
"Summary": articles[0]["summary"][0:120],
"Url": articles[0]["url"]
},
{
"Date": datetime.datetime.fromtimestamp(articles[1]["datetime"]/1000).strftime("%B %d"),
"ImageUrl": articles[1]["image"],
"Headline": articles[1]["headline"],
"Summary": articles[1]["summary"][0:120],
"Url": articles[1]["url"]
},
{
"Date": datetime.datetime.fromtimestamp(articles[2]["datetime"]/1000).strftime("%B %d"),
"ImageUrl": articles[2]["image"],
"Headline": articles[2]["headline"],
"Summary": articles[2]["summary"][0:120],
"Url": articles[2]["url"]
}
]
sym = "AAPL"
# art = news(sym)
cur.execute("SELECT * FROM news WHERE Symbol=%s;", (sym,))
row = cur.fetchall()
print(row)
# cur.execute("INSERT INTO news VALUES(%s,%s,%s,%s,%s,%s);",(sym, art[0]["Headline"], art[0]["ImageUrl"], art[0]["Summary"], art[0]["Url"], art[0]["Date"],))
# cur.execute("COMMIT;")
# symbol = 'BBBY'
# # my_time = datetime.datetime.fromtimestamp(1660647693000/1000).strftime("%B %d")
# # print(my_time)
# # api_key = 'SAOS0Y8B63XM4DPK'
# # url = f"https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol={symbol}&interval=1min&apikey={api_key}"
# # response = requests.get(url)
# # chart = response.json()
# # labels = []
# # data = []
# # for label in chart["Time Series (1min)"]:
# # data.append(round(float(chart["Time Series (1min)"][label]["4. close"]),2))
# # print(data)
# print(c.quote(symbol))