import json, urllib.request, urllib.parse, time
KEY = open('/home/claude/.xkey').read().strip()
def search(q, cursor=None):
    u = "https://api.twitterapi.io/twitter/tweet/advanced_search?queryType=Latest&query=" + urllib.parse.quote(q) + ("&cursor=" + urllib.parse.quote(cursor) if cursor else "")
    for t in range(4):
        try:
            return json.load(urllib.request.urlopen(urllib.request.Request(u, headers={"X-API-Key": KEY, "User-Agent": "Mozilla/5.0"}), timeout=40))
        except Exception as e:
            err = e; time.sleep(2 * (t + 1))
    raise err
