def handle_when(self, message): lat = self.location["coordinate"]["latitude"] lon = self.location["coordinate"]["longitude"] if not self.settings.get("passing_by"): params = {"lat": lat, "lon": lon} passing = self._session.get( "http://api.open-notify.org/iss-pass.json", params=params).json() self.settings["passing_by"] = passing["response"] next_passage = self.settings["passing_by"][0] ts = next_passage["risetime"] dt = datetime.fromtimestamp(ts) delta = datetime.now() - dt duration = nice_duration(delta, lang=self.lang) caption = self.location_pretty + " " + dt.strftime( "%m/%d/%Y, %H:%M:%S") image = self.generate_map(lat, lon) self.gui.show_image(image, caption=caption, fill='PreserveAspectFit') self.speak_dialog("location.when", { "duration": duration, "toponym": self.location_pretty }, wait=True) sleep(1) self.gui.clear()
def annotate_duration(self, text): delta, value = _annotate_duration_en(text) if delta: data = { "days": delta.days, "seconds": delta.seconds, "microseconds": delta.microseconds, "total_seconds": delta.total_seconds(), "spoken": nice_duration(delta) } yield Entity(value, "duration", source_text=text, data=data)
def handle_space_launch_intent(self, message): try: r = requests.get("https://launchlibrary.net/1.2/launch/next/1" "").json() dt = datetime.strptime(r['launches'][0]["windowstart"], "%B %d, %Y %H:%M:%S UTC") image = r['launches'][0]["rocket"]["imageURL"] description = str(r['launches'][0]["missions"][0]["description"]) rocket = str(r['launches'][0]['rocket']['name']) location = str(r['launches'][0]['location']['pads'][0]['name']) now = datetime.now() delta = dt - now if delta <= timedelta(days=2): date_time = nice_duration(delta, lang=self.lang, speech=True) self.speak_dialog("space.launch.delta", data={ 'rocket': rocket, 'delta': date_time, 'location': location }) else: date_time = nice_date(dt, lang=self.lang, now=now) self.speak_dialog("space.launch", data={ 'rocket': rocket, 'time': date_time, 'location': location }) self.gui.show_image( image, caption=location, title=rocket, override_idle=True, fill='PreserveAspectFit', ) self.set_context("launch_description", description) self.set_context("rocketPic", image) self.set_context("rocket", rocket) except Exception as e: self.log.error(e) self.speak_dialog("not.found")
def test_nice_duration(self): self.assertEqual(nice_duration(1), "one second") self.assertEqual(nice_duration(3), "three seconds") self.assertEqual(nice_duration(1, speech=False), "0:01") self.assertEqual(nice_duration(61), "one minute one second") self.assertEqual(nice_duration(61, speech=False), "1:01") self.assertEqual(nice_duration(5000), "one hour twenty three minutes twenty seconds") self.assertEqual(nice_duration(5000, speech=False), "1:23:20") self.assertEqual(nice_duration(50000), "thirteen hours fifty three minutes twenty seconds") self.assertEqual(nice_duration(50000, speech=False), "13:53:20") self.assertEqual(nice_duration(500000), "five days eighteen hours fifty three minutes twenty seconds") # nopep8 self.assertEqual(nice_duration(500000, speech=False), "5d 18:53:20") self.assertEqual(nice_duration(datetime.timedelta(seconds=500000), speech=False), "5d 18:53:20")
def test_nice_duration(self): self.assertEqual(nice_duration(1), "jedna sekunda") self.assertEqual(nice_duration(3), "tři sekundy") self.assertEqual(nice_duration(1, speech=False), "0:01") self.assertEqual(nice_duration(61), "jedna minuta jedna sekunda") self.assertEqual(nice_duration(61, speech=False), "1:01") self.assertEqual(nice_duration(5000), "jedna hodina dvacet tři minuty dvacet sekundy") self.assertEqual(nice_duration(5000, speech=False), "1:23:20") self.assertEqual(nice_duration(50000), "třináct hodiny padesát tři minuty dvacet sekundy") self.assertEqual(nice_duration(50000, speech=False), "13:53:20") self.assertEqual(nice_duration(500000,), "pět dní osmnáct hodiny padesát tři minuty dvacet sekundy") # nopep8 self.assertEqual(nice_duration(500000, speech=False), "5d 18:53:20") self.assertEqual(nice_duration(datetime.timedelta(seconds=500000), speech=False), "5d 18:53:20")
# pronounce datetime objects import datetime dt = datetime.datetime(2017, 1, 31, 13, 22, 3) assert nice_date(dt) == "tuesday, january thirty-first, twenty seventeen" assert nice_time(dt) == "one twenty two" assert nice_time(dt, use_ampm=True) == "one twenty two p.m." assert nice_time(dt, speech=False) == "1:22" assert nice_time(dt, speech=False, use_ampm=True) == "1:22 PM" assert nice_time(dt, speech=False, use_24hour=True) == "13:22" assert nice_time(dt, speech=False, use_24hour=True, use_ampm=True) == "13:22" assert nice_time(dt, use_24hour=True, use_ampm=True) == "thirteen twenty two" assert nice_time(dt, use_24hour=True, use_ampm=False) == "thirteen twenty two" assert nice_date_time(dt) == "tuesday, january thirty-first, twenty seventeen at one twenty two" # pronounce durations assert nice_duration(1) == "one second" assert nice_duration(3) == "three seconds" assert nice_duration(1, speech=False) == "0:01" assert nice_duration(61), "one minute one second" assert nice_duration(61, speech=False) == "1:01" assert nice_duration(5000) == "one hour twenty three minutes twenty seconds" assert nice_duration(5000, speech=False), "1:23:20" assert nice_duration(50000) == "thirteen hours fifty three minutes twenty seconds" assert nice_duration(50000, speech=False) == "13:53:20" assert nice_duration(500000) == "five days eighteen hours fifty three minutes twenty seconds" assert nice_duration(500000, speech=False), "5d 18:53:20" assert nice_duration(datetime.timedelta(seconds=500000), speech=False) == "5d 18:53:20"
def test_nice_duration(self): self.assertEqual(nice_duration(1), "jedna sekunda") self.assertEqual(nice_duration(3), "trzy sekundy") self.assertEqual(nice_duration(1, speech=False), "0:01") self.assertEqual(nice_duration(61), "jedna minuta jedna sekunda") self.assertEqual(nice_duration(61, speech=False), "1:01") self.assertEqual( nice_duration(5000), "jedna godzina dwadzieścia trzy minuty dwadzieścia sekund") self.assertEqual(nice_duration(5000, speech=False), "1:23:20") self.assertEqual( nice_duration(50000), "trzynaście godzin pięćdziesiąt trzy minuty dwadzieścia sekund") self.assertEqual(nice_duration(50000, speech=False), "13:53:20") self.assertEqual(nice_duration( 500000 ), "pięć dni osiemnaście godzin pięćdziesiąt trzy minuty dwadzieścia sekund" ) # nopep8 self.assertEqual(nice_duration(500000, speech=False), "5d 18:53:20") self.assertEqual( nice_duration(datetime.timedelta(seconds=500000), speech=False), "5d 18:53:20")
def test_nice_duration(self): # TODO implement better plural support for nice_duration # Correct results are in comments self.assertEqual(nice_duration(1), "ena sekunda") self.assertEqual(nice_duration(2), "dve sekund") # dve sekundi self.assertEqual(nice_duration(3), "tri sekund") # tri sekunde self.assertEqual(nice_duration(4), "štiri sekund") # štiri sekunde self.assertEqual(nice_duration(5), "pet sekund") self.assertEqual(nice_duration(6), "šest sekund") self.assertEqual(nice_duration(1, speech=False), "0:01") self.assertEqual(nice_duration(61), "ena minuta ena sekunda") self.assertEqual(nice_duration(61, speech=False), "1:01") self.assertEqual(nice_duration(5000), "ena ura triindvajset minut dvajset sekund") self.assertEqual(nice_duration(5000, speech=False), "1:23:20") self.assertEqual(nice_duration(50000), "trinajst ur triinpetdeset minut dvajset sekund") self.assertEqual(nice_duration(50000, speech=False), "13:53:20") self.assertEqual( nice_duration(500000), "pet dni osemnajst ur triinpetdeset minut dvajset sekund" ) # nopep8 self.assertEqual(nice_duration(500000, speech=False), "5d 18:53:20") self.assertEqual( nice_duration(datetime.timedelta(seconds=500000), speech=False), "5d 18:53:20")