Skip to content

Instantly share code, notes, and snippets.

@patrickhulce
Created July 8, 2023 19: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 patrickhulce/30d97d3be9c86ad104d12ece94d36a4c to your computer and use it in GitHub Desktop.
Save patrickhulce/30d97d3be9c86ad104d12ece94d36a4c to your computer and use it in GitHub Desktop.
Checks for a particular license plate combination in Texas.
async function checkAvailability(plate: string): Promise<string> {
const encodedPlate = encodeURIComponent(plate);
const url = `https://www.myplates.com/api/licenseplates/passenger/carbon-fiber/${encodedPlate}?_=${Date.now()}`;
const response = await fetch(url, {
headers: {
accept: 'application/json, text/javascript, */*; q=0.01',
'accept-language': 'en-US,en;q=0.9',
Referer: 'https://www.myplates.com/design/personalized/passenger/carbon-fiber/',
'Referrer-Policy': 'strict-origin-when-cross-origin',
},
method: 'GET',
});
if (!response.ok) throw new Error(`Check failed: ${await response.text()}`);
const body = await response.json();
console.log('replied!', body);
return body.status;
}
function generatePermutations(plate: string): string[] {
const replacements: Record<string, string[] | undefined> = {
A: ['A', '4'],
I: ['I', '1'],
E: ['E', '3'],
O: ['O', '0'],
S: ['S', '5'],
T: ['T', '7'],
};
const vowels = ['A', 'E', 'I', 'O', 'U'];
const result: string[] = [];
// Recursive function to generate permutations
function generate(remaining: string, current: string) {
if (remaining.length === 0) {
result.push(current);
return;
}
const [char, ...rest] = remaining;
const chars = replacements[char.toUpperCase()] || [char];
chars.forEach((replacement) => {
generate(rest.join(''), current + replacement);
});
if (vowels.includes(char.toUpperCase())) {
generate(rest.join(''), current); // Skip the vowel
}
}
generate(plate, '');
return result.filter((p) => p.replace(/\s+/g, '').length <= 7);
}
// Entry point
async function main() {
const desiredPlate = process.argv[2];
if (!desiredPlate || desiredPlate.replace(/\s+/g, '').length > 10) {
throw new Error(`Invalid plate "${desiredPlate}"`);
}
const permutations = generatePermutations(desiredPlate);
const availablePlates: string[] = [];
for (const plate of permutations) {
try {
console.log(`Checking ${plate} availability...`);
const availability = await checkAvailability(plate);
const isAvailable = availability === 'available';
console.log(isAvailable ? 'Available! 🎉' : 'Not available ❌');
if (isAvailable) availablePlates.push(plate);
} catch (error) {
console.error(`Error occurred while checking ${plate}: ${error}`);
}
await new Promise((r) => setTimeout(r, 1000 + Math.ceil(Math.random() * 2000)));
}
console.log(`Available plates:\n${availablePlates.join('\n')}`);
}
main().catch((error) => console.error(error));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment