Advertisement
jarekmor

NASK_ESA

Jan 1st, 2023
1,287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.71 KB | Source Code | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import requests
  4. from bs4 import BeautifulSoup
  5. import json
  6.  
  7. @service
  8. def scrape_nask(url="https://esa.nask.pl", station_id=None, entity_id=None, friendly_name=None, icon=None):
  9.  
  10.     log.debug('received parameters: ' + str(locals()))
  11.  
  12.     if station_id is None:
  13.         log.error('"station_id" is required but not passed on service call')
  14.         return
  15.     if entity_id is None:
  16.         log.error('"entity_id" is required but not passed on service call')
  17.         return
  18.    
  19.     # Get Token from https://esa.nask.pl
  20.     try:
  21.         url_token = task.executor(requests.get, url)
  22.     except:
  23.         log.error('exception when processing parameters: ' + str(locals()))
  24.         raise
  25.    
  26.     log.info('token requests result: ' + str(url_token))
  27.    
  28.     # Create Authorization Bearer Token
  29.     @pyscript_executor
  30.     def authorize(token):
  31.         token = BeautifulSoup(token.content, "html.parser").find(id="security").get("data-value")
  32.         auth = f"Bearer {token}"
  33.         return auth
  34.  
  35.     AUTH = authorize(url_token)
  36.     END_POINT = f"https://esa.nask.pl/api/data/id/{station_id}"
  37.     HEADERS = {
  38.                 'content-type': 'application/json',
  39.                 'Accept-Charset': 'UTF-8',
  40.                 'Authorization': AUTH }
  41.            
  42.     log.info('AUTH result: ' + AUTH)
  43.     log.info('END_POINT result: ' + END_POINT)
  44.  
  45.     # Request station_id json() output
  46.     try:
  47.         response = (task.executor(requests.get, END_POINT, headers=HEADERS)).json()
  48.     except:
  49.         log.error('exception when processing parameters: ' + str(locals()))
  50.         raise
  51.    
  52.     log.info('json request result: ' + str(response))
  53.    
  54.     # Set the entity_id attributes
  55.     attributes = {}
  56.    
  57.     if friendly_name:    
  58.         attributes['friendly_name'] = friendly_name
  59.     if icon:    
  60.         attributes['icon'] = icon
  61.    
  62.     attributes.update({'id': response['id']})
  63.     attributes.update({'project': response['project']})
  64.     attributes.update({'province': response['province']})
  65.     attributes.update({'street': response['street'] + " "+ response['streetNo']})
  66.     attributes.update({'city': response['city']})
  67.     attributes.update({'sensors': response['sensors'][0]['lastMeasurement']})
  68.    
  69.     # Set the entity_id state
  70.     STATUS = response['sensors'][0]['lastMeasurement']['pm25']['icon']
  71.    
  72.     log.info(f"Status : {STATUS}")
  73.     log.info(f"Attributes : {attributes}")    
  74.  
  75.     # Only create entity if the requests returns json
  76.     if response:
  77.         # Set the entity_id value to the last request value and attributes
  78.         state.set(entity_id, value=STATUS, new_attributes=attributes)
  79.    
  80.     log.info(f"Entity state value: {state.get(entity_id)}")
Tags: nask esa
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement