├── src
├── Techspecs.egg-info
│ ├── dependency_links.txt
│ ├── top_level.txt
│ ├── SOURCES.txt
│ └── PKG-INFO
└── techspecs
│ ├── __init__.py
│ └── techspecs.py
├── pyproject.toml
├── tests
├── test_get_all_brandlogos.py
├── test_machine_id_search.py
├── test_product_search.py
├── test_get_all_brands.py
├── test_get_all_categories.py
├── test_product_detail.py
└── test_techspecs_advanced_search.py
├── examples
├── machineid_search.py
├── product_search.py
├── get_all_brands.py
├── get_all_categories.py
├── product_detail.py
└── advanced_search.py
├── setup.cfg
├── LICENSE
└── README.md
/src/Techspecs.egg-info/dependency_links.txt:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/src/Techspecs.egg-info/top_level.txt:
--------------------------------------------------------------------------------
1 | techspecs
2 |
--------------------------------------------------------------------------------
/src/techspecs/__init__.py:
--------------------------------------------------------------------------------
1 | from .techspecs import *
2 |
--------------------------------------------------------------------------------
/pyproject.toml:
--------------------------------------------------------------------------------
1 | [build-system]
2 | requires = ["setuptools>=42"]
3 | build-backend = "setuptools.build_meta"
--------------------------------------------------------------------------------
/tests/test_get_all_brandlogos.py:
--------------------------------------------------------------------------------
1 | import techspecs
2 |
3 | # TechSpecs API base URL
4 | techspecs_base_url = "https://api.techspecs.io"
5 |
6 | # TechSpecs API bearer token
7 | techspecs_api_key = "your_techspecs_api_key"
8 |
9 | # Output mode ('raw' or 'pretty')
10 | mode = 'pretty'
11 |
12 | # Call TechSpecs API to get all brand logos
13 | try:
14 | response = techspecs.get_all_brand_logos(techspecs_base_url, techspecs_api_key, mode=mode)
15 | print(response)
16 | except Exception as e:
17 | print(f"An error occurred: {e}")
18 |
--------------------------------------------------------------------------------
/examples/machineid_search.py:
--------------------------------------------------------------------------------
1 | import techspecs
2 |
3 | # TechSpecs API base URL
4 | techspecs_base_url = "https://api.techspecs.io"
5 |
6 | # TechSpecs API bearer token
7 | techspecs_api_key = "your_techspecs_api_key"
8 |
9 | # Serial number of the Apple machine to look up machineid_or_codename ex iphone 11,6
10 | machine_id = "iphone 11,6"
11 |
12 | # Output mode ('raw' or 'pretty')
13 | mode = 'pretty'
14 |
15 | # Look up the Apple machine by its serial number
16 | try:
17 | response = techspecs.machine_id_search(techspecs_base_url, techspecs_api_key, machine_id, mode)
18 | print(response)
19 | except Exception as e:
20 | print(f"An error occurred: {e}")
21 |
--------------------------------------------------------------------------------
/tests/test_machine_id_search.py:
--------------------------------------------------------------------------------
1 | import techspecs
2 |
3 | # TechSpecs API base URL
4 | techspecs_base_url = "https://api.techspecs.io"
5 |
6 | # TechSpecs API bearer token
7 | techspecs_api_key = "your_techspecs_api_key"
8 |
9 | # Serial number of the Apple machine to look up machineid_or_codename ex iphone 11,6
10 | machine_id = "iphone 11,6"
11 |
12 | # Output mode ('raw' or 'pretty')
13 | mode = 'pretty'
14 |
15 | # Look up the Apple machine by its serial number
16 | try:
17 | response = techspecs.machine_id_search(techspecs_base_url, techspecs_api_key, machine_id, mode)
18 | print(response)
19 | except Exception as e:
20 | print(f"An error occurred: {e}")
21 |
--------------------------------------------------------------------------------
/setup.cfg:
--------------------------------------------------------------------------------
1 | [metadata]
2 | name = TechSpecs
3 | version = 0.0.0
4 | author = TechSpecs
5 | author_email = support@techspecs.io
6 | description = TechSpecs Python
7 | long_description = file: README.md
8 | long_description_content_type = text/markdown
9 | url = https://github.com/techspecs/techspecs-python
10 | project_urls =
11 | Bug Tracker = https://github.com/techspecs/techspecs-python/issues
12 | classifiers =
13 | Programming Language :: Python :: 3
14 | License :: OSI Approved :: MIT License
15 | Operating System :: OS Independent
16 |
17 | [options]
18 | package_dir =
19 | = src
20 | packages = find:
21 | python_requires = >=3.6
22 |
23 | [options.packages.find]
24 | where = src
25 |
--------------------------------------------------------------------------------
/examples/product_search.py:
--------------------------------------------------------------------------------
1 | import techspecs
2 | import json
3 |
4 | # TechSpecs API base URL
5 | techspecs_base_url = "https://api.techspecs.io"
6 |
7 | # TechSpecs API bearer token
8 | techspecs_api_key = "your_techspecs_api_key"
9 |
10 | query = {
11 | 'keyword': 'iPhone 13', # product name or version number to search
12 | 'category': '', # product category to search (e.g. 'Smartphones', 'Tablets', or leave empty to search all categories)
13 | 'page': 0 # page number to fetch results from
14 | }
15 |
16 | # Output mode ('raw' or 'pretty')
17 | mode = 'pretty'
18 |
19 | # Search for a product by name, version, or features
20 | try:
21 | response = techspecs.product_search(techspecs_base_url, query, techspecs_api_key, mode=mode)
22 | print(response)
23 | except Exception as e:
24 | print(f"An error occurred: {e}")
25 |
--------------------------------------------------------------------------------
/tests/test_product_search.py:
--------------------------------------------------------------------------------
1 | import techspecs
2 | import json
3 |
4 | # TechSpecs API base URL
5 | techspecs_base_url = "https://api.techspecs.io"
6 |
7 | # TechSpecs API bearer token
8 | techspecs_api_key = "your_techspecs_api_key"
9 |
10 | query = {
11 | 'keyword': 'iPhone 13', # product name or version number to search
12 | 'category': '', # product category to search (e.g. 'Smartphones', 'Tablets', or leave empty to search all categories)
13 | 'page': 0 # page number to fetch results from
14 | }
15 |
16 | # Output mode ('raw' or 'pretty')
17 | mode = 'pretty'
18 |
19 | # Search for a product by name, version, or features
20 | try:
21 | response = techspecs.product_search(techspecs_base_url, query, techspecs_api_key, mode=mode)
22 | print(response)
23 | except Exception as e:
24 | print(f"An error occurred: {e}")
25 |
--------------------------------------------------------------------------------
/src/Techspecs.egg-info/SOURCES.txt:
--------------------------------------------------------------------------------
1 | LICENSE
2 | README.md
3 | pyproject.toml
4 | setup.cfg
5 | src/TechSpecs.egg-info/PKG-INFO
6 | src/TechSpecs.egg-info/SOURCES.txt
7 | src/TechSpecs.egg-info/dependency_links.txt
8 | src/TechSpecs.egg-info/top_level.txt
9 | src/Techspecs.egg-info/PKG-INFO
10 | src/Techspecs.egg-info/SOURCES.txt
11 | src/Techspecs.egg-info/dependency_links.txt
12 | src/Techspecs.egg-info/top_level.txt
13 | src/techspecs/__init__.py
14 | src/techspecs/techspecs.py
15 | src/techspecs.egg-info/PKG-INFO
16 | src/techspecs.egg-info/SOURCES.txt
17 | src/techspecs.egg-info/dependency_links.txt
18 | src/techspecs.egg-info/top_level.txt
19 | tests/test_get_all_brandlogos.py
20 | tests/test_get_all_brands.py
21 | tests/test_get_all_categories.py
22 | tests/test_machine_id_search.py
23 | tests/test_product_detail.py
24 | tests/test_product_search.py
25 | tests/test_techspecs_advanced_search.py
--------------------------------------------------------------------------------
/examples/get_all_brands.py:
--------------------------------------------------------------------------------
1 | import techspecs
2 |
3 | # TechSpecs API base URL
4 | techspecs_base_url = "https://api.techspecs.io"
5 |
6 | # TechSpecs API bearer token
7 | techspecs_api_key = "your_techspecs_api_key"
8 |
9 | # Output mode ('raw' or 'pretty')
10 | mode = 'pretty'
11 |
12 | # Define function to validate parameters
13 | def validate_parameters(mode):
14 | if mode not in ['raw', 'pretty']:
15 | raise ValueError(f'Invalid mode: {mode}. Mode should be one of ["raw", "pretty"].')
16 |
17 | # Validate parameters
18 | try:
19 | validate_parameters(mode)
20 | except ValueError as e:
21 | print(f"Invalid parameters: {e}")
22 | exit()
23 |
24 | # Call TechSpecs API to get all brands
25 | try:
26 | response = techspecs.get_all_brands(techspecs_base_url, techspecs_api_key, mode=mode)
27 | print(response)
28 | except Exception as e:
29 | print(f"An error occurred: {e}")
30 |
--------------------------------------------------------------------------------
/tests/test_get_all_brands.py:
--------------------------------------------------------------------------------
1 | import techspecs
2 |
3 | # TechSpecs API base URL
4 | techspecs_base_url = "https://api.techspecs.io"
5 |
6 | # TechSpecs API bearer token
7 | techspecs_api_key = "your_techspecs_api_key"
8 |
9 | # Output mode ('raw' or 'pretty')
10 | mode = 'pretty'
11 |
12 | # Define function to validate parameters
13 | def validate_parameters(mode):
14 | if mode not in ['raw', 'pretty']:
15 | raise ValueError(f'Invalid mode: {mode}. Mode should be one of ["raw", "pretty"].')
16 |
17 | # Validate parameters
18 | try:
19 | validate_parameters(mode)
20 | except ValueError as e:
21 | print(f"Invalid parameters: {e}")
22 | exit()
23 |
24 | # Call TechSpecs API to get all brands
25 | try:
26 | response = techspecs.get_all_brands(techspecs_base_url, techspecs_api_key, mode=mode)
27 | print(response)
28 | except Exception as e:
29 | print(f"An error occurred: {e}")
30 |
--------------------------------------------------------------------------------
/examples/get_all_categories.py:
--------------------------------------------------------------------------------
1 | import techspecs
2 |
3 | # TechSpecs API base URL
4 | techspecs_base_url = "https://api.techspecs.io"
5 |
6 | # TechSpecs API bearer token
7 | techspecs_api_key = "your_techspecs_api_key"
8 |
9 | # Output mode ('raw' or 'pretty')
10 | mode = 'pretty'
11 |
12 | # Define constants
13 | DEFAULT_MODE = 'pretty'
14 | VALID_MODES = ['pretty', 'raw']
15 |
16 | # Validate parameters
17 | def validate_parameters(mode=DEFAULT_MODE):
18 | if mode not in VALID_MODES:
19 | raise ValueError(f'Invalid mode: {mode}. Mode should be one of {VALID_MODES}.')
20 |
21 | # Validate search parameters
22 | try:
23 | validate_parameters(mode=mode)
24 | except ValueError as e:
25 | print(f"Invalid search parameters: {e}")
26 | exit()
27 |
28 | # Call TechSpecs API to get all categories
29 | try:
30 | response = techspecs.get_all_categories(techspecs_base_url, techspecs_api_key, mode=mode)
31 | print(response)
32 | except Exception as e:
33 | print(f"An error occurred: {e}")
34 |
--------------------------------------------------------------------------------
/tests/test_get_all_categories.py:
--------------------------------------------------------------------------------
1 | import techspecs
2 |
3 | # TechSpecs API base URL
4 | techspecs_base_url = "https://api.techspecs.io"
5 |
6 | # TechSpecs API bearer token
7 | techspecs_api_key = "your_techspecs_api_key"
8 |
9 | # Output mode ('raw' or 'pretty')
10 | mode = 'pretty'
11 |
12 | # Define constants
13 | DEFAULT_MODE = 'pretty'
14 | VALID_MODES = ['pretty', 'raw']
15 |
16 | # Validate parameters
17 | def validate_parameters(mode=DEFAULT_MODE):
18 | if mode not in VALID_MODES:
19 | raise ValueError(f'Invalid mode: {mode}. Mode should be one of {VALID_MODES}.')
20 |
21 | # Validate search parameters
22 | try:
23 | validate_parameters(mode=mode)
24 | except ValueError as e:
25 | print(f"Invalid search parameters: {e}")
26 | exit()
27 |
28 | # Call TechSpecs API to get all categories
29 | try:
30 | response = techspecs.get_all_categories(techspecs_base_url, techspecs_api_key, mode=mode)
31 | print(response)
32 | except Exception as e:
33 | print(f"An error occurred: {e}")
34 |
--------------------------------------------------------------------------------
/examples/product_detail.py:
--------------------------------------------------------------------------------
1 | import techspecs
2 |
3 | # TechSpecs API base URL
4 | techspecs_base_url = "https://api.techspecs.io"
5 |
6 | # TechSpecs API bearer token
7 | techspecs_api_key = "your_techspecs_api_key"
8 |
9 | # TechSpecs product ID
10 | techspecs_product_id = "63e96260ff7af4b68a304e40"
11 |
12 | # Query dictionary
13 | query = {
14 | "productId": techspecs_product_id
15 | }
16 |
17 | # Output mode ('raw' or 'pretty')
18 | mode = 'pretty'
19 |
20 | try:
21 | # Validate techspecs_product_id
22 | if not isinstance(techspecs_product_id, str):
23 | raise ValueError('TechSpecs Product ID should be a string.')
24 |
25 | # Validate mode
26 | if mode not in ['raw', 'pretty']:
27 | raise ValueError('Invalid mode. Mode should be "raw" or "pretty".')
28 |
29 | # Call TechSpecs API to get product details
30 | response = techspecs.product_detail(techspecs_base_url, techspecs_product_id, techspecs_api_key, mode=mode)
31 |
32 | # Print the product details
33 | print(response)
34 | except Exception as e:
35 | print(f"An error occurred: {e}")
36 |
--------------------------------------------------------------------------------
/tests/test_product_detail.py:
--------------------------------------------------------------------------------
1 | import techspecs
2 |
3 | # TechSpecs API base URL
4 | techspecs_base_url = "https://api.techspecs.io"
5 |
6 | # TechSpecs API bearer token
7 | techspecs_api_key = "your_techspecs_api_key"
8 |
9 | # TechSpecs product ID
10 | techspecs_product_id = "63e96260ff7af4b68a304e40"
11 |
12 | # Query dictionary
13 | query = {
14 | "productId": techspecs_product_id
15 | }
16 |
17 | # Output mode ('raw' or 'pretty')
18 | mode = 'pretty'
19 |
20 | try:
21 | # Validate techspecs_product_id
22 | if not isinstance(techspecs_product_id, str):
23 | raise ValueError('TechSpecs Product ID should be a string.')
24 |
25 | # Validate mode
26 | if mode not in ['raw', 'pretty']:
27 | raise ValueError('Invalid mode. Mode should be "raw" or "pretty".')
28 |
29 | # Call TechSpecs API to get product details
30 | response = techspecs.product_detail(techspecs_base_url, techspecs_product_id, techspecs_api_key, mode=mode)
31 |
32 | # Print the product details
33 | print(response)
34 | except Exception as e:
35 | print(f"An error occurred: {e}")
36 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2018 The Python Packaging Authority
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy
4 | of this software and associated documentation files (the "Software"), to deal
5 | in the Software without restriction, including without limitation the rights
6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 | copies of the Software, and to permit persons to whom the Software is
8 | furnished to do so, subject to the following conditions:
9 |
10 | The above copyright notice and this permission notice shall be included in all
11 | copies or substantial portions of the Software.
12 |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 | SOFTWARE.
--------------------------------------------------------------------------------
/examples/advanced_search.py:
--------------------------------------------------------------------------------
1 | import techspecs
2 | import datetime
3 |
4 | # TechSpecs API base URL
5 | techspecs_base_url = "https://api.techspecs.io"
6 |
7 | # TechSpecs API bearer token
8 | techspecs_api_key = "your_techspecs_api_key"
9 |
10 | # Output mode ('raw' or 'pretty')
11 | mode = 'pretty'
12 |
13 | # Set constants
14 | DEFAULT_PAGE = 0
15 | DEFAULT_MODE = 'pretty'
16 | VALID_MODES = ['pretty', 'raw']
17 |
18 | # Define function to validate date format
19 | def is_valid_date(date_str):
20 | try:
21 | datetime.datetime.strptime(date_str, '%Y-%m-%d')
22 | return True
23 | except ValueError:
24 | return False
25 |
26 | # Define function to validate parameters
27 | def validate_parameters(brand, category, date=None, page=DEFAULT_PAGE, mode=DEFAULT_MODE):
28 | if not isinstance(brand, list):
29 | raise ValueError('Brand should be a list.')
30 |
31 | if not isinstance(category, list):
32 | raise ValueError('Category should be a list.')
33 |
34 | if date is not None:
35 | if not isinstance(date, dict):
36 | raise ValueError('Invalid date format. Date should be a dictionary with keys "from" and "to".')
37 | elif 'from' not in date or 'to' not in date:
38 | raise ValueError('Invalid date format. Date dictionary should have keys "from" and "to".')
39 | elif not is_valid_date(date['from']) or not is_valid_date(date['to']):
40 | raise ValueError('Invalid date format. Date should be in the format YYYY-MM-DD.')
41 |
42 | if not isinstance(page, int) or page < 0:
43 | raise ValueError('Page should be a non-negative integer.')
44 |
45 | if mode not in VALID_MODES:
46 | raise ValueError(f'Invalid mode: {mode}. Mode should be one of {VALID_MODES}.')
47 |
48 | # Define search parameters
49 | brand = ["Apple"]
50 | category = ["Smartphones"]
51 | date = {
52 | "from": "2010-01-01",
53 | "to": "2022-03-15"
54 | }
55 | page = 0
56 |
57 | # Validate search parameters
58 | try:
59 | validate_parameters(brand, category, date=date, page=page, mode=mode)
60 | except ValueError as e:
61 | print(f"Invalid search parameters: {e}")
62 | exit()
63 |
64 |
65 | # Call TechSpecs API to get all products
66 | try:
67 | response = techspecs.get_all_products(techspecs_base_url, techspecs_api_key, brand, category, date, page, mode=mode)
68 | print(response)
69 | except Exception as e:
70 | print(f"An error occurred: {e}")
71 |
--------------------------------------------------------------------------------
/tests/test_techspecs_advanced_search.py:
--------------------------------------------------------------------------------
1 | import techspecs
2 | import datetime
3 |
4 | # TechSpecs API base URL
5 | techspecs_base_url = "https://api.techspecs.io"
6 |
7 | # TechSpecs API bearer token
8 | techspecs_api_key = "your_techspecs_api_key"
9 |
10 | # Output mode ('raw' or 'pretty')
11 | mode = 'pretty'
12 |
13 | # Set constants
14 | DEFAULT_PAGE = 0
15 | DEFAULT_MODE = 'pretty'
16 | VALID_MODES = ['pretty', 'raw']
17 |
18 | # Define function to validate date format
19 | def is_valid_date(date_str):
20 | try:
21 | datetime.datetime.strptime(date_str, '%Y-%m-%d')
22 | return True
23 | except ValueError:
24 | return False
25 |
26 | # Define function to validate parameters
27 | def validate_parameters(brand, category, date=None, page=DEFAULT_PAGE, mode=DEFAULT_MODE):
28 | if not isinstance(brand, list):
29 | raise ValueError('Brand should be a list.')
30 |
31 | if not isinstance(category, list):
32 | raise ValueError('Category should be a list.')
33 |
34 | if date is not None:
35 | if not isinstance(date, dict):
36 | raise ValueError('Invalid date format. Date should be a dictionary with keys "from" and "to".')
37 | elif 'from' not in date or 'to' not in date:
38 | raise ValueError('Invalid date format. Date dictionary should have keys "from" and "to".')
39 | elif not is_valid_date(date['from']) or not is_valid_date(date['to']):
40 | raise ValueError('Invalid date format. Date should be in the format YYYY-MM-DD.')
41 |
42 | if not isinstance(page, int) or page < 0:
43 | raise ValueError('Page should be a non-negative integer.')
44 |
45 | if mode not in VALID_MODES:
46 | raise ValueError(f'Invalid mode: {mode}. Mode should be one of {VALID_MODES}.')
47 |
48 | # Define search parameters
49 | brand = ["Apple"]
50 | category = ["Smartphones"]
51 | date = {
52 | "from": "2010-01-01",
53 | "to": "2022-03-15"
54 | }
55 | page = 0
56 |
57 | # Validate search parameters
58 | try:
59 | validate_parameters(brand, category, date=date, page=page, mode=mode)
60 | except ValueError as e:
61 | print(f"Invalid search parameters: {e}")
62 | exit()
63 |
64 |
65 | # Call TechSpecs API to get all products
66 | try:
67 | response = techspecs.get_all_products(techspecs_base_url, techspecs_api_key, brand, category, date, page, mode=mode)
68 | print(response)
69 | except Exception as e:
70 | print(f"An error occurred: {e}")
71 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | # Introduction
6 |
7 | TechSpecs Python provides easy access to the standardized technical specifications of the world's consumer electronics, including the latest smartphones, tablets, smartwatches, laptops, monitors, TVs and more.
8 |
9 | # Database Stats
10 |
11 | At TechSpecs, we are committed to providing the most comprehensive and up-to-date database of technical specifications for consumer electronics products. Here are some statistics on our current database:
12 |
13 | Total number of products: 100,000+
14 | - Brands: 400+
15 | - Categories: 6 (Smartphones, Tablets, Smartwatches, Monitors, TVs, and Laptops)
16 | - Technical specifications: Over 300 for each product, including dimensions, weight, display features, connectivity options, and more.
17 | - Daily updates: We update our database on a daily basis to ensure that our API provides the most accurate and up-to-date technical specifications for the products you're interested in.
18 |
19 | We take pride in providing a database that is comprehensive and reliable, and we are constantly working to expand and improve it. If you have any questions or issues, please don't hesitate to reach out to our support team for assistance.
20 |
21 | ## API Key
22 |
23 | - [Signup](https://techspecs.io/) to get your TechSpecs API Key
24 |
25 | ## Requirements
26 |
27 | - Python 3.6+
28 |
29 |
30 | ## Installation
31 |
32 | ```sh
33 | pip install techspecs
34 | ```
35 |
36 | ## Usage
37 |
38 | The library needs to be configured with your TechSpecs api key and base URL which is
39 | available in your [TechSpecs Dashboard](https://techspecs.io/dashboard).
40 |
41 | Set `techspecs_api_key` to your key value and `techspecs_base_url` to your base value.
42 |
43 | ### Product Search
44 |
45 | ```python
46 | # Search for a product by name, version or features
47 | import techspecs
48 | import json
49 |
50 | # TechSpecs API base URL
51 | techspecs_base_url = "https://api.techspecs.io"
52 |
53 | # TechSpecs API bearer token
54 | techspecs_api_key = "your_techspecs_api_key"
55 |
56 | query = {
57 | 'keyword': 'iPhone 13', # product name or version number to search
58 | 'category': '', # product category to search (e.g. 'Smartphones', 'Tablets', or leave empty to search all categories)
59 | 'page': 0 # page number to fetch results from
60 | }
61 |
62 | # Output mode ('raw' or 'pretty')
63 | mode = 'pretty'
64 |
65 | # Search for a product by name, version, or features
66 | try:
67 | response = techspecs.product_search(techspecs_base_url, query, techspecs_api_key, mode=mode)
68 | print(response)
69 | except Exception as e:
70 | print(f"An error occurred: {e}")
71 |
72 |
73 | ```
74 |
75 |
76 | ### Product Details
77 |
78 | ```python
79 | # Get the detailed specifications of a product
80 | import techspecs
81 |
82 | # TechSpecs API base URL
83 | techspecs_base_url = "https://api.techspecs.io"
84 |
85 | # TechSpecs API bearer token
86 | techspecs_api_key = "your_techspecs_api_key"
87 |
88 | # TechSpecs product ID
89 | techspecs_product_id = "63e96260ff7af4b68a304e40"
90 |
91 | # Query dictionary
92 | query = {
93 | "productId": techspecs_product_id
94 | }
95 |
96 | # Output mode ('raw' or 'pretty')
97 | mode = 'pretty'
98 |
99 | try:
100 | # Validate techspecs_product_id
101 | if not isinstance(techspecs_product_id, str):
102 | raise ValueError('TechSpecs Product ID should be a string.')
103 |
104 | # Validate mode
105 | if mode not in ['raw', 'pretty']:
106 | raise ValueError('Invalid mode. Mode should be "raw" or "pretty".')
107 |
108 | # Call TechSpecs API to get product details
109 | response = techspecs.product_detail(techspecs_base_url, techspecs_product_id, techspecs_api_key, mode=mode)
110 |
111 | # Print the product details
112 | print(response)
113 | except Exception as e:
114 | print(f"An error occurred: {e}")
115 |
116 |
117 | ```
118 |
119 |
120 | ### List all categories
121 | ```python
122 | import techspecs
123 |
124 | # TechSpecs API base URL
125 | techspecs_base_url = "https://api.techspecs.io"
126 |
127 | # TechSpecs API bearer token
128 | techspecs_api_key = "your_techspecs_api_key"
129 |
130 | # Output mode ('raw' or 'pretty')
131 | mode = 'pretty'
132 |
133 | # Define constants
134 | DEFAULT_MODE = 'pretty'
135 | VALID_MODES = ['pretty', 'raw']
136 |
137 | # Validate parameters
138 | def validate_parameters(mode=DEFAULT_MODE):
139 | if mode not in VALID_MODES:
140 | raise ValueError(f'Invalid mode: {mode}. Mode should be one of {VALID_MODES}.')
141 |
142 | # Validate search parameters
143 | try:
144 | validate_parameters(mode=mode)
145 | except ValueError as e:
146 | print(f"Invalid search parameters: {e}")
147 | exit()
148 |
149 | # Call TechSpecs API to get all categories
150 | try:
151 | response = techspecs.get_all_categories(techspecs_base_url, techspecs_api_key, mode=mode)
152 | print(response)
153 | except Exception as e:
154 | print(f"An error occurred: {e}")
155 |
156 |
157 | ```
158 |
159 |
160 | ### List all brands
161 | ```python
162 | import techspecs
163 |
164 | # TechSpecs API base URL
165 | techspecs_base_url = "https://api.techspecs.io"
166 |
167 | # TechSpecs API bearer token
168 | techspecs_api_key = "your_techspecs_api_key"
169 |
170 | # Output mode ('raw' or 'pretty')
171 | mode = 'pretty'
172 |
173 | # Define function to validate parameters
174 | def validate_parameters(mode):
175 | if mode not in ['raw', 'pretty']:
176 | raise ValueError(f'Invalid mode: {mode}. Mode should be one of ["raw", "pretty"].')
177 |
178 | # Validate parameters
179 | try:
180 | validate_parameters(mode)
181 | except ValueError as e:
182 | print(f"Invalid parameters: {e}")
183 | exit()
184 |
185 | # Call TechSpecs API to get all brands
186 | try:
187 | response = techspecs.get_all_brands(techspecs_base_url, techspecs_api_key, mode=mode)
188 | print(response)
189 | except Exception as e:
190 | print(f"An error occurred: {e}")
191 |
192 |
193 | ```
194 |
195 |
196 | ### Advanced Search
197 | ```python
198 | # List all products by brand, category and release date
199 | import techspecs
200 | import datetime
201 |
202 | # TechSpecs API base URL
203 | techspecs_base_url = "https://api.techspecs.io"
204 |
205 | # TechSpecs API bearer token
206 | techspecs_api_key = "your_techspecs_api_key"
207 |
208 | # Output mode ('raw' or 'pretty')
209 | mode = 'pretty'
210 |
211 | # Set constants
212 | DEFAULT_PAGE = 0
213 | DEFAULT_MODE = 'pretty'
214 | VALID_MODES = ['pretty', 'raw']
215 |
216 | # Define function to validate date format
217 | def is_valid_date(date_str):
218 | try:
219 | datetime.datetime.strptime(date_str, '%Y-%m-%d')
220 | return True
221 | except ValueError:
222 | return False
223 |
224 | # Define function to validate parameters
225 | def validate_parameters(brand, category, date=None, page=DEFAULT_PAGE, mode=DEFAULT_MODE):
226 | if not isinstance(brand, list):
227 | raise ValueError('Brand should be a list.')
228 |
229 | if not isinstance(category, list):
230 | raise ValueError('Category should be a list.')
231 |
232 | if date is not None:
233 | if not isinstance(date, dict):
234 | raise ValueError('Invalid date format. Date should be a dictionary with keys "from" and "to".')
235 | elif 'from' not in date or 'to' not in date:
236 | raise ValueError('Invalid date format. Date dictionary should have keys "from" and "to".')
237 | elif not is_valid_date(date['from']) or not is_valid_date(date['to']):
238 | raise ValueError('Invalid date format. Date should be in the format YYYY-MM-DD.')
239 |
240 | if not isinstance(page, int) or page < 0:
241 | raise ValueError('Page should be a non-negative integer.')
242 |
243 | if mode not in VALID_MODES:
244 | raise ValueError(f'Invalid mode: {mode}. Mode should be one of {VALID_MODES}.')
245 |
246 | # Define search parameters
247 | brand = ["Apple"]
248 | category = ["Smartphones"]
249 | date = {
250 | "from": "2010-01-01",
251 | "to": "2022-03-15"
252 | }
253 | page = 0
254 |
255 | # Validate search parameters
256 | try:
257 | validate_parameters(brand, category, date=date, page=page, mode=mode)
258 | except ValueError as e:
259 | print(f"Invalid search parameters: {e}")
260 | exit()
261 |
262 |
263 | # Call TechSpecs API to get all products
264 | try:
265 | response = techspecs.get_all_products(techspecs_base_url, techspecs_api_key, brand, category, date, page, mode=mode)
266 | print(response)
267 | except Exception as e:
268 | print(f"An error occurred: {e}")
269 |
270 |
271 | ```
272 |
273 |
274 | ### Machine ID Search
275 | #### Search for Apple products by machine id
276 | ```python
277 | # Search for an Apple product by machine id
278 | import techspecs
279 |
280 | # TechSpecs API base URL
281 | techspecs_base_url = "https://api.techspecs.io"
282 |
283 | # TechSpecs API bearer token
284 | techspecs_api_key = "your_techspecs_api_key"
285 |
286 | # Serial number of the Apple machine to look up machineid_or_codename ex iphone 11,6
287 | machine_id = "iphone 11,6"
288 |
289 | # Output mode ('raw' or 'pretty')
290 | mode = 'pretty'
291 |
292 | # Look up the Apple machine by its serial number
293 | try:
294 | response = techspecs.machine_id_search(techspecs_base_url, techspecs_api_key, machine_id, mode)
295 | print(response)
296 | except Exception as e:
297 | print(f"An error occurred: {e}")
298 |
299 |
300 | ```
301 |
302 |
303 |
304 |
--------------------------------------------------------------------------------
/src/Techspecs.egg-info/PKG-INFO:
--------------------------------------------------------------------------------
1 | Metadata-Version: 2.1
2 | Name: techspecs
3 | Version: 2.0.0
4 | Summary: TechSpecs Python
5 | Home-page: https://github.com/techspecs/techspecs-python
6 | Author: TechSpecs
7 | Author-email: support@techspecs.io
8 | Project-URL: Bug Tracker, https://github.com/techspecs/techspecs-python/issues
9 | Keywords: TechSpecs API,specifications,phone specs,tablet specs,laptop specs,specs,smartwatch specs,smartphone specs,smartphones,tablets,smartwatches,laptops,Smartphone Specifications,Tablet Specifications,Smartwatch Specifications,Laptop Specifications,Technical Specifications,consumer electronics,ecommerce,techspecs-python,techspecs python,specifications library,tech specs API,tech specs
10 | Classifier: Programming Language :: Python :: 3
11 | Classifier: License :: OSI Approved :: MIT License
12 | Classifier: Operating System :: OS Independent
13 | Requires-Python: >=3.6
14 | Description-Content-Type: text/markdown
15 | License-File: LICENSE
16 |
17 |
18 |
19 |
20 |
21 | # Introduction
22 |
23 | TechSpecs Python provides easy access to the standardized technical specifications of the world's consumer electronics, including smartphones, tablets, smartwatches, laptops, monitors, TVs and more.
24 |
25 | ## Documentation
26 |
27 | - See the [TechSpecs API Docs](https://techspecs.readme.io)
28 |
29 | ## API Key
30 |
31 | - [Signup](https://techspecs.io/) to get your TechSpecs API Key
32 |
33 |
34 | ## Requirements
35 |
36 | - Python 3.6+
37 |
38 |
39 | ## Installation
40 |
41 | ```sh
42 | pip install techspecs
43 | ```
44 |
45 | ## Usage
46 |
47 | The library needs to be configured with your TechSpecs api key and base URL which is
48 | available in your [TechSpecs Dashboard](https://techspecs.io/dashboard).
49 |
50 | Set `techspecs_api_key` to your key value and `techspecs_base_url` to your base value.
51 |
52 | ### Product Search
53 | #### Search for a device by specifying it's model name, version number or features
54 |
55 | ```python
56 | # Search for a product by name, version or features
57 | import techspecs
58 | import json
59 |
60 | # TechSpecs API base URL
61 | techspecs_base_url = "https://api.techspecs.io"
62 |
63 | # TechSpecs API bearer token
64 | techspecs_api_key = "your_techspecs_api_key"
65 |
66 | query = {
67 | 'keyword': 'iPhone 13', # product name or version number to search
68 | 'category': '', # product category to search (e.g. 'Smartphones', 'Tablets', or leave empty to search all categories)
69 | 'page': 0 # page number to fetch results from
70 | }
71 |
72 | # Output mode ('raw' or 'pretty')
73 | mode = 'pretty'
74 |
75 | # Search for a product by name, version, or features
76 | try:
77 | response = techspecs.product_search(techspecs_base_url, query, techspecs_api_key, mode=mode)
78 | print(response)
79 | except Exception as e:
80 | print(f"An error occurred: {e}")
81 |
82 |
83 | ```
84 |
85 |
86 | ### Product Details
87 |
88 | ```python
89 | # Get the detailed specifications of a product
90 | import techspecs
91 |
92 | # TechSpecs API base URL
93 | techspecs_base_url = "https://api.techspecs.io"
94 |
95 | # TechSpecs API bearer token
96 | techspecs_api_key = "your_techspecs_api_key"
97 |
98 | # TechSpecs product ID
99 | techspecs_product_id = "63e96260ff7af4b68a304e40"
100 |
101 | # Query dictionary
102 | query = {
103 | "productId": techspecs_product_id
104 | }
105 |
106 | # Output mode ('raw' or 'pretty')
107 | mode = 'pretty'
108 |
109 | try:
110 | # Validate techspecs_product_id
111 | if not isinstance(techspecs_product_id, str):
112 | raise ValueError('TechSpecs Product ID should be a string.')
113 |
114 | # Validate mode
115 | if mode not in ['raw', 'pretty']:
116 | raise ValueError('Invalid mode. Mode should be "raw" or "pretty".')
117 |
118 | # Call TechSpecs API to get product details
119 | response = techspecs.product_detail(techspecs_base_url, techspecs_product_id, techspecs_api_key, mode=mode)
120 |
121 | # Print the product details
122 | print(response)
123 | except Exception as e:
124 | print(f"An error occurred: {e}")
125 |
126 |
127 | ```
128 |
129 |
130 | ### List all categories
131 | ```python
132 | import techspecs
133 |
134 | # TechSpecs API base URL
135 | techspecs_base_url = "https://api.techspecs.io"
136 |
137 | # TechSpecs API bearer token
138 | techspecs_api_key = "your_techspecs_api_key"
139 |
140 | # Output mode ('raw' or 'pretty')
141 | mode = 'pretty'
142 |
143 | # Define constants
144 | DEFAULT_MODE = 'pretty'
145 | VALID_MODES = ['pretty', 'raw']
146 |
147 | # Validate parameters
148 | def validate_parameters(mode=DEFAULT_MODE):
149 | if mode not in VALID_MODES:
150 | raise ValueError(f'Invalid mode: {mode}. Mode should be one of {VALID_MODES}.')
151 |
152 | # Validate search parameters
153 | try:
154 | validate_parameters(mode=mode)
155 | except ValueError as e:
156 | print(f"Invalid search parameters: {e}")
157 | exit()
158 |
159 | # Call TechSpecs API to get all categories
160 | try:
161 | response = techspecs.get_all_categories(techspecs_base_url, techspecs_api_key, mode=mode)
162 | print(response)
163 | except Exception as e:
164 | print(f"An error occurred: {e}")
165 |
166 |
167 | ```
168 |
169 |
170 | ### List all brands
171 | ```python
172 | import techspecs
173 |
174 | # TechSpecs API base URL
175 | techspecs_base_url = "https://api.techspecs.io"
176 |
177 | # TechSpecs API bearer token
178 | techspecs_api_key = "your_techspecs_api_key"
179 |
180 | # Output mode ('raw' or 'pretty')
181 | mode = 'pretty'
182 |
183 | # Define function to validate parameters
184 | def validate_parameters(mode):
185 | if mode not in ['raw', 'pretty']:
186 | raise ValueError(f'Invalid mode: {mode}. Mode should be one of ["raw", "pretty"].')
187 |
188 | # Validate parameters
189 | try:
190 | validate_parameters(mode)
191 | except ValueError as e:
192 | print(f"Invalid parameters: {e}")
193 | exit()
194 |
195 | # Call TechSpecs API to get all brands
196 | try:
197 | response = techspecs.get_all_brands(techspecs_base_url, techspecs_api_key, mode=mode)
198 | print(response)
199 | except Exception as e:
200 | print(f"An error occurred: {e}")
201 |
202 |
203 | ```
204 |
205 |
206 | ### Advanced Search
207 | #### List all products by brand, category and release date
208 | ```python
209 | # List all products by brand, category and release date
210 | import techspecs
211 | import datetime
212 |
213 | # TechSpecs API base URL
214 | techspecs_base_url = "https://api.techspecs.io"
215 |
216 | # TechSpecs API bearer token
217 | techspecs_api_key = "your_techspecs_api_key"
218 |
219 | # Output mode ('raw' or 'pretty')
220 | mode = 'pretty'
221 |
222 | # Set constants
223 | DEFAULT_PAGE = 0
224 | DEFAULT_MODE = 'pretty'
225 | VALID_MODES = ['pretty', 'raw']
226 |
227 | # Define function to validate date format
228 | def is_valid_date(date_str):
229 | try:
230 | datetime.datetime.strptime(date_str, '%Y-%m-%d')
231 | return True
232 | except ValueError:
233 | return False
234 |
235 | # Define function to validate parameters
236 | def validate_parameters(brand, category, date=None, page=DEFAULT_PAGE, mode=DEFAULT_MODE):
237 | if not isinstance(brand, list):
238 | raise ValueError('Brand should be a list.')
239 |
240 | if not isinstance(category, list):
241 | raise ValueError('Category should be a list.')
242 |
243 | if date is not None:
244 | if not isinstance(date, dict):
245 | raise ValueError('Invalid date format. Date should be a dictionary with keys "from" and "to".')
246 | elif 'from' not in date or 'to' not in date:
247 | raise ValueError('Invalid date format. Date dictionary should have keys "from" and "to".')
248 | elif not is_valid_date(date['from']) or not is_valid_date(date['to']):
249 | raise ValueError('Invalid date format. Date should be in the format YYYY-MM-DD.')
250 |
251 | if not isinstance(page, int) or page < 0:
252 | raise ValueError('Page should be a non-negative integer.')
253 |
254 | if mode not in VALID_MODES:
255 | raise ValueError(f'Invalid mode: {mode}. Mode should be one of {VALID_MODES}.')
256 |
257 | # Define search parameters
258 | brand = ["Apple"]
259 | category = ["Smartphones"]
260 | date = {
261 | "from": "2010-01-01",
262 | "to": "2022-03-15"
263 | }
264 | page = 0
265 |
266 | # Validate search parameters
267 | try:
268 | validate_parameters(brand, category, date=date, page=page, mode=mode)
269 | except ValueError as e:
270 | print(f"Invalid search parameters: {e}")
271 | exit()
272 |
273 |
274 | # Call TechSpecs API to get all products
275 | try:
276 | response = techspecs.get_all_products(techspecs_base_url, techspecs_api_key, brand, category, date, page, mode=mode)
277 | print(response)
278 | except Exception as e:
279 | print(f"An error occurred: {e}")
280 |
281 |
282 | ```
283 |
284 |
285 | ### Machine ID Search
286 | #### Search for Apple products by machine id
287 | ```python
288 | # Search for an Apple product by machine id
289 | import techspecs
290 |
291 | # TechSpecs API base URL
292 | techspecs_base_url = "https://api.techspecs.io"
293 |
294 | # TechSpecs API bearer token
295 | techspecs_api_key = "your_techspecs_api_key"
296 |
297 | # Serial number of the Apple machine to look up machineid_or_codename ex iphone 11,6
298 | machine_id = "iphone 11,6"
299 |
300 | # Output mode ('raw' or 'pretty')
301 | mode = 'pretty'
302 |
303 | # Look up the Apple machine by its serial number
304 | try:
305 | response = techspecs.machine_id_search(techspecs_base_url, techspecs_api_key, machine_id, mode)
306 | print(response)
307 | except Exception as e:
308 | print(f"An error occurred: {e}")
309 |
310 |
311 | ```
312 |
313 |
314 |
--------------------------------------------------------------------------------
/src/techspecs/techspecs.py:
--------------------------------------------------------------------------------
1 | import json
2 | import requests
3 |
4 | def product_search(techspecs_base_url, query: dict, techspecs_api_key, mode='raw'):
5 | """
6 | Search for products that match the specified query.
7 |
8 | :param base_url: the base URL of the TechSpecs API
9 | :param query: a dictionary that contains the search query parameters
10 | :param bearer_token: the bearer token for API authentication
11 | :param mode: the output mode ('raw' or 'pretty')
12 | :return: the search results in the specified output mode
13 | """
14 | q = query["keyword"].strip()
15 | url = f'{techspecs_base_url}/v4/product/search?query={q}'
16 | headers = {
17 | "Accept": "application/json",
18 | "Authorization": f"Bearer {techspecs_api_key}",
19 | "Content-Type": "application/json"
20 | }
21 | payload = {"category": query['category'].strip()}
22 | response = requests.post(url, json=payload, headers=headers).json()
23 | if mode == 'raw':
24 | return response
25 | elif mode == 'pretty':
26 | try:
27 | mod_list = response['data']
28 | return json.dumps(mod_list, indent=4)
29 | except KeyError:
30 | return response
31 | else:
32 | return 'Invalid Mode'
33 |
34 |
35 | def product_detail(techspecs_base_url, techspecs_product_id, techspecs_api_key, mode='raw'):
36 | """
37 | Get detailed information about a specific product.
38 |
39 | :param base_url: the base URL of the TechSpecs API
40 | :param product_id: the ID of the product to get information about
41 | :param bearer_token: the bearer token for API authentication
42 | :param mode: the output mode ('raw' or 'pretty')
43 | :return: the product information in the specified output mode
44 | """
45 | url = f'{techspecs_base_url}/v4/product/detail'
46 | params = {
47 | 'productId': techspecs_product_id.strip()
48 | }
49 | headers = {
50 | "Accept": "application/json",
51 | "Authorization": f"Bearer {techspecs_api_key}",
52 | }
53 | try:
54 | response = requests.get(url, headers=headers, params=params)
55 | response.raise_for_status()
56 | data = response.json()
57 | except requests.exceptions.HTTPError as error:
58 | return f"HTTP error occurred: {error}"
59 | except requests.exceptions.RequestException as error:
60 | return f"An error occurred: {error}"
61 | except ValueError as error:
62 | return f"Failed to decode JSON: {error}"
63 |
64 | if mode == 'raw':
65 | return data
66 | elif mode == 'pretty':
67 | try:
68 | mod_list = data['data']
69 | modified_data = []
70 | for m in mod_list:
71 | mod_dict = {}
72 | for a, b in m.items():
73 | try:
74 | mod_dict[a] = b
75 | except TypeError:
76 | for x, y in b:
77 | mod_dict[x] = y
78 | modified_data.append(mod_dict)
79 | return json.dumps(modified_data, indent=4)
80 | except KeyError:
81 | return data
82 | else:
83 | return 'Invalid Mode'
84 |
85 |
86 |
87 | def get_all_brands(techspecs_base_url, techspecs_api_key, mode='raw'):
88 | """
89 | Get a list of all brands.
90 |
91 | :param base_url: the base URL of the TechSpecs API
92 | :param bearer_token: the bearer token for API authentication
93 | :param mode: the output mode ('raw' or 'pretty')
94 | :return: the list of brands in the specified output mode
95 | """
96 | url = f'{techspecs_base_url}/v4/brand/all'
97 | headers = {
98 | "Accept": "application/json",
99 | "Authorization": f"Bearer {techspecs_api_key}"
100 | }
101 | try:
102 | response = requests.get(url, headers=headers)
103 | response.raise_for_status()
104 | data = response.json()
105 | except requests.exceptions.HTTPError as error:
106 | return f"HTTP error occurred: {error}"
107 | except requests.exceptions.RequestException as error:
108 | return f"An error occurred: {error}"
109 | except ValueError as error:
110 | return f"Failed to decode JSON: {error}"
111 |
112 | if mode == 'raw':
113 | return data
114 | elif mode == 'pretty':
115 | try:
116 | mod_list = data['data']
117 | return json.dumps(mod_list, indent=4)
118 | except KeyError:
119 | return data
120 | else:
121 | return 'Invalid Mode'
122 |
123 |
124 |
125 | def get_all_categories(techspecs_base_url, techspecs_api_key, mode='raw'):
126 | """
127 | Get a list of all categories.
128 |
129 | :param base_url: the base URL of the TechSpecs API
130 | :param bearer_token: the bearer token for API authentication
131 | :param mode: the output mode ('raw' or 'pretty')
132 | :return: the list of categories in the specified output mode
133 | """
134 | url = f'{techspecs_base_url}/v4/category/all'
135 | headers = {
136 | "Accept": "application/json",
137 | "Authorization": f"Bearer {techspecs_api_key}"
138 | }
139 | try:
140 | response = requests.get(url, headers=headers)
141 | response.raise_for_status()
142 | data = response.json()
143 | except requests.exceptions.HTTPError as error:
144 | return f"HTTP error occurred: {error}"
145 | except requests.exceptions.RequestException as error:
146 | return f"An error occurred: {error}"
147 | except ValueError as error:
148 | return f"Failed to decode JSON: {error}"
149 |
150 | if mode == 'raw':
151 | return data
152 | elif mode == 'pretty':
153 | try:
154 | mod_list = data['data']
155 | return json.dumps(mod_list, indent=4)
156 | except KeyError:
157 | return data
158 | else:
159 | return 'Invalid Mode'
160 |
161 |
162 |
163 | def get_all_products(techspecs_base_url, techspecs_api_key, brand=[], category=[], date={}, page=0, mode='raw'):
164 | """
165 | Get a list of products matching the specified criteria.
166 |
167 | :param base_url: the base URL of the TechSpecs API
168 | :param bearer_token: the bearer token for API authentication
169 | :param brand: a list of brand names to include in the search (default: [])
170 | :param category: a list of category names to include in the search (default: [])
171 | :param date: a dictionary containing 'from' and 'to' date strings to limit the search (default: {})
172 | :param page: the page number of the search results to retrieve (default: 0)
173 | :param mode: the output mode ('raw' or 'pretty')
174 | :return: the list of products in the specified output mode
175 | """
176 | url = f'{techspecs_base_url}/v4/product/all?page={page}'
177 | payload = {
178 | "brand": brand,
179 | "category": category,
180 | "from": date.get('from', ''),
181 | "to": date.get('to', '')
182 | }
183 | headers = {
184 | "Accept": "application/json",
185 | "Authorization": f"Bearer {techspecs_api_key}",
186 | "Content-Type": "application/json"
187 | }
188 | try:
189 | response = requests.post(url, json=payload, headers=headers)
190 | response.raise_for_status()
191 | data = response.json()
192 | except requests.exceptions.HTTPError as error:
193 | return f"HTTP error occurred: {error}"
194 | except requests.exceptions.RequestException as error:
195 | return f"An error occurred: {error}"
196 | except ValueError as error:
197 | return f"Failed to decode JSON: {error}"
198 |
199 | if mode == 'raw':
200 | return data
201 | elif mode == 'pretty':
202 | try:
203 | mod_list = data['data']
204 | return json.dumps(mod_list, indent=4)
205 | except KeyError:
206 | return data
207 | else:
208 | return 'Invalid Mode'
209 |
210 |
211 |
212 |
213 | def get_all_brand_logos(techspecs_base_url, techspecs_api_key, mode='raw'):
214 | """
215 | Get a list of all brand logos.
216 |
217 | :param base_url: the base URL of the TechSpecs API
218 | :param bearer_token: the bearer token for API authentication
219 | :param mode: the output mode ('raw' or 'pretty')
220 | :return: the list of brand logos in the specified output mode
221 | """
222 | url = f'{techspecs_base_url}/v4/brandlogo/all'
223 | headers = {
224 | "Accept": "application/json",
225 | "Authorization": f"Bearer {techspecs_api_key}"
226 | }
227 | try:
228 | response = requests.get(url, headers=headers)
229 | response.raise_for_status()
230 | data = response.json()
231 | except requests.exceptions.HTTPError as error:
232 | return f"HTTP error occurred: {error}"
233 | except requests.exceptions.RequestException as error:
234 | return f"An error occurred: {error}"
235 | except ValueError as error:
236 | return f"Failed to decode JSON: {error}"
237 |
238 | if mode == 'raw':
239 | return data
240 | elif mode == 'pretty':
241 | try:
242 | mod_list = data['data']
243 | return json.dumps(mod_list, indent=4)
244 | except KeyError:
245 | return data
246 | else:
247 | return 'Invalid Mode'
248 |
249 |
250 |
251 |
252 | def machine_id_search(techspecs_base_url, techspecs_api_key, machine_id, mode='raw'):
253 | """
254 | Look up a product by its machine ID or code name.
255 |
256 | :param base_url: the base URL of the TechSpecs API
257 | :param bearer_token: the bearer token for API authentication
258 | :param machine_id: the machine ID or code name of the product to look up
259 | :param mode: the output mode ('raw' or 'pretty')
260 | :return: the search results in the specified output mode
261 | """
262 | url = f'{techspecs_base_url}/v4/product/search/machineid?query={machine_id.strip()}'
263 | headers = {
264 | "Accept": "application/json",
265 | "Authorization": f"Bearer {techspecs_api_key}"
266 | }
267 | try:
268 | response = requests.post(url, headers=headers)
269 | response.raise_for_status()
270 | data = response.json()
271 | except requests.exceptions.HTTPError as error:
272 | return f"HTTP error occurred: {error}"
273 | except requests.exceptions.RequestException as error:
274 | return f"An error occurred: {error}"
275 | except ValueError as error:
276 | return f"Failed to decode JSON: {error}"
277 |
278 | if mode == 'raw':
279 | return data
280 | elif mode == 'pretty':
281 | return json.dumps(data, indent=4)
282 | else:
283 | return 'Invalid Mode'
284 |
285 |
--------------------------------------------------------------------------------