gas/app.py

42 lines
1.7 KiB
Python
Raw Normal View History

2025-03-26 23:04:03 -04:00
from flask import Flask,render_template,send_from_directory
2025-03-06 00:58:29 -05:00
from flask_caching import Cache
2025-04-20 11:34:54 -04:00
import httpx
2025-03-06 00:58:29 -05:00
2025-03-26 23:04:03 -04:00
app=Flask(__name__)
app.config['CACHE_TYPE']='SimpleCache'
cache=Cache(app)
2025-03-06 00:58:29 -05:00
@app.route("/")
def index():
2025-04-15 18:19:31 -04:00
return render_template("map.html")
2025-03-06 00:58:29 -05:00
@app.route("/gas.svg")
def favicon():
2025-04-15 18:19:31 -04:00
return send_from_directory(app.static_folder,"gas.svg")
2025-03-06 00:58:29 -05:00
2025-04-20 11:34:54 -04:00
@app.route("/samsclub.csv")
2025-03-26 23:04:03 -04:00
@cache.cached(timeout=1800)
2025-04-20 11:34:54 -04:00
def sams():
2025-04-15 18:19:31 -04:00
url='https://www.samsclub.com/api/node/vivaldi/browse/v2/clubfinder/list?distance=10000&nbrOfStores=1000&singleLineAddr=10001'
2025-04-20 11:34:54 -04:00
headers={'User-Agent':'Mozilla/5.0','Accept-Encoding':'ztsd'}
sams=httpx.get(url,headers=headers,timeout=10).json()
result=[]
2025-04-15 18:19:31 -04:00
for s in sams:
if 'gasPrices' in s:
p={g['gradeId']:int(g['price']*100) for g in s['gasPrices']}
result.append(f"{p[11]},{p[16]},{s['geoPoint']['latitude']},{s['geoPoint']['longitude']},{s['id']}")
return "\n".join(result)
2025-04-20 11:34:54 -04:00
@app.route("/costco.csv")
@cache.cached(timeout=1800)
def costco():
url='https://www.costco.com/AjaxWarehouseBrowseLookupView?hasGas=true&populateWarehouseDetails=true'
headers={'Accept':'*/*','Accept-Encoding':'gzip,deflate,br,zstd','Accept-Language':'en','Referer':'costco.com','Sec-Fetch-Mode':'cors','User-Agent':'Mozilla/5.0 Firefox/140.0'}
costco=httpx.Client(http2=True).get(url,headers=headers,timeout=60).json()[1:]
result=[]
for s in costco:
if 'regular' in s['gasPrices'] and s['country']=='US':
p={g:int(float(s['gasPrices'][g])*100) for g in ['regular','premium']}
result.append(f"{p['regular']},{p['premium']},{s['latitude']},{s['longitude']},{s['displayName']}")
return "\n".join(result)