Getting free beer

Every year in June, Guinness runs a giveaway called Brewery of Meteorology. If the weather is below 9.5c in your location, you’ll receive a $20 gift card, valid only at Dan Murphy’s locations. The website checks your eligibility using the HTTP Geolocation API. This can be easily spoofed using Chrome DevTools. I wrote a short script in python to quickly check the weather for a given postcode. This script will also give you coordinates to enter as your desired location. Grab a free API key from opencagedata.com. I’ve reused the API key for the weatherapi on the website. You can combine the payment on it with another method to buy something greater than $20.

import requests
import json

def get_coordinates(postcode):
    geocoding_api_key = '[get_your_own_api_key_from_opencagedata.com}'
    url = f'https://api.opencagedata.com/geocode/v1/json?q={postcode}&key={geocoding_api_key}'
    
    response = requests.get(url)
    if response.status_code == 200:
        data = response.json()
        if data['results']:
            lat = data['results'][0]['geometry']['lat']
            lon = data['results'][0]['geometry']['lng']
            return lat, lon
        else:
            raise ValueError("No results found for the provided postcode.")
    else:
        response.raise_for_status()

def fetch_weather_data(lat, lon):
    weather_api_key = '2917ead19a914ef29cf31605232404'
    url = f'https://api.weatherapi.com/v1/forecast.json?key={weather_api_key}&q={lat},{lon}&days=1'
    
    response = requests.get(url)
    if response.status_code == 200:
        return response.json()
    else:
        response.raise_for_status()

def is_local_guinness_weather(uV):
    try:
        min_temp_c = uV['forecast']['forecastday'][0]['day']['mintemp_c']
        return min_temp_c < 9.5
    except (KeyError, IndexError, TypeError):
        return False

def main():
    postcode = input("Please enter your postcode: ")
    try:
        lat, lon = get_coordinates(postcode)
        print(f"Latitude: {lat}, Longitude: {lon}")
        weather_data = fetch_weather_data(lat, lon)
        result = is_local_guinness_weather(weather_data)
        print(f"The minimum temperature for the provided postcode is {'below' if result else 'not below'} 9.5°C.")
    except Exception as e:
        print(f"An error occurred: {e}")

if __name__ == "__main__":
    main()