def main():
    query = input('Search Query: ')
    max_tw = int(input('Tweet Count: '))
    CK = os.getenv('TW_CK')
    CS = os.getenv('TW_CS')
    auth = tweepy.AppAuthHandler(CK, CS)
    api = tweepy.API(auth)
    got = 0
    filtered = 0
    saved = 0
    lookup_ids = []
    replies = {}
    max_id = None
    while (saved <= max_tw):
        try:
            statuses = api.search_tweets(q=query, lang='ja', count=100, max_id=max_id)
            max_id = statuses[(- 1)].id
            for status in statuses:
                got += 1
                if (not status.in_reply_to_status_id):
                    continue
                if (not is_valid_tweet(status)):
                    continue
                lookup_ids.append(status.in_reply_to_status_id)
                replies[status.in_reply_to_status_id] = Tweet(status)
                filtered += 1
                print(f'''
{got} => {filtered} => {saved}''', end='')
                if (len(lookup_ids) >= 100):
                    pstatuses = api.lookup_statuses(lookup_ids)
                    for pstatus in pstatuses:
                        if (not is_valid_tweet(pstatus)):
                            continue
                        reply = replies[pstatus.id]
                        if (pstatus.user.id == reply.user_id):
                            continue
                        intext = re.sub('@([A-Za-z0-9_]+)', '', pstatus.text)
                        intext = normalize(intext)
                        outtext = re.sub('@([A-Za-z0-9_]+)', '', reply.text)
                        outtext = normalize(outtext)
                        if ((not intext) or (not outtext)):
                            continue
                        if (f'''{intext}	{outtext}
''' in dup):
                            continue
                        if (saved <= (max_tw * 0.9)):
                            path = TRAINFILE
                        elif (saved <= (max_tw * 0.95)):
                            path = DEVFILE
                        else:
                            path = TESTFILE
                        with open(path, 'a') as f:
                            f.write(f'''{intext}	{outtext}
''')
                        saved += 1
                        print(f'''
{got} => {filtered} => {saved}''', end='')
                        if (saved > max_tw):
                            exit()
                    lookup_ids = []
                    replies = {}
        except Exception:
            print()
            limit_status = api.rate_limit_status()['resources']['search']['/search/tweets']
            while (limit_status['reset'] >= int(time.time())):
                print((('\rLimited: ' + ('   ' + str((limit_status['reset'] - int(time.time()))))[(- 3):]) + 's'), end='')
                time.sleep(0.5)
            print()
def main():
    query = input('Search Query: ')
    max_tw = int(input('Tweet Count: '))
    CK = os.getenv('TW_CK')
    CS = os.getenv('TW_CS')
    auth = tweepy.AppAuthHandler(CK, CS)
    api = tweepy.API(auth)
    got = 0
    filtered = 0
    saved = 0
    lookup_ids = []
    replies = {}
    max_id = None
    while (saved <= max_tw):
        try:
            statuses = api.search_tweets(q=query, lang='ja', count=100, max_id=max_id)
            max_id = statuses[(- 1)].id
            for status in statuses:
                got += 1
                if (not status.in_reply_to_status_id):
                    continue
                if (not is_valid_tweet(status)):
                    continue
                lookup_ids.append(status.in_reply_to_status_id)
                replies[status.in_reply_to_status_id] = Tweet(status)
                filtered += 1
                print(f'
{got} => {filtered} => {saved}', end=)
                if (len(lookup_ids) >= 100):
                    pstatuses = api.lookup_statuses(lookup_ids)
                    for pstatus in pstatuses:
                        if (not is_valid_tweet(pstatus)):
                            continue
                        reply = replies[pstatus.id]
                        if (pstatus.user.id == reply.user_id):
                            continue
                        intext = re.sub('@([A-Za-z0-9_]+)', , pstatus.text)
                        intext = normalize(intext)
                        outtext = re.sub('@([A-Za-z0-9_]+)', , reply.text)
                        outtext = normalize(outtext)
                        if ((not intext) or (not outtext)):
                            continue
                        if (f'{intext}	{outtext}
' in dup):
                            continue
                        if (saved <= (max_tw * 0.9)):
                            path = TRAINFILE
                        elif (saved <= (max_tw * 0.95)):
                            path = DEVFILE
                        else:
                            path = TESTFILE
                        with open(path, 'a') as f:
                            f.write(f'{intext}	{outtext}
')
                        saved += 1
                        print(f'
{got} => {filtered} => {saved}', end=)
                        if (saved > max_tw):
                            exit()
                    lookup_ids = []
                    replies = {}
        except Exception:
            print()
            limit_status = api.rate_limit_status()['resources']['search']['/search/tweets']
            while (limit_status['reset'] >= int(time.time())):
                print((('\rLimited: ' + ('   ' + str((limit_status['reset'] - int(time.time()))))[(- 3):]) + 's'), end=)
                time.sleep(0.5)
            print()