Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
crypto-chat/logic/currency.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
69 lines (54 sloc)
2.34 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
from database.mongo import Mongo | |
class Currency: | |
def __init__(self): | |
self.currencyName = None | |
self.currencyID = 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": "30", "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"]) | |
return True | |
def setCurrency(self, user, currencyName): | |
client = Mongo() | |
db = client.getClient() | |
userModel = db["account"] | |
data = self.apiAllCurrency() | |
for currency in data["data"]["currencies"]: | |
if currency["symbol"].lower() == currencyName.lower(): | |
self.currencyID = currency["uuid"] | |
self.currencyName = currency["symbol"] | |
if not self.currencyName and self.currencyID: | |
print("\nWe couldn't find any currency with the name you provided. Please try again.") | |
userModel.find_one_and_update({"email": user["email"]}, {'$set': {"currencyName": self.currencyName, "currencyID": self.currencyID}}) | |
return True | |
def getCurrency(self, user): | |
client = Mongo() | |
db = client.getClient() | |
userModel = db["account"] | |
userExist = userModel.find_one({"email": user["email"]}) | |
print("\033[1;3mName:\033[0m ", userExist["name"]) | |
print("\033[1;3mEmail:\033[0m ", userExist["email"]) | |
print("\033[1;3mCurrency:\033[0m ", userExist["currencyName"]) | |
print("\033[1;3mCurrency UUID:\033[0m ", userExist["currencyID"]) | |
return True |