From 9c9daacdb7007d75c03139162f2bd18f19c4a056 Mon Sep 17 00:00:00 2001 From: Dita Aji Pratama Date: Wed, 28 Aug 2024 19:46:13 +0700 Subject: [PATCH] First commit --- moon.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ usage.py | 22 ++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 moon.py create mode 100644 usage.py diff --git a/moon.py b/moon.py new file mode 100644 index 0000000..c084c3c --- /dev/null +++ b/moon.py @@ -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 diff --git a/usage.py b/usage.py new file mode 100644 index 0000000..24a6428 --- /dev/null +++ b/usage.py @@ -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