forked from MycroftAI/skill-weather
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from OpenVoiceOS/add/feat/ovos_backend_api
add support for ovos api service and caching
- Loading branch information
Showing
5 changed files
with
143 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import json | ||
import requests | ||
from json_database import JsonStorageXDG | ||
|
||
|
||
class OVOSApiService: | ||
|
||
def __init__(self) -> None: | ||
self.uuid_storage = JsonStorageXDG("ovos_api_uuid") | ||
self.token_storage = JsonStorageXDG("ovos_api_token") | ||
|
||
def register_device(self): | ||
if self.check_if_uuid_exists(): | ||
return | ||
else: | ||
created_challenge = requests.get('https://api.openvoiceos.com/create_challenge') | ||
challenge_response = created_challenge.json() | ||
register_device = requests.get('https://api.openvoiceos.com/register_device/' + challenge_response['challenge'] + '/' + challenge_response['secret']) | ||
register_device_uuid = challenge_response['challenge'] | ||
self.uuid_storage['uuid'] = register_device_uuid | ||
self.uuid_storage.store() | ||
|
||
def check_if_uuid_exists(self): | ||
if "uuid" in self.uuid_storage: | ||
return True | ||
return False | ||
|
||
def get_session_challenge(self): | ||
session_challenge_request = requests.get('https://api.openvoiceos.com/get_session_challenge') | ||
session_challenge_response = session_challenge_request.json() | ||
self.token_storage["challenge"] = session_challenge_response['challenge'] | ||
self.token_storage.store() | ||
|
||
def get_uuid(self): | ||
return self.uuid_storage.get("uuid", "") | ||
|
||
def get_session_token(self): | ||
return self.token_storage.get("challenge", "") | ||
|
||
def get_report_for_weather_onecall_type(self, query: dict): | ||
self.register_device() | ||
self.get_session_challenge() | ||
|
||
headers = {'session_challenge': self.get_session_token(), 'backend': 'OWM'} | ||
reqdata = {"lat": query.get("lat"), "lon": query.get("lon"), "units": query.get("units"), "lang": query.get("lang")} | ||
onecall_weather_request = requests.post('https://api.openvoiceos.com/weather/onecall_weather_report/' + self.get_uuid(), data=reqdata, headers=headers) | ||
onecall_weather_response = onecall_weather_request.text | ||
return json.loads(onecall_weather_response) |
This file contains 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