irongit
146 lines4.3 KBPython
1#!/usr/bin/env python3
2# /* ---- ๐Ÿ’ซ https://github.com/JaKooLit ๐Ÿ’ซ ---- */ #
3# original code https://gist.github.com/Surendrajat/ff3876fd2166dd86fb71180f4e9342d7
4# weather using python
5
6import requests
7import json
8import os
9from pyquery import PyQuery # install using `pip install pyquery`
10
11# weather icons
12weather_icons = {
13 "sunnyDay": "๓ฐ–™",
14 "clearNight": "๓ฐ–”",
15 "cloudyFoggyDay": "๎Œƒ",
16 "cloudyFoggyNight": "๎†",
17 "rainyDay": "๎Œ†",
18 "rainyNight": "๎Œฑ",
19 "snowyIcyDay": "๎ŒŠ",
20 "snowyIcyNight": "๎Œง",
21 "severe": "๎ฎ",
22 "default": "๎Œ‚",
23}
24
25
26# Get current location based on IP address
27def get_location():
28 response = requests.get("https://ipinfo.io")
29 data = response.json()
30 loc = data["loc"].split(",")
31 return float(loc[0]), float(loc[1])
32
33
34# Get latitude and longitude
35latitude, longitude = get_location()
36
37# Open-Meteo API endpoint
38url = f"https://weather.com/en-PH/weather/today/l/{latitude},{longitude}?unit=f"
39
40# manual location_id
41# NOTE: if you want to add manually, make sure you disable def get_location above
42# to get your own location_id, go to https://weather.com & search your location.
43# once you choose your location, you can see the location_id in the URL(64 chars long hex string)
44# like this: https://weather.com/en-PH/weather/today/l/bca47d1099e762a012b9a139c36f30a0b1e647f69c0c4ac28b537e7ae9c1c200
45# location_id = "bca47d1099e762a012b9a139c36f30a0b1e647f69c0c4ac28b537e7ae9c1c200" # TODO
46
47# NOTE to change to deg F, change the URL to your preffered location after weather.com
48# Default is English-Philippines with Busan, South Korea as location_id
49# get html page
50# url = "https://weather.com/en-PH/weather/today/l/" + location_id
51
52html_data = PyQuery(url=url)
53
54# current temperature
55temp = html_data("span[data-testid='TemperatureValue']").eq(0).text()
56
57# current status phrase
58status = html_data("div[data-testid='wxPhrase']").text()
59status = f"{status[:16]}.." if len(status) > 17 else status
60
61# status code
62status_code = html_data("#regionHeader").attr("class").split(" ")[2].split("-")[2]
63
64# status icon
65icon = (
66 weather_icons[status_code]
67 if status_code in weather_icons
68 else weather_icons["default"]
69)
70
71# temperature feels like
72temp_feel = html_data(
73 "div[data-testid='FeelsLikeSection'] > span > span[data-testid='TemperatureValue']"
74).text()
75temp_feel_text = f"Feels like {temp_feel}F"
76
77# min-max temperature
78temp_min = (
79 html_data("div[data-testid='wxData'] > span[data-testid='TemperatureValue']")
80 .eq(1)
81 .text()
82)
83temp_max = (
84 html_data("div[data-testid='wxData'] > span[data-testid='TemperatureValue']")
85 .eq(0)
86 .text()
87)
88temp_min_max = f"๏‹‹ {temp_min}\t\t๏‹‡ {temp_max}"
89
90# wind speed
91wind_speed = html_data("span[data-testid='Wind']").text().split("\n")[0]
92wind_text = f"๎‰พ {wind_speed}"
93
94# humidity
95humidity = html_data("span[data-testid='PercentageValue']").text()
96humidity_text = f"๎ณ {humidity}"
97
98# visibility
99visibility = html_data("span[data-testid='VisibilityValue']").text()
100visibility_text = f"๏ฎ {visibility}"
101
102# air quality index
103air_quality_index = html_data("text[data-testid='DonutChartValue']").text()
104
105# hourly rain prediction
106prediction = html_data("section[aria-label='Hourly Forecast']")(
107 "div[data-testid='SegmentPrecipPercentage'] > span"
108).text()
109prediction = prediction.replace("Chance of Rain", "")
110prediction = f"\n\n๎Œ˜ (hourly) {prediction}" if len(prediction) > 0 else prediction
111
112# tooltip text
113tooltip_text = str.format(
114 "\t\t{}\t\t\n{}\n{}\n{}\n\n{}\n{}\n{}{}",
115 f'<span size="xx-large">{temp}</span>',
116 f"<big> {icon}</big>",
117 f"<b>{status}</b>",
118 f"<small>{temp_feel_text}</small>",
119 f"<b>{temp_min_max}</b>",
120 f"{wind_text}\t{humidity_text}",
121 f"{visibility_text}\tAQI {air_quality_index}",
122 f"<i> {prediction}</i>",
123)
124
125# print waybar module data
126out_data = {
127 "text": f"{icon} {temp}",
128 "alt": status,
129 "tooltip": tooltip_text,
130 "class": status_code,
131}
132print(json.dumps(out_data))
133
134simple_weather = (
135 f"{icon} {status}\n"
136 + f"๏‹‰ {temp} ({temp_feel_text})\n"
137 + f"{wind_text} \n"
138 + f"{humidity_text} \n"
139 + f"{visibility_text} AQI{air_quality_index}\n"
140)
141
142try:
143 with open(os.path.expanduser("~/.cache/.weather_cache"), "w") as file:
144 file.write(simple_weather)
145except Exception as e:
146 print(f"Error writing to cache: {e}")