-
Notifications
You must be signed in to change notification settings - Fork 4
/
tagnames.py
57 lines (45 loc) · 1.75 KB
/
tagnames.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import requests
import json
import os
#this script creates a locally stored list of tags
#To know which TAGS might work in the Model tag search and which do not.
#Because there is also stuff on the list from the API that are not working. e.g. comma seperated words
#adds only new tags that are not yet present in the tag_names.txt file
def fetch_data(url):
headers = {"Content-Type": "application/json"}
response = requests.get(url, headers=headers)
if response.status_code == 200:
try:
data = response.json()
except json.JSONDecodeError as e:
print(f"Invalid JSON-data: {str(e)}")
return response.json()
else:
print(f"Error while retrieving data, Status-Code: {response.status_code}")
return None
def process_data(items, file_path, read_existing_tag):
with open(file_path, 'a', encoding='utf-8') as file:
for item in items:
name = item.get('name')
if name and name not in existing_tags:
file.write(name+ '\n')
existing_tags.add(name)
def read_existing_tag(file_path):
try:
with open(file_path, 'r', encoding='utf-8') as file:
return {line.strip() for line in file}
except FileNotFoundError:
return set()
file_path = os.path.join(os.getcwd(), 'tag_names.txt')
existing_tags = read_existing_tag(file_path)
url = "https://civitai.com/api/v1/tags?limit=200"
while url:
data = fetch_data(url)
if data:
items = data.get('items', [])
process_data(items, file_path, read_existing_tag)
existing_tags.update(item.get('name') for item in items if item.get('name'))
metadata = data.get('metadata', {})
url = metadata.get('nextPage')
else:
url = None