Skip to content
Permalink
Browse files
Set up helper functions for getting all coins a currency and setting …
…a defaul currency with getting a single detail on a coin.
  • Loading branch information
oshianor committed Nov 23, 2022
1 parent e42ec4d commit 68adcc6bda890b163f3f29eb9a206dfff85832e4
Show file tree
Hide file tree
Showing 16 changed files with 263 additions and 21 deletions.
Binary file not shown.

This file was deleted.

Binary file not shown.
@@ -0,0 +1,11 @@
import pymongo


class Mongo:
def __init__(self):
url = "mongodb+srv://:@churchee.qbavh.gcp.mongodb.net/?retryWrites=true&w=majority"
self.client = pymongo.MongoClient(url)

def getClient(self):
print("client", self.client)
return self.client['chatbox']
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,77 @@
import requests
import json

class Coin:
def __init__(self):
self.coinId = None
self.headers = {
"X-RapidAPI-Key": "eeb1cf1684msh83985ef0f081d92p1754ddjsn6941f32fe3d1",
"X-RapidAPI-Host": "coinranking1.p.rapidapi.com"
}

def apiAllCoin(self):
# we're getting all the coins
url = 'https://coinranking1.p.rapidapi.com/coins'
querystring = {"referenceCurrencyUuid": "yhjMzLPhuIDl", "timePeriod": "24h",
"tiers[0]": "1", "orderBy": "marketCap", "orderDirection": "desc", "limit": "50", "offset": "0"}
val = requests.get(url, headers=self.headers, params=querystring)
data = json.loads(val.text)
return data

def apiCoin(self):
# we're getting all the coins
url = 'https://coinranking1.p.rapidapi.com/coin/' + self.coinId
querystring = {
"referenceCurrencyUuid": "yhjMzLPhuIDl", "timePeriod": "24h"}
val = requests.get(url, headers=self.headers, params=querystring)
data = json.loads(val.text)
return data

def getAllCoin(self):
data = self.apiAllCoin()
# print(val.text)
print("\n")
print("Total Coins: ", data["data"]["stats"]["totalCoins"])
for coin in data["data"]["coins"]:
print("-----------------------------------------------------------------------------------------\n")
print("\033[1;3mRanking:\033[0m ", coin["rank"])
print("\033[1;3mSymbol:\033[0m ", coin["symbol"])
print("\033[1;3mCoin name:\033[0m ", coin["name"])
print("\033[1;3mBTC Price:\033[0m ", coin["btcPrice"])
print("\033[1;3mMarket Cap:\033[0m ", coin["marketCap"])
print("\033[1;3mPrice:\033[0m ", coin["price"])
print("\033[1;3mImage Link:\033[0m ", coin["iconUrl"])

return True

def getCoin(self, coinName):
data = self.apiAllCoin()

for x in data["data"]["coins"]:
if x["symbol"].lower() == coinName:
self.coinId = x["uuid"]

val = self.apiCoin()
coin = val["data"]["coin"]
print("-----------------------------------------------------------------------------------------\n")
print("\033[1;3mUUID:\033[0m ", coin["uuid"])
print("\033[1;3mRanking:\033[0m ", coin["rank"])
print("\033[1;3mSymbol:\033[0m ", coin["symbol"])
print("\033[1;3mCoin name:\033[0m ", coin["name"])
print("\033[1;3mBTC Price:\033[0m ", coin["btcPrice"])
print("\033[1;3mMarket Cap:\033[0m ", coin["marketCap"])
print("\033[1;3mPrice:\033[0m ", coin["price"])
print("\033[1;3mImage Link:\033[0m ", coin["iconUrl"])
print("\033[1;3mNumber Of Exchanges:\033[0m ", coin["numberOfExchanges"])
print("\033[1;3mWebsite:\033[0m ", coin["websiteUrl"])
print("\033[1;3mNumber Of Markets:\033[0m ", coin["numberOfMarkets"])
print("\033[1;3mPrice At:\033[0m ", coin["priceAt"])
print("\033[1;3m24hVolume:\033[0m ", coin["24hVolume"])
print("\033[1;3mFully Diluted Market Cap:\033[0m ", coin["fullyDilutedMarketCap"])
print("\033[1;3mCoin Ranking Url:\033[0m ", coin["coinrankingUrl"])
print("\033[1;3mChange:\033[0m ", coin["change"])

return True



@@ -0,0 +1,41 @@
import requests
import json


class Currency:
def __init__(self):
self.currencyName = None
self.url = "https://coinranking1.p.rapidapi.com/reference-currencies"
self.headers = {
"X-RapidAPI-Key": "eeb1cf1684msh83985ef0f081d92p1754ddjsn6941f32fe3d1",
"X-RapidAPI-Host": "coinranking1.p.rapidapi.com"
}
self.querystring = {"limit": "10", "offset": "0"}

def apiAllCurrency(self):
# we're getting all the coins
val = requests.get(self.url, headers=self.headers, params=self.querystring)
data = json.loads(val.text)
return data

def getAllCurrency(self):
data = self.apiAllCurrency()
print("\n data", data)
print("Total Currencies: ", data["data"]["stats"]["total"])
for coin in data["data"]["currencies"]:
print("-----------------------------------------------------------------------------------------\n")
print("\033[1;3mUUID:\033[0m ", coin["uuid"])
print("\033[1;3mtype:\033[0m ", coin["type"])
print("\033[1;3mSymbol:\033[0m ", coin["symbol"])
print("\033[1;3mName:\033[0m ", coin["name"])
print("\033[1;3mSign:\033[0m ", coin["sign"])
print("\033[1;3mImage Link:\033[0m ", coin["iconUrl"])

