Guest User

Untitled

a guest
Jul 16th, 2018
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. const fs = require('fs');
  2. const rl = require('readline');
  3. const Promise = require('bluebird');
  4. const NodeGeocoder = require('node-geocoder');
  5.  
  6. const geocoder = require('@google/maps').createClient({
  7. key: 'AIzaSyDh9h2rdiMnuvj3k5Ok82QsXufA5HuTkjs'
  8. });
  9.  
  10. // list files in text/ready directory
  11. const freeganShareablesFile = __dirname + '/../../data/txt/ready/dumpstersNY.txt';
  12. const fnbShareablesFile = __dirname + '/../../data/txt/ready/fnbNY.txt'
  13. const pantryShareablesFile = __dirname + '/../../data/txt/ready/all_pantries.txt'
  14.  
  15. Promise.all([
  16. processSubcategoryFile(freeganShareablesFile, 'freegan'),
  17. processSubcategoryFile(fnbShareablesFile, 'food not bombs'),
  18. processSubcategoryFile(pantryShareablesFile, 'pantry'),
  19. ])
  20. .then(results=>{
  21. const allShareables = results.reduce((curr, acc)=>acc.concat(curr), []);
  22. console.log(allShareables)
  23. })
  24.  
  25.  
  26. function getKVFromLine(line) {
  27. function cleanKey(key) {
  28. return key.toLowerCase().replace(/:/g, '')
  29. }
  30.  
  31. const regExp = /^\s*(.*?:+)\s*(\w.*)\s*/
  32. const kvPair = line.match(regExp);
  33. if (!kvPair) {
  34. console.warn("ERROR no match for line", line);
  35. }
  36. return {
  37. key: cleanKey(kvPair[1]),
  38. value: kvPair[2]
  39. }
  40. }
  41.  
  42. function processSubcategoryFile(filepath, subcategory) {
  43. return new Promise(function (resolve) {
  44. let subcategoryShareables = [];
  45.  
  46. const subcategoryLineInterface = rl.createInterface({
  47. input: fs.createReadStream(filepath)
  48. })
  49. let shareable = new Object();
  50. let kv;
  51.  
  52. subcategoryLineInterface.on('line', function (line) {
  53. // if blank line, skip regex
  54. if (line.match(/\w/)) {
  55.  
  56. // end old Shareable Record && start new Shareable Record
  57. if (line.startsWith('Name')) {
  58. // subcategoryShareables.push(shareable);
  59. shareable = new Object();
  60. shareable['subcategory'] = subcategory;
  61. console.log('name line', line);
  62. shareable['name'] = getKVFromLine(line).value;
  63. }
  64.  
  65. // if Key === 'Comment', add to comments array
  66. else if (line.startsWith('Comment')) {
  67. if (!shareable.comments) {
  68. shareable.comments = new Array();
  69. }
  70. shareable.comments.push(getKVFromLine(line).value);
  71. }
  72.  
  73. // for Each line "Key:Value", add to Shareable Object shareable[key] = value
  74. else {
  75. kv = getKVFromLine(line);
  76. shareable[kv.key] = kv.value;
  77.  
  78. /* If address, get the long and lat from the geocoder */
  79. if (kv.key === 'address') {
  80. setTimeout(function () {
  81. geocoder.geocode({address: `${shareable.address}`},
  82. function (err, response) {
  83. if (err) {
  84. console.error("error", err)
  85. } else {
  86. let location = response.json.results[0].geometry.location
  87. shareable.latitude = location.lat;
  88. shareable.longitude = location.lng;
  89. console.log('shareable', shareable)
  90. subcategoryShareables.push(shareable);
  91. }
  92. })
  93. }, 5000);
  94. /* .then(result => console.log('geocode', result))
  95. .catch(error => console.error('geocode error', error));
  96. }*/
  97. }
  98. }
  99. }
  100. resolve(subcategoryShareables);
  101. });
  102. })
  103. }
  104.  
  105.  
  106. /*
  107. // Geocode an address.
  108. googleMapsClient.geocode({
  109. address: '1600 Amphitheatre Parkway, Mountain View, CA'
  110. }, function(err, response) {
  111. if (!err) {
  112. console.log(response.json.results);
  113. }
  114. });
  115. */
Add Comment
Please, Sign In to add comment