Browse Source

Moving wrappers, combining sell and buy

JoostSijm 3 years ago
parent
commit
bb711358da

+ 4 - 0
src/rival_regions_marketbot/__init__.py

@@ -6,6 +6,8 @@ import logging
 from dotenv import load_dotenv
 from rival_regions_wrapper.middleware import LocalAuthentication
 
+from . import wrapper
+
 
 load_dotenv()
 
@@ -57,3 +59,5 @@ RR_WRAPPER = LocalAuthentication(False, CAPTCHA_KEY)
 RR_WRAPPER.set_credentials(
     RIVAL_REGIONS_USERNAME, RIVAL_REGIONS_PASSWORD, RIVAL_REGIONS_LOGIN_METHOD
 )
+
+PROFILE_ID = wrapper.Profile(RR_WRAPPER).info()["profile_id"]

+ 17 - 16
src/rival_regions_marketbot/__main__.py

@@ -3,7 +3,7 @@
 import time
 from datetime import datetime
 
-from rival_regions_marketbot import RR_WRAPPER, sell_bot#, buy_bot
+from rival_regions_marketbot import marketbot
 
 
 while True:
@@ -30,12 +30,13 @@ while True:
     ]
 
     sell_price = [
-        164,
-        150,
-        1548,
-        1500000,
-        4000,
-        800000,
+        160,
+        155,
+        1580,
+        1100000,
+        500000000,
+        500000000,
+        500000000,
         500000000,
         500000000,
         500000000,
@@ -47,15 +48,12 @@ while True:
         500000000,
         500000000,
         500000000,
-        19888000,
     ]
 
     buy_price = [
-        100,
-        100,
-        1000,
-        100000,
-        2000,
+        145,
+        140,
+        1450,
         100000,
         1,
         1,
@@ -69,12 +67,15 @@ while True:
         1,
         1,
         1,
+        1,
+        1,
     ]
 
     for index, item in enumerate(items):
