From ddee10958fd429fcade3b40ab7991f5a8a1a3439 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Mon, 18 Apr 2022 17:22:33 +0100 Subject: [PATCH] Make sources platform conscious --- handler/sources/agora.py | 15 +++---- handler/sources/localbitcoins.py | 71 ++++++++++++++++++++------------ 2 files changed, 52 insertions(+), 34 deletions(-) diff --git a/handler/sources/agora.py b/handler/sources/agora.py index af6e062..3346270 100644 --- a/handler/sources/agora.py +++ b/handler/sources/agora.py @@ -24,6 +24,7 @@ class Agora(util.Base): Initialise the AgoraDesk API. Initialise the last_dash storage for detecting new trades. """ + self.platform = "agora" super().__init__() self.agora = AgoraDesk(settings.Agora.Token) @@ -359,12 +360,12 @@ class Agora(util.Base): } if not assets: - assets = self.markets.get_all_assets("agora") + assets = self.markets.get_all_assets(self.platform) # Get all currencies we have ads for, deduplicated if not currencies: - currencies = self.markets.get_all_currencies("agora") + currencies = self.markets.get_all_currencies(self.platform) if not providers: - providers = self.markets.get_all_providers("agora") + providers = self.markets.get_all_providers(self.platform) # We want to get the ads for each of these currencies and return the result rates = self.money.cg.get_price(ids=["monero", "bitcoin"], vs_currencies=currencies) for asset in assets: @@ -378,7 +379,7 @@ class Agora(util.Base): ads_list = self.enum_public_ads(asset, currency, providers) if not ads_list: continue - ads = self.money.lookup_rates("agora", ads_list, rates=rates) + ads = self.money.lookup_rates(self.platform, ads_list, rates=rates) if not ads: continue self.write_to_es_ads("ads", ads) @@ -491,7 +492,7 @@ class Agora(util.Base): if payment_details: payment_details_text = self.markets.format_payment_details(currency, payment_details) ad_text = self.markets.format_ad(asset, currency, payment_details_text) - min_amount, max_amount = self.money.get_minmax(asset, currency) + min_amount, max_amount = self.money.get_minmax(self.platform, asset, currency) price_formula = f"coingecko{asset.lower()}usd*usd{currency.lower()}*{settings.Agora.Margin}" @@ -535,7 +536,7 @@ class Agora(util.Base): ( supported_currencies, account_info, - ) = self.markets.get_valid_account_details() + ) = self.markets.get_valid_account_details(self.platform) # 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: @@ -567,7 +568,7 @@ class Agora(util.Base): ( supported_currencies, account_info, - ) = self.markets.get_valid_account_details() + ) = self.markets.get_valid_account_details(self.platform) for asset, ad_id, countrycode, currency, provider in our_ads: if currency in supported_currencies: rtrn = self.create_ad( diff --git a/handler/sources/localbitcoins.py b/handler/sources/localbitcoins.py index 6431906..5bb29a4 100644 --- a/handler/sources/localbitcoins.py +++ b/handler/sources/localbitcoins.py @@ -24,6 +24,7 @@ class LBTC(util.Base): Initialise the LocalBitcoins API. Initialise the last_dash storage for detecting new trades. """ + self.platform = "lbtc" super().__init__() self.lbtc = LocalBitcoins(settings.LocalBitcoins.Token, settings.LocalBitcoins.Secret) @@ -220,18 +221,18 @@ class LBTC(util.Base): return ads_total @util.handle_exceptions - def enum_ads(self, requested_asset=None, page=0): + def enum_ads(self, requested_asset=None, page=1): query_values = {"page": page} if requested_asset: query_values["asset"] = requested_asset - ads = self.lbtc._api_call(api_method="ads", query_values=query_values) + ads = self.lbtc._api_call(api_method="api/ads/", query_values=query_values) if ads is False: return False ads_total = [] if not ads["success"]: return False for ad in ads["response"]["data"]["ad_list"]: - asset = ad["data"]["asset"] + asset = "BTC" ad_id = ad["data"]["ad_id"] country = ad["data"]["countrycode"] currency = ad["data"]["currency"] @@ -249,17 +250,32 @@ class LBTC(util.Base): ads_total.append([ad[0], ad[1], ad[2], ad[3], ad[4]]) return ads_total + def map_provider(self, provider): + provider_map = {"NATIONAL_BANK": "national-bank-transfer"} + try: + return provider_map[provider] + except KeyError: + return False + @util.handle_exceptions - def enum_public_ads(self, asset, currency, providers=None, page=0): + def enum_public_ads(self, asset, currency, providers=None, page=1): to_return = [] if not providers: - providers = ["REVOLUT"] + providers = ["NATIONAL_BANK"] + if len(providers) == 1: + provider = providers[0] + ads = self.lbtc._api_call( + api_method=f"buy-bitcoins-online/{currency}/{provider}/.json", + query_values={"page": page}, + ) + else: + ads = self.lbtc._api_call( + api_method=f"buy-bitcoins-online/{currency}/.json", + query_values={"page": page}, + ) # buy-monero-online, buy-bitcoin-online # Work around weirdness calling it bitcoins - ads = self.lbtc._api_call( - api_method=f"buy-bitcoins-online/{currency}", - query_values={"page": page}, - ) + # with open("pub.json", "a") as f: # import json # f.write(json.dumps([page, currency, asset, ads])+"\n") @@ -273,13 +289,13 @@ class LBTC(util.Base): if "data" not in ads["response"]: return False for ad in ads["response"]["data"]["ad_list"]: - if ad["data"]["online_provider"] not in providers: + if self.map_provider(ad["data"]["online_provider"]) not in providers: continue date_last_seen = ad["data"]["profile"]["last_online"] # Check if this person was seen recently if not util.last_online_recent(date_last_seen): continue - ad_id = ad["data"]["ad_id"] + ad_id = str(ad["data"]["ad_id"]) username = ad["data"]["profile"]["username"] temp_price = ad["data"]["temp_price"] provider = ad["data"]["online_provider"] @@ -330,7 +346,7 @@ class LBTC(util.Base): return False # Get the ads to update - to_update = self.markets.get_new_ad_equations(public_ads, assets) + to_update = self.markets.get_new_ad_equations(self.platform, public_ads, assets) self.slow_ad_update(to_update) # TODO: make generic and move to markets @@ -347,12 +363,15 @@ class LBTC(util.Base): } if not assets: - assets = self.markets.get_all_assets() + assets = self.markets.get_all_assets(self.platform) # Get all currencies we have ads for, deduplicated if not currencies: - currencies = self.markets.get_all_currencies() + currencies = self.markets.get_all_currencies(self.platform) if not providers: - providers = self.markets.get_all_providers() + providers = self.markets.get_all_providers(self.platform) + sinks_currencies = self.sinks.currencies + supported_currencies = [currency for currency in currencies if currency in sinks_currencies] + currencies = supported_currencies # We want to get the ads for each of these currencies and return the result rates = self.money.cg.get_price(ids=["bitcoin"], vs_currencies=currencies) for asset in assets: @@ -366,7 +385,7 @@ class LBTC(util.Base): ads_list = self.enum_public_ads(asset, currency, providers) if not ads_list: continue - ads = self.money.lookup_rates(ads_list, rates=rates) + ads = self.money.lookup_rates(self.platform, ads_list, rates=rates) if not ads: continue self.write_to_es_ads("ads", ads) @@ -376,7 +395,6 @@ class LBTC(util.Base): public_ads[currency].append(ad) else: public_ads[currency] = ads - return public_ads def write_to_es_ads(self, msgtype, ads): @@ -482,20 +500,19 @@ class LBTC(util.Base): if payment_details: payment_details_text = self.markets.format_payment_details(currency, payment_details) ad_text = self.markets.format_ad(asset, currency, payment_details_text) - min_amount, max_amount = self.money.get_minmax(asset, currency) - - price_formula = f"coingecko{asset.lower()}usd*usd{currency.lower()}*{settings.LocalBitcoins.Margin}" + min_amount, max_amount = self.money.get_minmax(self.platform, asset, currency) + price_formula = f"btc_in_usd*{settings.LocalBitcoins.Margin}" form = { "country_code": countrycode, "currency": currency, "trade_type": "ONLINE_SELL", - "asset": asset, "price_equation": price_formula, "track_max_amount": False, "require_trusted_by_advertiser": False, "online_provider": provider, - "payment_method_details": settings.Platform.PaymentMethodDetails, + "bank_name": payment_details["bank"], + "display_reference": False, } if visible is False: form["visible"] = False @@ -504,8 +521,8 @@ class LBTC(util.Base): if payment_details: form["account_info"] = payment_details_text form["msg"] = ad_text - form["min_amount"] = min_amount - form["max_amount"] = max_amount + form["min_amount"] = round(min_amount, 2) + form["max_amount"] = round(max_amount, 2) if edit: ad = self.lbtc.ad(ad_id=ad_id, **form) @@ -520,12 +537,12 @@ class LBTC(util.Base): :return: False or dict with response :rtype: bool or dict """ - dist_list = list(self.markets.create_distribution_list(filter_asset)) + dist_list = list(self.markets.create_distribution_list(self.platform, filter_asset)) our_ads = self.enum_ads() ( supported_currencies, account_info, - ) = self.markets.get_valid_account_details() + ) = self.markets.get_valid_account_details(self.platform) # 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: @@ -557,7 +574,7 @@ class LBTC(util.Base): ( supported_currencies, account_info, - ) = self.markets.get_valid_account_details() + ) = self.markets.get_valid_account_details(self.platform) for asset, ad_id, countrycode, currency, provider in our_ads: if currency in supported_currencies: rtrn = self.create_ad(