mvan231

Pollen.com API Widget

Nov 1st, 2021
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. /*Our pollen levels are on a scale of 12. Low is 0-2.4, Low-Medium is 2.5-4.8, Medium is 4.9-7.2, High-Medium is 7.3-9.6, and High is 9.7-12.0. These levels take into account how much pollen the allergy sufferer is likely to be exposed to for that given period.
  2.  
  3. https://www.pollen.com/help/faq
  4. */
  5. let indexDict = {"Low-Medium":{"lower":"2.5","upper":"4.8"},"High":{"lower":"9.7","upper":"12"},"High-Medium":{"lower":"7.3","upper":"9.6"},"Low":{"lower":"0","upper":"2.4"},"Medium":{"lower":"4.9","upper":"7.2"}}
  6.  
  7. let loc = await Location.current()
  8. log(loc)
  9.  
  10. const LAT = loc.latitude
  11. const LON = loc.longitude
  12.  
  13. var response = await Location.reverseGeocode(LAT, LON)
  14. log(response)
  15. var state = response[0].administrativeArea
  16. var LOCATION_NAME = response[0].postalAddress.city
  17. var zip = response[0].postalCode
  18. // log(state)
  19. // log(zip)
  20. // log(LOCATION_NAME)
  21. let url = `https://www.pollen.com/api/forecast/current/pollen/${zip}`
  22. let r = new Request(url)
  23. r.headers = {Referer:`https://www.pollen.com/forecast/current/pollen/${zip}`}
  24. let res = await r.loadJSON()
  25. log(res)
  26. let today = res.Location.periods[1]/*.forEach((f)=>{
  27. log(f.Triggers)
  28. })*/
  29. log(today)
  30. let level,levelFlg,ind = today.Index
  31.  
  32. Object.keys(indexDict).forEach((f) => {
  33.  
  34. if(!level)level = ((ind >= Number(indexDict[f]['lower'])) && (ind <= Number(indexDict[f]['upper'])))?f:false
  35. })
  36. log(level)
  37. let w= new ListWidget()
  38. w.addText(`Pollen.com Info For ${LOCATION_NAME}`)
  39. w.addText(`Index is ${level} ${ind}`)
  40. w.addText(`Top Allergens:`)
  41. today.Triggers.forEach((f)=>{
  42. w.addText(f.Name)
  43. })
  44.  
  45. Script.complete()
  46. Script.setWidget(w)
  47. w.presentMedium()
Advertisement
Add Comment
Please, Sign In to add comment