From 81e323245661e61fea7330a4ad1d019ab20008e3 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Tue, 1 Feb 2022 20:49:54 +0000 Subject: [PATCH] Implement editing ads and distributing providers --- handler/agora.py | 60 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 52 insertions(+), 8 deletions(-) diff --git a/handler/agora.py b/handler/agora.py index 8446650..2415a5e 100644 --- a/handler/agora.py +++ b/handler/agora.py @@ -85,6 +85,8 @@ class Agora(object): return False if dash is False: return False + if dash["response"] is None: + return False dash_tmp = {} if not dash.items(): return False @@ -442,6 +444,8 @@ class Agora(object): if our_ads is False: return False 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_btc = {} 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_btc = rates_crypto["bitcoin"][currency] 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: return False 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: return False except KeyError: @@ -673,7 +677,7 @@ class Agora(object): return (min_local, max_local) @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. 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 # if asset == "BTC": # del form["min_amount"] - ad = self.agora.ad_create(**form) + if edit: + ad = self.agora.ad(ad_id=ad_id, **form) + else: + ad = self.agora.ad_create(**form) 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): """ - 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. :return: False or dict with response :rtype: bool or dict """ - for asset in loads(settings.Agora.AssetList): - for currency, countrycode in loads(settings.Agora.DistList): - rtrn = self.create_ad(asset, countrycode, currency, "REVOLUT") + dist_list = list(self.create_distribution_list()) + our_ads = self.enum_ads() + # 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: return False 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 def strip_duplicate_ads(self): """