88 lines
4.1 KiB
Python
88 lines
4.1 KiB
Python
# Twisted/Klein imports
|
|
from twisted.logger import Logger
|
|
|
|
# Project imports
|
|
from settings import settings
|
|
|
|
|
|
class Markets(object):
|
|
""" "
|
|
Markets handler for generic market functions.
|
|
"""
|
|
|
|
def __init__(self):
|
|
self.log = Logger("markets")
|
|
|
|
def autoprice(self, ads, currency):
|
|
"""
|
|
Helper function to automatically adjust the price up/down in certain markets
|
|
in order to gain the most profits and sales.
|
|
:param ads: list of ads
|
|
:type ads: list of lists
|
|
:param currency: currency of the ads
|
|
:type currency: string
|
|
:return: the rate we should use for this currency
|
|
:rtype: float
|
|
"""
|
|
self.log.debug("Autoprice starting for {x}", x=currency)
|
|
# Find cheapest ad
|
|
# Filter by 3rd index on each ad list to find the cheapest
|
|
min_margin_ad = min(ads, key=lambda x: x[4])
|
|
self.log.debug("Minimum margin ad: {x}", x=min_margin_ad)
|
|
|
|
# Find second cheapest that is not us
|
|
# Remove results from ads that are us
|
|
ads_without_us = [ad for ad in ads if not ad[1] == settings.Agora.Username]
|
|
self.log.debug("Ads without us: {x}", x=ads_without_us)
|
|
# Find ads above our min that are not us
|
|
ads_above_our_min_not_us = [ad for ad in ads_without_us if ad[4] > float(settings.Agora.MinMargin)]
|
|
self.log.debug("Ads above our min not us: {x}", x=ads_above_our_min_not_us)
|
|
# Check that this list without us is not empty
|
|
if ads_without_us:
|
|
# Find the cheapest from these
|
|
min_margin_ad_not_us = min(ads_without_us, key=lambda x: x[4])
|
|
self.log.debug("Min margin ad not us: {x}", x=min_margin_ad_not_us)
|
|
# Lowball the lowest ad that is not ours
|
|
lowball_lowest_not_ours = min_margin_ad_not_us[4] # - 0.005
|
|
self.log.debug("Lowball lowest not ours: {x}", x=lowball_lowest_not_ours)
|
|
|
|
# Check if the username field of the cheapest ad matches ours
|
|
if min_margin_ad[1] == settings.Agora.Username:
|
|
self.log.debug("We are the cheapest for: {x}", x=currency)
|
|
# We are the cheapest!
|
|
# Are all of the ads ours?
|
|
all_ads_ours = all([ad[1] == settings.Agora.Username for ad in ads])
|
|
if all_ads_ours:
|
|
self.log.debug("All ads are ours for: {x}", x=currency)
|
|
# Now we know it's safe to return the maximum value
|
|
return float(settings.Agora.MaxMargin)
|
|
else:
|
|
self.log.debug("All ads are NOT ours for: {x}", x=currency)
|
|
# All the ads are not ours, but we are first...
|
|
# Check if the lowballed, lowest (that is not ours) ad's margin
|
|
# is less than our minimum
|
|
if lowball_lowest_not_ours < float(settings.Agora.MinMargin):
|
|
self.log.debug("Lowball lowest not ours less than MinMargin")
|
|
return float(settings.Agora.MinMargin)
|
|
elif lowball_lowest_not_ours > float(settings.Agora.MaxMargin):
|
|
self.log.debug("Lowball lowest not ours more than MaxMargin")
|
|
return float(settings.Agora.MaxMargin)
|
|
else:
|
|
self.log.debug("Returning lowballed figure: {x}", x=lowball_lowest_not_ours)
|
|
return lowball_lowest_not_ours
|
|
else:
|
|
self.log.debug("We are NOT the cheapest for: {x}", x=currency)
|
|
# We are not the cheapest :(
|
|
# Check if this list is empty
|
|
if not ads_above_our_min_not_us:
|
|
# Return the maximum margin?
|
|
return float(settings.Agora.MaxMargin)
|
|
# Find cheapest ad above our min that is not us
|
|
cheapest_ad = min(ads_above_our_min_not_us, key=lambda x: x[4])
|
|
cheapest_ad_margin = cheapest_ad[4] # - 0.005
|
|
if cheapest_ad_margin > float(settings.Agora.MaxMargin):
|
|
self.log.debug("Cheapest ad not ours more than MaxMargin")
|
|
return float(settings.Agora.MaxMargin)
|
|
self.log.debug("Cheapest ad above our min that is not us: {x}", x=cheapest_ad)
|
|
return cheapest_ad_margin
|