moon/moon.py
2024-08-28 19:46:13 +07:00

64 lines
1.9 KiB
Python

import requests
from bs4 import BeautifulSoup
from hijridate import Hijri
from datetime import date
def phase(country, city):
response = requests.get(f"https://www.timeanddate.com/moon/phases/{country}/{city}")
soup = BeautifulSoup(response.content, "html.parser")
moon_image = soup.find("img", id="cur-moon")
moon_image_src = f"https://www.timeanddate.com{moon_image.get('src')}" if moon_image else "Image not found"
moon_percent_span = soup.find("span", id="cur-moon-percent")
moon_percent = moon_percent_span.get_text(strip=True) if moon_percent_span else "Percent not found"
info_table = soup.find("div", class_="bk-focus__info").find("table")
table_data = []
if info_table:
rows = info_table.find_all("tr")
for row in rows:
cells = row.find_all(["th", "td"])
row_data = [cell.get_text(strip=True) for cell in cells]
table_data.append(row_data)
curtime = table_data[0][1]
curphase = table_data[1][1]
else:
table_data = "Table not found"
curtime = None
curphase = None
result = {
"image" :moon_image_src,
"percent" :moon_percent,
"time" :curtime,
"phase" :curphase
}
return result
def hijri():
hijri = Hijri.today()
result = {
"isoformat":hijri.isoformat(),
"day":{
"number":date.fromisoformat(hijri.isoformat()).day,
"ar":hijri.day_name('ar'),
"en":hijri.day_name('en')
},
"month":{
"number":date.fromisoformat(hijri.isoformat()).month,
"ar":hijri.month_name('ar'),
"en":hijri.month_name('en')
},
"year":date.fromisoformat( hijri.isoformat() ).year,
"notation":{
"ar":hijri.notation('ar'),
"en":hijri.notation('en')
}
}
return result