From 5649909385d14ad72e7644da18707927b7d13dbb Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Fri, 31 Dec 2021 00:39:52 +0000 Subject: [PATCH] Implement smarter ad management --- handler/agora.py | 81 ++++++++++++++++++++++++++++++++++++++++----- handler/commands.py | 23 ++++++++++++- 2 files changed, 94 insertions(+), 10 deletions(-) diff --git a/handler/agora.py b/handler/agora.py index 459f77e..83240c2 100644 --- a/handler/agora.py +++ b/handler/agora.py @@ -182,10 +182,25 @@ class Agora(object): return False for ad in ads["response"]["data"]["ad_list"]: ads_total.append(ad["data"]["ad_id"]) - if "next" in ads["response"]["pagination"]: - page += 1 - for ad in self.enum_ads(page): - ads_total.append(ad) + if "pagination" in ads["response"]: + if "next" in ads["response"]["pagination"]: + page += 1 + for ad in self.enum_ad_ids(page): + ads_total.append(ad) + return ads_total + + def enum_ads(self, page=0): + ads = self.agora._api_call(api_method="ads", query_values={"page": page}) + ads_total = [] + if not ads["success"]: + return False + for ad in ads["response"]["data"]["ad_list"]: + ads_total.append([ad["data"]["ad_id"], ad["data"]["countrycode"], ad["data"]["currency"]]) + if "pagination" in ads["response"]: + if "next" in ads["response"]["pagination"]: + page += 1 + for ad in self.enum_ads(page): + ads_total.append([ad[0], ad[1], ad[2]]) return ads_total def nuke_ads(self): @@ -194,11 +209,14 @@ class Agora(object): :return: True or False :rtype: bool """ - ads = self.enum_ads() + ads = self.enum_ad_ids() + return_ids = [] if not ads: return False for ad_id in ads: - self.agora.ad_delete(ad_id) + rtrn = self.agora.ad_delete(ad_id) + return_ids.append(rtrn["success"]) + return all(return_ids) def get_rates_all(self): """ @@ -285,6 +303,17 @@ class Agora(object): return False yield rtrn + def get_combinations(self): + """ + Get all combinations of currencies and countries from the configuration. + :return: list of [country, currency] + :rtype: list + """ + currencies = loads(settings.Agora.BruteCurrencies) + countries = loads(settings.Agora.BruteCountries) + combinations = [[country, currency] for country in countries for currency in currencies] + return combinations + def dist_bruteforce(self): """ Bruteforce all possible ads from the currencies and countries in the config. @@ -292,15 +321,49 @@ class Agora(object): :return: False or dict with response :rtype: bool or dict """ - currencies = loads(settings.Agora.BruteCurrencies) - countries = loads(settings.Agora.BruteCountries) - combinations = [[country, currency] for country in countries for currency in currencies] + combinations = self.get_combinations() for country, currency in combinations: rtrn = self.create_ad(country, currency) if not rtrn: yield False yield rtrn + def bruteforce_fill_blanks(self): + """ + Get the ads that we want to configure but have not, and fill in the blanks. + :return: False or dict with response + :rtype: bool or dict + """ + existing_ads = self.enum_ads() + combinations = self.get_combinations() + for country, currency in combinations: + if not [country, currency] in existing_ads: + rtrn = self.create_ad(country, currency) + if not rtrn: + yield False + yield rtrn + + def strip_duplicate_ads(self): + """ + Remove duplicate ads. + :return: list of duplicate ads + :rtype: list + """ + existing_ads = self.enum_ads() + _size = len(existing_ads) + repeated = [] + for i in range(_size): + k = i + 1 + for j in range(k, _size): + if existing_ads[i] == existing_ads[j] and existing_ads[i] not in repeated: + repeated.append(existing_ads[i]) + + actioned = [] + for ad_id, country, currency in repeated: + rtrn = self.agora.ad_delete(ad_id) + actioned.append(rtrn["success"]) + return all(actioned) + def release_funds(self, contact_id): """ Release funds for a contact_id. diff --git a/handler/commands.py b/handler/commands.py index f81d789..64c4fa0 100644 --- a/handler/commands.py +++ b/handler/commands.py @@ -93,7 +93,28 @@ class IRCCommands(object): if x["success"]: msg(f"{x['response']['data']['message']}: {x['response']['data']['ad_id']}") else: - msg(x["response"]["data"]["message"]) + msg(dumps(x)) + + class fillblanks(object): + name = "fillblanks" + authed = True + + @staticmethod + def run(cmd, spl, length, authed, msg, agora, revolut, tx): + for x in agora.bruteforce_fill_blanks(): + if x["success"]: + msg(f"{x['response']['data']['message']}: {x['response']['data']['ad_id']}") + else: + msg(dumps(x)) + + class stripdupes(object): + name = "stripdupes" + authed = True + + @staticmethod + def run(cmd, spl, length, authed, msg, agora, revolut, tx): + rtrn = agora.strip_duplicate_ads() + msg(dumps(rtrn)) class find(object): name = "find"