#Plume API

27 messages · Page 1 of 1 (latest)

charred sparrow
#

Hey all,

Hope you're well. I have tried using the Weather API template to setup the Air Quality API by Plume (found in the Asset Library) but cant seem to get the script right. Would anyone know how this can be set up? I am trying to get the air quality index of a specific city or using the user's current and get a value that can be used to connect to other elements in the lens!

Any help would be greatly appreciated.

Link to the documentation: [https://docs.snap.com/lens-studio/references/guides/lens-features/remote-apis/weather-api-documentation#air-quality-api-endpoints]

Thanks

fossil ferry
#

I can probably help. have you used a remote API in a lens before or is this the first time? they are a little different than most of the other assets in lens studio in terms of how you consume it

charred sparrow
#

@fossil ferry Hey! I have used the Weather API template but thats about it. Setting it up from scratch is why Im having issues. You're absolutely right that they are different. But I am basically trying to figure out how to script it by creating a controller script

fossil ferry
#

ahh yes, so you might need to know javascript pretty well to do this yourself. i remember it was kinda confusing when i made the controller script for the Places API. let me dig up that project and see if this is the key to your puzzle

#

// Import module
const Module = require("./Snapchat Places API API Module");
const ApiModule = new Module.ApiModule(script.remoteServiceModule);

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function fetchNearbyPlaces() {
  // Docs say to pass in Lat, Long but this does nor get used by the module
  // It knows where the user is and uses that

  var lat = 0; // not used
  var lng = 0; // not used

  var gps = 65; // might get used?
  var limit = 9; // might get used?

  // module executes a call back that needs to be in a specific format
  var cb = handleNearbyPlaces;

  // Make API call
  ApiModule.get_nearby_places(lat, lng, gps, limit, cb);
}

function handleNearbyPlaces(err, res) {
  if (err) {
    script.errorMsg.text = "there has been an error";
    return;
  }
  res.nearbyPlaces.forEach(fetchPlaceData);
}

function fetchPlaceData(p) {
  var cb = handlePlace;
  ApiModule.get_place(p.placeId, cb);
}

function handlePlace(err, res) {
  if (err) {
    script.errorMsg.text = "there has been an error";
    return;
  }
  if (!res.place) {
    script.errorMsg.text = "Missing Place";
    return;
  }
  appendPlaceToDisplay(res.place);
}

function appendPlaceToDisplay(place) {
  var instantiatedRow = script.textRowPrefab.instantiate(script.layoutContainer);
  var textC = instantiatedRow.getComponent("Component.Text");

  textC.text = "* " + place.name;
}

script.fetchNearbyPlaces = fetchNearbyPlaces;

#

so for you it would be like ApiModule.current_air_quality({"lat": 1, "lng": 2},callbackFunction)

#

and then callbackFunction for you looks like the handlePlace function, so name it like handleAirQualityData or something to help you remember wtf its doing. and the confusing part is the response. it comes in with a boolean if it errored and then the response might be in some field like response.data.airQuality or an array response[0].airQuality or who knows. you can always debug it by logging the response print(res) or print(Object.keys(res))

fossil ferry
#

if this doesnt make i am happy to go over this with you on a screenshare. the world needs more lenses that use air quality data 😎 🤘

charred sparrow
#

@fossil ferry Wow! Thank you sooo much for coming back to me with such an in depth response and taking the time to do this. Really appreciate it.

Okay I am going to try and test this out. I wouldnt say I know JS super well but let me try. if not, I might bother you again 🥲

fossil ferry
#

no problemo @charred sparrow 😄

#

just make sure to share the lens you're making

#

sounds cool

charred sparrow
#

I wish there were better tutorials on it. Maybe you can make one 😂

#

@fossil ferry Absolutely! Im trying to test out a concept and seeing how possible it is and was very close to giving up until now haha

fossil ferry
#

id say give it 20 more minutes. if youre not any closer then you probably wont be able to get it without first learning a bunch of javascript bs

#

and in that case, let me know and ill make the tutorial 😄

charred sparrow
#

@fossil ferry soooo I tried it and it doesnt seem to be working 🙈 Have you worked with it before? I havent even come across any tutorials regarding it.

neat token
#

@fossil ferry @charred sparrow Did you guys ever get this to work? I'm also a relative JS newbie and I'm really struggling with scripting the Plume API as well 🫠

charred sparrow
#

Although, I might go back to it and rebuild that project from scratch

neat token
sonic lodge
#

Are you guys getting an error or having trouble parsing the response?

#

You can always JSON.stringify the response to see all the fields. Here is an example that seems to work for me

#

`// @input Asset.RemoteServiceModule remoteServiceModule
//@input float lattitude = 73.9863
//@input float longitude = 40.7562
//@input Component.Text airQuality

// Import module
const Module = require("./Plume Labs Air Quality API Module");
const ApiModule = new Module.ApiModule(script.remoteServiceModule);

// Access functions defined in ApiModule like this:
//ApiModule.(function name)

ApiModule.current_air_quality(script.lattitude, script.longitude, callbackFunction);

function callbackFunction(error, response){
print(error);
print(JSON.stringify(response))

script.airQuality.text = response.airQuality.category;

}`

neat token
#

This looks really helpful, thanks! I was trying to parse the response just to begin with let alone use the data to influence the visuals. My problem personally is that I'm very new to JavaScript and don't know how to write syntax from scratch. I need to look at examples, but because the tutorials and templates that use remote APIs don't use Plume specifically, I haven't been able to translate their code into working for Plume!! For example, putting the latitude and longitude in as input floats - I didn't know to do that, because all the examples I could find were putting that as part of the function

neat token
#

I was mostly trying to follow the Lensathon tutorial where he uses the FTX API and was immediately stuck because trying to JSON.stringify that way did not work at all

sonic lodge