autoforge 1.1.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,123 @@
1
+ import os
2
+ import json
3
+ import requests
4
+ from tqdm import tqdm
5
+
6
+ # API endpoints
7
+ API_VERSION_URL = "https://filamentcolors.xyz/api/version/"
8
+ SWATCH_API_URL = "https://filamentcolors.xyz/api/swatch/?m=manufacturer"
9
+
10
+ # Local file names
11
+ DATA_FILE = "swatches.json"
12
+ VERSION_FILE = "swatches_version.json"
13
+
14
+ def get_api_version():
15
+ """
16
+ Fetches the current version information from the API.
17
+
18
+ Sends a GET request to the API version endpoint and returns the JSON response.
19
+
20
+ Returns:
21
+ dict: A dictionary containing the version information from the API.
22
+
23
+ Raises:
24
+ requests.RequestException: If the request to the API fails.
25
+ """
26
+ response = requests.get(API_VERSION_URL)
27
+ response.raise_for_status()
28
+ return response.json()
29
+
30
+ def load_local_version():
31
+ """
32
+ Loads the locally saved version info if it exists.
33
+
34
+ Checks if the version file exists in the local file system. If it does,
35
+ opens the file, reads the JSON content, and returns it as a dictionary.
36
+
37
+ Returns:
38
+ dict or None: A dictionary containing the version information if the file exists,
39
+ otherwise None.
40
+ """
41
+ if os.path.exists(VERSION_FILE):
42
+ with open(VERSION_FILE, 'r') as f:
43
+ return json.load(f)
44
+ return None
45
+ def save_local_version(version_info):
46
+ """
47
+ Saves the version info locally.
48
+
49
+ Args:
50
+ version_info (dict): A dictionary containing the version information to be saved.
51
+ """
52
+ with open(VERSION_FILE, 'w') as f:
53
+ json.dump(version_info, f)
54
+
55
+ def download_all_pages(url):
56
+ """
57
+ Downloads all pages of swatch data from the given URL.
58
+
59
+ This function sends GET requests to the provided URL and continues to follow
60
+ pagination links (if any) to download all pages of swatch data. The results
61
+ from each page are combined into a single list.
62
+
63
+ Args:
64
+ url (str): The initial URL to start downloading the swatch data from.
65
+
66
+ Returns:
67
+ list: A list containing all swatch data from all pages.
68
+
69
+ Raises:
70
+ requests.RequestException: If any of the requests to the URL fail.
71
+ """
72
+ all_results = []
73
+ tbar = tqdm(desc="Downloading", unit="swatches page")
74
+ while url:
75
+ response = requests.get(url)
76
+ response.raise_for_status()
77
+ data = response.json()
78
+ all_results.extend(data.get("results", []))
79
+ url = data.get("next")
80
+ tbar.update(1)
81
+ return all_results
82
+
83
+ def main():
84
+ """
85
+ Main function to manage the swatch data update process.
86
+
87
+ This function performs the following steps:
88
+ 1. Retrieves the current API version details.
89
+ 2. Loads the local version info if available.
90
+ 3. Checks if the local data is up-to-date with the API version.
91
+ 4. Downloads new data if the local copy is outdated or missing.
92
+ 5. Saves the downloaded data and updates the local version info.
93
+
94
+ Raises:
95
+ requests.RequestException: If any of the requests to the API fail.
96
+ """
97
+ try:
98
+ api_version = get_api_version()
99
+ except requests.RequestException as e:
100
+ print("Failed to retrieve API version:", e)
101
+ return
102
+
103
+ local_version = load_local_version()
104
+ if local_version and local_version.get("db_last_modified") == api_version.get("db_last_modified"):
105
+ print("Data is up-to-date. No download needed.")
106
+ return
107
+
108
+
109
+ print("Downloading new filament color data...")
110
+ try:
111
+ results = download_all_pages(SWATCH_API_URL)
112
+ except requests.RequestException as e:
113
+ print("Error downloading swatch data:", e)
114
+ return
115
+
116
+
117
+ with open(DATA_FILE, 'w') as f:
118
+ json.dump(results, f)
119
+
120
+ save_local_version(api_version)
121
+ print("Download complete, data saved to", DATA_FILE)
122
+ if __name__ == "__main__":
123
+ main()