Last active
June 11, 2025 01:26
-
-
Save mofosyne/8c9835f829d6103b1024c4509aa99a95 to your computer and use it in GitHub Desktop.
item part basic product search
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| # Item API Basic Product Search | |
| import urllib.request # Query API | |
| import json # Parsing API response | |
| import os # For cache support | |
| def get_url_content(url: str) -> str: | |
| """ | |
| Retrieves the content of a given URL as a string. | |
| Args: | |
| url (str): The URL to retrieve content from. | |
| Returns: | |
| str: The content of the response. | |
| """ | |
| try: | |
| headers = { | |
| 'User-Agent': 'Mozilla/5.0' | |
| } | |
| request = urllib.request.Request(url, headers=headers) | |
| response = urllib.request.urlopen(request) | |
| if not response.getcode() == 200: | |
| raise Exception(f"Failed to retrieve content. Status code: {response.getcode()}") | |
| return response.read().decode('utf-8') | |
| except urllib.error.HTTPError as e: | |
| print(f"HTTP Error: {e}") | |
| return None | |
| except Exception as e: | |
| print(f"An error occurred: {e}") | |
| return None | |
| def get_url_json_api(url: str) -> dict: | |
| """ | |
| Retrieves JSON content from a given URL. | |
| Args: | |
| url (str): The URL to retrieve JSON content from. | |
| Returns: | |
| dict: The parsed JSON content. | |
| """ | |
| received_content = get_url_content(url) | |
| if not received_content: | |
| return None | |
| try: | |
| return json.loads(received_content) | |
| except json.JSONDecodeError as e: | |
| print(f"Failed to parse JSON. Error: {e}") | |
| return None | |
| def get_item_product_data(id: str, cache_dir: str = None) -> dict: | |
| # id input can accept "0.0.448.04" or "0044804" or "44804" | |
| # and convert to api id internal format of "44804" | |
| id = int(id.replace('.', '')) | |
| api_id = str(id) | |
| # If cache exists, load it | |
| if cache_dir: | |
| cache_file = os.path.join(cache_dir, f"{api_id}.json") | |
| if os.path.exists(cache_file): | |
| with open(cache_file, 'r', encoding='utf-8') as f: | |
| try: | |
| json_content = json.load(f) | |
| if json_content.get("id", None) == api_id: | |
| return json_content | |
| except json.JSONDecodeError: | |
| print(f"Warning: Cache file {cache_file} is corrupted. Refetching...") | |
| # Not cached. Fetch | |
| api_url = f"https://api.item24.com/product-data/products/{api_id}" | |
| json_content = get_url_json_api(api_url) | |
| # Save to cache | |
| if cache_dir: | |
| with open(cache_file, 'w', encoding='utf-8') as f: | |
| json.dump(json_content, f, indent=2) | |
| return json_content | |
| # Call ITEM API for 0.0.026.33 | |
| json_content = get_item_product_data("0.0.026.33") | |
| # Print Interesting Data | |
| row = {} | |
| row["Part Number"] = json_content.get("formattedId") | |
| if "title" in json_content and "en" in json_content["title"] : | |
| row["Title"] = json_content["title"]["en"] | |
| if "texts" in json_content and "descriptionShort" in json_content["texts"] and "en" in json_content["texts"]["descriptionShort"]: | |
| row["Description"] = json_content["texts"]["descriptionShort"]["en"] | |
| if "slug" in json_content and "en" in json_content["slug"] : | |
| row["Web Link"] = "https://www.item24.com/en-au/" + json_content.get("slug").get("en") | |
| print(json.dumps(row, indent=2)) | |
| """ | |
| Expected Output: | |
| { | |
| "Part Number": "0.0.026.33", | |
| "Title": "Profile 8 40x40 light, natural", | |
| "Description": "item Profile 8 40x40 is suitable for all kinds of fastening options. ESD-safe and approved under Directive 2002/95/EC.", | |
| "Web Link": "https://www.item24.com/en-au/profile-8-40x40-light-natural-2633" | |
| } | |
| """ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment