import json
from math import sin
import urllib.parse
import logging
from CustomFormatter import CustomFormatter
from FileFormatter import FileFormatter
import time
import random
from bluegems import bluegems
from RequestManager import RequestManager
import HelperFunctions
import psycopg2
import requests
from selenium import webdriver
from bs4 import BeautifulSoup
from extension import proxies
from selenium.webdriver.common.by import By
import queue
from ips import ip_addresses
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import re
from FinalListing import FinalListing
import topfloatdb
import socket


logger = logging.getLogger("Marketbot")
logger.setLevel(logging.INFO)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
ch.setFormatter(CustomFormatter())
logger.addHandler(ch)
fh = logging.FileHandler("errors.txt")
fh.setFormatter(FileFormatter())
fh.setLevel(logging.ERROR)
logger.addHandler(fh)



class NewMarketBot(object):
    def __init__(
        self, high_low, weapon_name, webshare_ips, all_sessions, extended_scrape, listings_queue: queue.Queue, csgofloat_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.extended_scrape = extended_scrape
        self.RequestManager = RequestManager(all_sessions, webshare_ips)
        self.listings_queue = listings_queue
        self.csgofloat_queue = csgofloat_queue
        self.inspect_server_url = "http://23.88.122.57:1337/"

    def getHashedNameListHighOrLow(self):
        # self.session.headers.update({'Referer': "https://steamcommunity.com/market/search/"})
        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 = (
                "https://steamcommunity.com/market/search/render?currency=3&norender=1/?query=&start="
                + str(count)
                + "&count=100&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"
                + str(wear)
                + "&category_730_Rarity%5B%5D=tag_Rarity_Ancient"
            )
        else:
            all_skins_url = (
                "https://steamcommunity.com/market/search/render?currency=3&norender=1/?query=&start="
                + str(count)
                + "&count=100&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="
                + str(self.weapon_name)
                + "&category_730_Exterior%5B%5D=tag_WearCategory"
                + str(wear)
            )

        while not end:
            try:
                response = self.RequestManager.getRequest(all_skins_url)
                if response is None:
                    time.sleep(5)
                    continue
                if isinstance(response, str):
                    json_page = json.loads(response)
                else:
                    json_page = response.json()
            except Exception as e:
                logger.critical("Exception in getHashedNameListHighOrLow: " + 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 > 10:
                end = True
                logger.critical("json_page failed 10 times, skipping")
                continue

            if json_page == None:
                continue

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

            if json_page["total_count"] < json_page["pagesize"]:
                end = True
            else:
                logger.critical(str(self.weapon_name) + ": total_count < pagesize")

            for i in json_page["results"]:
                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 += 100

        if self.high_low == "high":
            with open("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("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 i in self.listings_url_set:
            if i == "" or i == None or i == "\n":
                logger.critical("i IS EMPTY????")
                continue
            end = False
            repeat_counter_overall = 0
            start = 0
            repeat_counter_len = 0
            while not end:
                repeat_counter_overall += 1

                if repeat_counter_overall >= 2:
                    random_time = random.uniform(1, 3)
                    time.sleep(repeat_counter_overall * random_time)

                if repeat_counter_overall > 7:
                    end = True
                    logger.critical("giving up with " + str(i))


                single_listings_url = (
                    str(i) + "&start=" + str(start) + "&count=100&currency=3"
                )
                encoded_market_name = urllib.parse.urlparse(single_listings_url).path.split("/")[4]
                if HelperFunctions.checkIfSkinExcluded(encoded_market_name) is True:
                    end = True
                    continue
                try:
                    response = self.RequestManager.getRequestOnlyAWS(single_listings_url, 2)
                    if response is None:
                        time.sleep(1)
                        continue
                    if response == 1337:
                        webshare_failed = False
                        iproyal_failed = False
                        response2 = self.RequestManager.getRequestWebshare(single_listings_url)
                        if response2 is None:
                            webshare_failed = True
                        else:
                            listings_page_json = None
                            try:
                                listings_page_json = response2.json()
                            except Exception as e:
                                webshare_failed = True
                                logger.error("buildInspectLinksList(): " + str(e))
                            if listings_page_json is not None:
                                if len(listings_page_json["listinginfo"]) == 0:
                                    webshare_failed = True
                                else:
                                    response = response2

                        if webshare_failed is True:
                            response3 = self.RequestManager.getRequestIPRoyal(single_listings_url)
                            if response3 is None:
                                iproyal_failed = True
                            else:
                                listings_page_json = None
                                try:
                                    listings_page_json = response3.json()
                                except Exception as e:
                                    iproyal_failed = True
                                    logger.error("buildInspectLinksList(): " + str(e))
                                if listings_page_json is not None:
                                    if len(listings_page_json["listinginfo"]) == 0:
                                        iproyal_failed = True
                                    else:
                                        response = response3

                        if webshare_failed is True and iproyal_failed is True:
                            repeat_counter_len += 1
                            if repeat_counter_len > 5:
                                logger.critical("IPROYAL AND WEBSHARE FAILED, LEN = 0 FOR FIVE TIMES, GIVING UP FOR " + str(single_listings_url))
                                end = True
                                continue
                            else:
                                time.sleep(1)
                                continue

                    if isinstance(response, str):
                        listings_page_json = json.loads(response)
                    else:
                        listings_page_json = response.json()
                except Exception as e:
                    logger.critical("Exception in buildInspectLinksList: " + str(e))
                    continue

                if listings_page_json == None:
                    time.sleep(2)
                    if repeat_counter_overall > 5:
                        end = True
                        logger.critical("listings_page_json failed 5 times, skipping")
                        time.sleep(5)
                        continue
                    continue

                if "listinginfo" not in listings_page_json:
                    time.sleep(2)
                    continue
                    
                if listings_page_json is None or listings_page_json["listinginfo"] is None:
                    continue
                
                
                if len(listings_page_json["listinginfo"]) == 0:
                    if repeat_counter_len == 0:
                        repeat_counter_len += 1
                        time.sleep(1)
                        continue
                    webshare_failed = False
                    iproyal_failed = False
                    response2 = self.RequestManager.getRequestWebshare(single_listings_url)
                    if response2 is None:
                        webshare_failed = True
                    else:
                        listings_page_json = None
                        try:
                            listings_page_json = response2.json()
                        except Exception as e:
                            webshare_failed = True
                            logger.error("buildInspectLinksList(): " + str(e))
                        if listings_page_json is not None:
                            if len(listings_page_json["listinginfo"]) == 0:
                                webshare_failed = True

                    if webshare_failed is True:
                        response3 = self.RequestManager.getRequestIPRoyal(single_listings_url)
                        if response3 is None:
                            iproyal_failed = True
                        else:
                            listings_page_json = None
                            try:
                                listings_page_json = response3.json()
                            except Exception as e:
                                iproyal_failed = True
                                logger.error("buildInspectLinksList(): " + str(e))
                            if listings_page_json is not None:
                                if len(listings_page_json["listinginfo"]) == 0:
                                    iproyal_failed = True
                    
                    if webshare_failed is True and iproyal_failed is True:
                        repeat_counter_len += 1
                        if repeat_counter_len > 5:
                            logger.critical("IPROYAL AND WEBSHARE FAILED, LEN = 0 FOR FIVE TIMES, GIVING UP FOR " + str(single_listings_url))
                            end = True
                            continue
                        else:
                            time.sleep(1)
                            continue
                end = True

                list_for_queue = []
                outer_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})
                    outer_list_for_queue.append(list_for_queue)
                    self.listings_queue.put(outer_list_for_queue)
                    time.sleep(1)
                except Exception as e:
                    logger.critical("buildInspectLinksList(): " + str(e))

    def getBestOrWorstSkinsBulk(self, splitted_bulk):
        if splitted_bulk == None or len(splitted_bulk) == 0:
            logger.error(
                "getBestOrWorstSkinsBulk(): 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("Exception with HelperFunctions.getSplittedBulkList(splitted_bulk): " + 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:
                time.sleep(5)
                logger.critical("Exception in getBestOrWorstSkinsBulk: " + str(e))
                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]:
                    continue
                if "m" not in bulk_response_json[j]:
                    continue
                
                if(bulk_response_json[j]["s"] == "0"):
                    single_inspect_server_url = (
                        self.inspect_server_url + "?url=steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20"
                        + "M" + str(bulk_response_json[j]["m"])
                        + "A" + str(bulk_response_json[j]["a"])
                        + "D" + str(bulk_response_json[j]["d"])
                    )
                if(bulk_response_json[j]["m"] == "0"):
                    single_inspect_server_url = (
                        self.inspect_server_url + "?url=steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20"
                        + "S" + str(bulk_response_json[j]["s"])
                        + "A" + str(bulk_response_json[j]["a"])
                        + "D" + str(bulk_response_json[j]["d"])
                    )
                
                #if "floatid" not in bulk_response_json[j]:
                #    HelperFunctions.floatidNotInBulk(single_inspect_server_url=single_inspect_server_url, bulk_at_j=bulk_response_json[j], request_manager=self.RequestManager)
                
                item_encoded = HelperFunctions.encodeItemName(
                    str(bulk_response_json[j]["full_item_name"])
                )
                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)

                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)
                
                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 = self.listings_info_dict[listing_to_check["m"]]["link"]
            if single_inspect_link is None:
                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 "high_rank" in listing_to_check:
                    if listing_to_check["high_rank"] > 5:
                        return None
                    else:
                        if float(listing_to_check["floatvalue"]) > (
                            float(listing_to_check["max"]) * 0.99
                        ):
                            top5_found_in_db = self.checkDBForFloat(single_full_item_name, listing_to_check["floatvalue"])
                            if top5_found_in_db == None:
                                self.csgofloat_queue.put([price, market_link, single_full_item_name, single_inspect_link, listing_to_check["m"], listing_to_check["s"], False, False, self.high_low])
                            elif top5_found_in_db == 1337:
                                return None
                            else:
                                final_listing = FinalListing(full_item_name=single_full_item_name, market_url=str(market_link), inspect_link=single_inspect_link, rank=str(top5_found_in_db), price=price)
                                self.singleCheckForPotentialBuy(final_listing=final_listing, inspect_link=single_inspect_link, rank=top5_found_in_db)

            if self.high_low == "low":
                if float(listing_to_check["min"]) == 0:
                    minval = 0.01
                else:
                    minval = float(listing_to_check["min"])
                if "low_rank" in listing_to_check:
                    if listing_to_check["low_rank"] > 5:
                        return None
                    else:
                        floatvalue = float(listing_to_check["floatvalue"])
                        floatvalue = f"{floatvalue:.20f}"
                        if float(floatvalue) < (float(minval) * 1.01) or str(
                            floatvalue
                        ).startswith("0.000"):
                            sih_float_rank = self.checkSIHForFloat(single_inspect_link)
                            if sih_float_rank is not None:
                               #logger.critical("SIH FLOAT RANK FOUND FOR " + str(single_full_item_name) + " = " + str(sih_float_rank))
                               final_listing = FinalListing(full_item_name=single_full_item_name, market_url=str(market_link), inspect_link=single_inspect_link, rank=str(sih_float_rank), price=price)
                               self.singleCheckForPotentialBuy(final_listing=final_listing, inspect_link=single_inspect_link, rank=sih_float_rank)
                            top5_found_in_db = self.checkDBForFloat(single_full_item_name, listing_to_check["floatvalue"])
                            if top5_found_in_db == None:
                                self.csgofloat_queue.put([price, market_link, single_full_item_name, single_inspect_link, listing_to_check["m"], listing_to_check["s"], False, False, self.high_low])
                            elif top5_found_in_db == 1337:
                                return None
                            else:
                                final_listing = FinalListing(full_item_name=single_full_item_name, market_url=str(market_link), inspect_link=single_inspect_link, rank=str(top5_found_in_db), price=price)
                                self.singleCheckForPotentialBuy(final_listing=final_listing, inspect_link=single_inspect_link, rank=top5_found_in_db)
        else:
            logger.critical("LISTING TO CHECK IS NONE, WAT????????? " + str(splitted_bulk))

    def checkSIHForFloat(self, inspect_link):
        #https://floats.steaminventoryhelper.com/?url=steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20S76561198074859638A35298009395D1055679187708722395
        url = "https://floats.steaminventoryhelper.com/?url=" + str(inspect_link)
        end = False
        counter = 0
        while not end:
            if counter > 7:
                end = True
            try:
                r = self.RequestManager.getRequestIPRoyal(url)
            except Exception as e:
                logger.critical(str(e))
                counter += 1
                time.sleep(2)
                continue
            if r is None:
                logger.critical("r is none")
                counter += 1
                time.sleep(2)
                continue
            else:
                r_json = r.json()
                end = True
        if self.high_low == "high":
            if r_json["iteminfo"]["globalRatingPos"] == None:
                pass
            else:
                if r_json["iteminfo"]["globalRatingPos"] > -1000:
                    return 1
                elif r_json["iteminfo"]["globalRatingPos"] > -2000:
                    return 2
                elif r_json["iteminfo"]["globalRatingPos"] > -3000:
                    return 3
                elif r_json["iteminfo"]["globalRatingPos"] > -4000:
                    return 4
                elif r_json["iteminfo"]["globalRatingPos"] > -5000:
                    return 5
                else:
                    return None


            if r_json["iteminfo"]["localRatingPos"] == None:
                return None
            else:
                pass


            #return None
        if self.high_low == "low":
            global_rank = None
            local_rank = None
            if r_json["iteminfo"]["globalRatingPos"] == None:
                pass
            else:
                if r_json["iteminfo"]["globalRatingPos"] > 1000:
                    global_rank = 1
                elif r_json["iteminfo"]["globalRatingPos"] > 2000:
                    global_rank = 2
                elif r_json["iteminfo"]["globalRatingPos"] > 3000:
                    global_rank = 3
                elif r_json["iteminfo"]["globalRatingPos"] > 4000:
                    global_rank = 4
                elif r_json["iteminfo"]["globalRatingPos"] > 5000:
                    global_rank = 5
                else:
                    global_rank = None

            if r_json["iteminfo"]["localRatingPos"] == None:
                local_rank = None
            else:
                if r_json["iteminfo"]["localRatingPos"] == 1:
                    local_rank = 1
                elif r_json["iteminfo"]["localRatingPos"] == 2:
                    local_rank = 2
                elif r_json["iteminfo"]["localRatingPos"] == 3:
                    local_rank = 3
                elif r_json["iteminfo"]["localRatingPos"] == 4:
                    local_rank = 4
                elif r_json["iteminfo"]["localRatingPos"] == 5:
                    local_rank = 5
                else:
                    local_rank = None

            if global_rank is None and local_rank is not None:
                return local_rank
            elif local_rank is None and global_rank is not None:
                return global_rank
            elif global_rank is not None and local_rank is not None:
                if global_rank < local_rank:
                    return global_rank
                else:
                    return local_rank
            else:
                    return None
        return None



    def checkActualDBForFloat(self, full_item_name, floatvalue):
        table_name = "skinport_id_to_name"
        query1 = "SELECT skinport_id, type FROM " + str(table_name)
        query2 = " WHERE name = %s"
        query = query1 + query2

        with topfloatdb.db_cursor() as cur:
            cur.execute(query, (full_item_name,))
            res = cur.fetchall()
            try:
                skinport_id = res[0][0]
                skinport_type = res[0][1]
            except Exception as e:
                logger.error("Exception in checkActualDBForFloat() FOR: " + str(full_item_name) + str(e))
                return None
            if skinport_id is None or skinport_type is None:
                logger.error("NO skinport_id_to_name ENTRY FOR: " + str(full_item_name))
                return None

            url = "https://float.skinport.com/api/assets?page=1&item_id=" + str(skinport_id) + "&category_id=" + str(skinport_type) + "&sort=" + str(self.high_low) + "_float"
            #print(url)
            try:
                r = self.RequestManager.getRequestWithoutAWS(url).json()
            except Exception as e:
                logger.critical(str(e))
                return None
            if "data" in r:
                if len(r["data"]) == 0:
                    url = "https://float.skinport.com/api/assets?page=1&item_id=" + str(skinport_id) + "&sort=" + str(self.high_low) + "_float"
                    r = self.RequestManager.getRequestWithoutAWS(url).json()
                    if "data" in r:
                        if len(r["data"]) == 0:
                            logger.critical("COULDNT FIND SKIN ON SKINPORT DB WITH SKINPORT ID " + str(skinport_id) + " AND TYPE " + str(skinport_type) + " FOR " + str(full_item_name))
                            return None
                
                for i in range(0,5):
                    floatvalue_skinport = r["data"][i]["float"]
                    rank = i + 1
                    if self.high_low == "high":
                        if floatvalue > floatvalue_skinport:
                            return rank
                    if self.high_low == "low":
                        if floatvalue < floatvalue_skinport:
                            return rank

        return 1337

    def checkDBForFloat(self, full_item_name, floatvalue):
        
        #####################################################UNCOMMENT IF SKINPORT FLOATDB IS BACK UP AGAIN 
        #actual_skinport_check = self.checkActualDBForFloat(full_item_name=full_item_name, floatvalue=floatvalue)
        #if actual_skinport_check is not None and actual_skinport_check != 1337:
        #    return actual_skinport_check
        #####try with only skinport floatdb check
        
        table_name = "full_floats_" + str(self.high_low)
        query1 = "SELECT float, rank FROM " + str(table_name)
        query2 = " WHERE market_hash_name = %s"
        query = query1 + query2

        with topfloatdb.db_cursor() as cur:
            cur.execute(query, (full_item_name,))
            top5_floats = cur.fetchall()

            if len(top5_floats) == 0:
                logger.error("NO TOP 5 FLOATS FOUND FOR: " + str(full_item_name))
                return None

            for topfloat in top5_floats:
                if self.high_low == "high":
                    if floatvalue > topfloat[0]:
                        return topfloat[1]
                if self.high_low == "low":
                    if floatvalue < topfloat[0]:
                        return topfloat[1]
        return 1337

    def singleCheckCsgofloatRank(self, market_url, single_full_item_name, single_inspect_link, m_to_find, price):
        #return None
        ########A anders mal
        end = False
        repeat_counter = 0
        repeat_counter_429 = 0
        while not end:
            repeat_counter += 1
            if repeat_counter > 3:
                logger.critical("Couldnt find skin after 3 times, giving up... " + str(single_inspect_link))
                if repeat_counter_429 > 0:
                    return "neger"
                else:
                    return None
            logger.error("SKIN WANTED: " + str(single_inspect_link))
        
            options = webdriver.ChromeOptions()
            options.add_argument('--ignore-ssl-errors=yes')
            options.add_argument('--ignore-certificate-errors')
            options.add_argument("--user-data-dir=/tmp/chrome_profiles2712")
            options.add_argument('--blink-settings=imagesEnabled=false')
            options.add_argument('--disable-dev-shm-usage')
            options.add_argument('--no-sandbox')


            iproyal_host = "geo.iproyal.com"
            iproyal_port = 12321
            iproyal_user = "alex133769"
            iproyal_pass = "mArgare1he_region-europe"

            options.add_extension(proxies(iproyal_user, iproyal_pass, iproyal_host, iproyal_port))
            try:
                driver = webdriver.Remote(
                    #command_executor='http://37.252.188.127:4567/wd/hub',
                    command_executor='http://23.88.122.57:4444/wd/hub',
                    options=options
                )
                #M TO FIND ÃƒÆ’Ã¢â‚¬Å¾NDERN!!!!!!!!!!!!!!!!!!
                #newsingle(driver=driver, single_full_item_name="StatTrakÃƒÂ¢Ã¢â‚¬Å¾Ã‚Â¢%20AK-47%20%7C%20Ice%20Coaled%20(Battle-Scarred)", m_to_find="4435542821859547986")
            except Exception as e:
                logger.critical("seleniumshit: " + str(e))
                time.sleep(10)
                continue
            
            try:
                driver.get(market_url)
                time.sleep(10)
            except Exception as e:
                logger.critical("seleniumshit: " + str(e))
                time.sleep(10)
                continue
            
            
            findby = "listing_" + m_to_find
            csfloat_item_row_wrappers = driver.find_elements(By.CLASS_NAME, findby)

            if len(csfloat_item_row_wrappers) == 0:
                logger.critical("429 OR SKIN NOT FOUND, TRYING AGAIN " + str(market_url))
                try:
                    driver.close()
                    driver.quit()
                except Exception as e:
                    logger.critical("Browser already closed")
                continue

            for wrapper in csfloat_item_row_wrappers:
                rank = re.findall("Rank #([0-9]*)", str(wrapper.text))
                if len(rank) == 0:
                    logger.critical("ITEM FOUND, BUT NO RANK FOR " + str(single_full_item_name) + " / " + str(m_to_find))
                    try:
                        driver.close()
                        driver.quit()
                    except Exception as e:
                        logger.critical("Browser already closed")
                    continue
                rank = rank[0]
                logger.critical(str(single_full_item_name) + ": #" + str(rank) + " found.")

                html = wrapper.get_attribute("outerHTML")
                soup = BeautifulSoup(html, "html.parser")
                try:
                    inspect_link = soup.find("a", {"class": "csfloat-easy-inspect"}).get("href")
                except Exception as e:
                    logger.critical("Couldn't get inspect link, WAT: " + str(e))
                
                m_s_from_inspect = HelperFunctions.returnMorSfromInspectLink(inspect_link)
                final_listing = FinalListing(full_item_name=single_full_item_name, market_url=str(market_url), inspect_link=inspect_link, rank=str(rank), price=price)
                
                try:
                    driver.close()
                    driver.quit()
                except Exception as e:
                    logger.critical("Browser already closed")

                if int(rank) < 6:
                    return final_listing

                HelperFunctions.insertShittyRankSupaBase(final_listing.inspect_link, final_listing.rank)

                return None


    def singleCheckForPotentialBuy(self, final_listing: FinalListing, inspect_link="", 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.getRequest("https://steamcommunity.com/market/priceoverview/?market_hash_name=" + HelperFunctions.encodeAidsItemName(final_listing.full_item_name) + "&appid=730&currency=3", False)
                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("Exception in singleCheckForPotentialBuy: " + 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.getRequest(
                    "https://steamcommunity.com/market/listings/730/"
                    + str(final_listing.full_item_name)
                )
                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.getRequest(nameid_url, False)
                    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 inspect_link != "" and rank == 1337:
                        try:
                            postgresql_conn = psycopg2.connect(database="postgres", user = "postgres", password = "Berufsorientierung1!", host = "23.88.122.57", port = "5432")
                            postgresql_cur = postgresql_conn.cursor()
                            postgresql_cur.execute('INSERT INTO floats(inspect_link, rank) VALUES(%s, %s)', (inspect_link, rank,))
                            postgresql_conn.commit()
                            postgresql_cur.close()
                        except (Exception, psycopg2.DatabaseError) as error:
                            print(error)
                        finally:
                            if postgresql_conn is not None:
                                postgresql_conn.close()
            except ValueError:
                logger.critical("ValueError in singleCheckForPotentialBuy: " + str(e))
            end = True


    def testInspectLinks(self):
        self.high_low = "low"
        self.listings_url_set.add("https://steamcommunity.com/market/listings/730/SCAR-20%20%7C%20Palm%20(Factory%20New)/render/?query=")
        self.buildInspectLinksList()
        self.getBestOrWorstSkinsBulk()

    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 startBotSingleSkin(self, listings_url, greater_than, lower_than):
        self.listings_url_set.add(listings_url)
        self.extended_scrape = True
        self.buildInspectLinksList()
        logger.info(
            "2/3 SINGLE Build Inspect Links List finished ("
            + str(self.weapon_name)
            + ")"
        )

        self.getSkinsWithDefinedValue(greater_than, lower_than)
        logger.info("3/3 SINGLE Get top skins finished (" + str(self.weapon_name) + ")")

    def testFun(self, aids):
        logger.critical(aids)
