search.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. if (hasCpv("45", item)) {
  13. const v = oddiel_V(item);
  14. if (v) {
  15. processV(v, item, async items => await writeFile(outfile, poorMansCsv(items) + "\r\n", {flag: "a"}));
  16. }
  17. }
  18. }
  19. }
  20. }
  21. function processV (v, item, out) {
  22. const zakazka = item.zakazka[0].$;
  23. const id = zakazka.id || '';
  24. const url = zakazka.url || "about:blank";
  25. const pocetPonuk = -1;
  26. const predpokladanaCena = '?';
  27. const konecnaCena = '?';
  28. const vitazIco = 0;
  29. out([id, url, pocetPonuk, predpokladanaCena, konecnaCena, vitazIco]);
  30. }
  31. function poorMansCsv (items) {
  32. return JSON.stringify(items).slice(1, -1);
  33. }
  34. function hasCpv (prefix, item) {
  35. for (let {tag, value, cancel} of eachFormElement(item)) {
  36. if (tag === "Part") {
  37. const match = value.$.Title && value.$.Title.match(/^ODDIEL\s+(\w+):/);
  38. if (match && match[1] !== "II") cancel();
  39. }
  40. if (tag === "Cpv" && value.$.Code.startsWith(prefix)) return true;
  41. if (tag === "SelectList" && value.$.Type === "CPV" && value.SelectListValue && value.SelectListValue[0].$.Title.startsWith(prefix)) return true;
  42. }
  43. return false;
  44. }
  45. function oddiel_V (item) {
  46. for (let {tag, value, cancel} of eachFormElement(item)) {
  47. if (tag === "Part") {
  48. const match = value.$.Title && value.$.Title.match(/^ODDIEL\s+(\w+):/);
  49. if (match)
  50. if (match[1] === "V") return value; else cancel();
  51. }
  52. }
  53. return null;
  54. }
  55. work(resolve(process.argv[2] || ''), resolve(process.argv[3] || 'result.csv'))
  56. .catch(error => console.error(error));