Skip to content

Commit

Permalink
Merge pull request #28 from suryavaddiraju/development
Browse files Browse the repository at this point in the history
New Update
  • Loading branch information
suryavaddiraju committed May 12, 2024
2 parents e505616 + c362741 commit 8471e28
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 33 deletions.
31 changes: 31 additions & 0 deletions lib/logging.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {get_current_time_api} from "./time_work.mjs";
import {appendFile} from "fs";
class LOG {
constructor(log = false, filename = "") {
this.log = log;
if (filename === "") {
this.generate_filename();
}
else {
this.filename = filename;
}
}
async generate_filename(){
const timestamp = await get_current_time_api();
const sfilename = timestamp.replaceAll("-","").replaceAll(":","").replaceAll("T","").replaceAll(".","").replaceAll("+0000","")
this.filename = `./${sfilename}.log`;
}
async log_function(message,level="INFO"){
if (this.log){
const online_time = await get_current_time_api();
const log_message = {
local_timestamp: new Date().toISOString(),
online_timestamp: online_time.slice(0, -9)+"Z",
log_level:level,
message: message
};
appendFile(this.filename, JSON.stringify(log_message)+"\n", (err) => err && console.error(err));
}
}
}
export {LOG};
28 changes: 15 additions & 13 deletions lib/time_work.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ function sleep(ms){
return new Promise(resolve => setTimeout(resolve, ms));
}

