|
| 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}") |
0 commit comments