npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dcts-google-places-api

v1.0.3

Published

Node client for Google Places API (placeSearch, placeDetails, placePhoto). Written by dcts.

Downloads

27

Readme

Google Places API (for node)

Node client to run the basic google places API requests (using axios). Published on NPM.

Installing

$ npm install dcts-google-places-api

Examples

Initialzation

const apiKey = "INSERT_YOUR_API_KEY"; // define API key
const GooglePlacesApi = require('dcts-google-places-api');
const googleapi = new GooglePlacesApi(apiKey); // initialize

Place Search

const searchQuery = "Pizza New York"; // example search

// with promises
googleapi.runPlaceSearch(searchQuery).then(placeId => {
  console.log(placeId); // returns placeId (as string) or null
});

// with async await
(async () => {
  let placeId = await googleapi.runPlaceSearch(searchQuery);
  console.log(placeId);
})();;

Place Details

Get place details by place_id.

const placeId = "ChIJHegKoJUfyUwRjMxaCcviZDA"; // placeId of "Pizza Chicken New York"

// with promises
googleapi.runPlaceDetails(placeId).then(placeDetails => {
  console.log(`place found: ${placeDetails.name}`);
})

// with async await
(async () => {
  let placeDetails = await googleapi.runPlaceDetails(placeId);
  console.log(`place found: ${placeDetails.name}`);
})();

Get place details by cid. This is a deprecated endpoint and not documented in the official google api documentation, but it still works. See also this answer on stackoverflow

const cid = "10056734717913051463"; // cid of "Brooklyn Boulders Chicago"

// with promises
googleapi.runPlaceDetailsCid(cid).then(placeDetails => {
  console.log(`place found: ${placeDetails.name} (${placeDetails.cid})`);
});

// with async await
(async () => {
  let placeDetails = await googleapi.runPlaceDetailsCid(cid);
  console.log(`place found: ${placeDetails.name} (${placeDetails.cid})`);
})();

Place Photos

⚠️ Be Aware: this function returns only the photoUrl, not the image itself.

const photoReference = 'CmRaAAAAqYV1efHXXLX3UB1msekeprgOUD362n4-8lxwYI3aSFANLw51oE1_KeNziEgnnbr5WQzJtQo9SbNnZFRfymg594T9h7yRWnLQL8w1n_ekN6BbyJzg1k0hadSJ4N0i63TmEhA3NIzf_JWUEZcW3VgXJ5FqGhRq7ij6D2Vl8DOSF2yHY1iuTYuAKA';

// with promises
googleapi.runPlacePhotos(photoReference).then(photoUrl => {
  console.log(`photo url: ${photoUrl}`);
});

// with async await
(async () => {
  let photoUrl = await googleapi.runPlacePhotos(photoReference);
  console.log(`photo url: ${photoUrl}`);
})();

Full Example

  1. Search for a place by textquery
  2. get place details from that place
  3. fetch all photo urls from that place
const textSearch = "Nolita Pizza New York";

// with promises
googleapi.runPlaceSearch(textSearch).then(placeId => {
  console.log(`place id found: ${placeId}`);
  return googleapi.runPlaceDetails(placeId);
}).then(placeDetails => {
  console.log(`place details fetched for ${placeDetails.name} (${placeDetails.place_id})`);
  const promises = [];
  placeDetails.photos.map(photo => {
    promises.push(googleapi.runPlacePhotos(photo.photo_reference));
  });
  return Promise.all(promises);
}).then(placePhotoUrls => {
  console.log(`${placePhotoUrls.length} photo urls fetched:`);
  console.log(placePhotoUrls);
});

// with async await
(async () => {
  let placeId = await googleapi.runPlaceSearch(textSearch)
  console.log(`place id found: ${placeId}`);

  let placeDetails = await googleapi.runPlaceDetails(placeId);
  console.log(`place details fetched for ${placeDetails.name} (${placeDetails.place_id})`);

  const placePhotoUrls = [];
  for (const photo of placeDetails.photos) {
    placePhotoUrls.push(await googleapi.runPlacePhotos(photo.photo_reference));
  }
  console.log(`${placePhotoUrls.length} photo urls fetched:`);
  console.log(placePhotoUrls);
})();

Run Tests (written with AVA js)

npm run test

ToDo's

  • [ ] run npm run test to see missing tests.
  • [ ] escape queries with encodeURI() or encodeURIComponent -> write tests for them first.
  • [ ] why some request work on google maps webinterface but not with the API, like this one: Bartleby’s Ice Cream Cakes -> no results from the API, but finds it on webinterface. I think it could be something with the top appostroph.

Credits

Written by dcts for personal use only. Check my portfolio website. Or go play tetris.