Created
June 17, 2021 23:44
-
-
Save yvbbrjdr/1a675902aad944c92bd943fca8978906 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let request = require('request'); | |
let USCIS_CASE_STATUS_URL = 'https://egov.uscis.gov/casestatus/mycasestatus.do?appReceiptNum='; | |
class USCISCaseStatus { | |
constructor(receipt_num, timestamp, title, content) { | |
this.receipt_num = receipt_num; | |
this.timestamp = timestamp; | |
this.title = title; | |
this.content = content; | |
} | |
static get(receipt_num) { | |
return new Promise((resolve, _) => { | |
let url = USCIS_CASE_STATUS_URL + receipt_num; | |
request(url, (err, _, body) => { | |
if (err) { | |
resolve(new USCISCaseStatus(receipt_num, Date.now(), 'Errored', err.message)); | |
return; | |
} | |
let lines = body.split('\n'); | |
var titleIdx = 0; | |
while (titleIdx < lines.length && !lines[titleIdx].match('<h1>')) | |
titleIdx++; | |
if (titleIdx === lines.length) { | |
resolve(new USCISCaseStatus(receipt_num, Date.now(), 'Errored', 'Unable to parse HTML.')); | |
return; | |
} | |
let title = lines[titleIdx].replace(/<[^>]*>/g, '').trim(); | |
let content = lines[titleIdx + 1].replace(/<[^>]*>/g, '').trim(); | |
if (title === '') { | |
resolve(new USCISCaseStatus(receipt_num, Date.now(), 'Errored', 'Unable to parse HTML.')); | |
return; | |
} | |
resolve(new USCISCaseStatus(receipt_num, Date.now(), title, content)); | |
}); | |
}); | |
} | |
toString() { | |
return this.receipt_num + ': ' + this.title + '\n' + (new Date(this.timestamp)).toString() + '\n\n' + this.content; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment