Jelajahi Sumber

Add super basic wikipedia details

Johannes Zellner 8 tahun lalu
induk
melakukan
7ddb0ff074
5 mengubah file dengan 174 tambahan dan 58 penghapusan
  1. 58 0
      package-lock.json
  2. 1 0
      package.json
  3. 17 11
      public/index.html
  4. 2 1
      public/index.js
  5. 96 46
      src/places.js

+ 58 - 0
package-lock.json

@@ -105,6 +105,11 @@
       "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.0.0.tgz",
       "integrity": "sha1-0ygVQE1olpn4Wk6k+odV3ROpYEg="
     },
+    "camelcase": {
+      "version": "4.1.0",
+      "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-4.1.0.tgz",
+      "integrity": "sha1-1UVjW+HjPFQmScaRc+Xeas+uNN0="
+    },
     "caseless": {
       "version": "0.12.0",
       "resolved": "https://registry.npmjs.org/caseless/-/caseless-0.12.0.tgz",
@@ -193,6 +198,14 @@
       "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-1.0.2.tgz",
       "integrity": "sha1-rT/0yG7C0CkyL1oCw6mmBslbP1k="
     },
+    "encoding": {
+      "version": "0.1.12",
+      "resolved": "https://registry.npmjs.org/encoding/-/encoding-0.1.12.tgz",
+      "integrity": "sha1-U4tm8+5izRq1HsMjgp0flIDHS+s=",
+      "requires": {
+        "iconv-lite": "~0.4.13"
+      }
+    },
     "error": {
       "version": "7.0.2",
       "resolved": "https://registry.npmjs.org/error/-/error-7.0.2.tgz",
@@ -366,6 +379,14 @@
         "safer-buffer": ">= 2.1.2 < 3"
       }
     },
+    "infobox-parser": {
+      "version": "3.0.3",
+      "resolved": "https://registry.npmjs.org/infobox-parser/-/infobox-parser-3.0.3.tgz",
+      "integrity": "sha512-KrEPqUtNfx0L5NiXrcqjcMMfDJxoEYpysdELOxI79Ymg28h/xLKzNWgyWe4JHamWQrnYW75htJSJdqI0mVDF5g==",
+      "requires": {
+        "camelcase": "^4.1.0"
+      }
+    },
     "inherits": {
       "version": "2.0.3",
       "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz",
@@ -376,11 +397,25 @@
       "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.8.0.tgz",
       "integrity": "sha1-6qM9bd16zo9/b+DJygRA5wZzix4="
     },
+    "is-stream": {
+      "version": "1.1.0",
+      "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-1.1.0.tgz",
+      "integrity": "sha1-EtSj3U5o4Lec6428hBc66A2RykQ="
+    },
     "is-typedarray": {
       "version": "1.0.0",
       "resolved": "https://registry.npmjs.org/is-typedarray/-/is-typedarray-1.0.0.tgz",
       "integrity": "sha1-5HnICFjfDBsR3dppQPlgEfzaSpo="
     },
+    "isomorphic-fetch": {
+      "version": "2.2.1",
+      "resolved": "https://registry.npmjs.org/isomorphic-fetch/-/isomorphic-fetch-2.2.1.tgz",
+      "integrity": "sha1-YRrhrPFPXoH3KVB0coGf6XM1WKk=",
+      "requires": {
+        "node-fetch": "^1.0.1",
+        "whatwg-fetch": ">=0.10.0"
+      }
+    },
     "isstream": {
       "version": "0.1.2",
       "resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz",
@@ -460,6 +495,15 @@
       "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.1.tgz",
       "integrity": "sha1-KzJxhOiZIQEXeyhWP7XnECrNDKk="
     },
+    "node-fetch": {
+      "version": "1.7.3",
+      "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-1.7.3.tgz",
+      "integrity": "sha512-NhZ4CsKx7cYm2vSrBAr2PvFOe6sWDf0UYLRqA6svUYg7+/TSfVAu49jYC4BvQ4Sms9SZgdqGBgroqfDhJdTyKQ==",
+      "requires": {
+        "encoding": "^0.1.11",
+        "is-stream": "^1.0.1"
+      }
+    },
     "oauth-sign": {
       "version": "0.9.0",
       "resolved": "https://registry.npmjs.org/oauth-sign/-/oauth-sign-0.9.0.tgz",
@@ -719,6 +763,20 @@
         "extsprintf": "^1.2.0"
       }
     },
