def main():
    status('Fetching latest paste IDs...')
    fetch_limit = 100
    current_request = requests.get('https://scrape.pastebin.com/api_scraping.php?limit={0}'.format(fetch_limit))
    current_json = []
    try:
        current_json = current_request.json()
    except decoder.JSONDecodeError:
        status(termcolor.colored('Unable to fetch latest paste IDs. Make sure your IP is whitelisted at https://pastebin.com/doc_scraping_api', 'red'))
        cleanup()
        exit(0)
    status('Paste IDs fetched. Processing...')
    cleaned_json = []
    for entry in current_json:
        if (entry['key'] not in paste_ids):
            cleaned_json.append(entry)
    if (len(cleaned_json) is not 0):
        with Bar('Processing', max=len(cleaned_json), fill='>') as bar:
            for entry in cleaned_json:
                entry_request = requests.get('https://scrape.pastebin.com/api_scrape_item.php?i={0}'.format(entry['key']))
                entry_content = entry_request.text
                path_file = path.join('files', '{0}.txt'.format(entry['key']))
                paste_ids.append(entry['key'])
                if (keywords is not None):
                    for keyword in keywords:
                        if (keyword.upper() in entry_content.upper()):
                            bar.suffix = ('%(index)d/%(max)d ' + termcolor.colored("[KEYWORD] Paste '{0}' contains keyword '{1}'".format(entry['key'], keyword), 'green'))
                            if (args.noSorting is False):
                                path_file = path.join('files', keyword, '{0}.txt'.format(entry['key']))
                            with open(path_file, 'w+', encoding='utf-8') as entry_file:
                                entry_file.write(entry_content)
                            break
                else:
                    with open(path_file, 'w+', encoding='utf-8') as entry_file:
                        entry_file.write(entry_content)
                    bar.suffix = "%(index)d/%(max)d Saving paste '{0}'".format(entry['key'])
                bar.next()
        bar.finish()
    else:
        status('No new pastes found, skipping downloads...')
    if (args.infinite is False):
        if (not isfile('runfile')):
            print()
            status('Runfile no longer found, exiting...')
            exit(0)
    skipped_pastes = (fetch_limit - len(cleaned_json))
    if (skipped_pastes != 0):
        status('Skipped {0} previously fetched pastes'.format(skipped_pastes))
    status('Cleaning up internal ID list...')
    while (len(paste_ids) > max_id_list_size):
        paste_ids.pop(0)
    status('Hibernating for 60 seconds...')
    with Bar('Hibernating', max=60, fill='>', suffix='') as bar:
        for i in range(60):
            sleep(1)
            bar.next()
        bar.finish()
    print()
    Thread(main()).start()
def main():
    status('Fetching latest paste IDs...')
    fetch_limit = 100
    current_request = requests.get('https://scrape.pastebin.com/api_scraping.php?limit={0}'.format(fetch_limit))
    current_json = []
    try:
        current_json = current_request.json()
    except decoder.JSONDecodeError:
        status(termcolor.colored('Unable to fetch latest paste IDs. Make sure your IP is whitelisted at https://pastebin.com/doc_scraping_api', 'red'))
        cleanup()
        exit(0)
    status('Paste IDs fetched. Processing...')
    cleaned_json = []
    for entry in current_json:
        if (entry['key'] not in paste_ids):
            cleaned_json.append(entry)
    if (len(cleaned_json) is not 0):
        with Bar('Processing', max=len(cleaned_json), fill='>') as bar:
            for entry in cleaned_json:
                entry_request = requests.get('https://scrape.pastebin.com/api_scrape_item.php?i={0}'.format(entry['key']))
                entry_content = entry_request.text
                path_file = path.join('files', '{0}.txt'.format(entry['key']))
                paste_ids.append(entry['key'])
                if (keywords is not None):
                    for keyword in keywords:
                        if (keyword.upper() in entry_content.upper()):
                            bar.suffix = ('%(index)d/%(max)d ' + termcolor.colored("[KEYWORD] Paste '{0}' contains keyword '{1}'".format(entry['key'], keyword), 'green'))
                            if (args.noSorting is False):
                                path_file = path.join('files', keyword, '{0}.txt'.format(entry['key']))
                            with open(path_file, 'w+', encoding='utf-8') as entry_file:
                                entry_file.write(entry_content)
                            break
                else:
                    with open(path_file, 'w+', encoding='utf-8') as entry_file:
                        entry_file.write(entry_content)
                    bar.suffix = "%(index)d/%(max)d Saving paste '{0}'".format(entry['key'])
                bar.next()
        bar.finish()
    else:
        status('No new pastes found, skipping downloads...')
    if (args.infinite is False):
        if (not isfile('runfile')):
            print()
            status('Runfile no longer found, exiting...')
            exit(0)
    skipped_pastes = (fetch_limit - len(cleaned_json))
    if (skipped_pastes != 0):
        status('Skipped {0} previously fetched pastes'.format(skipped_pastes))
    status('Cleaning up internal ID list...')
    while (len(paste_ids) > max_id_list_size):
        paste_ids.pop(0)
    status('Hibernating for 60 seconds...')
    with Bar('Hibernating', max=60, fill='>', suffix=) as bar:
        for i in range(60):
            sleep(1)
            bar.next()
        bar.finish()
    print()
    Thread(main()).start()