places.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. exports = module.exports = {
  2. search: search,
  3. details: details
  4. };
  5. var request = require('request'),
  6. yelp = require('yelp-fusion');
  7. var YELP_CLIENTID = 'VMEhsiPTvZni4eGAFSaPeA';
  8. var YELP_APIKEY = 'uN7lv_ywa-5osEguCmaGeE8vnGKZezJO3c2-ADLd1ySCPcGTWiK71XwdgvGNeavvw04Yc84rHaJzkSAhvNmFubQ5zyMAsdR0gCuLpVFhqHXTshl67BdbDO6WYSYlXHYx';
  9. const yelpClient = yelp.client(YELP_APIKEY);
  10. const HEADERS = {
  11. 'User-Agent': 'Cloudron Maps Application'
  12. };
  13. function search(req, res, next) {
  14. console.log('Search query:', req.query);
  15. const queryString = req.query.search;
  16. const lat = req.query.lat;
  17. const lon = req.query.lon;
  18. function amendDetails(results) {
  19. results.forEach(function (r) {
  20. // icons
  21. r.icon = 'fa-address-card';
  22. if (r.type === 'fast_food' || r.type === 'restaurant') r.icon = 'fa-utensils';
  23. if (r.type === 'doctors') r.icon = 'fa-user-md';
  24. if (r.type === 'beach') r.icon = 'fa-umbrella-beach';
  25. if (r.type === 'dentist') r.icon = 'fa-tooth';
  26. if (r.type === 'car') r.icon = 'fa-car';
  27. if (r.type === 'playground') r.icon = 'fa-child';
  28. if (r.type === 'recreation_ground') r.icon = 'fa-running';
  29. if (r.type === 'sports_centre' || r.type === 'pitch') r.icon = 'fa-futbol';
  30. if (r.type === 'bicycle-rental') r.icon = 'fa-bicycle';
  31. if (r.type === 'school') r.icon = 'fa-graduation-cap';
  32. if (r.type === 'post_box' || r.type === 'post_office') r.icon = 'fa-envelope-square';
  33. if (r.type === 'canal' || r.type === 'river' || r.type === 'water') r.icon = 'fa-water';
  34. if (r.type === 'station' || r.type === 'stop') r.icon = 'fa-subway';
  35. if (r.type === 'attraction') r.icon = 'fa-monument';
  36. if (r.type === 'city' || r.type === 'village' || r.type === 'administrative') r.icon = 'fa-city';
  37. if (r.type === 'administrative' || r.class === 'office') r.icon = 'fa-building';
  38. if (r.type === 'pub' || r.type === 'bar') r.icon = 'fa-beer';
  39. if (r.type === 'hotel' || r.type === 'hostel') r.icon = 'fa-bed';
  40. if (r.type === 'supermarket' || r.type === 'retail') r.icon = 'fa-shopping-cart';
  41. // ensure we have some city if only town is mentioned
  42. if (!r.address.city) r.address.city = r.address.town;
  43. // name
  44. r.name = r.display_name.split(',')[0];
  45. if (r.class === 'place' || r.class === 'building') r.name = r.address.road + ' ' + r.address.house_number;
  46. if (r.type === 'city') r.name = r.address.city;
  47. if (!r.name) r.name = r.display_name;
  48. });
  49. return results;
  50. }
  51. function removeDuplicates(results) {
  52. var tmp = [];
  53. results.forEach(function (a) {
  54. if (tmp.find(function (b) { return b.place_id === a.place_id; })) return;
  55. tmp.push(a);
  56. });
  57. return tmp;
  58. }
  59. function query(detail, callback) {
  60. var params = {
  61. format: 'json',
  62. limit: 20,
  63. addressdetails: 1,
  64. q: (detail && queryString.indexOf(detail) === -1) ? (queryString + ' , ' + detail) : queryString
  65. };
  66. request.get('https://nominatim.openstreetmap.org/search.php', { qs: params, headers: HEADERS }, function (error, response, body) {
  67. if (error) return console.error(error);
  68. return callback(JSON.parse(body));
  69. });
  70. }
  71. function noLocationSearch() {
  72. query(null, function (result) {
  73. result = removeDuplicates(result);
  74. res.status(200).send(amendDetails(result));
  75. });
  76. }
  77. if (!lat || !lon) return noLocationSearch();
  78. var results = [];
  79. var params = {
  80. format: 'json',
  81. lat: lat,
  82. lon: lon
  83. };
  84. request.get('https://nominatim.openstreetmap.org/reverse.php', { qs: params, headers: HEADERS }, function (error, response, body) {
  85. if (error) return console.error(error);
  86. const position = JSON.parse(body);
  87. if (!position || !position.address) return noLocationSearch();
  88. query(position.address.city + ' , ' + position.address.country, function (result) {
  89. results = removeDuplicates(results.concat(result));
  90. if (results.length >= 20) return res.status(200).send(amendDetails(results));
  91. query(position.address.country, function (result) {
  92. results = removeDuplicates(results.concat(result));
  93. if (results.length >= 20) return res.status(200).send(amendDetails(results));
  94. query(null, function (result) {
  95. results = removeDuplicates(results.concat(result));
  96. return res.status(200).send(amendDetails(results));
  97. });
  98. });
  99. });
  100. });
  101. }
  102. function details(req, res, next) {
  103. const name = req.query.name;
  104. const lat = req.query.lat;
  105. const lon = req.query.lon;
  106. yelpClient.search({
  107. term: name,
  108. latitude: lat,
  109. longitude: lon
  110. }).then(response => {
  111. if (!response.jsonBody.businesses[0]) return res.status(200).send({});
  112. yelpClient.business(response.jsonBody.businesses[0].id).then(response => {
  113. res.status(200).send(response.jsonBody);
  114. }).catch(e => {
  115. console.log(e);
  116. });
  117. }).catch(e => {
  118. console.log(e);
  119. });
  120. }