+    "whatwg-fetch": {
+      "version": "3.0.0",
+      "resolved": "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-3.0.0.tgz",
+      "integrity": "sha512-9GSJUgz1D4MfyKU7KRqwOjXCXTqWdFNvEr7eUBYchQiVc744mqK/MzXPNR2WsPkmkOa4ywfg8C2n8h+13Bey1Q=="
+    },
+    "wikijs": {
+      "version": "4.10.2",
+      "resolved": "https://registry.npmjs.org/wikijs/-/wikijs-4.10.2.tgz",
+      "integrity": "sha512-A3uK3AvSlCbC2qUGzOjZ72xJh1xD4fNDq7Ay8QdDwk0i/uPSDF5OTIHJNvhD1thRhO5DeCVp+B8fEkv2ZnDCjw==",
+      "requires": {
+        "infobox-parser": "3.0.3",
+        "isomorphic-fetch": "^2.2.1"
+      }
+    },
     "xtend": {
       "version": "4.0.1",
       "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.1.tgz",

+ 1 - 0
package.json

@@ -15,6 +15,7 @@
   "dependencies": {
     "express": "^4.16.4",
     "request": "^2.88.0",
+    "wikijs": "^4.10.2",
     "yelp-fusion": "^2.2.1"
   }
 }

+ 17 - 11
public/index.html

@@ -42,17 +42,23 @@
         <div style="cursor: pointer;" @click="markerDetailsOpen = false;">
           <i class="fas fa-arrow-left"></i> Back to search results
         </div>
-        <div>
-            <h3>{{ markerDetails.name }} {{ markerDetails.businessDetails ? markerDetails.businessDetails.price : '' }}</h3>
-            <div v-if="markerDetails.businessDetails">
-              <el-carousel height="150px" type="card" :autoplay="false">
-                <el-carousel-item v-for="photo in markerDetails.businessDetails.photos">
-                  <img :src="photo" height="150px"/>
-                </el-carousel-item>
-              </el-carousel>
-            </div>
-            <p v-if="markerDetails.businessDetails"><el-rate v-model="markerDetails.businessDetails.rating" disabled></el-rate></p>
-            <p v-if="markerDetails.businessDetails">Phone: {{ markerDetails.businessDetails.display_phone }}</p>
+        <div v-if="markerDetails.class === 'business'">
+          <h3>{{ markerDetails.name }} {{ markerDetails.businessDetails ? markerDetails.businessDetails.price : '' }}</h3>
+          <div v-if="markerDetails.businessDetails">
+            <el-carousel height="150px" type="card" :autoplay="false">
+              <el-carousel-item v-for="photo in markerDetails.businessDetails.photos">
+                <img :src="photo" height="150px"/>
+              </el-carousel-item>
+            </el-carousel>
+          </div>
+          <p v-if="markerDetails.businessDetails"><el-rate v-model="markerDetails.businessDetails.rating" disabled></el-rate></p>
+          <p v-if="markerDetails.businessDetails">Phone: {{ markerDetails.businessDetails.display_phone }}</p>
+        </div>
+        <div v-if="markerDetails.class === ''">
+          <h3>{{ markerDetails.name }}</h3>
+          <p><a :href="markerDetails.businessDetails.url" target="_blank">Wikipedia</a></p>
+          <img v-show="markerDetails.businessDetails.photo" :src="markerDetails.businessDetails.photo" style="max-height: 150px; max-width: 100%;"/>
+          <p>{{ markerDetails.businessDetails.summary }}</p>
         </div>
       </div>
     </div>

+ 2 - 1
public/index.js

