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 | Aphrodite" in i["name"] or
                    "M4A1-S | Party Animal" in i["name"] or
                    "AWP | Exothermic" in i["name"] or
                    "USP-S | Sleeping Potion" in i["name"] or
                    "Zeus x27 | Earth Mandala" in i["name"] or
                    "UMP-45 | Warm Blooded" in i["name"] or
                    "Galil AR | Sky Mandala" in i["name"] or
                    "Five-SeveN | Fraise Crane" in i["name"] or
                    "XM1014 | XoooM" in i["name"] or
                    "Tec-9 | Banana Leaf" in i["name"] or
                    "SSG 08 | Calligrafaux" in i["name"] or
                    "MP7 | Coral Paisley" in i["name"] or
                    "MP5-SD | Picnic" in i["name"] or
                    "Sawed-Off | Crimson Batik" in i["name"] or
                    "SG 553 | Safari Print" in i["name"] or
                    "R8 Revolver | Mauve Aside" in i["name"] or
                    "PP-Bizon | Thermal Currents" in i["name"] or
                    "MP9 | Bee-Tron" in i["name"] or
                    "FAMAS | Byproduct" in i["name"] or
                    "CZ75-Auto | Honey Paisley" in i["name"] or
                    "AWP | The End" in i["name"] or
                    "AK-47 | Breakthrough" in i["name"] or
                    "Glock-18 | Trace Lock" in i["name"] or
                    "AUG | Creep" in i["name"] or
                    "Desert Eagle | The Daily Deagle" in i["name"] or
                    "P2000 | Grip Tape" in i["name"] or
                    "P90 | Aeolian Light" in i["name"] or
                    "FAMAS | Vendetta" in i["name"] or
                    "M4A4 | Aeolian Dark" in i["name"] or
                    "MAC-10 | Snow Splash" in i["name"] or
                    "MP5-SD | Snow Splash" in i["name"] or
                    "R8 Revolver | Dark Chamber" in i["name"] or
                    "PP-Bizon | Bizoom" in i["name"] or
                    "Dual Berettas | Silver Pour" in i["name"] or
                    "M249 | Sleet" in i["name"] or
                    "MP9 | Dizzy" in i["name"] or
                    "Nova | Currents" in i["name"] or
                    "P250 | Sleet" in i["name"] or
                    "SCAR-20 | Zinc" in i["name"] or
                    "SSG 08 | Sans Comic" 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)
    #'{"links":["steam://run/730//+csgo_econ_action_preview%20E2F261291D4D5CE3FAE5C227EBCAE4D2EBDA7E203719E1A25BE4AAE2B2E280F6EAE1F220D1CFE2E2A223DFE084E1DCA762C4CEDE80F6EAE2F220D1CFE2E2A223DF52B83B5FA702B7A1DE80F6EAE1F220D1CFE2E2F223DFECC675DCA732A678DE80F6EAE0F273D6FFE835C1DDDFAD4480DCA79A89F6DF80F6EAE3F220D1CFE2E2A223DF86FAD85CA7E22884588A8092EA5C91E50C","steam://run/730//+csgo_econ_action_preview%2003138BD5F0918F021B0423C60A2B05330A3BC9E4D8F80043CF004B0353EC026B46730BD5F6094C"]}'
    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 > 3 and check_steamwebapi is True:
                    logger.error("repeat_counter_overall > 3")
                    market_hash_name = str(listings_url).split("/")[6]
                    steamwebapi_url = f"https://www.steamwebapi.com/steam/api/item?key=CJN62T0XGKR7TH5A&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_response = self.getInspectLinks(single_listings_url)
                listings_page_json = listings_page_response.json() if listings_page_response is not None else None
                listings_page_text = listings_page_response.text if listings_page_response is not None else None

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

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

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

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

                end = True

                list_for_queue = []
                try:
                    new_inspect_links = HelperFunctions.getNewInspectLinks(listings_page_text, high_low=self.high_low)
                    #logger.critical(f"NEW INSPECT LINKS FOR {encoded_market_name}: " + str(new_inspect_links))
                    self.checkForRankNew(new_inspect_links)
                    #self.listings_queue.put([list_for_queue])
                except Exception as e:
                    logger.critical(str(e))
                    continue
                # 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
                #         )
                #         #with open("test.txt", "a") as file:
                #         #    file.write(str(all_params))
                #         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
    
    
    #curl 'https://gateway.floatdb.com/v1/ranks/check' --data-raw '{"links":["steam://run/730//+csgo_econ_action_preview%20E2F261291D4D5CE3FAE5C227EBCAE4D2EBDA7E203719E1A25BE4AAE2B2E280F6EAE1F220D1CFE2E2A223DFE084E1DCA762C4CEDE80F6EAE2F220D1CFE2E2A223DF52B83B5FA702B7A1DE80F6EAE1F220D1CFE2E2F223DFECC675DCA732A678DE80F6EAE0F273D6FFE835C1DDDFAD4480DCA79A89F6DF80F6EAE3F220D1CFE2E2A223DF86FAD85CA7E22884588A8092EA5C91E50C","steam://run/730//+csgo_econ_action_preview%2003138BD5F0918F021B0423C60A2B05330A3BC9E4D8F80043CF004B0353EC026B46730BD5F6094C"]}'
    def checkForRankNew(self, new_inspect_links_list):
        inspect_link_dict = {"links": []}
        results = []
        for link_info in new_inspect_links_list:
            inspect_link = link_info[3]
            inspect_link_dict["links"].append(inspect_link)

        try:
            response = self.RequestManager.postRequestNaked(
                "https://gateway.floatdb.com/v1/ranks/check",
                _json=inspect_link_dict
            ).json()
            #logger.critical(f"RANK CHECK RESPONSE FOR {inspect_link_dict}: " + str(response))
            if "ranks" in response:
                ranks = response["ranks"]
                for rank_info in ranks:
                    listing_id = rank_info
                    rank_dict = ranks[rank_info]
                    key, value = list(rank_dict.items())[0]
                    if str(key).startswith("high"):
                        print(f"RANK CHECK RESPONSE KEY STARTS WITH HIGH FOR LISTING ID {listing_id}: " + str(rank_dict))
                    elif str(key).startswith("low"):
                        print(f"RANK CHECK RESPONSE KEY STARTS WITH LOW FOR LISTING ID {listing_id}: " + str(rank_dict))
                    else:
                        logger.error("RANK CHECK RESPONSE KEY NOT STARTING WITH HIGH OR LOW: " + str(key))
        except Exception as e:
            logger.critical(str(e))
        
            

    def getInspectLinks(self, single_listings_url: str):
        response = self.RequestManager.getRequestAllAvailableProxies(
            single_listings_url, False
        )
        if response is None:
            return None
        return response
        # 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]
            #with open("test2.txt", "a") as f:
            #    f.write(str(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

            #with open("test4.txt", "a") as f:
            #    f.write(str(bulk_response_json))

            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"],
            )
            logger.critical(str(single_inspect_link))

            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)
                            )
                            #with open("a", "test3.txt") as f:
                            #    f.write(str(csfloat_iteminfo))
                        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
