Advertisement
Guest User

Untitled

a guest
Apr 4th, 2023
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const location = await Location.current();
  2. const ZIP_CODE = await Location.reverseGeocode(location.latitude, location.longitude)
  3.   .then(results => results[0].postalCode);
  4. //Fuel Types
  5. //1=Regular
  6. //2=Midgrade
  7. //3=Premium
  8. //4=Diesel
  9. //5=E85
  10. //12=UNL88
  11. const FUEL_TYPE = '4';
  12.  
  13. // Get gas prices from GasBuddy
  14. const url = `https://www.gasbuddy.com/home?search=${ZIP_CODE}&fuel=${FUEL_TYPE}&method=all`;
  15. const response = await new Request(url).loadString();
  16.  
  17. // Parse gas prices using RegExp
  18. const regex = /<div class="SearchResults__name">(.+?)<\/div>[\s\S]*?<div class="SearchResults__price">(.+?)<\/div>/g;
  19. const gasPrices = [];
  20. let match;
  21. while ((match = regex.exec(response)) !== null) {
  22.   const stationName = match[1].trim();
  23.   const price = match[2].trim();
  24.   gasPrices.push({ stationName, price });
  25. }
  26.  
  27. // Create widget
  28. const widget = new ListWidget();
  29. widget.addSpacer();
  30.  
  31. const titleStack = widget.addStack();
  32. const title = titleStack.addText('Gas Prices');
  33. title.font = Font.mediumSystemFont(16);
  34. title.textColor = Color.white();
  35. titleStack.addSpacer();
  36.  
  37. widget.addSpacer();
  38.  
  39. if (gasPrices.length > 0) {
  40.   for (const gasPrice of gasPrices) {
  41.     const stack = widget.addStack();
  42.     stack.addSpacer();
  43.  
  44.     const stationName = stack.addText(gasPrice.stationName);
  45.     stationName.textColor = Color.white();
  46.  
  47.     stack.addSpacer();
  48.  
  49.     const price = stack.addText(gasPrice.price);
  50.     price.textColor = Color.yellow();
  51.  
  52.     stack.addSpacer();
  53.   }
  54. } else {
  55.   const stack = widget.addStack();
  56.   stack.addSpacer();
  57.   const noData = stack.addText('No data');
  58.   noData.textColor = Color.white();
  59.   stack.addSpacer();
  60. }
  61.  
  62. widget.addSpacer();
  63.  
  64. // Set widget background color
  65. widget.backgroundColor = Color.black();
  66.  
  67. // Set widget refresh interval
  68. widget.refreshAfterDate = new Date(Date.now() + 60 * 60 * 1000); // Refresh every hour
  69.  
  70. // Set widget URL scheme to open GasBuddy website
  71. widget.url = 'https://www.gasbuddy.com/home?search=${ZIP_CODE}&fuel=${FUEL_TYPE}&method=all';
  72.  
  73. // Present widget
  74. Script.setWidget(widget);
  75. Script.complete();
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement