From ade214af23b3e6828133a944a57bba02f85240a2 Mon Sep 17 00:00:00 2001 From: Jaroslav Drzik Date: Sat, 5 Mar 2022 14:22:28 +0100 Subject: [PATCH] Task 1 --- .idea/.gitignore | 3 + .idea/budapestKIWI.iml | 8 ++ .../inspectionProfiles/profiles_settings.xml | 6 ++ .idea/misc.xml | 4 + .idea/modules.xml | 8 ++ .idea/vcs.xml | 6 ++ main.py | 16 ++++ poetry.lock | 84 +++++++++++++++++++ pyproject.toml | 15 ++++ regiojet.py | 80 ++++++++++++++++++ 10 files changed, 230 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/budapestKIWI.iml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 main.py create mode 100644 poetry.lock create mode 100644 pyproject.toml create mode 100644 regiojet.py diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/budapestKIWI.iml b/.idea/budapestKIWI.iml new file mode 100644 index 0000000..d0876a7 --- /dev/null +++ b/.idea/budapestKIWI.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..758fc90 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..1eddd32 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..5596b44 --- /dev/null +++ b/main.py @@ -0,0 +1,16 @@ +# This is a sample Python script. + +# Press Shift+F10 to execute it or replace it with your code. +# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings. + + +def print_hi(name): + # Use a breakpoint in the code line below to debug your script. + print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint. + + +# Press the green button in the gutter to run the script. +if __name__ == '__main__': + print_hi('PyCharm') + +# See PyCharm help at https://www.jetbrains.com/help/pycharm/ diff --git a/poetry.lock b/poetry.lock new file mode 100644 index 0000000..0978063 --- /dev/null +++ b/poetry.lock @@ -0,0 +1,84 @@ +[[package]] +name = "certifi" +version = "2021.10.8" +description = "Python package for providing Mozilla's CA Bundle." +category = "main" +optional = false +python-versions = "*" + +[[package]] +name = "charset-normalizer" +version = "2.0.12" +description = "The Real First Universal Charset Detector. Open, modern and actively maintained alternative to Chardet." +category = "main" +optional = false +python-versions = ">=3.5.0" + +[package.extras] +unicode_backport = ["unicodedata2"] + +[[package]] +name = "idna" +version = "3.3" +description = "Internationalized Domain Names in Applications (IDNA)" +category = "main" +optional = false +python-versions = ">=3.5" + +[[package]] +name = "requests" +version = "2.27.1" +description = "Python HTTP for Humans." +category = "main" +optional = false +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, !=3.5.*" + +[package.dependencies] +certifi = ">=2017.4.17" +charset-normalizer = {version = ">=2.0.0,<2.1.0", markers = "python_version >= \"3\""} +idna = {version = ">=2.5,<4", markers = "python_version >= \"3\""} +urllib3 = ">=1.21.1,<1.27" + +[package.extras] +socks = ["PySocks (>=1.5.6,!=1.5.7)", "win-inet-pton"] +use_chardet_on_py3 = ["chardet (>=3.0.2,<5)"] + +[[package]] +name = "urllib3" +version = "1.26.8" +description = "HTTP library with thread-safe connection pooling, file post, and more." +category = "main" +optional = false +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, <4" + +[package.extras] +brotli = ["brotlipy (>=0.6.0)"] +secure = ["pyOpenSSL (>=0.14)", "cryptography (>=1.3.4)", "idna (>=2.0.0)", "certifi", "ipaddress"] +socks = ["PySocks (>=1.5.6,!=1.5.7,<2.0)"] + +[metadata] +lock-version = "1.1" +python-versions = "^3.10" +content-hash = "4ad0e50ab165d9914bab266bb7bbfd7d6107a5e888d859c1066445e4fd429f5c" + +[metadata.files] +certifi = [ + {file = "certifi-2021.10.8-py2.py3-none-any.whl", hash = "sha256:d62a0163eb4c2344ac042ab2bdf75399a71a2d8c7d47eac2e2ee91b9d6339569"}, + {file = "certifi-2021.10.8.tar.gz", hash = "sha256:78884e7c1d4b00ce3cea67b44566851c4343c120abd683433ce934a68ea58872"}, +] +charset-normalizer = [ + {file = "charset-normalizer-2.0.12.tar.gz", hash = "sha256:2857e29ff0d34db842cd7ca3230549d1a697f96ee6d3fb071cfa6c7393832597"}, + {file = "charset_normalizer-2.0.12-py3-none-any.whl", hash = "sha256:6881edbebdb17b39b4eaaa821b438bf6eddffb4468cf344f09f89def34a8b1df"}, +] +idna = [ + {file = "idna-3.3-py3-none-any.whl", hash = "sha256:84d9dd047ffa80596e0f246e2eab0b391788b0503584e8945f2368256d2735ff"}, + {file = "idna-3.3.tar.gz", hash = "sha256:9d643ff0a55b762d5cdb124b8eaa99c66322e2157b69160bc32796e824360e6d"}, +] +requests = [ + {file = "requests-2.27.1-py2.py3-none-any.whl", hash = "sha256:f22fa1e554c9ddfd16e6e41ac79759e17be9e492b3587efa038054674760e72d"}, + {file = "requests-2.27.1.tar.gz", hash = "sha256:68d7c56fd5a8999887728ef304a6d12edc7be74f1cfa47714fc8b414525c9a61"}, +] +urllib3 = [ + {file = "urllib3-1.26.8-py2.py3-none-any.whl", hash = "sha256:000ca7f471a233c2251c6c7023ee85305721bfdf18621ebff4fd17a8653427ed"}, + {file = "urllib3-1.26.8.tar.gz", hash = "sha256:0e7c33d9a63e7ddfcb86780aac87befc2fbddf46c58dbb487e0855f7ceec283c"}, +] diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..58ecd20 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,15 @@ +[tool.poetry] +name = "budapestkiwi" +version = "0.1.0" +description = "" +authors = ["Jaroslav Drzik "] + +[tool.poetry.dependencies] +python = "^3.10" +requests = "^2.27.1" + +[tool.poetry.dev-dependencies] + +[build-system] +requires = ["poetry-core>=1.0.0"] +build-backend = "poetry.core.masonry.api" diff --git a/regiojet.py b/regiojet.py new file mode 100644 index 0000000..8a367dc --- /dev/null +++ b/regiojet.py @@ -0,0 +1,80 @@ +import requests +import json +import pprint +import argparse +from datetime import date, datetime, time, timedelta + +url = "https://brn-ybus-pubapi.sa.cz" +tarrifs_path = "/restapi/consts/tariffs" +location_path = "/restapi/consts/locations" +route_path = "/restapi/routes/search/simple" + + +def json_serial(obj): + """JSON serializer for objects not serializable by default json code""" + if isinstance(obj, timedelta): + return str(obj) + if isinstance(obj, (datetime, date)): + return obj.isoformat() + raise TypeError("Type %s not serializable" % type(obj)) + + +def search_locations(country,city): + for location in locations: + if country == location['country']: + for _city in location['cities']: + if city == _city['name']: + return _city + + +def search_connection(from_station, to_station, tariff_type, to_location_type, from_location_type, departure): + r = requests.get(url + route_path, params={"tariffs_type": tariff_type, + "toLocationId": to_station["id"], + "fromLocationId": from_station["id"], + "fromLocationType": from_location_type, + "toLocationType": to_location_type, + "departureDate": departure}) + + routes = json.loads(r.content) + routes_ret = [] + for route in routes['routes']: + ret = {} + ret["departure_datetime"] = datetime.fromisoformat(route["departureTime"]) + ret["arrival_datetime"] = datetime.fromisoformat(route["arrivalTime"]) + ret["source"] = from_station["name"] + ret["destination"] = to_station["name"] + ret["source_id"] = from_station["id"] + ret["destination_id"] = to_station["id"] + ret["free_seats"] = route["freeSeatsCount"] + ret["carrier"] = "REGIOJET" + ret["type"] = route["vehicleTypes"][0] + ret["fare"] = { "amount": route["priceFrom"], "currency": "EUR"} + routes_ret.append(ret) + + return routes_ret + + +parser = argparse.ArgumentParser( + description='Search some connection') # use of ArgumentParser against of simple OptionParser +parser.add_argument("origin") +parser.add_argument("destination") +parser.add_argument("departure") +args = parser.parse_args() + + +r = requests.get(url + tarrifs_path) +tariffs = json.loads(r.content) + +r = requests.get(url + location_path) +locations = json.loads(r.content) + +city_from = search_locations('Czech Republic', args.origin) +city_to = search_locations('Czech Republic', args.destination) +#pprint.pp(city_from) +#pprint.pp(city_to) + + +ret = search_connection(city_from, city_to, 'REGULAR', 'CITY', 'CITY', args.departure) +print(json.dumps(ret, indent=4, default=json_serial, sort_keys=False)) + +