Finish implementation and tests for the cheat system #3

Closed
m wants to merge 67 commits from cheat-refactor into master
1 changed files with 67 additions and 15 deletions
Showing only changes of commit b7acf5a84d - Show all commits

View File

@ -14,6 +14,8 @@ from datetime import datetime
# Project imports # Project imports
from settings import settings from settings import settings
log = Logger("agora.global")
def handle_exceptions(func): def handle_exceptions(func):
def inner_function(*args, **kwargs): def inner_function(*args, **kwargs):
@ -21,10 +23,13 @@ def handle_exceptions(func):
rtrn = func(*args, **kwargs) rtrn = func(*args, **kwargs)
except (ReadTimeout, ReadError): except (ReadTimeout, ReadError):
return False return False
if "success" in rtrn: if isinstance(rtrn, dict):
if "message" in rtrn: if "success" in rtrn:
if not rtrn["success"] and rtrn["message"] == "API ERROR": if "message" in rtrn:
return False if not rtrn["success"] and rtrn["message"] == "API ERROR":
log.error("API error: {code}", code=rtrn["response"]["error"]["error_code"])
return False
return rtrn
return inner_function return inner_function
@ -69,7 +74,7 @@ class Agora(object):
@handle_exceptions @handle_exceptions
def wrap_dashboard(self): def wrap_dashboard(self):
dash = self.agora.dashboard_seller() dash = self.agora.dashboard_seller()
if not dash: if dash is False:
return False return False
dash_tmp = {} dash_tmp = {}
if "data" not in dash["response"].keys(): if "data" not in dash["response"].keys():
@ -100,8 +105,8 @@ class Agora(object):
""" """
dash = self.wrap_dashboard() dash = self.wrap_dashboard()
rtrn = [] rtrn = []
if not dash.items(): if dash is False:
return return False
for contact_id, contact in dash.items(): for contact_id, contact in dash.items():
reference = self.tx.tx_to_ref(contact_id) reference = self.tx.tx_to_ref(contact_id)
buyer = contact["data"]["buyer"]["username"] buyer = contact["data"]["buyer"]["username"]
@ -168,6 +173,8 @@ class Agora(object):
:rtype: list or bool :rtype: list or bool
""" """
dash = self.agora.dashboard_seller() dash = self.agora.dashboard_seller()
if dash is False:
return False
dash_tmp = [] dash_tmp = []
if "data" not in dash["response"]: if "data" not in dash["response"]:
self.log.error("Data not in dashboard response: {content}", content=dash) self.log.error("Data not in dashboard response: {content}", content=dash)
@ -200,6 +207,8 @@ class Agora(object):
""" """
messages_tmp = {} messages_tmp = {}
messages = self.agora.recent_messages() messages = self.agora.recent_messages()
if messages is False:
return False
if not messages["success"]: if not messages["success"]:
return False return False
if "data" not in messages["response"]: if "data" not in messages["response"]:
@ -241,6 +250,8 @@ class Agora(object):
@handle_exceptions @handle_exceptions
def enum_ad_ids(self, page=0): def enum_ad_ids(self, page=0):
ads = self.agora._api_call(api_method="ads", query_values={"page": page}) ads = self.agora._api_call(api_method="ads", query_values={"page": page})
if ads is False:
return False
ads_total = [] ads_total = []
if not ads["success"]: if not ads["success"]:
return False return False
@ -249,13 +260,21 @@ class Agora(object):
if "pagination" in ads["response"]: if "pagination" in ads["response"]:
if "next" in ads["response"]["pagination"]: if "next" in ads["response"]["pagination"]:
page += 1 page += 1
for ad in self.enum_ad_ids(page): ads_iter = self.enum_ad_ids(page)
if ads_iter is None:
return False
if ads_iter is False:
return False
for ad in ads_iter:
ads_total.append(ad) ads_total.append(ad)
return ads_total return ads_total
@handle_exceptions @handle_exceptions
def enum_ads(self, page=0): def enum_ads(self, page=0):
ads = self.agora._api_call(api_method="ads", query_values={"page": page}) ads = self.agora._api_call(api_method="ads", query_values={"page": page})
if ads is False:
print("enum ads is false for", page)
return False
ads_total = [] ads_total = []
if not ads["success"]: if not ads["success"]:
return False return False
@ -264,7 +283,15 @@ class Agora(object):
if "pagination" in ads["response"]: if "pagination" in ads["response"]:
if "next" in ads["response"]["pagination"]: if "next" in ads["response"]["pagination"]:
page += 1 page += 1
for ad in self.enum_ads(page): ads_iter = self.enum_ads(page)
print("ADS ITER", ads_iter)
if ads_iter is None:
print("enum ads iter is none for", page)
return False
if ads_iter is False:
print("enum ads iter is false for", page)
return False
for ad in ads_iter:
ads_total.append([ad[0], ad[1], ad[2], ad[3]]) ads_total.append([ad[0], ad[1], ad[2], ad[3]])
return ads_total return ads_total
@ -289,7 +316,8 @@ class Agora(object):
if coin == "bitcoin": if coin == "bitcoin":
coin = "bitcoins" coin = "bitcoins"
ads = self.agora._api_call(api_method=f"buy-{coin}-online/{currency}/REVOLUT", query_values={"page": page}) ads = self.agora._api_call(api_method=f"buy-{coin}-online/{currency}/REVOLUT", query_values={"page": page})
if not ads["success"]: if ads is False:
print("public ads is false for", coin, currency)
return False return False
for ad in ads["response"]["data"]["ad_list"]: for ad in ads["response"]["data"]["ad_list"]:
if ad["data"]["online_provider"] == "REVOLUT": if ad["data"]["online_provider"] == "REVOLUT":
@ -301,7 +329,14 @@ class Agora(object):
if "pagination" in ads["response"]: if "pagination" in ads["response"]:
if "next" in ads["response"]["pagination"]: if "next" in ads["response"]["pagination"]:
page += 1 page += 1
for ad in self.enum_public_ads(coin, currency, page): ads_iter = self.enum_public_ads(coin, currency, page)
if ads_iter is None:
print("public ads iter is false for", coin, currency, page)
return False
if ads_iter is False:
print("public ads iter is none for", coin, currency, page)
return False
for ad in ads_iter:
yield [ad[0], ad[1], ad[2]] yield [ad[0], ad[1], ad[2]]
def wrap_public_ads(self, asset, currency, rates=None): def wrap_public_ads(self, asset, currency, rates=None):
@ -312,7 +347,11 @@ class Agora(object):
coin = "monero" coin = "monero"
elif asset == "BTC": elif asset == "BTC":
coin = "bitcoin" coin = "bitcoin"
ads = list(self.enum_public_ads(coin, currency.upper())) ads_obj = self.enum_public_ads(coin, currency.upper())
if ads_obj is False:
print("wrap public ads is false for", asset, currency)
return False
ads = list(ads_obj)
if not rates: if not rates:
# Set the price based on the asset # Set the price based on the asset
base_currency_price = self.cg.get_price(ids=coin, vs_currencies=currency)[coin][currency.lower()] base_currency_price = self.cg.get_price(ids=coin, vs_currencies=currency)[coin][currency.lower()]
@ -333,6 +372,12 @@ class Agora(object):
@handle_exceptions @handle_exceptions
def update_prices(self, xmr=True, btc=True): def update_prices(self, xmr=True, btc=True):
our_ads = self.enum_ads() our_ads = self.enum_ads()
if our_ads is None:
print("our ads is none for", xmr, btc)
return False
if our_ads is False:
print("our ads is false for", xmr, btc)
return False
currencies = [x[3].lower() for x in our_ads] currencies = [x[3].lower() for x in our_ads]
public_ad_dict_xmr = {} public_ad_dict_xmr = {}
public_ad_dict_btc = {} public_ad_dict_btc = {}
@ -343,8 +388,14 @@ class Agora(object):
rates_btc = rates_crypto["bitcoin"][currency] rates_btc = rates_crypto["bitcoin"][currency]
if xmr: if xmr:
public_ad_dict_xmr[currency] = self.wrap_public_ads("XMR", currency, rates=rates_xmr) public_ad_dict_xmr[currency] = self.wrap_public_ads("XMR", currency, rates=rates_xmr)
if public_ad_dict_xmr[currency] is False:
print("public ad dict xmr is false for", currency)
return False
if btc: if btc:
public_ad_dict_btc[currency] = self.wrap_public_ads("BTC", currency, rates=rates_btc) public_ad_dict_btc[currency] = self.wrap_public_ads("BTC", currency, rates=rates_btc)
if public_ad_dict_btc[currency] is False:
print("public ad dict btc is false for", currency)
return False
except KeyError: except KeyError:
self.log.error("Error getting public ads for currency {currency}", currency=currency) self.log.error("Error getting public ads for currency {currency}", currency=currency)
continue continue
@ -370,7 +421,8 @@ class Agora(object):
# Get all of our ads # Get all of our ads
our_ads_list = [ad for ad in public_ads if ad[1] == settings.Agora.Username] our_ads_list = [ad for ad in public_ads if ad[1] == settings.Agora.Username]
if not len(our_ads_list) == 1: if not len(our_ads_list) == 1:
self.log.error("More ads than expected: {ads}", ads=our_ads_list) if not len(set([x[3] for x in our_ads_list])) == 1:
self.log.error("Our ads have different margins: {ads}", ads=our_ads_list)
# Get one from the list, they're probably the same, if not we will deal with the other # Get one from the list, they're probably the same, if not we will deal with the other
# ones in a later pass # ones in a later pass
@ -464,7 +516,7 @@ class Agora(object):
""" """
ads = self.enum_ad_ids() ads = self.enum_ad_ids()
return_ids = [] return_ids = []
if not ads: if ads is False:
return False return False
for ad_id in ads: for ad_id in ads:
rtrn = self.agora.ad_delete(ad_id) rtrn = self.agora.ad_delete(ad_id)
@ -580,7 +632,7 @@ class Agora(object):
for asset in loads(settings.Agora.AssetList): for asset in loads(settings.Agora.AssetList):
for currency, countrycode in loads(settings.Agora.DistList): for currency, countrycode in loads(settings.Agora.DistList):
rtrn = self.create_ad(asset, countrycode, currency) rtrn = self.create_ad(asset, countrycode, currency)
if not rtrn: if rtrn is False:
return False return False
yield rtrn yield rtrn