From 45b874864d5e5befe0f152384ed76a659a639572 Mon Sep 17 00:00:00 2001 From: Simon Junod Date: Thu, 2 May 2024 17:06:26 +0200 Subject: [PATCH] ... --- cambot/ephemeris.py | 79 ++++++++++++++++++++++++--------------------- foo.py | 8 +++++ 2 files changed, 50 insertions(+), 37 deletions(-) create mode 100644 foo.py diff --git a/cambot/ephemeris.py b/cambot/ephemeris.py index e04fd22..e0c80dd 100644 --- a/cambot/ephemeris.py +++ b/cambot/ephemeris.py @@ -1,4 +1,3 @@ -import os import requests from datetime import datetime from nextcord import Embed, Color @@ -9,6 +8,7 @@ from bs4 import BeautifulSoup as bs from .settings import * from .saints import SAINTS + def citation(): try: req = requests.get("http://www.unjourunpoeme.fr") @@ -22,27 +22,29 @@ def citation(): except Exception: return "Impossible de trouver la citation du jour. Bouuuh !" + def saint(): today = datetime.now().strftime("%m/%d") - saint = SAINTS[today] - return saint + return SAINTS[today] + def weather_emoji(_id): - emojis = {re.compile(r"2.."):":thunder_cloud_rain:", - re.compile(r"3.."):":white_sun_rain_cloud:", - re.compile(r"5.."):":cloud_rain:", - re.compile(r"6.."):":snowflake:", - re.compile(r"7.."):":interrobang:", - re.compile(r"800"):":sunny:", - re.compile(r"801"):":white_sun_small_cloud:", - re.compile(r"80[23]"):":white_sun_cloud:", - re.compile(r"804"):":cloud:"} + emojis = {re.compile(r"2.."): ":thunder_cloud_rain:", + re.compile(r"3.."): ":white_sun_rain_cloud:", + re.compile(r"5.."): ":cloud_rain:", + re.compile(r"6.."): ":snowflake:", + re.compile(r"7.."): ":interrobang:", + re.compile(r"800"): ":sunny:", + re.compile(r"801"): ":white_sun_small_cloud:", + re.compile(r"80[23]"): ":white_sun_cloud:", + re.compile(r"804"): ":cloud:"} try: code = next(emoji[1] for emoji in emojis.items() if emoji[0].match(str(_id))) except StopIteration: code = ":negative_squared_cross_mark:" return code + def weather(lat, lon): r = requests.get(f"https://api.openweathermap.org/data/2.5/" f"onecall?lat={lat}&lon={lon}&appid={OWM_KEY}&lang=fr&units=metric") @@ -51,45 +53,48 @@ def weather(lat, lon): next_hours = [] for i in (2, 8, 14): hourly = j["hourly"][i] - time = datetime.fromtimestamp(hourly["dt"]).strftime("%H:%M") - temp = round(hourly["temp"]) - feels_like = hourly["feels_like"] - humidity = hourly["humidity"] - wind_speed = round(hourly["wind_speed"] * 3.6) - description = hourly["weather"][0]["description"] - emoji = weather_emoji(hourly["weather"][0]["id"]) - next_hours.append({"time":time, - "emoji":emoji, - "description":description, - "temp":temp, - "wind_speed":wind_speed + next_hours.append({"time": datetime.fromtimestamp(hourly["dt"]).strftime("%H:%M"), + "emoji": weather_emoji(hourly["weather"][0]["id"]), + "description": hourly["weather"][0]["description"], + "temp": round(hourly["temp"]), + "wind_speed": round(hourly["wind_speed"] * 3.6) }) return next_hours + def digest(): try: locale.setlocale(locale.LC_ALL, "fr_CH.utf-8") except locale.Error: pass now = datetime.now() - d = now.strftime("%A %-d %B") - c = citation() - s = saint() - w_strings = [] - for w in weather(46.5196661, 6.6325467): - w_strings.append(f"{w['emoji']} {w['description']} ({w['temp']}°C, {w['wind_speed']} km/h)") - embed = Embed() + include_citation = False + include_weather = True + include_artwork = True + + embed = Embed(color=[Color.red(), Color.gold(), Color.orange(), Color.blue(), Color.green(), Color.magenta(), Color.purple()][now.weekday()]) embed.title = "Bonjour !" - embed.description = f"Nous sommes le {d} ({s})\n\n{c}\n\u200B" - colors = [Color.red(), Color.gold(), Color.orange(), Color.blue(), Color.green(), Color.magenta(), Color.purple()] - embed.color = colors[now.weekday()] - embed.add_field(name="Matin", value=w_strings[0]) - embed.add_field(name="Après-midi", value=w_strings[1]) - embed.add_field(name="Soir", value=w_strings[2]) + embed.description = f"Nous sommes le {now.strftime('%A %-d %B')} ({saint()})" + + if include_citation: + embed.description += f"\n\n{citation()}\n\u200B" + + if include_weather: + w_strings = [] + for w in weather(46.5196661, 6.6325467): + w_strings.append(f"{w['emoji']} {w['description']} ({w['temp']}°C, {w['wind_speed']} km/h)") + embed.add_field(name="Matin", value=w_strings[0]) + embed.add_field(name="Après-midi", value=w_strings[1]) + embed.add_field(name="Soir", value=w_strings[2]) + + if include_artwork: + embed.set_image("https://uploads3.wikiart.org/00340/images/tintoretto/the-miracle-of-st-mark-freeing-the-slave.jpg") + embed.set_footer(text="Coucou") return embed + if __name__ == "__main__": print(citation()) diff --git a/foo.py b/foo.py new file mode 100644 index 0000000..15e8a6d --- /dev/null +++ b/foo.py @@ -0,0 +1,8 @@ +import requests +import json +from bs4 import BeautifulSoup as bs + +html = requests.get("https://www.wikiart.org/").text +soup = bs(html, features="html.parser") + +print(json.loads(soup.find("main", {"class": "wiki-layout-main-page"})["ng-init"].splitlines()[0][28:-1])) \ No newline at end of file