Make sources platform conscious

This commit is contained in:
Mark Veidemanis 2022-04-18 17:22:33 +01:00
parent d2d74a2086
commit ddee10958f
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
2 changed files with 52 additions and 34 deletions

View File

@ -24,6 +24,7 @@ class Agora(util.Base):
Initialise the AgoraDesk API. Initialise the AgoraDesk API.
Initialise the last_dash storage for detecting new trades. Initialise the last_dash storage for detecting new trades.
""" """
self.platform = "agora"
super().__init__() super().__init__()
self.agora = AgoraDesk(settings.Agora.Token) self.agora = AgoraDesk(settings.Agora.Token)
@ -359,12 +360,12 @@ class Agora(util.Base):
} }
if not assets: 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 # Get all currencies we have ads for, deduplicated
if not currencies: if not currencies:
currencies = self.markets.get_all_currencies("agora") currencies = self.markets.get_all_currencies(self.platform)
if not providers: 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 # 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) rates = self.money.cg.get_price(ids=["monero", "bitcoin"], vs_currencies=currencies)
for asset in assets: for asset in assets:
@ -378,7 +379,7 @@ class Agora(util.Base):
ads_list = self.enum_public_ads(asset, currency, providers) ads_list = self.enum_public_ads(asset, currency, providers)
if not ads_list: if not ads_list:
continue 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: if not ads:
continue continue
self.write_to_es_ads("ads", ads) self.write_to_es_ads("ads", ads)
@ -491,7 +492,7 @@ class Agora(util.Base):
if payment_details: if payment_details:
payment_details_text = self.markets.format_payment_details(currency, payment_details) payment_details_text = self.markets.format_payment_details(currency, payment_details)
ad_text = self.markets.format_ad(asset, currency, payment_details_text) 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}" price_formula = f"coingecko{asset.lower()}usd*usd{currency.lower()}*{settings.Agora.Margin}"
@ -535,7 +536,7 @@ class Agora(util.Base):
( (
supported_currencies, supported_currencies,
account_info, 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 # 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] our_ads = [(x[0], x[2], x[3], x[4]) for x in our_ads]
for asset, countrycode, currency, provider in dist_list: for asset, countrycode, currency, provider in dist_list:
@ -567,7 +568,7 @@ class Agora(util.Base):
( (
supported_currencies, supported_currencies,
account_info, 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: for asset, ad_id, countrycode, currency, provider in our_ads:
if currency in supported_currencies: if currency in supported_currencies:
rtrn = self.create_ad( rtrn = self.create_ad(

View File

@ -24,6 +24,7 @@ class LBTC(util.Base):
Initialise the LocalBitcoins API. Initialise the LocalBitcoins API.
Initialise the last_dash storage for detecting new trades. Initialise the last_dash storage for detecting new trades.
""" """
self.platform = "lbtc"
super().__init__() super().__init__()
self.lbtc = LocalBitcoins(settings.LocalBitcoins.Token, settings.LocalBitcoins.Secret) self.lbtc = LocalBitcoins(settings.LocalBitcoins.Token, settings.LocalBitcoins.Secret)
@ -220,18 +221,18 @@ class LBTC(util.Base):
return ads_total return ads_total
@util.handle_exceptions @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} query_values = {"page": page}
if requested_asset: if requested_asset:
query_values["asset"] = 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: if ads is False:
return False return False
ads_total = [] ads_total = []
if not ads["success"]: if not ads["success"]:
return False return False
for ad in ads["response"]["data"]["ad_list"]: for ad in ads["response"]["data"]["ad_list"]:
asset = ad["data"]["asset"] asset = "BTC"
ad_id = ad["data"]["ad_id"] ad_id = ad["data"]["ad_id"]
country = ad["data"]["countrycode"] country = ad["data"]["countrycode"]
currency = ad["data"]["currency"] 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]]) ads_total.append([ad[0], ad[1], ad[2], ad[3], ad[4]])
return ads_total 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 @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 = [] to_return = []
if not providers: if not providers:
providers = ["REVOLUT"] providers = ["NATIONAL_BANK"]
# buy-monero-online, buy-bitcoin-online if len(providers) == 1:
# Work around weirdness calling it bitcoins provider = providers[0]
ads = self.lbtc._api_call( ads = self.lbtc._api_call(
api_method=f"buy-bitcoins-online/{currency}", api_method=f"buy-bitcoins-online/{currency}/{provider}/.json",
query_values={"page": page}, 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
# with open("pub.json", "a") as f: # with open("pub.json", "a") as f:
# import json # import json
# f.write(json.dumps([page, currency, asset, ads])+"\n") # f.write(json.dumps([page, currency, asset, ads])+"\n")
@ -273,13 +289,13 @@ class LBTC(util.Base):
if "data" not in ads["response"]: if "data" not in ads["response"]:
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"] not in providers: if self.map_provider(ad["data"]["online_provider"]) not in providers:
continue continue
date_last_seen = ad["data"]["profile"]["last_online"] date_last_seen = ad["data"]["profile"]["last_online"]
# Check if this person was seen recently # Check if this person was seen recently
if not util.last_online_recent(date_last_seen): if not util.last_online_recent(date_last_seen):
continue continue
ad_id = ad["data"]["ad_id"] ad_id = str(ad["data"]["ad_id"])
username = ad["data"]["profile"]["username"] username = ad["data"]["profile"]["username"]
temp_price = ad["data"]["temp_price"] temp_price = ad["data"]["temp_price"]
provider = ad["data"]["online_provider"] provider = ad["data"]["online_provider"]
@ -330,7 +346,7 @@ class LBTC(util.Base):
return False return False
# Get the ads to update # 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) self.slow_ad_update(to_update)
# TODO: make generic and move to markets # TODO: make generic and move to markets
@ -347,12 +363,15 @@ class LBTC(util.Base):
} }
if not assets: 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 # Get all currencies we have ads for, deduplicated
if not currencies: if not currencies:
currencies = self.markets.get_all_currencies() currencies = self.markets.get_all_currencies(self.platform)
if not providers: 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 # 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) rates = self.money.cg.get_price(ids=["bitcoin"], vs_currencies=currencies)
for asset in assets: for asset in assets:
@ -366,7 +385,7 @@ class LBTC(util.Base):
ads_list = self.enum_public_ads(asset, currency, providers) ads_list = self.enum_public_ads(asset, currency, providers)
if not ads_list: if not ads_list:
continue continue
ads = self.money.lookup_rates(ads_list, rates=rates) ads = self.money.lookup_rates(self.platform, ads_list, rates=rates)
if not ads: if not ads:
continue continue
self.write_to_es_ads("ads", ads) self.write_to_es_ads("ads", ads)
@ -376,7 +395,6 @@ class LBTC(util.Base):
public_ads[currency].append(ad) public_ads[currency].append(ad)
else: else:
public_ads[currency] = ads public_ads[currency] = ads
return public_ads return public_ads
def write_to_es_ads(self, msgtype, ads): def write_to_es_ads(self, msgtype, ads):
@ -482,20 +500,19 @@ class LBTC(util.Base):
if payment_details: if payment_details:
payment_details_text = self.markets.format_payment_details(currency, payment_details) payment_details_text = self.markets.format_payment_details(currency, payment_details)
ad_text = self.markets.format_ad(asset, currency, payment_details_text) 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.LocalBitcoins.Margin}"
price_formula = f"btc_in_usd*{settings.LocalBitcoins.Margin}"
form = { form = {
"country_code": countrycode, "country_code": countrycode,
"currency": currency, "currency": currency,
"trade_type": "ONLINE_SELL", "trade_type": "ONLINE_SELL",
"asset": asset,
"price_equation": price_formula, "price_equation": price_formula,
"track_max_amount": False, "track_max_amount": False,
"require_trusted_by_advertiser": False, "require_trusted_by_advertiser": False,
"online_provider": provider, "online_provider": provider,
"payment_method_details": settings.Platform.PaymentMethodDetails, "bank_name": payment_details["bank"],
"display_reference": False,
} }
if visible is False: if visible is False:
form["visible"] = False form["visible"] = False
@ -504,8 +521,8 @@ class LBTC(util.Base):
if payment_details: if payment_details:
form["account_info"] = payment_details_text form["account_info"] = payment_details_text
form["msg"] = ad_text form["msg"] = ad_text
form["min_amount"] = min_amount form["min_amount"] = round(min_amount, 2)
form["max_amount"] = max_amount form["max_amount"] = round(max_amount, 2)
if edit: if edit:
ad = self.lbtc.ad(ad_id=ad_id, **form) ad = self.lbtc.ad(ad_id=ad_id, **form)
@ -520,12 +537,12 @@ class LBTC(util.Base):
:return: False or dict with response :return: False or dict with response
:rtype: bool or dict :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() our_ads = self.enum_ads()
( (
supported_currencies, supported_currencies,
account_info, 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 # 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] our_ads = [(x[0], x[2], x[3], x[4]) for x in our_ads]
for asset, countrycode, currency, provider in dist_list: for asset, countrycode, currency, provider in dist_list:
@ -557,7 +574,7 @@ class LBTC(util.Base):
( (
supported_currencies, supported_currencies,
account_info, 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: for asset, ad_id, countrycode, currency, provider in our_ads:
if currency in supported_currencies: if currency in supported_currencies:
rtrn = self.create_ad( rtrn = self.create_ad(