-        print("Procces item: {}".format(item))
-        # buy_bot.BuyMarket(RR_WRAPPER).info(items[index], buy_price[index])
-        sell_bot.MarketBot(RR_WRAPPER).info(items[index], sell_price[index])
+        print("Buy:  {}".format(item))
+        marketbot.buy(items[index], buy_price[index])
+        print("Sell: {}".format(item))
+        marketbot.sell(items[index], sell_price[index])
 
     SECONDS = 301
     print(

+ 0 - 87
src/rival_regions_marketbot/buy_bot.py

@@ -1,87 +0,0 @@
-"""Buy bot"""
-
-import re
-
-from bs4 import BeautifulSoup
-from rival_regions_wrapper.wrapper.abstract_wrapper import AbstractWrapper
-from rival_regions_wrapper import util
-
-from . import RR_WRAPPER
-
-
-class CurrentPrice(AbstractWrapper):
-    def info(self, resource):
-        """Get profile"""
-        if isinstance(resource, str) and resource in util.ITEM_KEYS:
-            resource = util.ITEM_KEYS[resource]
-        path = "storage/listed/{}".format(resource)
-        response = self.middleware.get(path)
-        soup = BeautifulSoup(response, "html.parser")
-        offers_tree = soup.find_all(class_="list_link")
-        test = 0
-        prijs = 0
-        for offer_tree in offers_tree:
-            if test == 0:
-                test = 1
-                prijs = int(
-                    float(offer_tree.select(".list_level")[1]["rat"]) * 100
-                )
-        return prijs
-
-
-class Amount(AbstractWrapper):
-    def info(self, resource):
-        """Get profile"""
-        if isinstance(resource, str) and resource in util.ITEM_KEYS:
-            resource = util.ITEM_KEYS[resource]
-        path = "storage/listed/{}".format(resource)
-        response = self.middleware.get(path)
-        soup = BeautifulSoup(response, "html.parser")
-        offers_tree = soup.find_all(class_="list_link")
-        test = 0
-        for offer_tree in offers_tree:
-            if test == 0:
-                test = 1
-                amount = int(
-                    offer_tree.select_one(".list_level.imp.small")["rat"]
-                )
-        return amount
-
-
-class Profile(AbstractWrapper):
-    def info(self, resource):
-        """Get profile"""
-        if isinstance(resource, str) and resource in util.ITEM_KEYS:
-            resource = util.ITEM_KEYS[resource]
-        path = "storage/listed/{}".format(resource)
-        response = self.middleware.get(path)
-        soup = BeautifulSoup(response, "html.parser")
-        offers_tree = soup.find_all(class_="list_link")
-        test = 0
-        profiel = 0
-        for offer_tree in offers_tree:
-            if test == 0:
-                test = 1
-                profiel = int(
-                    re.sub(
-                        r"^.*\/",
-                        "",
-                        offer_tree.select_one(".results_date")["action"],
-                    )
-                )
-        return profiel
-
-
-class BuyMarket(AbstractWrapper):
-    def info(self, resource, price):
-        current_price = CurrentPrice(RR_WRAPPER).info(resource) / 100
-        if current_price < price:
-            profiel = Profile(RR_WRAPPER).info(resource)
-            amount = Amount(RR_WRAPPER).info(resource)
-            item = util.ITEM_KEYS[resource]
-            self.middleware.post(
-                "storage/buy/{}/{}/{}/{}".format(
-                    item, profiel, amount, current_price
-                )
-            )
-        return True

+ 72 - 0
src/rival_regions_marketbot/marketbot.py

@@ -0,0 +1,72 @@
+"""Sell bot"""
+
+
+from rival_regions_wrapper import util
+from rival_regions_wrapper.wrapper import Storage
+
+from rival_regions_marketbot import RR_WRAPPER, PROFILE_ID, wrapper
+
+
+def sell(item, minimum_price):
+    if minimum_price >= 500000000:
+        return
+    market = wrapper.Market(RR_WRAPPER)
+    first_offer = market.first_offer(item)
+    if first_offer["profile_id"] != PROFILE_ID:
+        # op_markt = market.offer_by(item, PROFILE_ID)
+        totaal = Storage(RR_WRAPPER).info()[item]
+        # totaal = opslag + op_markt
+        price = max(minimum_price, (first_offer["price"] * 100 - 10) / 100)
+        if item == "helium-3":
+            totaal = min(totaal, 153600)
+        if item == "liquid_oxygen":
+            totaal = min(totaal, 38400000)
+        if item == "oil":
+            totaal = min(totaal, 614400000)
+        if item == "ore":
+            totaal = min(totaal, 614400000)
+        if item == "uranium":
+            totaal = min(totaal, 15360000)
+        if item == "diamonds":
+            totaal = min(totaal, 153600)
+        if item == "rivalium":
+            totaal = min(totaal, 614400)
+        if item == "antirad":
+            totaal = min(totaal, 76800)
+        if item == "spacerockets":
+            totaal = min(totaal, 3840)
+        if item == "lss":
+            totaal = min(totaal, 15360000)
+        if item == "tanks":
+            totaal = min(totaal, 4388571)
+        if item == "aircrafts":
+            totaal = min(totaal, 640000)
+        if item == "missiles":
+            totaal = min(totaal, 256000)
+        if item == "bombers":
+            totaal = min(totaal, 128000)
+        if item == "battleships":
+            totaal = min(totaal, 128000)
+        if item == "laser_drones":
+            totaal = min(totaal, 256000)
+        if item == "moon_tanks":
+            totaal = min(totaal, 12800)
+        if item == "space_stations":
+            totaal = min(totaal, 1280)
+        # if totaal == op_markt and own_price == minimum_price:
+        if totaal != 0:
+            item = util.ITEM_KEYS[item]
+            RR_WRAPPER.post("storage/sell/{}".format(item))
+            RR_WRAPPER.post(
+                "storage/newsell/{}/{}/{}".format(item, totaal, price)
+            )
+
+
+def buy(item, maximum_price):
+    """Buy item based on maximum price"""
+    if maximum_price <= 1:
+        return
+    market = wrapper.Market(RR_WRAPPER)
+    offer = market.first_offer(item)
+    if offer["price"] <= maximum_price:
+        market.buy(offer)

+ 0 - 182
src/rival_regions_marketbot/sell_bot.py

@@ -1,182 +0,0 @@
-"""Sell bot"""
-
-import re
-
-from bs4 import BeautifulSoup
-from rival_regions_wrapper import util
-from rival_regions_wrapper.wrapper.abstract_wrapper import AbstractWrapper
-
-from . import RR_WRAPPER
-
-
-class Market2(AbstractWrapper):
-    def info(self, resource):
-        """Get profile"""
-        if isinstance(resource, str) and resource in util.ITEM_KEYS:
-            resource = util.ITEM_KEYS[resource]
-        path = "storage/listed/{}".format(resource)
-        response = self.middleware.get(path)
-        soup = BeautifulSoup(response, "html.parser")
-        offers_tree = soup.find_all(class_="list_link")
-        test = 0
-        i = 0
-        for offer_tree in offers_tree:
-            if (
-                int(
-                    re.sub(
-                        r"^.*\/",
-                        "",
-                        offer_tree.select_one(".results_date")["action"],
-                    )
-                )
-                == 2000883512
-            ):
-                test = int(
-                    offer_tree.select_one(".list_level.imp.small")["rat"]
-                )
-            i = i + 1
-        return test
-
-
-class Storage(AbstractWrapper):
-    """Wrapper class for storage"""
-
-    def info(self, resource):
-        """storage info"""
-        path = "storage"
-        resource = util.ITEM_KEYS[resource]
-        response = self.middleware.get(path)
-        soup = BeautifulSoup(response, "html.parser")
-        storage = int(
-            soup.find("span", {"urlbar": resource}).text.replace(".", "")
-        )
-        return storage
-
-
-class Market(AbstractWrapper):
-    def info(self, resource):
-        """Get profile"""
-        if isinstance(resource, str) and resource in util.ITEM_KEYS:
-            resource = util.ITEM_KEYS[resource]
-        path = "storage/listed/{}".format(resource)
-        response = self.middleware.get(path)
-        soup = BeautifulSoup(response, "html.parser")
-        offers_tree = soup.find_all(class_="list_link")
-        test = 0
-        prijs = 0
-        for offer_tree in offers_tree:
-            if test == 0:
-                test = 1
-                prijs = int(
-                    float(offer_tree.select(".list_level")[1]["rat"]) * 100
-                )
-                if (
-                    int(
-                        re.sub(
-                            r"^.*\/",
-                            "",
-                            offer_tree.select_one(".results_date")["action"],
-                        )
-                    )
-                    == 2000883512
-                ):
-                    prijs = 0
-        return prijs
-
-
-class MarketBot(AbstractWrapper):
-    def info(self, resource, minprice):
-        test = 0
-        current_price = Market(RR_WRAPPER).info(resource)
-        if current_price != 0:
-            op_markt = Market2(RR_WRAPPER).info(resource)
-            opslag = Storage(RR_WRAPPER).info(resource)
-            totaal = opslag + op_markt
-            mijn_prijs = (current_price - 10) / 100
-            check = 0
-
-            if mijn_prijs < minprice:
-                mijn_prijs = minprice
-            if resource == "helium-3":
-                if totaal >= 153600:
-                    totaal = 153600
-                    check = 1
-            if resource == "liquid_oxygen":
-                if totaal >= 38400000:
-                    totaal = 38400000
-                    check = 1
-            if resource == "oil":
-                if totaal >= 614400000:
-                    totaal = 614400000
-                    check = 1
-            if resource == "ore":
-                if totaal >= 614400000:
-                    totaal = 614400000
-                    check = 1
-            if resource == "uranium":
-                if totaal >= 15360000:
-                    totaal = 15360000
-                    check = 1
-            if resource == "diamonds":
-                if totaal >= 153600:
-                    totaal = 153600
-                    check = 1
-            if resource == "rivalium":
-                if totaal >= 614400:
-                    totaal = 614400
-                    check = 1
-            if resource == "antirad":
-                if totaal >= 76800:
-                    totaal = 76800
-                    check = 1
-            if resource == "spacerockets":
-                if totaal >= 3840:
-                    totaal = 3840
-                    check = 1
-            if resource == "lss":
-                if totaal >= 15360000:
-                    totaal = 15360000
-                    check = 1
-            if resource == "tanks":
-                if totaal >= 4388571:
-                    totaal = 4388571
-                    check = 1
-            if resource == "aircrafts":
-                if totaal >= 640000:
-                    totaal = 640000
-                    check = 1
-            if resource == "missiles":
-                if totaal >= 256000:
-                    totaal = 256000
-                    check = 1
-            if resource == "bombers":
-                if totaal >= 128000:
-                    totaal = 128000
-                    check = 1
-            if resource == "battleships":
-                if totaal >= 128000:
-                    totaal = 128000
-                    check = 1
-            if resource == "laser_drones":
-                if totaal >= 256000:
-                    totaal = 256000
-                    check = 1
-            if resource == "moon_tanks":
-                if totaal >= 12800:
-                    totaal = 12800
-                    check = 1
-            if resource == "space_stations":
-                if totaal >= 1280:
-                    totaal = 1280
-                    check = 1
-            if ((totaal == op_markt) and (mijn_prijs == minprice)) or (
-                minprice == 500000000
-            ):
-                return True
-            if totaal != 0:
-                item = util.ITEM_KEYS[resource]
-                self.middleware.post("storage/sell/{}".format(item))
-                self.middleware.post(
-                    "storage/newsell/{}/{}/{}".format(item, totaal, mijn_prijs)
-                )
-        return True

+ 126 - 0
src/rival_regions_marketbot/wrapper.py

@@ -0,0 +1,126 @@
+"""Wrapper classes"""
+
+import re
+
+from bs4 import BeautifulSoup
+from rival_regions_wrapper import util
+from rival_regions_wrapper import wrapper
+
+
+class Profile(wrapper.Profile):
+    """Wrapper class for profile"""
+
+    def __init__(self, middleware, profile_id=None):
+        wrapper.Profile.__init__(self, middleware, profile_id=profile_id)
+
+    def info(self):
+        """Get profile"""
+        if self.profile_id:
+            path = "slide/profile/{}".format(self.profile_id)
+        else:
+            path = "slide/profile"
+        response = self.middleware.get(path)
+        soup = BeautifulSoup(response, "html.parser")
+        level = soup.select_one("div.oil:nth-child(2) > div:nth-child(2)").text
+        perks = soup.select("table tr:nth-child(2) span")
+        profile = {
+            "profile_id": int(
+                soup.select_one(".history_link")["action"].replace(
+                    "log/user/", ""
+                ),
+            ),
+            "name": re.sub(r".*:\s", "", soup.find("h1").text),
+            "level": int(re.sub(r"^Level\:\s|\s\(.*\)$", "", level)),
+            "level_percentage": int(
+                re.sub(r"^Level\:\s(\d+)\s\(|\s\%\)$", "", level)
+            ),
+            "strenght": int(perks[0].text),
+            "education": int(perks[1].text),
+            "endurance": int(perks[2].text),
+        }
+        return profile
+
+
+class Market(wrapper.Market):
+    """New marketwrapper bases on Market"""
+
+    def offer_by(self, item, profile_id):
+        """Get current offer by profile id"""
+        if isinstance(item, str) and item in util.ITEM_KEYS:
+            item = util.ITEM_KEYS[item]
+        path = "storage/listed/{}".format(item)
+        response = self.middleware.get(path)
+        soup = BeautifulSoup(response, "html.parser")
+        offers_tree = soup.find_all(class_="list_link")
+        for offer_tree in offers_tree:
+            if (
+                int(
+                    re.sub(
+                        r"^.*\/",
+                        "",
+                        offer_tree.select_one(".results_date")["action"],
+                    )
+                )
+                == profile_id
+            ):
+                return int(
+                    offer_tree.select_one(".list_level.imp.small")["rat"]
+                )
+        return 0
+
+    # TODO: What is this doing? Currently not used anywhere
+    # Check if lowest offer is our own?
+    def info(self, item):
+        """Get profile"""
+        if isinstance(item, str) and item in util.ITEM_KEYS:
+            item = util.ITEM_KEYS[item]
+        path = "storage/listed/{}".format(item)
+        response = self.middleware.get(path)
+        soup = BeautifulSoup(response, "html.parser")
+        offers_tree = soup.find_all(class_="list_link")
+        test = 0
+        price = 0
+        for offer_tree in offers_tree:
+            if test == 0:
+                test = 1
+                price = int(
+                    float(offer_tree.select(".list_level")[1]["rat"]) * 100
+                )
+                if (
+                    int(
+                        re.sub(
+                            r"^.*\/",
+                            "",
+                            offer_tree.select_one(".results_date")["action"],
+                        )
+                    )
+                    == 2000883512
+                ):
+                    price = 0
+        return price
+
+    def first_offer(self, item_id):
+        if isinstance(item_id, str) and item_id in util.ITEM_KEYS:
+            item_id = util.ITEM_KEYS[item_id]
+        path = "storage/listed/{}".format(item_id)
+        response = self.middleware.get(path)
+        soup = BeautifulSoup(response, "html.parser")
+        return {
+            "item_id": item_id,
+            "profile_id": int(soup.select_one(".list_link")["user"]),
+            "amount": int(soup.select_one(".list_level")["rat"]),
+            "price": int(float(soup.select(".list_level")[1]["rat"]) * 100)
+            / 100,
+        }
+
+    def buy(self, offer, amount=None):
+        """Buy item"""
+        if isinstance(item, str) and item in util.ITEM_KEYS:
+            item = util.ITEM_KEYS[item]
+        if not amount:
+            amount = offer["ammount"]
+        self.middleware.post(
+            "storage/buy/{}/{}/{}/{}".format(
+                offer["item_id"], offer["profile_id"], amount, offer["price"]
+            )
+        )