Surya_from_1627

WSP5

Sep 9th, 2026 (edited)
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. import requests
  2.  
  3. # 1. Update to the brand new official Data.gov Catalog v4 API endpoint
  4. url = "https://api.gsa.gov/technology/datagov/v4/search"
  5.  
  6. # Note: 'DEMO_KEY' is built-in for testing purposes
  7. params = {
  8.     'sort': 'last_harvested_date',
  9.     'per_page': 1,
  10.     'api_key': 'DEMO_KEY'
  11. }
  12.  
  13. try:
  14.     response = requests.get(url, params=params, timeout=15)
  15.     response.raise_for_status()
  16.  
  17.     data = response.json()
  18.    
  19.     # Extract data from the new response body structure
  20.     datasets = data.get('results', [])
  21.    
  22.     if datasets:
  23.         latest_dataset = datasets[0]
  24.         dataset_name = latest_dataset.get('title', 'No title found')
  25.         harvest_date = latest_dataset.get('last_harvested_date', 'Unknown date')
  26.        
  27.         print("\n--- Success! ---")
  28.         print(f"Most recently updated dataset: {dataset_name}")
  29.         print(f"Last Harvested Date: {harvest_date}")
  30.     else:
  31.         print("No datasets found matching your parameters.")
  32.  
  33. except requests.exceptions.RequestException as e:
  34.     print(f"Error fetching data from the server: {e}")
  35. except ValueError:
  36.     print("Error: The server still failed to parse JSON.")
  37.  
Advertisement
Add Comment
Please, Sign In to add comment