1
0
mirror of https://github.com/janvarev/Irene-Voice-Assistant.git synced 2025-11-23 22:45:08 +02:00
Files
Irene-Voice-Assistant/plugins/plugin_yandex_rasp.py

93 lines
2.6 KiB
Python

# Яндекс.Расписания - работает точно для электричек
# author: Vladislav Janvarev
import os
from vacore import VACore
modname = os.path.basename(__file__)[:-3] # calculating modname
# функция на старте
def start(core:VACore):
manifest = {
"name": "Яндекс Расписания",
"version": "1.0",
"require_online": True,
"default_options": {
"apiKey": "", # get at https://yandex.ru/dev/rasp/raspapi/
"from": "s9600681",
"to1": "s2000002",
},
"commands": {
"ближайший поезд|электричка|электрички": run_poezd,
}
}
return manifest
def start_with_options(core:VACore, manifest:dict):
pass
def run_poezd(core:VACore, phrase:str):
options = core.plugin_options(modname)
if options["apiKey"] == "":
core.play_voice_assistant_speech("Нужен ключ апи для получения расписания")
return
try:
# datetime
import datetime
now = datetime.datetime.now().__str__()
print(now)
from datetime import date
current_date = date.today().__str__()
import requests
res = requests.get("https://api.rasp.yandex.net/v3.0/search/",
params={'from': options["from"], 'to': options["to1"], 'format':'json',
'date': current_date,
'apikey': options["apiKey"]})
data = res.json()
print(data)
cnt = 1
txt = ""
for segment in data["segments"]:
dep = str(segment["departure"]).replace("T"," ")
if dep > now:
hours = dep[11:13]
min = dep[14:16]
if cnt == 1:
txt += "Ближайшая электричка в {0} {1}. ".format(hours,min)
#print(txt)
cnt += 1
elif cnt == 2:
txt += "Следующая в {0} {1}. ".format(hours,min)
#print(txt)
cnt += 1
elif cnt == 3:
txt += "Дальше в {0} {1}. ".format(hours,min)
#print(txt)
cnt += 1
break
#print(dep)
print(txt)
core.play_voice_assistant_speech(txt)
except:
import traceback
traceback.print_exc()
core.play_voice_assistant_speech("Проблемы с расписанием. Посмотрите логи")
return