Hello all !
I want to send, via a Python script, the value of the Safran stock retrieved from the Boursorama website to the Arduino Cloud.
I managed to code the script below, but the sending only occurs once
import logging
import sys
import time
sys.path.append("lib")
from arduino_iot_cloud import ArduinoCloudClient
import requests
from bs4 import BeautifulSoup
DEVICE_ID = b"xxx"
SECRET_KEY = b"xxx"
def logging_func():
logging.basicConfig(
datefmt="%H:%M:%S",
format="%(asctime)s.%(msecs)03d %(message)s",
level=logging.INFO,
)
def get_value_from_boursorama():
requete = requests.get("https://www.boursorama.com/cours/1rPSAF/")
page = requete.content
soup = BeautifulSoup(page, 'lxml')
h1 = soup.find("span", {"class": "c-instrument c-instrument--last"})
return h1.string
if __name__ == "__main__":
logging_func()
client = ArduinoCloudClient(device_id=DEVICE_ID, username=DEVICE_ID, password=SECRET_KEY)
client.register('datavalue')
client['datavalue'] = float(get_value_from_boursorama())
client.start()
I'm a beginner in programming, and the API documentation isn't clear to me. Does the API allow this? If so, how?
Thank you in advance
J-M-L
April 1, 2024, 3:55pm
2
look at the example
# This object allows scheduling recurring events from the cloud UI. On activation of the event, if the
# on_active callback is provided, it gets called with the client object and the schedule object value.
# Note: The activation status of the object can also be polled using client["schedule"].active.
client.register(Schedule("schedule", on_active=lambda client, value: logging.info(f"Schedule activated {value}!")))
# The client can also schedule user code in a task and run it along with the other cloud objects.
# To schedule a user function, use the Task object and pass the task name and function in "on_run"
# to client.register().
client.register(Task("user_task", on_run=user_task, interval=1.0))
scrapping stock price out of boursorama web site is likely not in line with their T&Cs (and your bot is possibly going to be blocked). you should get a proper feed.
Thank you J-M-L
I updated the script as well and it works!
import logging
import sys
import time
sys.path.append("lib")
from arduino_iot_cloud import ArduinoCloudClient
from arduino_iot_cloud import Task
import requests
from bs4 import BeautifulSoup
DEVICE_ID = b"xxx"
SECRET_KEY = b"xxx"
def logging_func():
logging.basicConfig(
datefmt="%H:%M:%S",
format="%(asctime)s.%(msecs)03d %(message)s",
level=logging.INFO,
)
def get_value_from_boursorama():
requete = requests.get("https://www.boursorama.com/cours/1rPSAF/")
page = requete.content
soup = BeautifulSoup(page, 'lxml')
h1 = soup.find("span", {"class": "c-instrument c-instrument--last"})
return h1.string
def user_task(client):
client['datavalue'] = float(get_value_from_boursorama())
if __name__ == "__main__":
logging_func()
client = ArduinoCloudClient(device_id=DEVICE_ID, username=DEVICE_ID, password=SECRET_KEY)
client.register('datavalue')
#client['datavalue'] = float(get_value_from_boursorama())
client.register(Task("user_task", on_run=user_task, interval=60.0))
client.start()
You're right, I'm using this solution temporarily until I find a permanent one