Skip to content

Commit

Permalink
1.3.5
Browse files Browse the repository at this point in the history
  • Loading branch information
Anghkooey committed Dec 27, 2023
1 parent 9c31e9a commit 2374f59
Show file tree
Hide file tree
Showing 5 changed files with 387 additions and 353 deletions.
24 changes: 23 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
# Changelog

## [v1.3.4]() - 04.12.2023
## [v1.3.5]() - 27.12.2023

### Improved
- **files: poetry.lock, client.py**
- **functions: _make_request**

### Added
- **libraries: from asyncio import sleep as async_sleep, from aiohttp.client_exceptions import ClientConnectorError**
- **functions: alert_loop**

### More info
- **_make_request: `match response.status:
case 200:
pass
case 401:
raise PermissionError("Invalid API token")
case 503:
raise Exception("API is currently unavailable")
case unknown_status:
raise Exception(f"Request failed with status code {unknown_status}\n{await response.text()}")`**


## [v1.3.4](https://github.com/user-sspmynxdvb/ua_alarm/tree/9c31e9a778af3e239ca153a40c42eeec91435bc1) - 04.12.2023

### Improved
- **functions: __init__, _make_request**
Expand Down
72 changes: 1 addition & 71 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,80 +46,10 @@ os.system('cls' if os.name == 'nt' else 'clear')
api_token = "YOUR_API_KEY"
client = UkraineAlertApiClient(api_token)


async def main():
# Example usage: Get a list of alerts
alerts = await client.get_alerts()
print(alerts)


alert_changed_time = ""


async def alert_loop():
"""
A loop that continuously monitors for changes in the alert state for a specific region.
The loop fetches region alerts using the UkraineAlert API and checks for changes in the last update time.
If a change is detected, it prints information about the active alert, if any.
Note: The loop sleeps for 30 seconds between iterations.
Raises:
ClientConnectorError: If there's a connection issue during API request.
Global Variables:
alert_changed_time (str): A global variable to store the timestamp of the last alert change.
API Token:
The API token is hardcoded for demonstration purposes.
API Client:
An instance of UkraineAlertApiClient is used to interact with the UkraineAlert API.
"""
global alert_changed_time

# Continuously monitor for changes in the alert state
while True:
try:
# Fetch region alerts (region_id=28)
data = await client.get_alerts(28)
data = data[0]

# Extract the last update time from the fetched data
changed_str = data.lastUpdate

# Check if there's a change in the alert state
if not changed_str == alert_changed_time:
alert_old_changed_time = alert_changed_time
alert_changed_time = changed_str

# Skip if duration less than or equal to 10 seconds
if alert_old_changed_time:
if int((alert_changed_time - alert_old_changed_time).total_seconds()) <= 10:
continue

# Construct a text message based on the active alert status
if data.activeAlerts:
text = f"({changed_str}) [{data.activeAlerts[0].type}] Оголошено тривогу"
else:
text = f"({changed_str}) Відбій тривоги"

# Print the alert information
print(text)

# Continue to the next iteration if there's a connection issue
except ClientConnectorError:
pass

# Sleep for 30 seconds before checking for changes again
await sleep(30)


# Run the main function
if __name__ == '__main__':
try:
run(main())
run(client.get_alerts())
except KeyboardInterrupt:
exit()
```
Loading

0 comments on commit 2374f59

Please sign in to comment.