-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_price.py
84 lines (69 loc) · 2.32 KB
/
get_price.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
# A PYTHON SCRIPT TO GET BITCOIN PRICE
# IMPORT REQUEST MODULE
import requests
# IMPORT SQL CONFIGS
from sqlconf import show_database, save_database
# GENERATE DATE AND TIME
def date_time():
import datetime
now = datetime.datetime.now()
global date , time
date = str(now.strftime("%Y-%m-%d"))
time = str(now.strftime("%H:%M:%S"))
def programme():
choose = input("""
<Menu>
1) SHOW BITCOIN CURRENT PRICE
2) SHOW CHART OF BITCOIN FROM YOUR DATABASE
3) EXIT
:? """)
# SHOW BITCOIN PRICE
if choose == "1" :
print("\nGetting Current Price .. ")
date_time()
# GET PRICE OF BITCOIN
def prices():
global name,price
name = "bitcoin"
r = requests.get(" https://api.coindesk.com/v1/bpi/currentprice.json")
respond = r.json()
price = respond["bpi"]["USD"]["rate"]
# SHOW PRICE AND OTHER INFO TO USER IN A TABLE
print("")
print("**",name,"**")
print("")
print("+","="*17,"+","="*17,"+","="*17,"+",sep="")
print("|","Date".center(15),"|","time".center(15),"|","price".center(15),"|")
print("+","-"*17,"+","-"*17,"+","-"*17,"+",sep="")
print("|",date.center(15),"|",time.center(15),"|",price.center(15),"|")
print("")
# ASK TO SAVE IN DATABASE OR NOT
answer = input("Do you want to save this data in mysql?(y/n): ")
if answer.upper() == "Y" :
try :
save_database(date,time,price)
except :
print("An error occurred when saving data in database!")
finally :
programme()
else:
programme()
prices()
# SHOW DATABASE
elif choose == "2" :
print("Enter username and password to connect to database")
try :
show_database()
except :
print("\nAn error occured when showing data from database!")
finally :
programme()
# EXIT
elif choose == "3" :
print("Bye!")
exit()
# WRONG SELECTION
else :
print("\nWrong selection! try again.")
programme()
programme()