First commit

This commit is contained in:
Dita Aji Pratama 2024-08-28 19:46:13 +07:00
commit 9c9daacdb7
2 changed files with 85 additions and 0 deletions

63
moon.py Normal file
View File

@ -0,0 +1,63 @@
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

22
usage.py Normal file
View File

@ -0,0 +1,22 @@
import moon
from datetime import datetime
country = "Indonesia"
city = "Jakarta"
phase = moon.phase(country.lower().replace(" ", "-"), city.lower().replace(" ", "-"))
hijri = moon.hijri()
print("================================================")
print(phase['image'])
print(phase['phase'], f"({phase['percent']})")
print("================================================")
print(hijri['day']['ar'], f"({hijri['day']['en']})")
print(hijri['day']['number'], hijri['month']['ar'], f"({hijri['month']['en']})", hijri['year'], hijri['notation']['en'])
print(f"{country}, {city}")
print("================================================")
print(datetime.now().strftime("%I:%M:%S %p"))
print("================================================")
# Not support a day changing on sunset (Maghrib)
# NEXT: Prayer times