### Install Korail2 from source Source: https://github.com/carpedm20/korail2/blob/master/docs/index.md Manual installation of korail2 by cloning the repository and running the setup script. This method is useful for developers who want to modify the library or contribute to its development. ```shell $ git clone git://github.com/carpedm20/korail2.git $ cd korail2 $ python setup.py install ``` -------------------------------- ### Install Korail2 using pip or easy_install Source: https://github.com/carpedm20/korail2/blob/master/docs/index.md Instructions for installing the korail2 library using package managers like pip or easy_install. This is the simplest way to get started with the library. ```shell $ pip install korail2 ``` ```shell $ easy_install korail2 ``` -------------------------------- ### Install Korail2 manually from source Source: https://github.com/carpedm20/korail2/blob/master/docs/_build/html/_sources/index.txt This snippet outlines the manual installation process for korail2 by cloning the repository and running the setup script. This method is useful for development or when direct access to the source code is needed. ```bash git clone git://github.com/carpedm20/korail2.git cd korail2 python setup.py install ``` -------------------------------- ### Install Korail2 using easy_install Source: https://github.com/carpedm20/korail2/blob/master/docs/_build/html/_sources/index.txt This snippet demonstrates installing the korail2 library using the easy_install command. This is an alternative to pip for package installation. ```bash easy_install korail2 ``` -------------------------------- ### Install Korail2 using pip Source: https://github.com/carpedm20/korail2/blob/master/docs/_build/html/_sources/index.txt This snippet shows how to install the korail2 library using the pip package installer. Ensure pip is installed and up-to-date. ```bash pip install korail2 ``` -------------------------------- ### Install Korail2 Package Source: https://github.com/carpedm20/korail2/blob/master/README.rst Instructions on how to install the korail2 library using pip, easy_install, or manual installation via source code. ```bash $ pip install korail2 ``` ```bash $ easy_install korail2 ``` ```bash $ git clone git://github.com/carpedm20/korail2.git $ cd korail2 $ python setup.py install ``` -------------------------------- ### Specify Train Types in Korail2 Search Python Source: https://context7.com/carpedm20/korail2/llms.txt Demonstrates how to filter train searches by specific train types using the Korail2 Python library. Examples include searching for KTX, ITX-Saemaeul, and all train types. Lists the available `TrainType` options for customization. ```python from korail2 import Korail, TrainType korail = Korail("12345678", "your_password") # Search for KTX trains only (high-speed rail) ktx_trains = korail.search_train( "서울", "부산", "20240815", "100000", train_type=TrainType.KTX ) # Search for ITX-Saemaeul trains itx_trains = korail.search_train( "서울", "부산", "20240815", "100000", train_type=TrainType.ITX_SAEMAEUL ) # Search all train types (default) all_trains = korail.search_train( "서울", "부산", "20240815", "100000", train_type=TrainType.ALL ) # Available train types: # TrainType.KTX - KTX, KTX-산천 # TrainType.SAEMAEUL - 새마을호 # TrainType.MUGUNGHWA - 무궁화호 # TrainType.TONGGUEN - 통근열차 # TrainType.NURIRO - 누리로 # TrainType.ALL - 전체 ``` -------------------------------- ### Define passenger types for train search in korail2 Python Source: https://github.com/carpedm20/korail2/blob/master/README.md This section explains how to create lists of Passenger Objects to search for train schedules for multiple passengers. It covers `AdultPassenger`, `ChildPassenger`, and `SeniorPassenger` with examples of combinations and handling of counts. ```python # for 1 adult, 1 child psgrs = [AdultPassenger(), ChildPassenger()] # for 2 adults, 1 child psgrs = [AdultPassenger(2), ChildPassenger(1)] # ditto. They are being added each other by same group. psgrs = [AdultPassenger(), AdultPassenger(), ChildPassenger()] # for 2 adults, 1 child, 1 senior psgrs = [AdultPassenger(2), ChildPassenger(), SeniorPassenger()] # for 1 adult, It supports negative count or zero count. # But it uses passengers which the sum is greater than zero. psgrs = [AdultPassenger(2), AdultPassenger(-1)] psgrs = [AdultPassenger(), SeniorPassenger(0)] # Nothing psgrs = [AdultPassenger(0), SeniorPassenger(0)] ``` -------------------------------- ### Login to Korail using Korail2 Python Source: https://github.com/carpedm20/korail2/blob/master/README.md These examples show how to create a Korail object for logging into the Korail service. You can log in using membership number, email, or phone number. The `auto_login` parameter can be set to `False` to perform login manually. ```python from korail2 import * korail = Korail("12345678", YOUR_PASSWORD) # with membership number korail = Korail("carpedm20@gmail.com", YOUR_PASSWORD) # with email korail = Korail("010-9964-xxxx", YOUR_PASSWORD) # with phone number ``` ```python korail = Korail("12345678", YOUR_PASSWORD, auto_login=False) korail.login() ``` ```python korail.login(ANOTHER_ID, ANOTHER_PASSWORD) ``` -------------------------------- ### Search All Trains for Entire Day Source: https://context7.com/carpedm20/korail2/llms.txt Search all available trains from a specific time until the end of the day using repeated API calls. This is useful for getting a comprehensive list of all possible train departures. ```APIDOC ## Search All Trains for Entire Day ### Description Searches for all available trains from a specific departure time until the end of the day by making sequential API calls. This is useful when you need a complete list of train options for a given route and date, including those that might not appear in a single, time-limited search. ### Method GET (Implicit via `search_train_allday` method) ### Endpoint N/A (Internal Korail API calls) ### Parameters #### `search_train_allday` Parameters - **dep** (string) - Required - Departure station name (e.g., "서울"). - **arr** (string) - Required - Arrival station name (e.g., "부산"). - **date** (string) - Required - Departure date in `yyyyMMdd` format (e.g., "20240815"). - **start_time** (string) - Required - The initial time to start searching from, in `hhmmss` format (e.g., "100000" for 10:00:00). - **include_no_seats** (boolean) - Optional - If true, includes trains that are sold out in the results. Defaults to false. ### Request Example ```python from korail2 import Korail, NoResultsError korail = Korail("12345678", "your_password") try: # Search all trains after 10:00 AM all_trains = korail.search_train_allday("서울", "부산", "20240815", "100000") print(f"Found {len(all_trains)} trains") for train in all_trains: print(f"{train.train_type_name}: {train.dep_time} -> {train.arr_time}") # Filter only trains with available seats (default behavior) # For all trains including sold-out: all_trains_including_sold_out = korail.search_train_allday( "서울", "부산", "20240815", "100000", include_no_seats=True ) except NoResultsError: print("No trains found for this route/date") ``` ### Response #### Success Response (200) A list of `Train` objects representing all trains found for the entire day from the specified start time. Returns an empty list if no trains are found. #### Response Example ```json [ { "train_no": "101", "train_type_name": "KTX", "dep_station_name": "서울", "arr_station_name": "부산", "dep_time": "103000", "arr_time": "131000", "general_seat_available": true, "special_seat_available": true, "waiting_list_available": false }, { "train_no": "103", "train_type_name": "KTX", "dep_station_name": "서울", "arr_station_name": "부산", "dep_time": "110000", "arr_time": "134000", "general_seat_available": false, "special_seat_available": true, "waiting_list_available": true } ] ``` #### Error Response - **404 NoResultsError**: If no trains are found for the specified route and date. ``` -------------------------------- ### Python: Get Ticket Information Source: https://github.com/carpedm20/korail2/blob/master/docs/_build/html/index.html Retrieves information about purchased tickets. The `tickets` method returns a list of tickets, including details such as train type, date, route, seat, and price. ```python tickets = k.tickets() print(tickets) ``` -------------------------------- ### Python: Search All Trains for Entire Day with Korail2 Source: https://context7.com/carpedm20/korail2/llms.txt Searches for all available trains from a specified departure time until the end of the day by making repeated API calls. It can optionally include trains that are sold out. Requires the 'korail2' library and handles 'NoResultsError'. Input includes station names, date, and start time. Outputs a list of all found trains or a message if none are found. ```python from korail2 import Korail, NoResultsError korail = Korail("12345678", "your_password") try: # Search all trains after 10:00 AM all_trains = korail.search_train_allday("서울", "부산", "20240815", "100000") print(f"Found {len(all_trains)} trains") for train in all_trains: print(f"{train.train_type_name}: {train.dep_time} -> {train.arr_time}") # Filter only trains with available seats (default behavior) # For all trains including sold-out: all_trains_including_sold_out = korail.search_train_allday( "서울", "부산", "20240815", "100000", include_no_seats=True ) except NoResultsError: print("No trains found for this route/date") ``` -------------------------------- ### Make Train Reservations with Korail2 Python Source: https://context7.com/carpedm20/korail2/llms.txt Demonstrates how to reserve train seats using the Korail2 Python library. It covers default reservations, reservations with seat class preferences, handling multiple passengers, and using a waiting list. Includes error handling for sold-out trains. ```python from korail2 import Korail, ReserveOption, SoldOutError, AdultPassenger, ChildPassenger korail = Korail("12345678", "your_password") # Search for trains trains = korail.search_train("서울", "부산", "20240815", "144000") try: # Reserve with default option (general seat first) reservation = korail.reserve(trains[0]) print(reservation) # Output: [KTX] 8월 15일, 서울~부산(14:40~17:23) 42500원(1석), 구입기한 8월 20일 14:05 print(f"Reservation ID: {reservation.rsv_id}") print(f"Price: {reservation.price}") print(f"Seat count: {reservation.seat_no_count}") print(f"Buy limit: {reservation.buy_limit_date} {reservation.buy_limit_time}") # Reserve with seat class preference reservation = korail.reserve(trains[0], option=ReserveOption.SPECIAL_FIRST) # Options: GENERAL_FIRST, GENERAL_ONLY, SPECIAL_FIRST, SPECIAL_ONLY # Reserve for multiple passengers passengers = [AdultPassenger(2), ChildPassenger(1)] reservation = korail.reserve(trains[0], passengers=passengers) print(f"Reserved {reservation.seat_no_count} seats") # Reserve with waiting list fallback reservation = korail.reserve(trains[0], try_waiting=True) except SoldOutError: print("Train is sold out and no waiting list available") ``` -------------------------------- ### Initialize Korail Object for Login Source: https://github.com/carpedm20/korail2/blob/master/README.rst Demonstrates how to create a Korail object for automatic login using membership number, email, or phone number. Includes an option to disable automatic login and perform login manually. ```python from korail2 import * korail = Korail("12345678", YOUR_PASSWORD) # with membership number korail = Korail("carpedm20@gmail.com", YOUR_PASSWORD) # with email korail = Korail("010-9964-xxxx", YOUR_PASSWORD) # with phone number # Without auto-login korail = Korail("12345678", YOUR_PASSWORD, auto_login=False) korail.login() True # Change ID using existing object korail.login(ANOTHER_ID, ANOTHER_PASSWORD) True ``` -------------------------------- ### Initialize Korail object for authentication Source: https://github.com/carpedm20/korail2/blob/master/docs/_build/html/_sources/index.txt This Python snippet shows how to initialize the Korail object with different authentication methods: membership number, email, or phone number. Replace 'YOUR_PASSWORD' with the actual password. ```python from korail2 import Korail # with membership number korail = Korail("12345678", "YOUR_PASSWORD") # with email korail = Korail("carpedm20@gmail.com", "YOUR_PASSWORD") # with phone number korail = Korail("010-9964-xxxx", "YOUR_PASSWORD") ``` -------------------------------- ### View Existing Reservations with Korail2 Python Source: https://context7.com/carpedm20/korail2/llms.txt Shows how to retrieve a list of all current, unpurchased train reservations using the Korail2 Python library. It iterates through the reservations and prints details such as reservation ID, route, train information, and purchase deadlines. Includes error handling for cases where no reservations are found. ```python from korail2 import Korail, NoResultsError korail = Korail("12345678", "your_password") try: reservations = korail.reservations() if reservations: print(f"You have {len(reservations)} active reservations:") for rsv in reservations: print(f"\nReservation ID: {rsv.rsv_id}") print(f"Route: {rsv.dep_name} -> {rsv.arr_name}") print(f"Train: {rsv.train_type_name} #{rsv.train_no}") print(f"Departure: {rsv.dep_date} {rsv.dep_time}") print(f"Arrival: {rsv.arr_date} {rsv.arr_time}") print(f"Price: {rsv.price} KRW") print(f"Seats: {rsv.seat_no_count}") print(f"Buy by: {rsv.buy_limit_date} {rsv.buy_limit_time}") else: print("No active reservations") except NoResultsError: print("No reservations found") ``` -------------------------------- ### Search for train schedules Source: https://github.com/carpedm20/korail2/blob/master/docs/_build/html/_sources/index.txt This Python snippet demonstrates how to use the `search_train` method to find train schedules. It requires departure ('dep') and arrival ('arr') stations, with optional arguments for date, time, and train type. The result is a list of available trains. ```python dep = '서울' arr = '동대구' date = '20140815' time = '144000' trains = korail.search_train(dep, arr, date, time) # The output is a list of strings, each describing a train, e.g.: # [[KTX] 8월 3일, 서울~부산(11:00~13:42) [특실:1][일반실:1] 예약가능] ``` -------------------------------- ### Configure Passenger Types Source: https://context7.com/carpedm20/korail2/llms.txt Define passenger groups with different types (adult, child, toddler, senior) and apply discounts or special fares. Passenger types can be mixed and will be automatically merged by type. ```APIDOC ## Configure Passenger Types ### Description Allows for the configuration of passenger groups, including different types such as adults, children, toddlers, and seniors. This enables the application of specific fares or discounts for each passenger category. The library automatically merges passengers of the same type when multiple instances are provided. ### Method N/A (Configuration occurs during object instantiation or when passing passenger lists to search methods) ### Endpoint N/A ### Parameters Passenger objects can be created using the following classes: - **AdultPassenger(count=1, ...)**: Represents adult passengers. Optional parameters for discount_type, card, card_no, and card_pw can be provided for discounted fares. - **ChildPassenger(count=1)**: Represents child passengers. - **ToddlerPassenger(count=1)**: Represents toddler passengers. - **SeniorPassenger(count=1)**: Represents senior passengers. #### Discount Parameters (for `AdultPassenger`) - **discount_type** (string) - Optional - Code representing the discount type. - **card** (string) - Optional - Code for a specific discount card. - **card_no** (string) - Optional - Card number. - **card_pw** (string) - Optional - Card password. ### Request Example ```python from korail2 import AdultPassenger, ChildPassenger, ToddlerPassenger, SeniorPassenger # Single adult (default) passengers = [AdultPassenger()] # Multiple passengers of same type passengers = [AdultPassenger(2), ChildPassenger(1)] # Mixed passenger types passengers = [ AdultPassenger(2), # 2 adults ChildPassenger(1), # 1 child ToddlerPassenger(1), # 1 toddler SeniorPassenger(1) # 1 senior ] # Passengers are automatically merged by type passengers = [AdultPassenger(), AdultPassenger(), ChildPassenger()] # Equivalent to: [AdultPassenger(2), ChildPassenger(1)] # Use in search # Assuming 'korail' is an authenticated Korail object # trains = korail.search_train("서울", "부산", "20240815", "100000", passengers=passengers) # Example of applying discount card (structure only) adult_with_card = AdultPassenger( count=1, discount_type='000', # Discount type code card='', # Card code card_no='', # Card number card_pw='' ) ``` ### Response #### Success Response Passenger configurations are used as parameters in other API calls (e.g., `search_train`). The response is determined by the method utilizing these passenger configurations. #### Response Example N/A (Configuration is used internally) ``` -------------------------------- ### Cancel Reservations with Korail2 Python Source: https://context7.com/carpedm20/korail2/llms.txt Provides Python code using the Korail2 library to cancel unpaid train reservations. It demonstrates how to fetch existing reservations, cancel a specific one, and then cancel all remaining reservations. Includes verification of cancellation by checking the remaining reservation count. ```python from korail2 import Korail korail = Korail("12345678", "your_password") # Get all reservations reservations = korail.reservations() if reservations: # Cancel specific reservation reservation_to_cancel = reservations[0] success = korail.cancel(reservation_to_cancel) if success: print(f"Cancelled reservation {reservation_to_cancel.rsv_id}") # Cancel all reservations for rsv in korail.reservations(): korail.cancel(rsv) print(f"Cancelled: {rsv.dep_name} -> {rsv.arr_name}") # Verify cancellation remaining = korail.reservations() print(f"Remaining reservations: {len(remaining)}") ``` -------------------------------- ### Search Train Schedules Source: https://context7.com/carpedm20/korail2/llms.txt Search for available trains between two stations with optional filters for date, time, train type, and passenger configuration. Supports searching for specific train types and checking seat availability. ```APIDOC ## Search Train Schedules ### Description Search for available trains between two stations with optional filters for date, time, train type, and passenger configuration. This function allows specifying departure and arrival stations, date, time, and can filter by train type and passenger count. ### Method GET (Implicit via `search_train` method) ### Endpoint N/A (Internal Korail API calls) ### Parameters #### `search_train` Parameters - **dep** (string) - Required - Departure station name (e.g., "서울"). - **arr** (string) - Required - Arrival station name (e.g., "부산"). - **date** (string) - Required - Departure date in `yyyyMMdd` format (e.g., "20240815"). - **time** (string) - Required - Departure time in `hhmmss` format (e.g., "144000" for 14:40:00). - **train_type** (TrainType enum) - Optional - Filter by train type (e.g., `TrainType.KTX`). Defaults to all types. - **passengers** (list) - Optional - A list of passenger objects (e.g., `AdultPassenger`, `ChildPassenger`). Defaults to one adult. - **include_no_seats** (boolean) - Optional - If true, includes trains that are sold out in the results. Defaults to false. ### Request Example ```python from korail2 import Korail, TrainType, AdultPassenger, ChildPassenger, SeniorPassenger korail = Korail("12345678", "your_password") # Basic search (returns up to 10 results) dep = "서울" arr = "부산" date = "20240815" time = "144000" trains = korail.search_train(dep, arr, date, time) for train in trains: print(train) # Search specific train type ktx_trains = korail.search_train(dep, arr, date, time, train_type=TrainType.KTX) # Search with multiple passengers passengers = [AdultPassenger(2), ChildPassenger(1)] trains = korail.search_train(dep, arr, date, time, passengers=passengers) # Include sold-out trains in results all_trains = korail.search_train(dep, arr, date, time, include_no_seats=True) # Check train seat availability for train in trains: if train.has_general_seat(): print(f"Train {train.train_no} has general seats") if train.has_special_seat(): print(f"Train {train.train_no} has special seats") if train.has_waiting_list(): print(f"Train {train.train_no} has waiting list available") ``` ### Response #### Success Response (200) A list of `Train` objects, each containing details about an available train, including schedule, type, and seat availability. #### Response Example ```json [ { "train_no": "101", "train_type_name": "KTX", "dep_station_name": "서울", "arr_station_name": "부산", "dep_time": "144000", "arr_time": "172300", "general_seat_available": true, "special_seat_available": false, "waiting_list_available": false } ] ``` ``` -------------------------------- ### Search for Train Schedules Source: https://github.com/carpedm20/korail2/blob/master/README.rst Shows how to search for train schedules using the `search_train` method. It accepts departure and arrival stations, date, time, and optionally train type and passenger information. The `include_no_seats` parameter can be used to see sold-out trains. ```python dep = '서울' arr = '동대구' date = '20140815' time = '144000' trains = korail.search_train(dep, arr, date, time) # Example output: # [[KTX] 8월 3일, 서울~부산(11:00~13:42) 특실,일반실 예약가능, # [ITX-새마을] 8월 3일, 서울~부산(11:04~16:00) 일반실 예약가능, # [KTX] 8월 3일, 서울~부산(12:00~14:43) 특실,일반실 예약가능, # [KTX] 8월 3일, 서울~부산(12:30~15:13) 특실,일반실 예약가능, # [KTX] 8월 3일, 서울~부산(12:40~15:45) 특실,일반실 예약가능, # [KTX] 8월 3일, 서울~부산(12:55~15:26) 특실,일반실 예약가능, # [KTX] 8월 3일, 서울~부산(13:00~15:37) 특실,일반실 예약가능, # [KTX] 8월 3일, 서울~부산(13:10~15:58) 특실,일반실 예약가능] # To see sold-out trains: trains = korail.search_train(dep, arr, date, time, include_no_seats=True) # Example output: # [[KTX] 8월 3일, 서울~부산(11:00~13:42) 특실,일반실 예약가능, # [ITX-새마을] 8월 3일, 서울~부산(11:04~16:00) 일반실 예약가능, # [무궁화호] 8월 3일, 서울~부산(11:08~16:54) 입석 역발매중, # [ITX-새마을] 8월 3일, 서울~부산(11:50~16:50) 입석 역발매중, # [KTX] 8월 3일, 서울~부산(12:00~14:43) 특실,일반실 예약가능, # [KTX] 8월 3일, 서울~부산(12:30~15:13) 특실,일반실 예약가능, # [KTX] 8월 3일, 서울~부산(12:40~15:45) 특실,일반실 예약가능, # [KTX] 8월 3일, 서울~부산(12:55~15:26) 특실,일반실 예약가능, # [KTX] 8월 3일, 서울~부산(13:00~15:37) 특실,일반실 예약가능, # [KTX] 8월 3일, 서울~부산(13:10~15:58) 특실,일반실 예약가능] ``` -------------------------------- ### Retrieve already paid tickets Source: https://github.com/carpedm20/korail2/blob/master/docs/index.md Illustrates how to fetch a list of tickets that have already been paid for using the `tickets` method. The output includes details about the ticket, such as train information and price. ```python tickets = korail.tickets() print tickets ``` -------------------------------- ### View Purchased Tickets with Korail2 Python Source: https://context7.com/carpedm20/korail2/llms.txt Illustrates how to retrieve a list of purchased train tickets using the Korail2 Python library. The code iterates through the tickets, displaying details such as train information, route, dates, times, seat assignments, price, and buyer name. Includes basic error handling. ```python from korail2 import Korail korail = Korail("12345678", "your_password", want_feedback=True) try: tickets = korail.tickets() if tickets: print(f"You have {len(tickets)} tickets:") for ticket in tickets: print(f"\nTrain: {ticket.train_type_name} #{ticket.train_no}") print(f"Route: {ticket.dep_name} -> {ticket.arr_name}") print(f"Date: {ticket.dep_date}") print(f"Departure: {ticket.dep_time}") print(f"Arrival: {ticket.arr_time}") print(f"Car: {ticket.car_no}, Seat: {ticket.seat_no}") print(f"Price: {ticket.price} KRW") print(f"Buyer: {ticket.buyer_name}") print(f"Sale date: {ticket.sale_date}") print(f"Ticket number: {ticket.get_ticket_no()}") else: print("No tickets found") except Exception as e: print(f"Error retrieving tickets: {e}") ``` -------------------------------- ### Define Passenger Types for Search Source: https://github.com/carpedm20/korail2/blob/master/README.rst Illustrates how to define lists of Passenger Objects (AdultPassenger, ChildPassenger, SeniorPassenger) to search for trains accommodating multiple passengers. The counts can be positive, negative, or zero. ```python # for 1 adult, 1 child psgrs = [AdultPassenger(), ChildPassenger()] # for 2 adults, 1 child psgrs = [AdultPassenger(2), ChildPassenger(1)] # or by adding instances psgrs = [AdultPassenger(), AdultPassenger(), ChildPassenger()] # for 2 adults, 1 child, 1 senior psgrs = [AdultPassenger(2), ChildPassenger(), SeniorPassenger()] # Supports negative or zero counts, but uses passengers with sum > 0 psgrs = [AdultPassenger(2), AdultPassenger(-1)] psgrs = [AdultPassenger(), SeniorPassenger(0)] # Nothing (all counts are zero) psgrs = [AdultPassenger(0), SeniorPassenger(0)] # Then search or reserve train trains = korail.search_train(dep, arr, date, time, passengers=psgrs) korail.reserve(trains[0], psgrs) ``` -------------------------------- ### Python: Initialize Korail Object Source: https://github.com/carpedm20/korail2/blob/master/docs/_build/html/index.html Initializes a Korail object for interacting with the Korail service. It supports authentication using membership number, email, or phone number. Ensure you replace 'YOUR_PASSWORD' with the actual password. ```python from korail2 import Korail korail = Korail("12345678", YOUR_PASSWORD) # with membership number korail = Korail("carpedm20@gmail.com", YOUR_PASSWORD) # with email korail = Korail("010-9964-xxxx", YOUR_PASSWORD) # with phone number ``` -------------------------------- ### Python: Search Train Schedules with Korail2 Source: https://context7.com/carpedm20/korail2/llms.txt Searches for available trains between specified departure and arrival stations, with options to filter by date, time, train type, and passenger configuration. It can also include sold-out trains and check seat availability. Requires the 'korail2' library and its associated enums/classes. Input includes station names, date/time strings, and optional passenger lists. Outputs a list of train objects or an empty list if no trains are found. ```python from korail2 import Korail, TrainType, AdultPassenger, ChildPassenger, SeniorPassenger korail = Korail("12345678", "your_password") # Basic search (returns up to 10 results) dep = "서울" arr = "부산" date = "20240815" # yyyyMMdd format time = "144000" # hhmmss format trains = korail.search_train(dep, arr, date, time) for train in trains: print(train) # Output: [KTX] 8월 15일, 서울~부산(14:40~17:23) 특실,일반실 예약가능 # Search specific train type ktx_trains = korail.search_train(dep, arr, date, time, train_type=TrainType.KTX) # Search with multiple passengers passengers = [AdultPassenger(2), ChildPassenger(1)] trains = korail.search_train(dep, arr, date, time, passengers=passengers) # Include sold-out trains in results all_trains = korail.search_train(dep, arr, date, time, include_no_seats=True) # Check train seat availability for train in trains: if train.has_general_seat(): print(f"Train {train.train_no} has general seats") if train.has_special_seat(): print(f"Train {train.train_no} has special seats") if train.has_waiting_list(): print(f"Train {train.train_no} has waiting list available") ``` -------------------------------- ### Reserve a train seat Source: https://github.com/carpedm20/korail2/blob/master/docs/_build/html/_sources/index.txt This Python snippet shows how to reserve a train seat using the `reserve` method after searching for trains. It takes a train object as input and returns a confirmation message and details of the reserved seat. Note that successful reservation might also include messages about existing bookings. ```python trains = korail.search_train(dep, arr, date, time) seat = korail.reserve(trains[0]) # Example output: # 정상처리되었습니다 # 동일시간대 예약발매내역이 있습니다. # [KTX] 8월 3일, 서울~부산(11:00~:) 16호 6A ``` -------------------------------- ### Python: User Authentication and Login with Korail2 Source: https://context7.com/carpedm20/korail2/llms.txt Authenticates with Korail servers using membership number, email, or phone number. Supports manual login control and changing credentials on an existing object. Requires the 'korail2' library. Outputs login status, user details upon success, and requires credentials as input. ```python from korail2 import Korail # Login with membership number (8 digits) korail = Korail("12345678", "your_password") # Login with email korail = Korail("user@example.com", "your_password") # Login with phone number korail = Korail("010-1234-5678", "your_password") # Manual login (disable auto_login) korail = Korail("12345678", "your_password", auto_login=False) if korail.login(): print("Login successful") print(f"Name: {korail.name}") print(f"Email: {korail.email}") print(f"Membership: {korail.membership_number}") # Change credentials on existing object korail.login("another_id", "another_password") # Logout korail.logout() ``` -------------------------------- ### Search train schedules using korail2 Python Source: https://github.com/carpedm20/korail2/blob/master/README.md This code snippet illustrates how to search for train schedules using the `search_train` method in the Korail2 Python wrapper. It specifies departure and arrival stations, date, and time. The `include_no_seats` argument can be set to `True` to include trains with no available seats. ```python dep = '서울' arr = '동대구' date = '20140815' time = '144000' trains = korail.search_train(dep, arr, date, time) ``` ```python trains = korail.search_train(dep, arr, date, time, include_no_seats=True) ``` -------------------------------- ### User Authentication and Login Source: https://context7.com/carpedm20/korail2/llms.txt Authenticate with Korail servers using membership number, email, or phone number to access booking functionality. Supports automatic login on initialization or manual login. ```APIDOC ## User Authentication and Login ### Description Authenticate with Korail servers using membership number, email, or phone number to access booking functionality. The library can automatically log in upon initialization or allow for manual login. ### Method POST (Implicit via Korail object initialization or login method) ### Endpoint N/A (Internal Korail API calls) ### Parameters #### Initialization Parameters - **identifier** (string) - Required - User's membership number, email, or phone number. - **password** (string) - Required - User's password. - **auto_login** (boolean) - Optional - If true (default), attempts to log in automatically upon object creation. #### Login Method Parameters - **identifier** (string) - Required - User's membership number, email, or phone number. - **password** (string) - Required - User's password. ### Request Example ```python from korail2 import Korail # Login with membership number (8 digits) korail = Korail("12345678", "your_password") # Login with email korail = Korail("user@example.com", "your_password") # Login with phone number korail = Korail("010-1234-5678", "your_password") # Manual login (disable auto_login) korail = Korail("12345678", "your_password", auto_login=False) if korail.login(): print("Login successful") # Change credentials on existing object korail.login("another_id", "another_password") # Logout korail.logout() ``` ### Response #### Success Response (200) Upon successful login, the `Korail` object will be authenticated and attributes like `name`, `email`, and `membership_number` will be populated. #### Response Example ```json { "message": "Login successful", "name": "User Name", "email": "user@example.com", "membership_number": "12345678" } ``` ``` -------------------------------- ### Reserve train tickets Source: https://github.com/carpedm20/korail2/blob/master/docs/index.md Code snippet demonstrating how to reserve a train ticket using the `reserve` method after searching for available trains. It takes a train object as input and returns the reserved seat information. ```python trains = korail.search_train(dep, arr, date, time) seat = korail.reserve(trains[0]) ``` -------------------------------- ### Python: Configure Passenger Types for Korail2 Searches Source: https://context7.com/carpedm20/korail2/llms.txt Defines and configures passenger groups for train searches, allowing for multiple adults, children, toddlers, and seniors, including options for discount cards. Passenger types are automatically merged by count. Used as input for the `search_train` function. Outputs a list of passenger objects. ```python from korail2 import AdultPassenger, ChildPassenger, ToddlerPassenger, SeniorPassenger # Single adult (default) passengers = [AdultPassenger()] # Multiple passengers of same type passengers = [AdultPassenger(2), ChildPassenger(1)] # Mixed passenger types passengers = [ AdultPassenger(2), # 2 adults ChildPassenger(1), # 1 child ToddlerPassenger(1), # 1 toddler SeniorPassenger(1) # 1 senior ] # Passengers are automatically merged by type passengers = [AdultPassenger(), AdultPassenger(), ChildPassenger()] # Equivalent to: [AdultPassenger(2), ChildPassenger(1)] # Use in search trains = korail.search_train("서울", "부산", "20240815", "100000", passengers=passengers) # Apply discount card (example structure) adult_with_card = AdultPassenger( count=1, discount_type='000', # Discount type code card='', # Card code card_no='', # Card number card_pw='' # Card password ) ``` -------------------------------- ### Python: Search Train Schedules Source: https://github.com/carpedm20/korail2/blob/master/docs/_build/html/index.html Searches for available train schedules between specified departure and arrival stations. Optional parameters include date, time, and train type. The method returns a list of available trains with their details. ```python dep = '서울' arr = '동대구' date = '20140815' time = '144000' trains = korail.search_train(dep, arr, date, time) ``` -------------------------------- ### Python: Reserve Train Seat Source: https://github.com/carpedm20/korail2/blob/master/docs/_build/html/index.html Reserves a seat on a selected train. This function takes a train object (obtained from `search_train`) as input and attempts to make a reservation. It returns the details of the reserved seat upon successful processing. ```python trains = korail.search_train(dep, arr, date, time) seat = korail.reserve(trains[0]) print(seat) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.