Last active
December 6, 2023 01:12
-
-
Save l0ki000/c3c5b0d331d91448c6dd0a9380109b72 to your computer and use it in GitHub Desktop.
Script for automated check for foreign passport status in Russian MID / Скрипт для проверки статуса зявления на заграничный паспорт в МИД РФ
This file contains 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
// usage: | |
// 1) npm install | |
// 2) node ./app.js <YOUR_SUBMISSION_ID> | |
// or add something like this in your crontab/anacron | |
// 1 0 mid.pass.check node <FULL_PATH>/app.js <YOUR_SUBMISSION_ID> | |
// app will create YOUR_SUBMISSION_ID.txt on the first run, and than will write you an email after any changes in status | |
const process = require('node:process'); | |
{ | |
const ver = [...process.version.matchAll(/v(\d+)\.(\d+)/g)]; | |
const num = Number(`${ver[0][1]}.${ver[0][2]}`); | |
if (!Number.isNaN(num) && num < 18.0) { | |
console.error("minimum node version required is 18.0, but got ", num); | |
process.exit(); | |
} | |
} | |
const http = require('node:http'); | |
const https = require('node:https'); | |
const fs = require('node:fs'); | |
const diff = require('deep-object-diff'); | |
const _ = require('lodash'); | |
const EMAIL = ''; | |
const EMAIL_SECRET = ''; | |
if (_.isEmpty(EMAIL) || _.isEmpty(EMAIL_SECRET)) { | |
throw new Error("Please set here your gmail/secret. Google account -> Security -> 2 step verification -> App passwords"); | |
} | |
const argv = process.argv.slice(2); | |
let MID_ID = _.isArray(argv) && _.first(argv) ? BigInt(_.first(argv)) : NaN; | |
if (Number.isNaN(MID_ID)) { | |
throw new Error(`Unable to parse argument to number: ${argv}`); | |
} else { | |
MID_ID = MID_ID.toLocaleString('fullwide', { useGrouping: false }); | |
} | |
const url = new URL(MID_ID, 'https://info.midpass.ru/api/request/'); | |
const LTS_FILE_NAME = `./${MID_ID}.txt`; | |
// read and parse file | |
let lastData, | |
lastDataStat, | |
formattedLastData; | |
try { | |
lastDataStat = fs.statSync(LTS_FILE_NAME); | |
lastData = JSON.parse(fs.readFileSync(LTS_FILE_NAME, {encoding: "utf8"})); | |
} catch (e) { | |
console.error(e); | |
} | |
function nodemailer_send(subj, msg) { | |
const nodemailer = require('nodemailer'); | |
const transporter = nodemailer.createTransport({ | |
service: 'gmail', | |
auth: { | |
user: EMAIL, | |
pass: EMAIL_SECRET | |
} | |
}); | |
const mailOptions = { | |
from: EMAIL, | |
to: EMAIL, | |
subject: subj, | |
text: msg | |
}; | |
transporter.sendMail(mailOptions, (error, info) => { | |
if (error) { | |
console.error(error); | |
} else { | |
console.log('Email sent: ' + info.response); | |
} | |
}); | |
} | |
const req = https.request(url, { | |
headers: { | |
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8", | |
// "Accept-Encoding": "gzip, deflate, br", | |
// "Accept-Language": "ru,en-US;q=0.7,en;q=0.3", | |
// "Alt-Used": "info.midpass.ru", | |
// "Connection": "keep-alive", | |
// "Host": "info.midpass.ru", | |
"Referer": `https://info.midpass.ru/?id=${MID_ID}`, | |
// Sec-Fetch-Dest document | |
// Sec-Fetch-Mode navigate | |
// Sec-Fetch-Site none | |
// Sec-Fetch-User ?1 | |
"User-Agent": 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/114.0' | |
} | |
}, (res) => { | |
if (res.statusCode !== 200) { | |
console.log('request headers:', JSON.stringify(req.getHeaders(), null, 4)); | |
console.log('response statusCode: %u, %s', res.statusCode, http.STATUS_CODES[res.statusCode]); | |
console.log('response headers:', res.headers); | |
} | |
let rawData = ''; | |
res.on('data', (d) => { | |
rawData += d; | |
}); | |
res.on('end', () => { | |
try { | |
console.log("rawData: ", rawData); | |
const data = JSON.parse(rawData), | |
formattedData = JSON.stringify(data, null, 4); | |
console.log("data:", formattedData); | |
// no initial file | |
if (!lastDataStat) { | |
console.warn("No initial file: ", LTS_FILE_NAME); | |
try { | |
fs.writeFileSync(LTS_FILE_NAME, rawData, {encoding: "utf8"}); | |
} catch (e) { | |
console.error(e); | |
} | |
return; | |
} | |
if (_.isEqual(lastData, data)) { | |
console.log("No difference in data since:", lastDataStat.mtime); | |
return; | |
} else { | |
formattedLastData = JSON.stringify(lastData, null, 4); | |
const objUpdateDiff = diff.updatedDiff(lastData, data); | |
const msg = ` | |
https://info.midpass.ru/?id=${MID_ID} | |
Updated data: ${JSON.stringify(objUpdateDiff, null, 4)} | |
================== New data ================== | |
${formattedData} | |
================== Old data ================== | |
${formattedLastData} | |
`; | |
nodemailer_send(`изменение статуса заявления на паспорт МИД`, msg); | |
// dump new data | |
fs.writeFileSync(LTS_FILE_NAME, rawData, {encoding: "utf8"}); | |
} | |
} catch (e) { | |
console.error(e); | |
} | |
}); | |
}); | |
req.on('error', (e) => { | |
console.error(e); | |
}); | |
req.end(); |
This file contains 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
{ | |
"name": "ck.midpass", | |
"version": "1.0.0", | |
"dependencies": { | |
"deep-object-diff": "^1.1.9", | |
"lodash": "^4.17.21", | |
"nodemailer": "^6.9.3" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment