|
import requests |
|
import json |
|
|
|
def get_middle(val1, val2): |
|
return int((val1+val2)/2) |
|
|
|
|
|
def page_exists(url, API_KEY): |
|
headers = { |
|
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36', |
|
'CH-API-KEY': API_KEY} |
|
|
|
text_result = requests.get(url, headers=headers).text |
|
json_result = json.loads(text_result) |
|
|
|
if json_result["data"]["nodes"]: |
|
return True |
|
else: |
|
return False |
|
|
|
def find_page(url, max_value, API_KEY): |
|
max_possible_result = max_value |
|
|
|
next_to_check = max_possible_result |
|
max_good_result = -1 |
|
min_none_result = 9999 |
|
count = 0 |
|
|
|
while True: |
|
|
|
if page_exists(replace_page_number(url, next_to_check), API_KEY): |
|
print("+ found characters at page ", next_to_check) |
|
if max_good_result < next_to_check: |
|
max_good_result = next_to_check |
|
if max_good_result != -1 and min_none_result != 9999: |
|
next_to_check = get_middle(max_good_result, min_none_result) |
|
else: |
|
next_to_check = int(next_to_check + next_to_check/2) |
|
|
|
else: |
|
print("- no characters at page ", next_to_check) |
|
if min_none_result > next_to_check: |
|
min_none_result = next_to_check |
|
if max_good_result != -1 and min_none_result != 9999: |
|
next_to_check = get_middle(max_good_result, min_none_result) |
|
else: |
|
next_to_check = int(next_to_check/2) |
|
count += 1 |
|
|
|
if max_good_result == next_to_check: |
|
print("\n[VALUE FOUND]: ", next_to_check) |
|
print("[FINISHED IN]: ", count, " turns!") |
|
break |
|
|
|
|
|
def replace_page_number(url, new_page): |
|
base_url, query_string = url.split('?', 1) |
|
query_params = query_string.split('&') |
|
updated_params = [] |
|
for param in query_params: |
|
if param.startswith('page='): |
|
updated_params.append(f'page={new_page}') |
|
else: |
|
updated_params.append(param) |
|
|
|
updated_query_string = '&'.join(updated_params) |
|
return f"{base_url}?{updated_query_string}" |
|
|
|
test = "https://api.chub.ai/search?excludetopics=&first=20&page=1&namespace=characters&search=&include_forks=true&nsfw=true&nsfw_only=false&require_custom_prompt=false&require_images=false&require_expressions=false&nsfl=true&asc=false&min_tokens=50&max_tokens=100000&chub=true&require_lore=false&exclude_mine=true&require_lore_embedded=false&require_lore_linked=false&sort=default&topics=Female,Redhead&venus=true&count=false" |
|
|
|
|