Korail2

repository·master·Indexed 42 days ago

https://github.com/carpedm20/korail2

A Python wrapper for the Korail web service that allows developers to programmatically search for train schedules, make reservations, manage existing reservations, and view paid tickets.

Tokens
2K
Snippets
10
Records
12
Agent score
4%

What's inside Korail2

  1. Configure passengers for search and reservation

    master

    The passengers argument accepts a list or tuple of Passenger objects. Supported types are AdultPassenger, ChildPassenger, and SeniorPassenger.

    Note: While you can pass negative or zero counts, the library effectively uses the sum of passengers that is greater than zero.

    # 1 adult, 1 child
    psgrs = [AdultPassenger(), ChildPassenger()]
    
    # 2 adults, 1 child
    psgrs = [AdultPassenger(2), ChildPassenger(1)]
    
    # 2 adults, 1 child, 1 senior
    psgrs = [AdultPassenger(2), ChildPassenger(), SeniorPassenger()]
    
    # Using search with passengers
    trains = korail.search_train(dep, arr, date, time, passengers=psgrs)
  2. Authenticate with Korail

    master

    To use the library, you must first create a Korail object. You can authenticate using a membership number, email, or phone number.

    By default, the object attempts to log in immediately. To prevent automatic login, set auto_login=False and call .login() manually.

    from korail2 import *
    
    # Using membership number
    korail = Korail("12345678", YOUR_PASSWORD)
    
    # Using email
    korail = Korail("carpedm20@gmail.com", YOUR_PASSWORD)
    
    # Using phone number
    korail = Korail("010-9964-xxxx", YOUR_PASSWORD)
    
    # Manual login control
    korail = Korail("12345678", YOUR_PASSWORD, auto_login=False)
    korail.login()
    
    # Changing ID with an existing object
    korail.login(ANOTHER_ID, ANOTHER_PASSWORD)
  3. Initialize the Korail client

    master

    To use the library, create an instance of the Korail class. You can authenticate using a membership number, an email address, or a phone number along with your password.

    from korail2 import Korail
    
    # Using membership number
    korail = Korail("12345678", YOUR_PASSWORD)
    
    # Using email
    korail = Korail("carpedm20@gmail.com", YOUR_PASSWORD)
    
    # Using phone number
    korail = Korail("010-9964-xxxx", YOUR_PASSWORD)
  4. Retrieve paid tickets with tickets()

    master

    Use the tickets() method to get a list of your already paid/issued tickets.

    >>> tickets = korail.tickets()
    >>> print(tickets)
    # Output example: [[KTX] 8월 10일, 동대구~울산(09:26~09:54) => 5호 4A, 13900원]
  5. Manage reservations and tickets

    master

    View current reservations

    Use reservations() to get a list of current reservation objects.

    Cancel a reservation

    Pass a Reservation object obtained from reservations() to the cancel() method.

    View paid tickets

    Use tickets() to see already paid tickets. To see detailed feedback (e.g., '정상발매처리'), initialize the Korail object with want_feedback=True.

    # Show reservations
    reservations = korail.reservations()
    
    # Cancel a reservation
    korail.cancel(reservations[0])
    
    # Get paid tickets with feedback
    korail = Korail("12345678", YOUR_PASSWORD, want_feedback=True)
    tickets = korail.tickets()
  6. Make a reservation with reserve()

    master

    After searching for trains, you can reserve a specific train from the results using the reserve method.

    >>> trains = korail.search_train(dep, arr, date, time)
    >>> seat = korail.reserve(trains[0])
    # Output example: [KTX] 8월 3일, 서울~부산(11:00~:) 16호 6A
  7. Search train schedules with search_train()

    master

    The search_train method allows you to find available train schedules based on departure/arrival stations, date, time, and train type.

    >>> dep = '서울'
    >>> arr = '동대구'
    >>> date = '20140815'
    >>> time = '144000'
    >>> trains = korail.search_train(dep, arr, date, time)
  8. Make a train reservation

    master

    Use the reserve method to book a train. If seats are insufficient for the requested passengers, it raises a SoldOutError.

    You can specify seat priority using ReserveOption constants:

    • ReserveOption.GENERAL_FIRST: Economic than Comfortable.
    • ReserveOption.GENERAL_ONLY: Reserve only general seats.
    • ReserveOption.SPECIAL_FIRST: Comfortable than Economic.
    • ReserveOption.SPECIAL_ONLY: Reserve only special seats.
    # Basic reservation for one passenger
    seat = korail.reserve(trains[0])
    
    # Reservation with specific passengers and seat priority
    psgrs = [AdultPassenger(2), ChildPassenger()]
    seat = korail.reserve(trains[0], passengers=psgrs, ReserveOption.GENERAL_ONLY)
  9. Search for trains

    master

    Use search_train for a fast search (returns up to 10 results) or search_train_allday to retrieve all results after a specific time.

    Arguments

    • dep: Departure station (Korean, e.g., '서울')
    • arr: Arrival station (Korean, e.g., '부산')
    • date: (Optional) Departure date in yyyyMMdd format
    • time: (Optional) Departure time in hhmmss format
    • train_type: (Optional) A constant from the TrainType class. Defaults to TrainType.ALL.
    • passengers: (Optional) A list of Passenger objects.
    • include_no_seats: (Optional) If True, includes trains with no available seats.
    # Basic search
    trains = korail.search_train('서울', '동대구', '20140815', '144000')
    
    # Search including sold-out trains
    trains = korail.search_train('서울', '동대구', '20140815', '144000', include_no_seats=True)
  10. Reference: search_train arguments

    master

    The search_train method accepts the following arguments:

    • dep: A departure station in Korean (e.g., '서울').
    • arr: An arrival station in Korean (e.g., '부산').
    • date: (optional) Departure date in yyyyMMdd format.
    • time: (optional) Departure time in hhmmss format.
    • train_type: (optional) A type of train. Use the following codes:
      • 00: KTX
      • 01: 새마을호
      • 02: 무궁화호
      • 03: 통근열차
      • 04: 누리로
      • 05: 전체 (default)
      • 06: 공학직통
      • 07: KTX-산천
      • 08: ITX-새마을
      • 09: ITX-청춘
  11. Reference: TrainType constants

    master

    Use these constants for the train_type argument in search methods:

    • 00: TrainType.KTX
    • 01: TrainType.SAEMAEUL
    • 02: TrainType.MUGUNGHWA
    • 03: TrainType.TONGGEUN
    • 04: TrainType.NURIRO
    • 05: TrainType.ALL
    • 06: TrainType.AIRPORT
    • 07: TrainType.KTX_SANCHEON
    • 08: TrainType.ITX_SAEMAEUL
    • 09: TrainType.ITX_CHEONGCHUN