ScrappyCocco / HowLongToBeat-PythonAPI

A simple Python API to read data from howlongtobeat
https://pypi.org/project/howlongtobeatpy/
MIT License
83 stars 5 forks source link

[BUG] API broke again #31

Open Eniterusx opened 5 hours ago

Eniterusx commented 5 hours ago

Looks like howlongtobeat changed the api yet again, despite them changing it like 2 days ago...

First of all, they returned to the api_key solution, however this time it consists of two concats instead of one. /api/search/".concat("7b0f03b2").concat("54cc3099") Here's the quick code I whipped to make the send_website_request_getcode retriever work again:

if script_resp.status_code == 200 and script_resp.text is not None:
    pattern = r'\/api\/search\/"(?:\.concat\("[^"]*"\))*'
    matches = re.findall(pattern, script_resp.text)
    matches = str(matches).split('.concat')
    matches = [re.sub(r'["\(\)\[\]\']', '', match) for match in matches[1:]]
    key = ''.join(matches)
    return key

Also the headers and payload changed ever so slightly. Those worked for me:

headers = {
    'Host': 'howlongtobeat.com',
    'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:132.0) Gecko/20100101 Firefox/132.0',
    'Accept': '*/*',
    'Accept-Language': 'en-US,en;q=0.5',
    'Accept-Encoding': 'gzip, deflate, br, zstd',
    'Referer': 'https://howlongtobeat.com',
    'Content-Type': 'application/json',
    'Origin': 'https://howlongtobeat.com',
    'Connection': 'keep-alive'
}

payload = {
    'searchType': "games",
    'searchTerms': game_name.split(),
    'searchPage': page,
    'size': 20,
    'searchOptions': {
        'games': {
            'userId': 0,
            'platform': "",
            'sortCategory': "popular",
            'rangeCategory': "main",
            'rangeTime': {
                'min': 0,
                'max': 0
            },
            'gameplay': {
                'perspective': "",
                'flow': "",
                'genre': "",
                'subGenre': ""
            },
            'modifier': search_modifiers.value,
        },
        'users': {
            'sortCategory': "postcount"
        },
        'lists': {
            'sortCategory': "follows"
        },
        'filter': "",
        'sort': 0,
        'randomizer': 0
    }
}

I guess the freshly added solution should still be kept, in case they change the api yet again. Possibly first try the api key, then the custom userId in case the key doesn't work?

ScrappyCocco commented 5 hours ago

It's starting to get a little bothering that they keep changing the format, I'll have a look as soon as I have time guys, sorry for the wait