diff --git a/rasa/actions/actions.py b/rasa/actions/actions.py index e82c283ac1f6d7fbe5091248afee91db203480cb..b5bebba7ff814f816bef5db2898649f40f8f7e86 100644 --- a/rasa/actions/actions.py +++ b/rasa/actions/actions.py @@ -4,8 +4,59 @@ from rasa_sdk import Action, Tracker from rasa_sdk.executor import CollectingDispatcher import os.path import urllib.request +from datetime import datetime filepath = "../data/iss-location.txt" +url = "https://nasa-public-data.s3.amazonaws.com/iss-coords/current/ISS_OEM/ISS.OEM_J2K_EPH.txt" + + +def download_file(): + # download (and replace) file + if os.path.isfile(filepath): + os.remove(filepath) + urllib.request.urlretrieve(url, filepath) + + +def find_location(timestamp): + file = open(filepath, "r") + + smaller_entry = "" + bigger_entry = "" + + values = [ + int(timestamp[:4]), # year + int(timestamp[5:7]), # month + int(timestamp[8:10]), # day + int(timestamp[11:13]), # hour + int(timestamp[14:16]) # minute + ] + + starting_line_found = False + index = 1 + + for line in file: + if not starting_line_found: + if "COMMENT End sequence of events" in line: + starting_line_found = True + else: + values_line = [int(line[:4]), int(line[5:7]), int(line[8:10]), int(line[11:13]), int(line[14:16])] + i = 0 + while i <= 4: + if values[i] > values_line[i]: + smaller_entry = line + break + elif values[i] < values_line[i]: + bigger_entry = line + break + i += 1 + if bigger_entry != "": + break + index += 1 + + if smaller_entry == "": + return bigger_entry + return smaller_entry + class ActionCurrentIssLocation(Action): @@ -16,26 +67,14 @@ class ActionCurrentIssLocation(Action): tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict[Text, Any]]: - # download (and replace) file - if os.path.isfile(filepath): - os.remove(filepath) - urllib.request.urlretrieve("https://nasa-public-data.s3.amazonaws.com/iss-coords/current/ISS_OEM/ISS.OEM_J2K_EPH.txt", filepath) - - print(os.path.abspath(__file__)) - file = open(filepath, "r") - end_reached = False - location = "" - for line in file: - if end_reached: - location = line - break - if "COMMENT End sequence of events" in line: - end_reached = True + download_file() + location = find_location(str(datetime.now())) - dispatcher.utter_message(text="location: " + location) + dispatcher.utter_message(text=f"location: {location}") return [] + class ActionFutureIssLocation(Action): def name(self) -> Text: @@ -45,15 +84,14 @@ class ActionFutureIssLocation(Action): tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict[Text, Any]]: - # download (and replace) file - if os.path.isfile("../../api-data/iss-location.txt"): - os.remove("../../api-data/iss-location.txt") - urllib.request.urlretrieve("https://nasa-public-data.s3.amazonaws.com/iss-coords/current/ISS_OEM/ISS.OEM_J2K_EPH.txt", "text.txt") - + download_file() + if(len(tracker.latest_message['entities']) == 0): dispatcher.utter_message(text="I'm sorry, but I didn't understand your formatting. Please try again.") else: - dispatcher.utter_message(text="[Placeholder for the ISS location at: " + tracker.latest_message['entities'][0]['value'] + " ]") + entity = tracker.latest_message['entities'][0]['value'] + location = find_location(entity) + dispatcher.utter_message(text=f"location: {location}") return []