@@ -75,7 +75,8 @@ app = new Vue({
             var params = {
                 name: item.name,
                 lat: item.lat,
-                lon: item.lon
+                lon: item.lon,
+                class: item.class
             };
 
             axios.get('/api/places/details', {

+ 96 - 46
src/places.js

@@ -5,8 +5,11 @@ exports = module.exports = {
 };
 
 var request = require('request'),
+    wikijs = require('wikijs').default,
     yelp = require('yelp-fusion');
 
+var wiki = wikijs();
+
 var YELP_CLIENTID = 'VMEhsiPTvZni4eGAFSaPeA';
 var YELP_APIKEY = 'uN7lv_ywa-5osEguCmaGeE8vnGKZezJO3c2-ADLd1ySCPcGTWiK71XwdgvGNeavvw04Yc84rHaJzkSAhvNmFubQ5zyMAsdR0gCuLpVFhqHXTshl67BdbDO6WYSYlXHYx';
 
@@ -23,41 +26,63 @@ function search(req, res, next) {
     const lat = req.query.lat;
     const lon = req.query.lon;
 
-    function amendDetails(results) {
-        results.forEach(function (r) {
+    function translateSearchResultItems(results) {
+        var classBusiness = [
+            'fast_food',
+            'restaurant',
+            'pub',
+            'bar',
+            'hotel',
+            'hostel',
+            'supermarket',
+            'retail'
+        ];
+
+        return results.map(function (r) {
+            var tmp = {
+                icon: 'fa-address-card',
+                address: r.address,
+                lat: r.lat,
+                lon: r.lon,
+                type: r.type,
+                class: classBusiness.indexOf(r.type) !== -1 ? 'business' : '',
+                display_name: r.display_name
+            };
+
             // icons
-            r.icon = 'fa-address-card';
-            if (r.type === 'fast_food' || r.type === 'restaurant') r.icon = 'fa-utensils';
-            if (r.type === 'doctors') r.icon = 'fa-user-md';
-            if (r.type === 'beach') r.icon = 'fa-umbrella-beach';
-            if (r.type === 'dentist') r.icon = 'fa-tooth';
-            if (r.type === 'car') r.icon = 'fa-car';
-            if (r.type === 'playground') r.icon = 'fa-child';
-            if (r.type === 'recreation_ground') r.icon = 'fa-running';
-            if (r.type === 'sports_centre' || r.type === 'pitch') r.icon = 'fa-futbol';
-            if (r.type === 'bicycle-rental') r.icon = 'fa-bicycle';
-            if (r.type === 'school') r.icon = 'fa-graduation-cap';
-            if (r.type === 'post_box' || r.type === 'post_office') r.icon = 'fa-envelope-square';
-            if (r.type === 'canal' || r.type === 'river' || r.type === 'water') r.icon = 'fa-water';
-            if (r.type === 'station' || r.type === 'stop') r.icon = 'fa-subway';
-            if (r.type === 'attraction') r.icon = 'fa-monument';
-            if (r.type === 'city' || r.type === 'village' || r.type === 'administrative') r.icon = 'fa-city';
-            if (r.type === 'administrative' || r.class === 'office') r.icon = 'fa-building';
-            if (r.type === 'pub' || r.type === 'bar') r.icon = 'fa-beer';
-            if (r.type === 'hotel' || r.type === 'hostel') r.icon = 'fa-bed';
-            if (r.type === 'supermarket' || r.type === 'retail') r.icon = 'fa-shopping-cart';
+            if (r.type === 'fast_food' || r.type === 'restaurant') tmp.icon = 'fa-utensils';
+            if (r.type === 'doctors') tmp.icon = 'fa-user-md';
+            if (r.type === 'beach') tmp.icon = 'fa-umbrella-beach';
+            if (r.type === 'dentist') tmp.icon = 'fa-tooth';
+            if (r.type === 'car') tmp.icon = 'fa-car';
+            if (r.type === 'playground') tmp.icon = 'fa-child';
+            if (r.type === 'recreation_ground') tmp.icon = 'fa-running';
+            if (r.type === 'sports_centre' || r.type === 'pitch') tmp.icon = 'fa-futbol';
+            if (r.type === 'bicycle-rental') tmp.icon = 'fa-bicycle';
+            if (r.type === 'school') tmp.icon = 'fa-graduation-cap';
+            if (r.type === 'post_box' || r.type === 'post_office') tmp.icon = 'fa-envelope-square';
+            if (r.type === 'canal' || r.type === 'river' || r.type === 'water') tmp.icon = 'fa-water';
+            if (r.type === 'station' || r.type === 'stop') tmp.icon = 'fa-subway';
+            if (r.type === 'attraction') tmp.icon = 'fa-monument';
+            if (r.type === 'city' || r.type === 'village' || r.type === 'administrative') tmp.icon = 'fa-city';
+            if (r.type === 'administrative' || r.class === 'office') tmp.icon = 'fa-building';
+            if (r.type === 'pub' || r.type === 'bar') tmp.icon = 'fa-beer';
+            if (r.type === 'hotel' || r.type === 'hostel') tmp.icon = 'fa-bed';
+            if (r.type === 'supermarket' || r.type === 'retail') tmp.icon = 'fa-shopping-cart';
 
             // ensure we have some city if only town is mentioned
-            if (!r.address.city) r.address.city = r.address.town;
+            if (!tmp.address.city) tmp.address.city = tmp.address.town;
 
             // name
-            r.name = r.display_name.split(',')[0];
-            if (r.class === 'place' || r.class === 'building') r.name = r.address.road + ' ' + r.address.house_number;
-            if (r.type === 'city') r.name = r.address.city;
-            if (!r.name) r.name = r.display_name;
-        });
+            tmp.name = r.display_name.split(',')[0];
+            if (r.class === 'place' || r.class === 'building') tmp.name = r.address.road + ' ' + r.address.house_number;
+            if (r.type === 'city') tmp.name = r.address.city;
+
+            // fallback to display name
+            if (!tmp.name) tmp.name = r.display_name;
 
-        return results;
+            return tmp;
+        });
     }
 
     function removeDuplicates(results) {
@@ -89,7 +114,7 @@ function search(req, res, next) {
     function noLocationSearch() {
         query(null, function (result) {
             result = removeDuplicates(result);
-            res.status(200).send(amendDetails(result));
+            res.status(200).send(translateSearchResultItems(result));
         });
     }
 
@@ -112,16 +137,16 @@ function search(req, res, next) {
 
         query(position.address.city + ' , ' + position.address.country, function (result) {
             results = removeDuplicates(results.concat(result));
-            if (results.length >= 20) return res.status(200).send(amendDetails(results));
+            if (results.length >= 20) return res.status(200).send(translateSearchResultItems(results));
 
             query(position.address.country, function (result) {
                 results = removeDuplicates(results.concat(result));
-                if (results.length >= 20) return res.status(200).send(amendDetails(results));
+                if (results.length >= 20) return res.status(200).send(translateSearchResultItems(results));
 
                 query(null, function (result) {
                     results = removeDuplicates(results.concat(result));
 
-                    return res.status(200).send(amendDetails(results));
+                    return res.status(200).send(translateSearchResultItems(results));
                 });
             });
         });
@@ -133,20 +158,45 @@ function details(req, res, next) {
     const name = req.query.name;
     const lat = req.query.lat;
     const lon = req.query.lon;
-
-    yelpClient.search({
-        term: name,
-        latitude: lat,
-        longitude: lon
-    }).then(response => {
-        if (!response.jsonBody.businesses[0]) return res.status(200).send({});
-
-        yelpClient.business(response.jsonBody.businesses[0].id).then(response => {
-            res.status(200).send(response.jsonBody);
+    const itemClass = req.query.class;
+
+    if (itemClass === 'business') {
+        yelpClient.search({
+            term: name,
+            latitude: lat,
+            longitude: lon
+        }).then(response => {
+            if (!response.jsonBody.businesses[0]) return res.status(200).send({});
+
+            yelpClient.business(response.jsonBody.businesses[0].id).then(response => {
+                res.status(200).send(response.jsonBody);
+            }).catch(e => {
+                console.log(e);
+            });
         }).catch(e => {
             console.log(e);
         });
-    }).catch(e => {
-        console.log(e);
-    });
+    } else {
+        wiki.find(name).then(function (page) {
+            page.info().then(function (info) {
+                info.url = page.raw.fullurl;
+
+                page.summary().then(function (summary) {
+                    page.mainImage().then(function (mainImage) {
+                        info.photo = mainImage;
+                        info.summary = summary;
+                        res.status(200).send(info);
+                    }).catch(function (e) {
+                        console.log(e);
+                    });
+                }).catch(function (e) {
+                    console.log(e);
+                });
+            }).catch(function (e) {
+                console.log(e);
+            });
+        }).catch(function (e) {
+            console.log(e);
+        });
+    }
 }