import json
import urllib.parse
import time
import random
from bluegems import bluegems
from RequestManager import RequestManager
import HelperFunctions
import psycopg2
import queue
from FinalListing import FinalListing
import topfloatdb
from LoggingFormatter import logger
import SeleniumExecutor
import ast


class NewMarketBot(object):
    def __init__(
        self,
        high_low: str,
        weapon_name: str,
        webshare_ips: list,
        all_sessions: list,
        listings_queue: queue.Queue,
    ):
        self.high_low = high_low
        self.weapon_name = weapon_name
        self.listings_url_set = set()
        self.inspect_links_list = []
        self.listings_info_list = []
        self.listings_info_dict = {}
        self.best_or_worst_skins = []
        self.webshare_ips = webshare_ips
        self.RequestManager = RequestManager(all_sessions, webshare_ips)
        self.listings_queue = listings_queue
        self.inspect_server_url = "http://23.88.122.57:1337/"

    def startBot(self):
        self.getHashedNameListHighOrLow()
        logger.info("1/2 Build Listing URLs finished (" + str(self.weapon_name) + ")")

        self.buildInspectLinksList()
        logger.info(
            "2/2 Build Inspect Links List finished (" + str(self.weapon_name) + ")"
        )

    def getHashedNameListHighOrLow(self):
        end = False
        count = 0
        repeat_counter = 0

        if self.high_low == "high":
            wear = 4
        elif self.high_low == "low":
            wear = 0

        if self.weapon_name == "gloves":
            all_skins_url = (
                f"https://steamcommunity.com/market/search/render?currency=3&norender=1&query=&"
                "start={count}&count=10&search_descriptions=0&sort_column=price&sort_dir=asc&"
                "appid=730&category_730_ItemSet%5B%5D=any&category_730_ProPlayer%5B%5D=any&"
                "category_730_StickerCapsule%5B%5D=any&category_730_TournamentTeam%5B%5D=any&"
                "category_730_Weapon%5B%5D=any&category_730_Exterior%5B%5D=tag_WearCategory{wear}&"
                "category_730_Rarity%5B%5D=tag_Rarity_Ancient"
            ).format(count=count, wear=wear)
        else:
            all_skins_url = (
                f"https://steamcommunity.com/market/search/render?currency=3&norender=1&query=&"
                "start={count}&count=10&search_descriptions=0&sort_column=price&sort_dir=asc&appid=730&category_730_ItemSet"
                "%5B%5D=any&category_730_ProPlayer%5B%5D=any&category_730_StickerCapsule%5B%5D=any"
                "&category_730_TournamentTeam%5B%5D=any&category_730_Weapon%5B%5D={weapon_name}"
                "&category_730_Exterior%5B%5D=tag_WearCategory{wear}"
            ).format(count=count, weapon_name=self.weapon_name, wear=wear)

        while not end:
            if self.weapon_name == "gloves":
                all_skins_url = (
                    f"https://steamcommunity.com/market/search/render?currency=3&norender=1&query=&"
                    "start={count}&count=10&search_descriptions=0&sort_column=price&sort_dir=asc&"
                    "appid=730&category_730_ItemSet%5B%5D=any&category_730_ProPlayer%5B%5D=any&"
                    "category_730_StickerCapsule%5B%5D=any&category_730_TournamentTeam%5B%5D=any&"
                    "category_730_Weapon%5B%5D=any&category_730_Exterior%5B%5D=tag_WearCategory{wear}&"
                    "category_730_Rarity%5B%5D=tag_Rarity_Ancient"
                ).format(count=count, wear=wear)
            else:
                all_skins_url = (
                    f"https://steamcommunity.com/market/search/render?currency=3&norender=1&query=&"
                    "start={count}&count=10&search_descriptions=0&sort_column=price&sort_dir=asc&appid=730&category_730_ItemSet"
                    "%5B%5D=any&category_730_ProPlayer%5B%5D=any&category_730_StickerCapsule%5B%5D=any"
                    "&category_730_TournamentTeam%5B%5D=any&category_730_Weapon%5B%5D={weapon_name}"
                    "&category_730_Exterior%5B%5D=tag_WearCategory{wear}"
                ).format(count=count, weapon_name=self.weapon_name, wear=wear)
            try:
                response = self.RequestManager.getRequestAllAvailableProxies(
                    all_skins_url, skins_or_inspect_links="skins"
                )
                if response is None:
                    time.sleep(5)
                    continue
                if isinstance(response, str):
                    json_page = json.loads(response.text)
                else:
                    json_page = response.json()
            except Exception as e:
                logger.critical(str(e))
                continue

            repeat_counter += 1

            if repeat_counter >= 2:
                random_time = random.uniform(0.5, 1.5)
                time.sleep(repeat_counter * random_time)

            if repeat_counter > 20:
                end = True
                logger.critical("json_page failed 20 times, skipping")
                continue

            if json_page == None:
                continue

            if len(json_page["results"]) == 0:
                continue

            if json_page["start"] + 10 > json_page["total_count"]:
                end = True
                #logger.critical(str(self.weapon_name) + ":  json_page['start'] + 10 > json_page['total_count']:")
                #logger.critical(str(len(self.listings_url_set)))
                logger.critical("We end because of: " + str(int(json_page["start"]) + 10) + " > " + str(json_page["total_count"]))
                #logger.critical(str(json_page["total_count"]))
                #logger.critical(str(all_skins_url))
                #logger.critical(str(repeat_counter))
                #logger.critical(str(json_page))
            else:
                logger.warning(str(json_page["start"]) + " - " + str(json_page['total_count']))
            #if json_page["total_count"] < json_page["pagesize"]:
            #    end = True
            #else:
            #    logger.critical(str(self.weapon_name) + ": total_count < pagesize")
            #    logger.critical(str(json_page["total_count"]))
            #    logger.critical(str(json_page["pagesize"]))
            #    logger.critical(str(all_skins_url))
            #    logger.critical(str(repeat_counter))
            #    logger.critical(str(json_page))

            for i in json_page["results"]:
                if (
                    "AK-47 | Searing Rage" in i["name"] or
                    "AK-47 | Nouveau Rouge" in i["name"] or
                    "AK-47 | Midnight Laminate" in i["name"] or
                    "AK-47 | Wintergreen" in i["name"] or
                    "AK-47 | VariCamo Grey" in i["name"] or
                    "AUG | Commando Company" in i["name"] or
                    "AWP | Printstream" in i["name"] or
                    "AWP | Green Energy" in i["name"] or
                    "AWP | Arsenic Spill" in i["name"] or
                    "PP-Bizon | Wood Block Camo" in i["name"] or
                    "CZ75-Auto | Pink Pearl" in i["name"] or
                    "Desert Eagle | Serpent Strike" in i["name"] or
                    "Desert Eagle | Mint Fan" in i["name"] or
                    "Desert Eagle | Mulberry" in i["name"] or
                    "Dual Berettas | BorDeux" in i["name"] or
                    "Dual Berettas | Polished Malachite" in i["name"] or
                    "Dual Berettas | Rose Nacre" in i["name"] or
                    "FAMAS | Bad Trip" in i["name"] or
                    "FAMAS | Yeti Camo" in i["name"] or
                    "FAMAS | Grey Ghost" in i["name"] or
                    "FAMAS | Palm" in i["name"] or
                    "Five-SeveN | Sky Blue" in i["name"] or
                    "Five-SeveN | Autumn Thicket" in i["name"] or
                    "G3SG1 | Red Jasper" in i["name"] or
                    "G3SG1 | Green Cell" in i["name"] or
                    "Galil AR | Control" in i["name"] or
                    "Galil AR | Acid Dart" in i["name"] or
                    "Galil AR | Grey Smoke" in i["name"] or
                    "Galil AR | O-Ranger" in i["name"] or
                    "Galil AR | Robin's Egg" in i["name"] or
                    "Glock-18 | Shinobu" in i["name"] or
                    "Glock-18 | Ocean Topo" in i["name"] or
                    "Glock-18 | Coral Bloom" in i["name"] or
                    "Glock-18 | Glockingbird" in i["name"] or
                    "P2000 | Sure Grip" in i["name"] or
                    "P2000 | Marsh" in i["name"] or
                    "P2000 | Royal Baroque" in i["name"] or
                    "M249 | Sage Camo" in i["name"] or
                    "M4A4 | Steel Work" in i["name"] or
                    "M4A4 | Naval Shred Camo" in i["name"] or
                    "M4A4 | Choppa" in i["name"] or
                    "M4A1-S | Stratosphere" in i["name"] or
                    "M4A1-S | Glitched Paint" in i["name"] or
                    "M4A1-S | Rose Hex" in i["name"] or
                    "M4A4 | Sheet Lightning" in i["name"] or
                    "MAC-10 | Acid Hex" in i["name"] or
                    "MAC-10 | Poplar Thicket" in i["name"] or
                    "MAC-10 | Storm Camo" in i["name"] or
                    "MAC-10 | Bronzer" in i["name"] or
                    "MAG-7 | Resupply" in i["name"] or
                    "MAG-7 | Copper Oxide" in i["name"] or
                    "MP5-SD | Gold Leaf" in i["name"] or
                    "MP5-SD | Lime Hex" in i["name"] or
                    "MP7 | Short Ochre" in i["name"] or
                    "MP9 | Buff Blue" in i["name"] or
                    "MP9 | Cobalt Paisley" in i["name"] or
                    "MP9 | Shredded" in i["name"] or
                    "MP9 | Multi-Terrain" in i["name"] or
                    "MP9 | Nexus" in i["name"] or
                    "MP9 | Pine" in i["name"] or
                    "Negev | Sour Grapes" in i["name"] or
                    "Negev | Raw Ceramic" in i["name"] or
                    "Nova | Rising Sun" in i["name"] or
                    "Nova | Turquoise Pour" in i["name"] or
                    "Nova | Marsh Grass" in i["name"] or
                    "P250 | Red Tide" in i["name"] or
                    "P250 | Sedimentary" in i["name"] or
                    "P250 | Copper Oxide" in i["name"] or
                    "P250 | Plum Netting" in i["name"] or
                    "P90 | Desert Halftone" in i["name"] or
                    "P90 | Reef Grief" in i["name"] or
                    "P90 | Mustard Gas" in i["name"] or
                    "P90 | Blue Tac" in i["name"] or
                    "P90 | Wave Breaker" in i["name"] or
                    "R8 Revolver | Leafhopper" in i["name"] or
                    "R8 Revolver | Cobalt Grip" in i["name"] or
                    "Sawed-Off | Runoff" in i["name"] or
                    "SCAR-20 | Short Ochre" in i["name"] or
                    "SG 553 | Basket Halftone" in i["name"] or
                    "SG 553 | Night Camo" in i["name"] or
                    "SSG 08 | Tiger Tear" in i["name"] or
                    "SSG 08 | Grey Smoke" in i["name"] or
                    "SSG 08 | Blush Pour" in i["name"] or
                    "SSG 08 | Green Ceramic" in i["name"] or
                    "SSG 08 | Memorial" in i["name"] or
                    "Zeus x27 | Swamp DDPAT" in i["name"] or
                    "Zeus x27 | Electric Blue" in i["name"] or
                    "Zeus x27 | Tosai" in i["name"] or
                    "Tec-9 | Garter-9" in i["name"] or
                    "Tec-9 | Citric Acid" in i["name"] or
                    "Tec-9 | Raw Ceramic" in i["name"] or
                    "Tec-9 | Blue Blast" in i["name"] or
                    "UMP-45 | Green Swirl" in i["name"] or
                    "UMP-45 | K.O. Factory" in i["name"] or
                    "USP-S | Bleeding Edge" in i["name"] or
                    "USP-S | Royal Guard" in i["name"] or
                    "USP-S | Tropical Breeze" in i["name"] or
                    "USP-S | PC-GRN" in i["name"] or
                    "XM1014 | Copperflage" in i["name"] or
                    "XM1014 | Gum Wall Camo" in i["name"] or
                    "XM1014 | Canvas Cloud" in i["name"] or
                    "XM1014 | Mockingbird" in i["name"] or
                    "AWP | LongDog" in i["name"] or
                    "M4A4 | Hellish" in i["name"] or
                    "MP9 | Latte Rush" in i["name"] or
                    "MAC-10 | Derailment" in i["name"] or
                    "Tec-9 | Whiteout" in i["name"] or
                    "Zeus x27 | Charged Up" in i["name"] or
                    "XM1014 | Run Run Run" in i["name"] or
                    "FAMAS | 2A2F" in i["name"] or
                    "Glock-18 | Green Line" in i["name"] or
                    "UMP-45 | Late Night Transit" in i["name"] or
                    "Galil AR | Green Apple" in i["name"] or
                    "P250 | Constructivist" in i["name"] or
                    "Nova | Rain Station" in i["name"] or
                    "CZ75-Auto | Copper Fiber" in i["name"] or
                    "AUG | Steel Sentinel" in i["name"] or
                    "P90 | Straight Dimes" in i["name"]
                ):
                    logger.info("SKIPPING NEW SKIN: " + str(i["name"]))
                    continue
                if i["sell_listings"] == 0:
                    logger.critical("NO LISTINGS FOR " + str(i["name"]))
                    continue
                listings_url = (
                    "https://steamcommunity.com/market/listings/730/"
                    + urllib.parse.quote(str(i["name"]).encode("utf-8"))
                    + "/render/?query="
                )
                self.listings_url_set.add(listings_url)
            count += 10

        if self.high_low == "high":
            with open("capped_skins/skins_high_new.json") as json_file_high:
                skins_high = json.load(json_file_high)
                if self.weapon_name in skins_high:
                    for skin in skins_high[self.weapon_name]:
                        full_skin_name = str(skin[0]) + " (" + str(skin[2]) + ")"
                        listings_url = (
                            "https://steamcommunity.com/market/listings/730/"
                            + urllib.parse.quote(str(full_skin_name).encode("utf-8"))
                            + "/render/?query="
                        )
                        self.listings_url_set.add(listings_url)

        if self.high_low == "low":
            with open("capped_skins/skins_low_new.json") as json_file_low:
                skins_low = json.load(json_file_low)
                if self.weapon_name in skins_low:
                    for skin in skins_low[self.weapon_name]:
                        full_skin_name = str(skin[0]) + " (" + str(skin[1]) + ")"
                        listings_url = (
                            "https://steamcommunity.com/market/listings/730/"
                            + urllib.parse.quote(str(full_skin_name).encode("utf-8"))
                            + "/render/?query="
                        )
                        self.listings_url_set.add(listings_url)

    def buildInspectLinksList(self):
        time.sleep(random.uniform(0.2, 0.5))
        for listings_url in self.listings_url_set:
            if listings_url == "" or listings_url == None or listings_url == "\n":
                logger.critical("listings_url IS EMPTY????")
                continue
            end = False
            repeat_counter_overall = 0
            start = 0
            check_steamwebapi = True
            while not end:
                single_listings_url = (
                    str(listings_url) + "&start=" + str(start) + "&count=100&currency=3"
                )
                
                repeat_counter_overall += 1

                if repeat_counter_overall > 1:
                    random_time = random.uniform(1, 4)
                    time.sleep(repeat_counter_overall + random_time)

                if repeat_counter_overall > 2 and check_steamwebapi is True:
                    logger.error("repeat_counter_overall > 2")
                    market_hash_name = str(listings_url).split("/")[6]
                    steamwebapi_url = f"https://www.steamwebapi.com/steam/api/item?key=V0ZEQSIJVABFWREV&market_hash_name={market_hash_name}&currency=EUR&with_groups=false"
                    logger.error(
                        "TRYING TO CHECK IF THERE ARE OFFERS FOR "
                        + str(market_hash_name)
                        + " WITH "
                        + str(steamwebapi_url)
                    )
                    try:
                        response = self.RequestManager.getRequestNaked(
                            steamwebapi_url
                        ).json()
                        if "offervolume" in response:
                            if (
                                response["offervolume"] == 0
                                or response["offervolume"] is None
                            ):
                                logger.error(
                                    "NO OFFERS FOR " + str(market_hash_name)
                                )
                                end = True
                                if response["sold7d"] == 0:
                                    logger.error(
                                        "ALSO NO SELLS FOR 7 DAYS, ADDING TO EXCLUDED SKINS FOR " + str(market_hash_name)
                                    )
                                    with open("excluded_skins.txt", "r") as file:
                                        content = file.read()
                                    excluded_skins = ast.literal_eval(content)
                                    excluded_skins.append(market_hash_name)
                                    with open("excluded_skins.txt", "w") as file:
                                        file.write(str(excluded_skins))
                                continue
                            else:
                                check_steamwebapi = False
                                logger.error("THERE ARE OFFERS FOR " + str(market_hash_name) + " - SLEEPING, GOOD NIGHT!!!")
                                time.sleep(5)
                        else:
                            if "status" in response:
                                check_steamwebapi = False
                                logger.error("STEAMWEBAPI FREE REQUESTS USED UP??" + str(response))

                    except Exception as e:
                        logger.critical(str(e))
                        time.sleep(2)
                        continue

                if repeat_counter_overall > 15:
                    end = True
                    logger.critical(
                        "listings_page_json failed 15 times with "
                        + str(single_listings_url)
                    )

                encoded_market_name = urllib.parse.urlparse(
                    single_listings_url
                ).path.split("/")[4]
                if HelperFunctions.checkIfSkinExcluded(encoded_market_name) is True:
                    end = True
                    continue

                listings_page_json = self.getInspectLinks(single_listings_url)

                if listings_page_json == None:
                    time.sleep(2)
                    continue

                if "listinginfo" not in listings_page_json:
                    time.sleep(2)
                    continue

                if listings_page_json["listinginfo"] is None:
                    time.sleep(2)
                    continue

                end = True

                list_for_queue = []
                try:
                    for listing_id in listings_page_json["listinginfo"].keys():
                        listing_info = listings_page_json["listinginfo"][
                            str(listing_id)
                        ]
                        all_params = HelperFunctions.buildListingInfoList(
                            listing_info=listing_info, high_low=self.high_low
                        )
                        list_for_queue.append({str(listing_id): all_params})
                    # logger.info(list_for_queue)
                    self.listings_queue.put([list_for_queue])
                    time.sleep(1)
                except Exception as e:
                    logger.critical(str(e))
        return None

    def getInspectLinks(self, single_listings_url: str):
        response = self.RequestManager.getRequestAllAvailableProxies(
            single_listings_url, False
        )
        if response is None:
            return None

        if isinstance(response, str):
            listings_page_json = json.loads(response)
        else:
            listings_page_json = response.json()
        return listings_page_json

    def getBestOrWorstSkinsBulk(self, splitted_bulk):
        if splitted_bulk == None or len(splitted_bulk) == 0:
            logger.error("splitted_bulk is None or len is 0")
            return None
        new_splitted_bulk = HelperFunctions.getSplittedBulkList(splitted_bulk)
        if isinstance(new_splitted_bulk, tuple):
            if new_splitted_bulk[0] == None:
                logger.critical(str(new_splitted_bulk[1]))
        for j in splitted_bulk:
            self.listings_info_dict[list(j.keys())[0]] = list(j.values())[0]
        best_float = 0
        best_float_id = 0
        listing_to_check = None
        end = False
        while not end:
            try:
                bulk_response_json = self.RequestManager.postRequestNaked(
                    self.inspect_server_url + "bulk", _json=new_splitted_bulk
                ).json()
            except Exception as e:
                logger.critical(str(e))
                time.sleep(5)
                continue

            if bulk_response_json == None:
                continue

            if len(splitted_bulk) == 0:
                end = True
                continue

            if "error" in bulk_response_json:
                time.sleep(2)
                continue

            if len(bulk_response_json) == 0:
                continue

            end = True
            time.sleep(0.1)

            for j in bulk_response_json:
                if "error" in bulk_response_json[j]:
                    tmp = bulk_response_json[j]
                    bulk_response_json[j] = HelperFunctions.retryBulkError(new_splitted_bulk=new_splitted_bulk, j=j)
                    if bulk_response_json[j] is None:
                        logger.error(str(tmp) + " // error in bulk")
                        continue
                    else:
                        logger.error("RETRYING BULK ERROR SUCCESSFUL FOR " + str(bulk_response_json[j]))
                        
                if "m" not in bulk_response_json[j]:
                    logger.error(str(bulk_response_json[j]) + " // m not in bulk")
                    continue

                item_encoded = HelperFunctions.encodeItemName(
                    str(bulk_response_json[j]["full_item_name"])
                )

                self.checkForStickers(bulk_response_json[j], item_encoded)
                self.checkForBluegem(bulk_response_json[j], item_encoded)

                if self.high_low == "high":
                    if float(bulk_response_json[j]["floatvalue"]) > best_float:
                        best_float = float(bulk_response_json[j]["floatvalue"])
                        best_float_id = j

                if self.high_low == "low":
                    if best_float == 0:
                        best_float = bulk_response_json[j]["max"]
                    if float(bulk_response_json[j]["floatvalue"]) < best_float:
                        best_float = float(bulk_response_json[j]["floatvalue"])
                        best_float_id = j
                listing_to_check = bulk_response_json[best_float_id]

        if listing_to_check != None:
            item_encoded = HelperFunctions.encodeItemName(
                str(listing_to_check["full_item_name"])
            )

            price = self.listings_info_dict[listing_to_check["m"]]["price"]
            market_link = HelperFunctions.generateMarketLink(
                item_encoded=item_encoded,
                m=listing_to_check["m"],
                a=listing_to_check["a"],
            )
            single_full_item_name = listing_to_check["full_item_name"]

            single_inspect_link = HelperFunctions.generateInspectLink(
                m=listing_to_check["m"],
                s=listing_to_check["s"],
                a=listing_to_check["a"],
                d=listing_to_check["d"],
            )

            if self.high_low == "high":
                if (
                    self.checkForHighRank(
                        listing_to_check,
                        single_full_item_name,
                        price,
                        market_link,
                        single_inspect_link,
                    )
                    is None
                ):
                    return None

            if self.high_low == "low":
                if (
                    self.checkForLowRank(
                        listing_to_check,
                        single_full_item_name,
                        price,
                        market_link,
                        single_inspect_link,
                    )
                    is None
                ):
                    return None

        else:
            logger.critical(
                "LISTING TO CHECK IS NONE, WAT????????? " + str(splitted_bulk)
            )

    def checkForStickers(self, bulk_response_json_j, item_encoded):
        sticki = bulk_response_json_j["stickers"]
        for sticker in sticki:
            if "material" in sticker:
                if str(sticker["material"]).startswith("emskatowice2014"):
                    if "Holo" in str(sticker["name"]):
                        market_link = (
                            "https://steamcommunity.com/market/listings/730/"
                            + str(item_encoded)
                            + "#buylisting|"
                            + str(bulk_response_json_j["m"])
                            + "|730|2|"
                            + str(bulk_response_json_j["a"])
                        )
                        single_sticki = {}
                        single_sticki[str(bulk_response_json_j["full_item_name"])] = [
                            "Stickomat",
                            self.listings_info_dict[bulk_response_json_j["m"]]["price"],
                            market_link,
                        ]
                        HelperFunctions.writeSingleAnyToFile(
                            single_sticki, "stickers.txt"
                        )
                        final_listing = FinalListing(
                            full_item_name=str(bulk_response_json_j["full_item_name"]),
                            market_url=str(market_link),
                            inspect_link=self.listings_info_dict[
                                bulk_response_json_j["m"]
                            ]["link"],
                            rank=str("Stickomat"),
                            price=self.listings_info_dict[bulk_response_json_j["m"]][
                                "price"
                            ],
                        )
                        self.singleCheckForPotentialBuy(final_listing)

    def checkForBluegem(self, bulk_response_json_j, item_encoded):
        if "item_name" in bulk_response_json_j:
            if bulk_response_json_j["item_name"] == "Case Hardened":
                pattern = bulk_response_json_j["paintseed"]
                if str(self.weapon_name) in bluegems:
                    if str(pattern) in bluegems[str(self.weapon_name)]:
                        market_link = (
                            "https://steamcommunity.com/market/listings/730/"
                            + str(item_encoded)
                            + "#buylisting|"
                            + str(bulk_response_json_j["m"])
                            + "|730|2|"
                            + str(bulk_response_json_j["a"])
                        )
                        single_bluegem = {}
                        single_bluegem[str(bulk_response_json_j["full_item_name"])] = [
                            "BlueGem",
                            self.listings_info_dict[bulk_response_json_j["m"]]["price"],
                            market_link,
                        ]
                        HelperFunctions.writeSingleAnyToFile(
                            single_bluegem, "bluegems.txt"
                        )
                        final_listing = FinalListing(
                            full_item_name=str(bulk_response_json_j["full_item_name"]),
                            market_url=str(market_link),
                            inspect_link=self.listings_info_dict[
                                bulk_response_json_j["m"]
                            ]["link"],
                            rank=str("Stickomat"),
                            price=self.listings_info_dict[bulk_response_json_j["m"]][
                                "price"
                            ],
                        )
                        self.singleCheckForPotentialBuy(final_listing)

    def checkForHighRank(
        self,
        listing_to_check,
        single_full_item_name,
        price,
        market_link,
        single_inspect_link,
    ):
        if "floatid" not in listing_to_check and "high_rank" not in listing_to_check:
            listing_to_check = HelperFunctions.floatIdNotInSingle(single_inspect_link=single_inspect_link)
            if listing_to_check is None:
                logger.error("listing_to_check is None with " + str(single_inspect_link))
                return None
        if "high_rank" in listing_to_check:
            if listing_to_check["high_rank"] > 5:
                return None
            else:
                if self.alreadyChecked(single_inspect_link=single_inspect_link) is True:
                    return None
                else:
                    end = False
                    repeats = 0
                    while not end:
                        repeats += 1
                        if repeats > 10:
                            logger.critical("SeleniumExecutor fucked up for 10 times with " + str(single_inspect_link) + " - skipping...")
                            end = True
                        try:
                            csfloat_iteminfo = SeleniumExecutor.getItemInfo(
                                str(single_inspect_link)
                            )
                        except Exception as e:
                            logger.critical(str(e))
                            continue
                        if csfloat_iteminfo is None:
                            logger.error("csfloat_iteminfo is None, repeating...")
                            time.sleep(2)
                            continue
                        if "error" in csfloat_iteminfo:
                            logger.info(str(single_inspect_link))
                        if "iteminfo" in csfloat_iteminfo:
                            logger.error(
                                "SELENIUMNEXECUTOR GOT: "
                                + str(csfloat_iteminfo["iteminfo"])
                            )
                            if "high_rank" in csfloat_iteminfo["iteminfo"]:
                                if csfloat_iteminfo["iteminfo"]["high_rank"] < 6:
                                    final_listing = FinalListing(
                                        full_item_name=single_full_item_name,
                                        market_url=str(market_link),
                                        inspect_link=single_inspect_link,
                                        rank=str(
                                            csfloat_iteminfo["iteminfo"]["high_rank"]
                                        ),
                                        price=price,
                                    )
                                    self.singleCheckForPotentialBuy(
                                        final_listing=final_listing,
                                        rank=csfloat_iteminfo["iteminfo"]["high_rank"],
                                    )
                            end = True

    def checkForLowRank(
        self,
        listing_to_check,
        single_full_item_name,
        price,
        market_link,
        single_inspect_link,
    ):
        if "floatid" not in listing_to_check and "low_rank" not in listing_to_check:
            listing_to_check = HelperFunctions.floatIdNotInSingle(single_inspect_link=single_inspect_link)
            if listing_to_check is None:
                logger.error("listing_to_check is None with " + str(single_inspect_link))
                return None
        if "low_rank" in listing_to_check:
            if listing_to_check["low_rank"] > 5:
                return None
            else:
                if self.alreadyChecked(single_inspect_link=single_inspect_link) is True:
                    return None
                else:
                    end = False
                    repeats = 0
                    while not end:
                        repeats += 1
                        if repeats > 10:
                            logger.critical("SeleniumExecutor fucked up for 10 times with " + str(single_inspect_link) + " - skipping...")
                            end = True
                        try:
                            csfloat_iteminfo = SeleniumExecutor.getItemInfo(
                                str(single_inspect_link)
                            )
                        except Exception as e:
                            logger.critical(str(e))
                            continue
                        if csfloat_iteminfo is None:
                            logger.error("csfloat_iteminfo is None, repeating...")
                            time.sleep(2)
                            continue
                        if "iteminfo" in csfloat_iteminfo:
                            logger.error(
                                "SELENIUMNEXECUTOR GOT: "
                                + str(csfloat_iteminfo["iteminfo"])
                            )
                            if "low_rank" in csfloat_iteminfo["iteminfo"]:
                                if csfloat_iteminfo["iteminfo"]["low_rank"] < 6:
                                    final_listing = FinalListing(
                                        full_item_name=single_full_item_name,
                                        market_url=str(market_link),
                                        inspect_link=single_inspect_link,
                                        rank=str(
                                            csfloat_iteminfo["iteminfo"]["low_rank"]
                                        ),
                                        price=price,
                                    )
                                    self.singleCheckForPotentialBuy(
                                        final_listing=final_listing,
                                        rank=csfloat_iteminfo["iteminfo"]["low_rank"],
                                    )
                            end = True

    def alreadyChecked(self, single_inspect_link):
        already_in_db = False
        with topfloatdb.db_cursor() as cur:
            cur.execute(
                "SELECT inspect_link FROM checked WHERE inspect_link = %s",
                (single_inspect_link,),
            )

            try:
                ret = len(cur.fetchall())
                if ret > 0:
                    already_in_db = True
                    logger.info(str(single_inspect_link) + " already in DB, skipping.")
                else:
                    try:
                        already_in_db = False
                        cur.execute(
                            "INSERT INTO checked(inspect_link) VALUES(%s)",
                            (single_inspect_link,),
                        )
                    except Exception as e:
                        logger.error(str(e))
            except Exception as e:
                logger.error(str(e))
                return False
        return already_in_db

    def singleCheckForPotentialBuy(self, final_listing: FinalListing, rank=0):
        single_cheap_top_skin = {}
        end = False
        repeat_counter = 0
        while not end:
            lowest_price = 0
            repeat_counter += 1
            try:
                price_overview_page = self.RequestManager.getRequestAllAvailableProxies(
                    "https://steamcommunity.com/market/priceoverview/?market_hash_name="
                    + HelperFunctions.encodeAidsItemName(final_listing.full_item_name)
                    + "&appid=730&currency=3",
                    "price",
                )
                if price_overview_page is None:
                    if repeat_counter > 10:
                        end = True
                        logger.error(
                            "Couldn't get price for "
                            + str(final_listing.full_item_name)
                            + " after 10 times, skipping for now"
                        )
                    continue

                price_overview_json = json.loads(price_overview_page.text)
            except Exception as e:
                logger.critical(str(e))
                continue

            if price_overview_json == None:
                continue

            if "lowest_price" in price_overview_json:
                lowest_price = price_overview_json["lowest_price"]
                lowest_price = lowest_price[:-1]
                lowest_price = str(lowest_price).replace(",", ".")
            else:
                response = self.RequestManager.getRequestAllAvailableProxies(
                    "https://steamcommunity.com/market/listings/730/"
                    + str(final_listing.full_item_name),
                    "price",
                )
                if price_overview_page is None:
                    continue
                listing_source_page = response.text
                nameid_url = HelperFunctions.manuallyGetNameIDURL(
                    listing_source_page=listing_source_page
                )
                if nameid_url is None:
                    continue
                else:
                    alt_response = self.RequestManager.getRequestAllAvailableProxies(
                        nameid_url, "price"
                    )
                    if alt_response is None:
                        continue
                    alt_price_overview_json = json.loads(alt_response.text)
                    if len(str(alt_price_overview_json["lowest_sell_order"])) > 2:
                        lowest_price = str(
                            alt_price_overview_json["lowest_sell_order"]
                        )[:-2]

            lowest_price = str(lowest_price).replace(" ", "")
            lowest_price = str(lowest_price).replace("--", "0")

            try:
                if float(final_listing.price) <= float(lowest_price) * 5:
                    single_cheap_top_skin[str(final_listing.full_item_name)] = [
                        str(final_listing.rank),
                        str(final_listing.price),
                        str(final_listing.market_url),
                    ]
                    HelperFunctions.writeSingleCheapSkinToFile(single_cheap_top_skin)
                else:
                    if final_listing.inspect_link != "" and rank == 1337:
                        try:
                            postgresql_conn = psycopg2.connect(
                                database="postgres",
                                user="postgres",
                                password="Berufsorientierung1!",
                                host="23.88.122.57",
                                port="6432",
                            )
                            postgresql_cur = postgresql_conn.cursor()
                            postgresql_cur.execute(
                                "INSERT INTO floats(inspect_link, rank) VALUES(%s, %s)",
                                (
                                    final_listing.inspect_link,
                                    rank,
                                ),
                            )
                            postgresql_conn.commit()
                            postgresql_cur.close()
                        except (Exception, psycopg2.DatabaseError) as error:
                            logger.error(str(error))
                        finally:
                            if postgresql_conn is not None:
                                postgresql_conn.close()
            except ValueError:
                logger.critical("ValueError in singleCheckForPotentialBuy: " + str(e))
            end = True
