Created
February 15, 2023 18:58
-
-
Save simonuvarov/933fe324dd40a0a3deb09c6d49d3e725 to your computer and use it in GitHub Desktop.
A Python script used to self-sample your energy levels
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 os | |
| import sys | |
| import time | |
| import numpy as np | |
| import requests | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| PUSHCUT_API_KEY = os.getenv('PUSHCUT_API_KEY') | |
| NOTIFICATION_NAME = os.getenv('NOTIFICATION_NAME') | |
| AIRTABLE_API_KEY = os.getenv('AIRTABLE_API_KEY') | |
| AIRTABLE_BASE_ID = os.getenv('AIRTABLE_BASE_ID') | |
| AIRTABLE_TABLE_NAME = os.getenv('AIRTABLE_TABLE_NAME') | |
| START_TIME = int(os.getenv('START_TIME').split(':')[0]) | |
| END_TIME = int(os.getenv('END_TIME').split(':')[0]) | |
| # Generate a random interval using exponential distribution | |
| def generate_next_interval(): | |
| inter = np.random.exponential(0.5, 1) | |
| return inter | |
| def generate_action_body(action_name=None, action_value=None, timestamp=None): | |
| return { | |
| "name": action_name, | |
| "url": "https://api.airtable.com/v0/" + AIRTABLE_BASE_ID + "/" + AIRTABLE_TABLE_NAME, | |
| "urlBackgroundOptions": { | |
| "httpMethod": "POST", | |
| "httpContentType": "application/json", | |
| "httpHeader": [{"key": "Authorization", "value": "Bearer " + AIRTABLE_API_KEY}], | |
| "httpBody": | |
| "{\"records\":[{\"fields\":{\"Timestamp\":" + str(timestamp) + ", \"Value\":" + | |
| str(action_value) + "}}]}" | |
| } | |
| } | |
| # Send request to Pushcut | |
| def send_request(): | |
| request_body = { | |
| "title": "What is your energy level?", | |
| "input": "input value", | |
| "actions": [ | |
| generate_action_body(action_name="High", action_value=1, timestamp=int(time.time()) | |
| ), | |
| generate_action_body(action_name="Medium", action_value=0, timestamp=int(time.time()) | |
| ), | |
| generate_action_body(action_name="Low", action_value=-1, timestamp=int(time.time()) | |
| ), | |
| ] | |
| } | |
| r = requests.post( | |
| f'https://api.pushcut.io/{PUSHCUT_API_KEY}/notifications/{NOTIFICATION_NAME}', json=request_body) | |
| print(r.status_code) | |
| while True: | |
| next_interval = generate_next_interval() | |
| interval_in_minutes = int(next_interval[0] * 60) | |
| time.sleep(interval_in_minutes * 60) | |
| current_time = time.localtime() | |
| if current_time.tm_hour >= START_TIME and current_time.tm_hour <= END_TIME: | |
| send_request() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment