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 52 additions and 8 deletions
Showing only changes of commit a1a13478cb - Show all commits

View File

@ -85,6 +85,8 @@ class Agora(object):
return False return False
if dash is False: if dash is False:
return False return False
if dash["response"] is None:
return False
dash_tmp = {} dash_tmp = {}
if not dash.items(): if not dash.items():
return False return False
@ -442,6 +444,8 @@ class Agora(object):
if our_ads is False: if our_ads is False:
return False return False
currencies = [x[3].lower() for x in our_ads] currencies = [x[3].lower() for x in our_ads]
# Sets deduplicate by default
providers = list(set([x[4] for x in our_ads]))
public_ad_dict_xmr = {} public_ad_dict_xmr = {}
public_ad_dict_btc = {} public_ad_dict_btc = {}
rates_crypto = self.cg.get_price(ids=["monero", "bitcoin"], vs_currencies=currencies) rates_crypto = self.cg.get_price(ids=["monero", "bitcoin"], vs_currencies=currencies)
@ -450,11 +454,11 @@ class Agora(object):
rates_xmr = rates_crypto["monero"][currency] rates_xmr = rates_crypto["monero"][currency]
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, providers=providers, rates=rates_xmr)
if public_ad_dict_xmr[currency] is False: if public_ad_dict_xmr[currency] is False:
return False 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, providers=providers, rates=rates_btc)
if public_ad_dict_btc[currency] is False: if public_ad_dict_btc[currency] is False:
return False return False
except KeyError: except KeyError:
@ -673,7 +677,7 @@ class Agora(object):
return (min_local, max_local) return (min_local, max_local)
@handle_exceptions @handle_exceptions
def create_ad(self, asset, countrycode, currency, provider): def create_ad(self, asset, countrycode, currency, provider, edit=True, ad_id=None):
""" """
Post an ad with the given asset in a country with a given currency. Post an ad with the given asset in a country with a given currency.
Convert the min and max amounts from settings to the given currency with CurrencyRates. Convert the min and max amounts from settings to the given currency with CurrencyRates.
@ -739,23 +743,63 @@ class Agora(object):
# Dirty hack to test # Dirty hack to test
# if asset == "BTC": # if asset == "BTC":
# del form["min_amount"] # del form["min_amount"]
if edit:
ad = self.agora.ad(ad_id=ad_id, **form)
else:
ad = self.agora.ad_create(**form) ad = self.agora.ad_create(**form)
return ad return ad
def create_distribution_list(self):
"""
Create a list for distribution of ads.
:return: generator of asset, countrycode, currency, provider
:rtype: generator of tuples
"""
# Iterate providers like REVOLUT, NATIONAL_BANK
for provider in loads(settings.Agora.ProviderList):
# Iterate assets like XMR, BTC
for asset in loads(settings.Agora.AssetList):
# Iterate pairs of currency and country like EUR, GB
for currency, countrycode in loads(settings.Agora.DistList):
yield (asset, countrycode, currency, provider)
def dist_countries(self): def dist_countries(self):
""" """
Distribute our advert into all countries listed in the config. Distribute our advert into all countries and providers listed in the config.
Exits on errors. Exits on errors.
:return: False or dict with response :return: False or dict with response
:rtype: bool or dict :rtype: bool or dict
""" """
for asset in loads(settings.Agora.AssetList): dist_list = list(self.create_distribution_list())
for currency, countrycode in loads(settings.Agora.DistList): our_ads = self.enum_ads()
rtrn = self.create_ad(asset, countrycode, currency, "REVOLUT") # Let's get rid of the ad IDs and make it a tuple like dist_list
our_ads = [(x[0], x[2], x[3], x[4]) for x in our_ads]
for asset, countrycode, currency, provider in dist_list:
if (asset, countrycode, currency, provider) not in our_ads:
# Create the actual ad and pass in all the stuff
rtrn = self.create_ad(asset, countrycode, currency, provider)
# Bail on first error, let's not continue
if rtrn is False: if rtrn is False:
return False return False
yield rtrn yield rtrn
def redist_countries(self):
"""
Redistribute our advert details into all our listed adverts.
This will edit all ads and update the details. Only works if we have already run dist.
This will not post any new ads.
Exits on errors.
:return: False or dict with response
:rtype: bool or dict
"""
our_ads = self.enum_ads()
for asset, ad_id, countrycode, currency, provider in our_ads:
rtrn = self.create_ad(asset, countrycode, currency, provider, edit=True, ad_id=ad_id)
# Bail on first error, let's not continue
if rtrn is False:
return False
yield (rtrn, ad_id)
@handle_exceptions @handle_exceptions
def strip_duplicate_ads(self): def strip_duplicate_ads(self):
""" """