# Buggy code
def trending(self, count=30, language="en", region="US", proxy=None) -> dict:
    """
    Gets trending TikToks
    """
    response = []
    maxCount = 50
    maxCursor = 0
    first = True

    while len(response) < count:
        if count < maxCount:
            realCount = count
        else:
            realCount = maxCount

        query = {
            "count": realCount,
            "id": 1,
            "type": 5,
            "secUid": "",
            "maxCursor": maxCursor,
            "minCursor": 0,
            "sourceType": 12,
            "appId": 1233,
            "region": region,
            "language": language,
        }
        api_url = "{}api/item_list/?{}&{}".format(
            BASE_URL, self.__add_new_params__(), urlencode(query)
        )
        b = browser(api_url, language=language, proxy=proxy)
        res = self.getData(b, proxy=proxy)

        if "items" in res.keys():
            for t in res["items"]:
                response.append(t)

        if not res["hasMore"] and not first:
            if self.debug:
                print("TikTok isn't sending more TikToks beyond this point.")
            return response

        realCount = count - len(response)
        maxCursor = res["maxCursor"]

        first = False

    return response[:count]


# Traceback (json.decoder.JSONDecodeError)
api.byHashtag('love')

{'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36', 'accept-encoding': 'gzip, deflate, br', 'accept': 'application/json, text/plain, */*', 'Connection': 'keep-alive', 'authority': 'm.tiktok.com', 'method': 'GET', 'path': '/share/item/list?aid=1988&amp;app_name=tiktok_web&amp;device_platform=web&amp;referer=&amp;user_agent=Mozilla%2F5.0+(Windows+NT+10.0%3B+Win64%3B+x64)+AppleWebKit%2F537.36+(KHTML,+like+Gecko)+Chrome%2F84.0.4147.125+Safari%2F537.36&amp;cookie_enabled=true&amp;screen_width=2560&amp;screen_height=1440&amp;browser_language=&amp;browser_platform=&amp;browser_name=&amp;browser_version=&amp;browser_online=true&amp;timezone_name=&amp;priority_region=&amp;appId=1233&amp;appType=m&amp;isAndroid=false&amp;isMobile=false&amp;isIOS=false&amp;OS=windows&amp;did=822148675&amp;region=US&amp;secUid=&amp;id=4231&amp;type=3&amp;count=30&amp;minCursor=0&amp;maxCursor=0&amp;shareUid=&amp;recType=&amp;lang=en&amp;verifyFp=AaN99VturDQ2&amp;_signature=_02B4Z6wo00f01SDv8tgAAIBB6rMaD8GoX..ZAABdw74', 'scheme': 'https', 'accept-language': 'en-US,en;q=0.9', 'referrer': 'https://www.tiktok.com/', 'sec-fetch-dest': 'empty', 'sec-fetch-mode': 'cors', 'sec-fetch-site': 'same-site'}
Converting response to JSON failed response is below (probably empty)
Traceback (most recent call last):
File "~/PycharmProjects/backup_scrapers/vevn/lib/python3.8/site-packages/TikTokApi/tiktok.py", line 85, in getData
return r.json()
File "~/PycharmProjects/backup_scrapers/vevn/lib/python3.8/site-packages/requests/models.py", line 898, in json
return complexjson.loads(self.text, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "~/PycharmProjects/backup_scrapers/vevn/lib/python3.8/site-packages/TikTokApi/tiktok.py", line 357, in byHashtag
res = self.getData(api_url, b, proxy=proxy, language=language)
File "~/PycharmProjects/backup_scrapers/vevn/lib/python3.8/site-packages/TikTokApi/tiktok.py", line 91, in getData
raise Exception('Invalid Response')
Exception: Invalid Response

# Fixed code
def trending(self, count=30, language="en", region="US", proxy=None) -> dict:
    """
    Gets trending TikToks
    """
    response = []
    maxCount = 50
    maxCursor = 0
    first = True

    while len(response) < count:
        if count < maxCount:
            realCount = count
        else:
            realCount = maxCount

        query = {
            "count": realCount,
            "id": 1,
            "type": 5,
            "secUid": "",
            "maxCursor": maxCursor,
            "minCursor": 0,
            "sourceType": 12,
            "appId": 1233,
            "region": region,
            "priority_region": region,
            "language": language,
        }
        api_url = "{}api/item_list/?{}&{}".format(
            BASE_URL, self.__add_new_params__(), urlencode(query)
        )
        b = browser(api_url, language=language, proxy=proxy)
        res = self.getData(b, proxy=proxy)

        if "items" in res.keys():
            for t in res["items"]:
                response.append(t)

        if not res["hasMore"] and not first:
            if self.debug:
                print("TikTok isn't sending more TikToks beyond this point.")
            return response

        realCount = count - len(response)
        maxCursor = res["maxCursor"]

        first = False

    return response[:count]