def setCurrency(self, currencyName):
data = self.apiAllCoin()

print("set all currency all currency")
# coinId
for currency in data["data"]["coins"]:
if currency["symbol"].lower() == currencyName:
self.currencyName = currency["uuid"]
@@ -0,0 +1,73 @@
from logic.coin import Coin
from logic.currency import Currency
import time


class Help:

funcList = [
{"action": "get all currency", "details": "This gives you details about all the currency we support"}, {
"action": "set currency [currency symbol]", "details": "You can use this command to set a default currency to your account profile eg 'set currency usd'"},
{"action": "get all coin",
"details": "You can get all the coins we currently support"},
{"action": "get coin [coin symbol]",
"details": "This gives you information about the specific coin eg 'get coin bitcoin'"}
]

def howItWorks(self):
print(
"\n \033[1;3mPlease read the following steps to guide you on how this platform works \033[0m")
# time.sleep(2)
print(
"\033[1;3mWe've created a shortcut list of commands you can input to get up to speed fast \033[0m\n")
# time.sleep(2)
print(
"\033[1;3mPlease enter the following command below to get started. Goodluck...\033[0m \n")
for x in self.funcList:
print("* \033[1;3m" + x["action"] + "\033[0m")
print(x["details"])
print("\n")

def callFunc(self):

print("\n")
time.sleep(1)

# get an action from the user
action = input("Please enter your command to get started: ")

# split the command and convert it to lower case
cmd = action.lower().split()

# if we want the program to continue
cund = False

# So we're
newCoin = Coin()
newCurrency = Currency()
if cmd[0] == "get":
if cmd[1] == "all":
if cmd[2] == "coin":
cund = newCoin.getAllCoin()
elif cmd[2] == "currency":
cund = newCurrency.getAllCurrency()
else:
print(
"The command you provided isn't recornized. Review \033[1;3m how it works \033[0m for better understanding.")
print("Enter \033[1;3mExit\033[0m to quit the program")
self.callFunc()
if cmd[1] == "coin":
cund = newCoin.getCoin(cmd[2])
elif (["set", "currency"] and cmd) == cmd and len(cmd) == 3:
cund = newCurrency.setCurrency(cmd[2])
elif cmd[0] == "exit":
exit()
else:
print(
"The command you provided isn't recornized. Review \033[1;3m how it works \033[0m for better understanding.")
print("Enter \033[1;3mExit\033[0m to quit the program")
self.howItWorks()
self.callFunc()

if cund:
self.callFunc()
@@ -1,20 +1,39 @@
from data.user import User
from model.user import User
from database.mongo import Mongo

class Welcome:

def welcomeUser(self):
client = Mongo()
db = client.getClient()

print("Welcome to crypto chat-box.\n")
print("Where we make it easier for you to access realtime prices from your favourite coin")
print("To enable your experience smooth, We've created a helper function to help do this faster with a shortcut.\n")

# get the user name
name = self.provideName()
# get the email address
email = self.provideEmail()
# newUser = User(name, email)
userModel = db["account"]

# first we need to find the account if it present
# we'll use their email address
userExist = userModel.find_one({"email": email})

print("\nThank you for providing your name. We just need one last thing from you.")
# print(userExist)
# check if a user was found
if userExist:
print("Welcome " + userExist["name"].capitalize() +
" back to crypto chat-box.\n")
else:
print("\nThank you for providing your Email. We just need one last thing from you.")
# get the user name
name = self.provideName()
print("Thank you for completing your onboarding.. " + name.capitalize())
userModel.insert_one({"email": email, "name": name,
"currencyName": "USD", "currencyID": "yhjMzLPhuIDl"})

# get the mobile number
phoneNumber = self.providePhoneNumber()
newUser = User(name, phoneNumber)
return True

def provideName(self):
name = str(input("how can we address you. Please provide your name: \n"))
@@ -23,9 +42,9 @@ class Welcome:
self.provideName()
return name

def providePhoneNumber(self):
phoneNumber = str(input("Please provide your mobile number: \n"))
if phoneNumber == "":
print("To proceed, you must provide your mobile number.")
self.providePhoneNumber()
return phoneNumber
def provideEmail(self):
email = str(input("Please provide your email address: \n"))
if email == "":
print("To proceed, you must provide your email address.")
self.provideemail()
return email
25 main.py
@@ -1,9 +1,28 @@
from logic.welcome import Welcome
from logic.help import Help
from database.mongo import Mongo
import time

def main():
newWelcome = Welcome()
# Welcome the user and get their private information
newWelcome.welcomeUser()

proceed = False
newWelcome = Welcome()
# Welcome the user and get their private information
proceed = newWelcome.welcomeUser()

# check if we've completed the task for
# welcoming the user then proceed to the next task
if not proceed:
print("We're sorry but we're going to have to terminate this process becuase we couldn't onboard you. \n")
print("Please try again. \n")
time.sleep(2)
exit()

newHelp = Help()
newHelp.howItWorks()
newHelp.callFunc()



if __name__ == "__main__":
main()
Binary file not shown.
@@ -0,0 +1,5 @@
class User:
def __init__(self, name, email):
self.name = name
self.email = email

@@ -0,0 +1,2 @@
pymongo>=4.3.3
requests>=2.28.1

0 comments on commit 68adcc6

Please sign in to comment.