Skip to content

Instantly share code, notes, and snippets.

@rakeshjuyal
Created July 13, 2021 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rakeshjuyal/08d997101809c8bd32490116c3b6d232 to your computer and use it in GitHub Desktop.
Save rakeshjuyal/08d997101809c8bd32490116c3b6d232 to your computer and use it in GitHub Desktop.
Find Passport appointment slot @ USPS
/*
Working fine as of 7/13/2021
1. Goto https://tools.usps.com/rcas.htm?locationId=1371262
2. Edit location. Use your zip code.
3. Find all nearby USPS ( code below )
4. Modify var nearbyUPS and var howManyDays ( may be keep it 30 days ).
5. Once everything ran successfully, log uspsGist.vars on console to see slots in all USPS.
*/
// https://tools.usps.com/rcas.htm?locationId=1371262
// Lynwood: 1371262
// Everett: 1362862
/**
*
* Get all nearby USPS.
var nearbyUPS = {};
$('#searchResultList > .row').each( function(){
var loc = "";
nearbyUPS [ $(this).find("h4").text() ] = $(this).attr("id");
});
//
{Lynnwood: "1371262", Snohomish: "1381935", Everett: "1362862", Redmond: "1378997", Bellevue: "1354541"}
*/
(function( ns ){
ns = ns || {};
window.uspsGist = ns;
var nearbyUPS = {Lynnwood: "1371262", Snohomish: "1381935", Everett: "1362862", Redmond: "1378997", Bellevue: "1354541"};
var year = "2021";
var daysSlots = {};
var upsLocDaysSlots = {}; // { UPSLocation_UpsID : [ {} , {} ] , }
var howManyDays = 5;
var today = new Date();
window.getFormattedDateForUSPS = function( _date1 ) {
var formattedDateForUSPS =
_date1.getFullYear()
+ ( ( _date1.getMonth() + 1 ) < 10 ? "0" : "" ) + ( _date1.getMonth() + 1 )
+ ( _date1.getDate() < 10 ? "0" : "" ) + _date1.getDate();
return formattedDateForUSPS;
};
window.getAppointmentSlotsForUPS = function( upsID, upsLocation ){
for ( dayCtr = 0; dayCtr < howManyDays; dayCtr++ ){
var statusSet = new Set();
var curDate = new Date();
curDate.setDate( today.getDate() + dayCtr );
var yearMonthDate = getFormattedDateForUSPS( curDate );
fetch("https://tools.usps.com/UspsToolsRestServices/rest/v2/appointmentTimeSearch", {
"headers": {
"accept": "application/json, text/javascript, */*; q=0.01",
"accept-language": "en-US,en;q=0.9,de;q=0.8,es;q=0.7,ru;q=0.6",
"content-type": "application/json;charset=UTF-8",
"sec-ch-ua": "\" Not;A Brand\";v=\"99\", \"Google Chrome\";v=\"91\", \"Chromium\";v=\"91\"",
"sec-ch-ua-mobile": "?0",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"x-requested-with": "XMLHttpRequest"
},
"referrer": "https://tools.usps.com/rcas.htm?locationId=" + upsID,
"referrerPolicy": "strict-origin-when-cross-origin",
"body": "{\"date\":" + yearMonthDate + ",\"productType\":\"PASSPORT\",\"numberOfAdults\":\"0\",\"numberOfMinors\":\"1\",\"excludedConfirmationNumber\":[\"\"],\"fdbId\":[\"" + upsID + "\"],\"skipEndOfDayRecord\":true}",
"method": "POST",
"mode": "cors",
"credentials": "include"
}).then( r => r.json() )
.then ( function( d ) {
//console.log(d);
statusSet = new Set();
if ( d && d.appointmentTimeDetailExtended ){
var msgStr = [];
var availableSlots = [];
d.appointmentTimeDetailExtended.forEach(element => {
msgStr.push( "\n" );
if ( element.appointmentStatus == "Available"){
availableSlots.push( "\n" );
availableSlots.push( "AppointmentStatus: " + element.appointmentStatus );
availableSlots.push( "startDateTime: " + element.startDateTime );
availableSlots.push( "title: " + element.title );
availableSlots.push( "color: " + element.color );
availableSlots.push( "\n" );
}
msgStr.push( "AppointmentStatus: " + element.appointmentStatus );
msgStr.push( "startDateTime: " + element.startDateTime );
msgStr.push( "title: " + element.title );
msgStr.push( "\n" );
statusSet.add( element.appointmentStatus );
});
var uniqueStatus = [];
statusSet.forEach(function(value) {
uniqueStatus.push(value);
});
//console.log(d.startOfBusinessDay + " :: " + uniqueStatus.join(" ,") + " :: " + msgStr.join("\n"));
//console.log(d.startOfBusinessDay + " :: " + uniqueStatus.join(" ,") + " :: " + availableSlots.join("\n"));
daysSlots[ upsLocation + "_" + upsID + "_" + d.startOfBusinessDay] = uniqueStatus.join(" ,") + " :: " + availableSlots.join("\n");
// Fill up upsLocDaysSlots
if (!upsLocDaysSlots ){
upsLocDaysSlots = {};
}
if (!upsLocDaysSlots[ upsLocation + "_" + upsID ] ){
upsLocDaysSlots[ upsLocation + "_" + upsID ] = {};
}
upsLocDaysSlots[ upsLocation + "_" + upsID ][d.startOfBusinessDay] = uniqueStatus.join(" ,") + " :: " + availableSlots.join("\n");
}
});
}
};
getAppointmentSlotsForAllUSPS = function() {
for( upsLoc in nearbyUPS ) {
var beginMsg = " ---- Searching all appointments @ " + upsLoc + " :: " + nearbyUPS [ upsLoc ] + " ----- ";
//alert( beginMsg );
console.log( daysSlots );
console.log( beginMsg );
//daysSlots = {};
var upsID = nearbyUPS [ upsLoc ];
getAppointmentSlotsForUPS( upsID, upsLoc);
}
};
exportVars = function(){
ns.vars = ns.vars || {};
ns.vars['upsLocDaysSlots'] = upsLocDaysSlots;
ns.vars['daysSlots'] = daysSlots;
};
ns.init = function(){
getAppointmentSlotsForAllUSPS();
exportVars();
};
ns.init();
})( window.uspsGist );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment