kennell / schiene Goto Github PK
View Code? Open in Web Editor NEW:train2: schiene is a Python library for interacting with Bahn.de
License: MIT License
:train2: schiene is a Python library for interacting with Bahn.de
License: MIT License
Hi,
I've got small question - I want to use this integration with HomeAssistant, but I have problems using it in Polish connections (Polish trains are visible on bahn.de).
I want to query for i.e. such connection:
https://reiseauskunft.bahn.de/bin/query.exe/dn?ld=43166&protocol=https:&seqnr=1&ident=i3.012020166.1659559405&rt=1&OK#hfsseq2|i3.012020166.1659559405
(starting point is Mysłowice and target is Katowice) and it works for "Myslowice"
I try to use it for "Mikołów" (Mikolow) and then there's no results at all, although there should be some:
I'm able to get data from:
import schiene
s = schiene.Schiene()
s.stations('Mikolow')
but not for
s.connections('Mikolow', 'Katowice')
Thanks for any help!
Hello,
Use HAFAS instead off scraping. It should be more reliable and approved by Home Assistant as proper integration again.
https://github.com/FahrplanDatenGarten/pyhafas
I am happy to try and help to change it. Do you see any issue in using pyhafas?
I am now trying to add it into schiene. If you can give me any hints, I'd be very happy.
Hi, your setup.py
seems to miss lxml as a required library (it is not automatically installed with BeautifulSoup4). Not a big deal, but you should probably add it (or use "html.parser", which afaik is always available).
Somehow it's working now. :)
Instead of marking a train as cancelled there are details which state that the train is cancelled, see screenshot.
Is that something that could be caught by the client? Not sure how, maybe report the comment so others (i.e. the home
assistant integration) could filter based on that? Not sure if the text is always the same though.
Hi! I'm working with the VBB public transport API, which is based on HAFAS. I wrote a JavaScript API client for it.
Deutsche Bahn uses HAFAS as well, so consider using the API instead of parsing a "steaming pile" of HTML. ;)
time: 0:55
arrival: 14:01
delay: 0
next_on: 13:29
ontime: false
icon: mdi:train
price: null
transfers: 1
products: RE,STR
friendly_name: Menden(Rheinl) to Bonn Gronau Olof-Palme-Allee
next: 13:29
departure: 13:06
As you can the the train is not ontime, but the delay is 0.
on the page www.bahn.de a delay of 20 minutes is shown.
i use this package with home-assistant.
Since i upgraded i have following error:
"/usr/local/lib/python3.5/dist-packages/homeassistant/components/sensor/deutsche_bahn.py", line 82, in update
"/usr/local/lib/python3.5/dist-packages/homeassistant/components/sensor/deutsche_bahn.py", line 105, in update
Here is the same error reported what i had a couple times in log:
self.data.update self.only_direct
Could you please fix it? :-)
Thanks a lot!
Hi there,
great package...it's really helpful.
Do you have any idea how I can do many requests at once?
thanks
I am playing with this project and sometimes i get an empty list from the connections()
function.
Is this due to requesting too often or due to some other error? Is there a specific limit?
File "/usr/local/lib/python3.4/site-packages/schiene/schiene.py", line 102, in connections
return parse_connections(rsp.text)
File "/usr/local/lib/python3.4/site-packages/schiene/schiene.py", line 33, in parse_connections
data['price'] = float(data['price'])
ValueError: could not convert string to float: 'ab\xa02.70 EUR'
Not working:
>>> s.connections('Hamburg Burgwedel', 'Hamburg-Eidelstedt')
[]
Working:
>>> s.connections('Hamburg Burgwedel', 'Hamburg-Eidelstedt Zentrum')
[{'details': '...snipped..., 'canceled': False}]
"Hamburg-Eidelstedt" and "Hamburg-Eidelstedt Zentrum" are different stations and I want to have the first one as my target stations but it does not work. Is there something I can do? I am actually using homeassistent but I tried this explicitly in a python shell to pinpoint the issue.
https://developer.deutschebahn.com/store/apis/info?name=Timetables&version=v1&provider=DBOpenData
What do you think? It would need registration by everyone who whats to use it, but we would get rid of the html paresing. In terms of stability and future proof ness it would get a lot better I think. For Home-Assistant I think it's the right way.
The Connection today from Stuttgart HBf to Ulm HBf at 10:51 was canceled (see. screenshot below), but connection is reported on time from schiene:
s.connections('Stuttgart HBf', 'Ulm HBf', datetime(2024, 4, 25, 10, 50, 0, 0))
[
{
"details": "https://mobile.bahn.de/bin/mobil/query.exe/dox?ld=4357&protocol=https:&n=1&i=dt.01855957.1714033988&rt=1&use_realtime_filter=1&co=C0-0&vca&HWAI=CONNECTION$C0-0!details=opened!detailsVerbund=opened!&",
"departure": "10:51",
"arrival": "11:34",
"transfers": 0,
"time": "0:43",
"products": ["ICE"],
"price": None,
"ontime": True,
"delay": {"delay_departure": 0, "delay_arrival": 0},
"canceled": False,
},
]
note: screenshot is taken from details-URL above, saved website is attached response.zip
I've used this API for one of my projects, but Marudor just made his HAFAS API online.
https://docs.marudor.de/#tag/HAFAS-V1
It has many more features and doesen't rely on some web scraping.
Just use some POST-Requests.
I just started getting this error-message when trying get a connection.
Other destinations seem to work (e.g. 'Mannheim HbF' to 'Stuttgart HbF').
Is there anything I can check?
`$ python3
Python 3.7.3 (default, Oct 31 2022, 14:04:00)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
import schiene
s = schiene.Schiene()
s.connections('Wernau(neckar)', 'Wendlingen(neckar)')
Traceback (most recent call last):
File "", line 1, in
File "/home/dev/.local/lib/python3.7/site-packages/schiene/schiene.py", line 150, in connections
return parse_connections(rsp.text)
File "/home/dev/.local/lib/python3.7/site-packages/schiene/schiene.py", line 35, in parse_connections
data = parse_delay(data)
File "/home/dev/.local/lib/python3.7/site-packages/schiene/schiene.py", line 65, in parse_delay
delay_departure_raw.text)
File "/home/dev/.local/lib/python3.7/site-packages/schiene/schiene.py", line 106, in calculate_delay
diff = fullday - original + delayed
TypeError: unsupported operand type(s) for -: 'int' and 'datetime.datetime'`
python
Python 2.7.15rc1 (default, Nov 12 2018, 14:31:15)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import schiene
s = schiene.Schiene()
s.connections('Wolfbusch, Stuttgart', 'Stuttgart-Feuerbach')
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python2.7/dist-packages/schiene/schiene.py", line 135, in connections
return parse_connections(rsp.text)
File "/usr/local/lib/python2.7/dist-packages/schiene/schiene.py", line 35, in parse_connections
data = parse_delay(data)
File "/usr/local/lib/python2.7/dist-packages/schiene/schiene.py", line 65, in parse_delay
delay_arrival_raw = soup.find('div', class_="routeEnd").find('span', class_=["delay", "delayOnTime"])
AttributeError: 'NoneType' object has no attribute 'find'
Hi, the code crashes when trying to query the next train to Vienna. The 'Wien Hbf' is a valid name for the station as returned by s.stations('Wien')
s.connections('München','Wien Hbf')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "schiene.py", line 135, in connections
return parse_connections(rsp.text)
File "schiene.py", line 35, in parse_connections
data = parse_delay(data)
File "schiene.py", line 57, in parse_delay
delay_departure_raw = soup.find('div', class_="routeStart").find('span', class_=["delay", "delayOnTime"])
AttributeError: 'NoneType' object has no attribute 'find'
Hey
I'm experimenting with your API and in general it is working. However when I make a
import schiene
data = schiene.Schiene()
res = data.connections('start', 'goal')
call, it returns a list of the next four trains for this connection. I think res[0] should be the next connection, however I usually see tat res[0] is the one after the next, and the next connecton is not
pulled in at all.
I tried using Schiene as a home assistant sensor but after the first try—it did work for a brief amount of time—, the underlying library stopped working.
The problematic connection for me has been from Leverkusen Mitte to Köln Hansaring
deutsche_bahn: Error on device update!
Traceback (most recent call last):
File "/usr/src/app/homeassistant/helpers/entity_component.py", line 397, in _async_add_entity
yield from entity.async_device_update(warning=False)
File "/usr/src/app/homeassistant/helpers/entity.py", line 308, in async_device_update
yield from self.hass.async_add_job(self.update)
File "/usr/local/lib/python3.6/asyncio/futures.py", line 327, in __iter__
yield self # This tells Task to wait for completion.
File "/usr/local/lib/python3.6/asyncio/tasks.py", line 250, in _wakeup
future.result()
File "/usr/local/lib/python3.6/asyncio/futures.py", line 243, in result
raise self._exception
File "/usr/local/lib/python3.6/concurrent/futures/thread.py", line 56, in run
result = self.fn(*self.args, **self.kwargs)
File "/usr/src/app/homeassistant/components/sensor/deutsche_bahn.py", line 78, in update
self.data.update()
File "/usr/src/app/homeassistant/components/sensor/deutsche_bahn.py", line 99, in update
self.start, self.goal, dt_util.as_local(dt_util.utcnow()))
File "/usr/local/lib/python3.6/site-packages/schiene/schiene.py", line 135, in connections
return parse_connections(rsp.text)
File "/usr/local/lib/python3.6/site-packages/schiene/schiene.py", line 35, in parse_connections
data = parse_delay(data)
File "/usr/local/lib/python3.6/site-packages/schiene/schiene.py", line 57, in parse_delay
delay_departure_raw = soup.find('div', class_="routeStart").find('span', class_="delay")
AttributeError: 'NoneType' object has no attribute 'find'
I tried using Schiene directly using the command line but the same error arose.
>>> import schiene
>>> s = schiene.Schiene()
>>> s.connections('Leverkusen Mitte', 'Hansaring, Köln')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.6/site-packages/schiene/schiene.py", line 135, in connections
return parse_connections(rsp.text)
File "/usr/local/lib/python3.6/site-packages/schiene/schiene.py", line 35, in parse_connections
data = parse_delay(data)
File "/usr/local/lib/python3.6/site-packages/schiene/schiene.py", line 57, in parse_delay
delay_departure_raw = soup.find('div', class_="routeStart").find('span', class_="delay")
AttributeError: 'NoneType' object has no attribute 'find'
If there are more than 24 hours between start and arrival the "time" value is not accurate. It does not take day changes into account.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.