0

i've been struggling for some time right now trying to find how to make this faster somehow

the code

def get_resellers(sellerid, price, userassetid, id):
    data = {"expectedCurrency":1,"expectedPrice":price, "expectedSellerId":sellerid,"userAssetId":userassetid}
    headers = {"X-CSRF-TOKEN":csrftoken}
    k = requests.post(f"https://economy.roblox.com/v1/purchases/products/{id}", data=data, headers=headers, cookies=cookies)

def check_price(id):
    while True:
        try:
            t0 = time.time()
            soup = BeautifulSoup(requests.get(f"https://www.roblox.com/catalog/{id}").content, 'html.parser')
            data_expected_price, data_expected_seller_id, data_userasset_id = soup.select_one('[data-expected-price]')['data-expected-price'], soup.select_one('[data-expected-seller-id]')['data-expected-seller-id'], soup.select_one('[data-lowest-private-sale-userasset-id]')['data-lowest-private-sale-userasset-id']
            t1 = time.time()
            total = t1 - t0
            print(total)
            if int(data_expected_price) < 0.7*int(data_expected_price):
                    get_resellers(data_expected_seller_id, data_expected_price, data_userasset_id, id)
        except:
                pass

is there any faster way to do it or extract the stuff, or make the http request etc. anything can help! also: it takes like 0.7 seconds to buy and price check since it needs to load the site everytime is there anyway to do it faster?

Max
  • 1
  • 3
  • Please explain what you are doing. Why `for i in range(sys.maxsize**10)`? It would be faster if you did not repeat the loop more times than there are atoms in the known universe... – zvone Aug 29 '20 at 08:09
  • yes thats almost a infinite for loop since i heard for loop has less opcode than while loop so it means its faster – Max Aug 29 '20 at 08:10
  • i used it instead of while loop – Max Aug 29 '20 at 08:10
  • 1
    What do you mean by _"faster"_? It never ends. It is not faster than anything. And no, I am certain a `while True` would not be slower in any way. – zvone Aug 29 '20 at 08:11
  • Cool :) Now, which part would you want to be faster? Not the whole thing, for sure, because that runs forever. And why are you repeating it forever anyways? – zvone Aug 29 '20 at 08:14
  • Do you want to keep trying until the `if` condition is satisfied? If so, you can add a `break` after the `print` to exit the loop. If you want to try repeatedly I'd suggest adding a `time.sleep(number_of_seconds)` at the end of the loop or you may find the server bans your IP. – snakecharmerb Aug 29 '20 at 08:14
  • no they dont ipban so im fine and i need it to be as fast as possible – Max Aug 29 '20 at 08:15
  • @zvone its basically for checking a items price and buying it if it dropped and i just want it to check the price as fast as possible – Max Aug 29 '20 at 08:16
  • 1
    OK, that makes sense, but two things: 1) is it not fast enough already? 2) I am not sure this is a fine thing to do. You are certainly breaking some sort of etiquette by repeating the request that often. This code could be classified as a DoS attack, with theoretically more severe consequences than an ip ban. – zvone Aug 29 '20 at 08:20
  • there are like 50 programs out there that do the same and alot of those are faster then mine also roblox doesnt take actions against it since it doesnt affect them since they got like a 100 trillion dataserver that can handle almost everything – Max Aug 29 '20 at 08:22
  • tbh I doubt there's much you can do in Python that will make much difference: I'd expect the bulk of the time to be taken up by the http requests. That said, there are two functions being called - `calculation` and `get_resellers` for which we can't see the code, so perhaps they could be improved. (I'm assuming that requests and BeautifulSoup are already optimal). – snakecharmerb Aug 29 '20 at 08:22
  • ill edit the code give me 1 sec – Max Aug 29 '20 at 08:24
  • edited the code – Max Aug 29 '20 at 08:24
  • also my friend has one that takes 100ms and mine takes like 300 or 200 so it could be improved his is also made in python – Max Aug 29 '20 at 08:25
  • any idea how i can improve it? – Max Aug 29 '20 at 08:35
  • 1) in check_price what's the purpose of the code before while True (i.e. it's not used)? 2) Since the code is an infinite loop until the price changes what is meant by the speed of the program (i.e. you mention 300 ms vs. 100 ms for a friend)? Is it the time taken to complete a single loop cycle? 3) stop condition seems to be equivalent to: `if int(data_expected_price) < 0.7*int(data_expected_price):`. How can this ever be True? – DarrylG Aug 29 '20 at 09:10
  • its the time taken to make the buy request , the request to get the elements and filtering the elements, and it is true if the price is under 70% of the original price. – Max Aug 29 '20 at 09:17
  • @Max--I don't see the original price variable or how formula uses it if it existed. – DarrylG Aug 29 '20 at 09:23
  • data_expected_price is the element that it takes from the price it states it here: data_expected_price = soup.select_one('[data-expected-price]')['data-expected-price'] – Max Aug 29 '20 at 09:29
  • @Max--I saw that code. Just pointing out that your calculation basically returns 0.7*data_expected_price. So `if int(data_expected_price) < int(calculation(data_expected_price)):` is equivalent to: `if int(data_expected_price) < int(0.7*int(data_expected_price)):` which would never to True. since you're taking 70% of the currently detected price rather than an original price. – DarrylG Aug 29 '20 at 09:39
  • that's not true, it does a while loop and keeps refreshing the price so if its like 200 and first it was 700 wich would return true since its under the 0.70*data_expected_price. then it would buy it. – Max Aug 29 '20 at 09:44
  • and if u meant that calculation function was useless yes i noticed i removed it thanks – Max Aug 29 '20 at 09:48
  • @Max--The question is is there a value that will make: ` if int(data_expected_price) < 0.7*int(data_expected_price):` True? Answer is no. You need ` if int(data_expected_price) < 0.7*int(original_price):`. Are you trying to make something like [Roblox+](https://chrome.google.com/webstore/detail/roblox%20/jfbnmfgkohlfclfnplnlenbalpppohkm?hl=en) (although no idea on how quickly it detects item updates? – DarrylG Aug 29 '20 at 09:52
  • no it's to auto buy a limited, and it calculates profit and if it can make profit from the price it went to it auto buys it and it resells it for the price that it was before the drop. – Max Aug 29 '20 at 09:57
  • is there any way that i can make it faster? – Max Aug 29 '20 at 12:11

0 Answers0