V
V
Vladislav Dudashvili2021-01-18 14:57:26
Python
Vladislav Dudashvili, 2021-01-18 14:57:26

How to get data through the Yandex Schedule API for the current day?

My code:

import json
import requests
from pprint import pprint


link = "https://api.rasp.yandex.net/v3.0/schedule/?apikey=f3a6c277-5bcc-48d9-95ed-6373ba1b6fa5&station=s2014001&transport_types=train&direction=departure&date=2021-01-18"
r = requests.get(link)
trains = json.loads(r.text)
for train in trains["schedule"]:
    print(f"Станция: {trains['station']['title']}")
    print(f"Отправка в {train['departure']}")
    print(f"Номер поезда: {train['thread']['number']}")
    print(f"Направление: {train['thread']['title']}\n") 
 


link = "https://api.rasp.yandex.net/v3.0/schedule/?apikey=f3a6c277-5bcc-48d9-95ed-6373ba1b6fa5&station=s2014001&transport_types=train&event=arrival&date=2021-01-18"
r = requests.get(link)
trains = json.loads(r.text)
for train in trains["schedule"]:
    print(f"Станция: {trains['station']['title']}")
    print(f"Прибытие в: {train['arrival']}")
    print(f"Номер поезда: {train['thread']['number']}")
    print(f"Направление: {train['thread']['title']}\n")

First question:
Please tell me how to get data from the Yandex schedule for today (the current day) without adding a date to the requests itself. I am writing a bot and I want it to display only the arrivals and departures of trains for today. In the Yandex documentation, I did not find commands like "today", it indicates that the date must be specified in the YYYY-MM-DD format.

The second question:
How can I make sure that when I display data, I receive only data that would be relevant at the moment, i.e. departure and stay of trains for this day, but whose time has already passed I do not need. We only need relevant data, from this table https://voronezhvokzal.ru/raspisanie-poezdov/
I'm a beginner, sorry for stupid questions ..
Here's what it displays when you request this:

Station: Voronezh-1
Departure at 2021-01-18T02:09:00+03:00
Train number: 382Ya
Direction: Moscow — Grozny

Station: Voronezh-1
Departure at 2021-01-18T04:10:00+03:00
Number train: 360H
Direction: Kaliningrad — Adler

Station: Voronezh-1
Departure at 2021-01-18T07:23:00+03:00
Train number: 034С
Direction: Moscow — Vladikavkaz

Station: Voronezh-1
Departure at 2021-01-18T07: 33:00+03:00
Train number: 737Ж
Direction: Voronezh — Moscow

Station: Voronezh-1
Departure at 2021-01-18T07:55:00+03:00
Train number: 170М
Direction: Moscow — Rosa Khutor

Station: Voronezh- one
Departure at 2021-01-18T12:55:00+03:00
Train number: 035А
Direction: St. Petersburg — Adler

Station: Voronezh-1
Departure at 2021-01-18T14:55:00+03:00
Train number: 084С
Direction: Adler – Moscow

Station: Voronezh-1
Departure at 2021-01-18T14:57:00+03:00
Train number: 036C
Direction: Adler – St. Petersburg

Station: Voronezh-1
Departure at 2021-01-18T16:21 :00+03:00
Train number: 739Ж
Direction: Voronezh — Moscow

Station: Voronezh-1
Departure at 2021-01-18T17:10:00+03:00
Train number: 175В
Direction: Voronezh — Kislovodsk

Station: Voronezh-1
Departure at 2021-01-18T18:04:00+03:00
Train number: 124В
Direction: Belgorod — Novosibirsk

Station: Voronezh-1
Departure at 2021-01-18T20:30:00+03:00
Train number: 025В
Direction: Voronezh — Moscow

Station: Voronezh-1
Departure at 2021-01-18T20 :55:00+03:00
Train number: 033С
Direction: Vladikavkaz — Moscow

Station: Voronezh-1
Departure at 2021-01-18T21:28:00+03:00
Train number: 170С
Direction: Rosa Khutor — Moscow

Station: Voronezh -1
Departure at 2021-01-18T23:50:00+03:00
Train number: 360С
Direction: Adler — Kaliningrad

Station: Voronezh-1
Departure at 2021-01-19T00:28:00+03:00
Train number: 382С
Direction: Grozny — Moscow

Station: Voronezh-1
Departure at 2021-01-19T02:00:00+03:00
Train number: 123Н
Direction: Novosibirsk — Belgorod

Station: Voronezh-1
Arrival at: 2021-01-18T01:24: 00+03:00
Train number: 382Я
Direction: Moscow — Grozny

Station: Voronezh-1
Arrival at: 2021-01-18T03:23:00+03:00
Train number: 360Ч
Direction: Kaliningrad — Adler

Station: Voronezh-1
Arrival at: 2021-01-18T06:48:00+03:00
Train number: 034С
Direction: Moscow — Vladikavkaz

Station: Voronezh-1
Arrival at: 2021-01-18T07:12:00+03:00
Train number: 170М
Direction : Moscow - Rosa Khutor

Station: Voronezh-1
Arrival at: 2021-01-18T08:30:00+03:00
Train number: 025Ya
Direction: Moscow - Voronezh

Station: Voronezh-1
Arrival at: 2021-01-18T09:30:00+03: 00
Train number: 175С
Direction: Kislovodsk — Voronezh

Station: Voronezh-1
Arrival at: 2021-01-18T12:14:00+03:00
Train number: 035А
Direction: St. Petersburg — Adler

Station: Voronezh-1
Arrival at: 2021-01-18T14:00:00+03:00
Train number: 084С
Direction: Adler — Moscow

Station: Voronezh-1
Arrival at: 2021-01-18T14:18:00+03:00
Train number: 036С
Direction: Adler — St. Petersburg

Station: Voronezh-1
Arrival at: 2021-01-18T14:39:00+03:00
Train number: 740Ж
Direction: Moscow — Voronezh

Station: Voronezh-1
Arrival at: 2021-01-18T16:41:00+03:00
Train number: 124В
Direction: Belgorod — Novosibirsk

Station: Voronezh-1
Arrival at: 2021-01-18T20:06:00+03:00
Train number: 033С
Direction: Vladikavkaz — Moscow

Station: Voronezh-1
Arrival at: 2021-01-18T20:46 :00+03:00
Train number: 170С
Direction: Rosa Khutor — Moscow

Station: Voronezh-1
Arrival at: 2021-01-18T22:55:00+03:00
Train number: 360С
Direction: Adler — Kaliningrad

Station: Voronezh- 1
Arrival at: 2021-01-18T23:12:00+03:00
Train number: 738Ж
Direction: Moscow — Voronezh

Station: Voronezh-1
Arrival at: 2021-01-18T23:22:00+03:00
Train number: 382С
Direction: Grozny — Moscow

Station: Voronezh-1
Arrival at: 2021-01 -19T00:20:00+03:00
Train number: 123N
Direction: Novosibirsk - Belgorod

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-01-18
@SoreMix

1. What is the problem to get the current data and transfer it?

from datetime import datetime, timedelta
today = datetime.now().strftime('%Y-%m-%d')

2. Compare the current date with the date from the schedule, and do not display if the flight has already passed
from datetime import datetime, timedelta
today_date = datetime.now()

link = "https://api.rasp.yandex.net/v3.0/schedule/?apikey=f3a6c277-5bcc-48d9-95ed-6373ba1b6fa5&station=s2014001&transport_types=train&direction=departure&date=2021-01-18"
r = requests.get(link)
trains = json.loads(r.text)
for train in trains["schedule"]:

    date, tz = train['departure'].split('+')

    date = datetime.strptime(date, '%Y-%m-%dT%H:%M:%S')
    tz = datetime.strptime(tz, '%H:%M')

    date = date + timedelta(hours=tz.hour)

    if date < today_date:
        print('Поезд ушел')

    print(f"Станция: {trains['station']['title']}")
    print(f"Отправка в {train['departure']}")
    print(f"Номер поезда: {train['thread']['number']}")
    print(f"Направление: {train['thread']['title']}\n")

maybe I made a mistake somewhere in the code

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question