Created
          June 10, 2025 14:33 
        
      - 
      
- 
        Save mofosyne/042d45e042bf944ff7211cf43cf3028b to your computer and use it in GitHub Desktop. 
    Builtin Only Python URL getter for plain and json apis (Intended for use with simple script for accessing simple APIs)
  
        
  
    
      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
    
  
  
    
  | import urllib.request | |
| import json | |
| 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 | |
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment