search.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // node --require regenerator-runtime/runtime --require ./babel-local search.js ..\out result.csv
  2. import {promises} from 'fs';
  3. import {resolve} from 'path';
  4. import {eachFormElement, eachItem, eachVestnik} from './lib/read';
  5. const {writeFile, truncate} = promises;
  6. async function work (prefix, outfile) {
  7. console.info(`Input directory: ${prefix}`);
  8. await truncate(outfile);
  9. console.info(`Output file: ${outfile}`);
  10. for await (let vestnik of eachVestnik(prefix)) {
  11. for (let item of eachItem(vestnik)) {
  12. const zakazka = item.zakazka[0].$;
  13. const id = zakazka.id || '';
  14. const url = zakazka.url || "about:blank";
  15. if (hasCpv("45", item)) {
  16. const v = oddiel_V(item);
  17. if (v) {
  18. const pocetPonuk = -1;
  19. const predpokladanaCena = '?';
  20. const konecnaCena = '?';
  21. const vitazIco = 0;
  22. const out = async () => await writeFile(
  23. outfile,
  24. poorMansCsv([id, url, pocetPonuk, predpokladanaCena, konecnaCena, vitazIco]) + "\r\n",
  25. {flag: "a"}
  26. );
  27. out();
  28. }
  29. }
  30. }
  31. }
  32. }
  33. function poorMansCsv (items) {
  34. return JSON.stringify(items).slice(1, -1);
  35. }
  36. function hasCpv (prefix, item) {
  37. for (let {tag, value, cancel} of eachFormElement(item)) {
  38. if (tag === "Part") {
  39. const match = value.$.Title && value.$.Title.match(/^ODDIEL\s+(\w+):/);
  40. if (match && match[1] !== "II") cancel();
  41. }
  42. if (tag === "Cpv" && value.$.Code.startsWith(prefix)) return true;
  43. if (tag === "SelectList" && value.$.Type === "CPV" && value.SelectListValue && value.SelectListValue[0].$.Title.startsWith(prefix)) return true;
  44. }
  45. return false;
  46. }
  47. function oddiel_V (item) {
  48. for (let {tag, value, cancel} of eachFormElement(item)) {
  49. if (tag === "Part") {
  50. const match = value.$.Title && value.$.Title.match(/^ODDIEL\s+(\w+):/);
  51. if (match)
  52. if (match[1] === "V") return value; else cancel();
  53. }
  54. }
  55. return null;
  56. }
  57. work(resolve(process.argv[2] || ''), resolve(process.argv[3] || 'result.csv'))
  58. .catch(error => console.error(error));