async function get_current_time_api(){
const response = await axios.get(
"https://worldtimeapi.org/api/timezone/Etc/UTC",
);
return response.data.utc_datetime;
async function get_current_time_api(zone="utc"){
if (zone === "utc"){
const response = await axios.get(
"https://worldtimeapi.org/api/timezone/Etc/UTC",
);
return response.data.utc_datetime;
}
}

const psg_input_await = {
Expand Down Expand Up @@ -44,7 +46,7 @@ async function sleep_for_login(ticket_time){
else if (ticket_time === 2){
const date_time_string = await get_current_time_api();
const currentTime = new Date(date_time_string);
const targetTime = new Date(currentTime);
let targetTime = new Date(currentTime);
targetTime.setUTCHours(5, 28, 0, 0);
if (currentTime < targetTime) {
const balance_time = targetTime - currentTime;
Expand All @@ -65,7 +67,7 @@ async function sleep_for_availability_check(ticket_time){
if (ticket_time === 1){
const date_time_string = await get_current_time_api();
const currentTime = new Date(date_time_string);
const targetTime = new Date(currentTime);
let targetTime = new Date(currentTime);
targetTime.setUTCHours(4, 30, 1, 0);
if (currentTime < targetTime) {
const balance_time = targetTime - currentTime;
Expand All @@ -80,7 +82,7 @@ async function sleep_for_availability_check(ticket_time){
else if (ticket_time === 2){
const date_time_string = await get_current_time_api();
const currentTime = new Date(date_time_string);
const targetTime = new Date(currentTime);
let targetTime = new Date(currentTime);
targetTime.setUTCHours(5, 30, 1, 0);
if (currentTime < targetTime) {
const balance_time = targetTime - currentTime;
Expand All @@ -101,9 +103,9 @@ async function start_before_3_minutes(ticket_time){
if (ticket_time === 1) {
const date_time_string = await get_current_time_api();
const currentTime = new Date(date_time_string);
const targetTime = new Date(currentTime);
let targetTime = new Date(currentTime);
targetTime.setUTCHours(4, 27, 0, 0);
const stoptime = new Date(currentTime);
let stoptime = new Date(currentTime);
stoptime.setUTCHours(18, 30, 0, 0);
if (currentTime < targetTime || currentTime > stoptime) {
throw new Error("Please Start the booking after 09:57:00.000 AM IST");
Expand All @@ -115,9 +117,9 @@ async function start_before_3_minutes(ticket_time){
else if (ticket_time === 2) {
const date_time_string = await get_current_time_api();
const currentTime = new Date(date_time_string);
const targetTime = new Date(currentTime);
let targetTime = new Date(currentTime);
targetTime.setUTCHours(5, 27, 0, 0);
const stoptime = new Date(currentTime);
let stoptime = new Date(currentTime);
stoptime.setUTCHours(18, 30, 0, 0);
if (currentTime < targetTime || currentTime > stoptime) {
throw new Error("Please Start the booking after 10:57:00.000 AM IST");
Expand All @@ -131,4 +133,4 @@ async function start_before_3_minutes(ticket_time){
}
}

export {sleep, psg_input_wait, sleep_for_login, sleep_for_availability_check, start_before_3_minutes};
export {sleep, psg_input_wait, sleep_for_login, sleep_for_availability_check, start_before_3_minutes,get_current_time_api};
42 changes: 32 additions & 10 deletions lib/workings.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { URL } from 'url';
import {verify_booking_params, initialize_booking_variables, input, base64DecodeUnicode} from './utils.mjs';
import { psg_input_wait, sleep_for_login, sleep_for_availability_check} from "./time_work.mjs";
import {headers as main_headers} from './browser_headers.mjs';
import {LOG} from './logging.mjs';
class IRCTC{
constructor(){
wrapper(axios);
Expand All @@ -18,9 +19,9 @@ class IRCTC{
withCredentials: true,
});
}
log_data(data){
log_data(data,level="DEBUG"){
if (this.logging){
console.log(data);
this.log_file.log_function(data,level);
}
}
async load_irctc(){
Expand Down Expand Up @@ -188,13 +189,31 @@ class IRCTC{
headers["Content-Length"] = postdata_string.length.toString();
await sleep_for_availability_check(this.params.ticket_time);
this.log_data("Fetching seats Availability for the given Train Number, From and To stations, Journey Date and Class Type by sending parameters to IRCTC");
const response = await this.axios_instance.post(
`https://www.irctc.co.in/eticketing/protected/mapps1/avlFarenquiry/${this.train_number}/${this.journey_date}/${this.from_stn}/${this.to_stn}/${this.class_type}/${this.quota_type}/N`,
postdata_string,
{
headers: headers,
let response = null;
const maxRetries = 5;
let retries = 0;
const cust_url = `https://www.irctc.co.in/eticketing/protected/mapps1/avlFarenquiry/${this.train_number}/${this.journey_date}/${this.from_stn}/${this.to_stn}/${this.class_type}/${this.quota_type}/N`;
while (retries < maxRetries) {
try{
response = await this.axios_instance.post(
cust_url,
postdata_string,
{
headers: headers,
}
)
break;
}
);
catch(error){
if (error.response && error.response.status === 502){
console.log("Received Bad Gateway Response, Retrying again");
retries++;
}
else{
throw error;
}
}
}
this.csrf_token = response.headers["csrf-token"];
const data = response.data;
if ("errorMessage" in data) {
Expand Down Expand Up @@ -545,7 +564,9 @@ class IRCTC{
throw new Error(y["body"]["resultInfo"]["resultMsg"])
}
else{
console.log(`---> INFO <---\nPlease approve the UPI Collect request from the UPI ID ${y["body"]["content"]["MERCHANT_VPA"]} for INR ${y["body"]["content"]["TXN_AMOUNT"]} with payment remarks as \'Oid${y["body"]["content"]["ORDER_ID"]}@IRCTCWebUPI\' in your ${y["body"]["content"]["upiHandleInfo"]["upiAppName"]} app`);
const my_string = `Please approve the UPI Collect request from the UPI ID ${y["body"]["content"]["MERCHANT_VPA"]} for INR ${y["body"]["content"]["TXN_AMOUNT"]} with payment remarks as \'Oid${y["body"]["content"]["ORDER_ID"]}@IRCTCWebUPI\' in your ${y["body"]["content"]["upiHandleInfo"]["upiAppName"]} app`;
this.log_data(my_string, "INFO");
console.log(my_string);
}
this.paycon = y["body"]["content"];
await this.get_upi_transaction_status();
Expand Down Expand Up @@ -645,7 +666,7 @@ class IRCTC{
return "IRCTC CallBack Payment Successful";
}
catch(e){
this.log_data("Received a redirect Response from IRCTC\nGoing to that redirect url");
this.log_data("Received a redirect Response from IRCTC. Going to that redirect url");
const response = await this.axios_instance.get(
e.response.headers.location,
{
Expand Down Expand Up @@ -673,6 +694,7 @@ class IRCTC{
return data;
}
async book(params){
this.log_file = new LOG(params.log);
this.logging = params.log;
this.params = params;
await verify_booking_params(this.params);
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "irctc-api",
"description": "An exclusive NodeJs only package built on top of IRCTC Website APIs to book train tickets, managing user profile faster and simpler from anywhere in the world",
"version": "2.0.3",
"version": "2.0.4",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand Down
11 changes: 4 additions & 7 deletions test/book_on_windows_x64.mjs → test/book.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const params = {
"to": "PDKN",
"journey_date": "20240530",
"mobile_number": "9876543210",
"userID": "XXXXXX",
"userID": "XXXXXXXX",
"password": "XXXXXXXXX",
"passengers": [
{
Expand All @@ -20,9 +20,6 @@ const params = {
],
"log": true
};
async function custom_command_name(params) {
const irctc = new IRCTC();
const response = await irctc.book(params);
console.log(response);
};
await custom_command_name(params);
const irctc = new IRCTC();
const response = await irctc.book(params);
console.log(response);

0 comments on commit 8471e28

Please sign in to comment.