From 74391218babdf54f2d787b868b71f8b2a12fc972 Mon Sep 17 00:00:00 2001 From: Vaddiraju Surya Teja Date: Tue, 16 Apr 2024 18:32:47 +0530 Subject: [PATCH] new compile do not know what will happen --- index.mjs => index.js | 7 +++--- lib/{stations.json => stations.mjs} | 5 ++-- lib/utils.mjs | 3 ++- lib/workings.mjs | 3 ++- package.json | 4 +-- test/book_ticket.js | 38 +++++++++++++++++++++++++++++ 6 files changed, 51 insertions(+), 9 deletions(-) rename index.mjs => index.js (73%) rename lib/{stations.json => stations.mjs} (99%) create mode 100644 test/book_ticket.js diff --git a/index.mjs b/index.js similarity index 73% rename from index.mjs rename to index.js index 9e7d21c..614f42c 100644 --- a/index.mjs +++ b/index.js @@ -1,5 +1,5 @@ -import {IRCTC as main_class} from "./lib/workings.mjs"; -export default class IRCTC extends main_class{ +import {main_class} from "./lib/workings.mjs" +class IRCTC extends main_class{ constructor(){ super(); } @@ -12,4 +12,5 @@ export default class IRCTC extends main_class{ async pnr_status(params){ return await super.pnr_status(params); } -}; \ No newline at end of file +}; +export {IRCTC}; \ No newline at end of file diff --git a/lib/stations.json b/lib/stations.mjs similarity index 99% rename from lib/stations.json rename to lib/stations.mjs index 5d6e914..1b188db 100644 --- a/lib/stations.json +++ b/lib/stations.mjs @@ -1,4 +1,4 @@ -[ +const stations_list = [ "NDLS", "MAS", "HWH", @@ -8488,4 +8488,5 @@ "ZP", "ZNP", "ZPR" -] \ No newline at end of file +]; +export {stations_list}; \ No newline at end of file diff --git a/lib/utils.mjs b/lib/utils.mjs index 797a0e1..631f847 100644 --- a/lib/utils.mjs +++ b/lib/utils.mjs @@ -1,8 +1,9 @@ import fs from 'fs'; import readline from 'readline'; import {start_before_3_minutes} from "./time_work.mjs"; +import { stations_list } from './stations.mjs'; async function verify_booking_params(params) { - let stationsList = JSON.parse(fs.readFileSync('./stations.json', 'utf-8')); + let stationsList = stations_list; if (!params.quota || !params.class || !params.train_number || !params.journey_date || !params.from || !params.to || !params.userID || !params.password || !params.passengers || !params.UPI || !params.mobile_number) { throw new Error("Missing parameters\nThe required parameters are quota, class, train_number, journey_date, from, to, userID, password, passengers, UPI, mobile_number"); } diff --git a/lib/workings.mjs b/lib/workings.mjs index 4f3c91e..c5ce5e7 100644 --- a/lib/workings.mjs +++ b/lib/workings.mjs @@ -73,6 +73,7 @@ class IRCTC{ async send_login(){ const data = `grant_type=password&username=${this.username}&password=${this.password}&captcha=${this.captcha_answer}&uid=${this.captcha_status}&otpLogin=false&nlpIdentifier=&nlpAnswer=&nlpToken=&lso=&encodedPwd=true`; const headers = main_headers.headers_4; + console.log(headers); headers["Content-Length"] = data.length.toString(); await sleep_for_login(this.params.ticket_time); const response = await this.axios_instance.post( @@ -694,4 +695,4 @@ class IRCTC{ return data; } }; -export {IRCTC}; \ No newline at end of file +export {IRCTC as main_class}; \ No newline at end of file diff --git a/package.json b/package.json index 4d452fd..afaa8b1 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "irctc-api", "description": "A package built on top of IRCTC Website APIs to make train tickets , managing user profile faster and simpler. Currently this package only works on NodeJs environment and we were not recommending this to use on browser or any other Javascript environment.", - "version": "1.0.2", + "version": "1.0.3", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, @@ -31,6 +31,6 @@ "node": ">=20.0.0", "npm": ">=10.0.0" }, - "main": "index.mjs", + "module": "index.js", "type": "module" } \ No newline at end of file diff --git a/test/book_ticket.js b/test/book_ticket.js new file mode 100644 index 0000000..2eef9e4 --- /dev/null +++ b/test/book_ticket.js @@ -0,0 +1,38 @@ +import {IRCTC} from "../index.js"; +const params = { + "UPI": "9876543210@ybl", // Your NPCI UPI VPA ID + "class": "2S", // class code such as 2A | 3A | SL | CC | 2S | FC | 1A + "quota": "GN", // GN | TQ | PT + "train_number": "17201", // 5 Digit Train Number + "from": "GNT", // Station code + "to": "PDKN", // Station code + "journey_date": "20240530", // YYYYMMDD + "mobile_number": "9876543210", // 10 Digit Mobile Number + "userID": "username_here", // Secret User ID + "password": "someSecret_here", // Secret Password + "passengers": [ // Passengers List - Max 4 members for Tatkal and 6 for General Quota + { + "age": "22", // Age of Passenger + "food": "", // Food Preference - Leave as Empty string, if food is not required + "name": "Virat Kohli", // Full Name of Passenger + "sex": "M" // Sex of Passenger - M | F + }, + { + "age": "21", // Age of Passenger + "food": "", // Food Preference - Leave as Empty string, if food is not required + "name": "Anushka Sharma", // Full Name of Passenger + "sex": "F" // Sex of Passenger - M | F + } + ] +}; +async function custom_command_name(params) { + try{ + const irctc = new IRCTC(); + const response = await irctc.book(params); + console.log(response); + } + catch(e){ + throw new Error(e); + } +}; +await custom_command_name(params); \ No newline at end of file