Exemple #1
0
 async def apod(self, ctx: aoi.AoiContext, *, date: dtime() = None):
     if not date:
         date = datetime.datetime.now()
     dt = date.strftime('%Y-%m-%d')
     if dt not in self.apod_cache:
         async with ctx.typing():
             async with aiohttp.ClientSession() as sess:
                 async with sess.get(
                         f"https://api.nasa.gov/planetary/apod?api_key={self.bot.nasa}&"
                         f"date={dt}") as resp:
                     js = await resp.json()
         if js.get("code", None) in [404, 400, 403, 401]:
             self.apod_cache[dt] = (str(js["code"]), "404", "404",
                                    js["msg"])
             return await ctx.send_error(js["msg"])
         url = js["url"]
         hdurl = js.get("hdurl", url)
         expl = js["explanation"][:1900]
         title = js["title"] + " " + dt
         self.apod_cache[dt] = (title, hdurl, url, expl)
     else:
         title, hdurl, url, expl = self.apod_cache[dt]
         if title == "404":
             await ctx.send_error(expl)
     await ctx.embed(
         title=title,
         description=
         f"{expl}\n\n[Normal Resolution]({url})  [High Resolution]({hdurl})",
         image=url)
Exemple #2
0
 async def wxhourly(self, ctx: aoi.AoiContext, *,
                    location: gmaps.LocationCoordinates):
     async with ctx.typing():
         conditions = (await self.wx.lookup_hourly(location))
     await ctx.paginate(
         fmt=f"Resolved Address: {location.location or location}```%s```\n",
         lst=[cond.line() for cond in conditions],
         n=24,
         title="Weather lookup",
         thumbnails=[c.icon for c in conditions[3::24]])
Exemple #3
0
 async def landsat(self,
                   ctx: aoi.AoiContext,
                   coords: gmaps.LocationCoordinates,
                   date: dtime() = None):
     lat = coords.lat
     long = coords.long
     dt = date or datetime.datetime.now()
     url = f"https://api.nasa.gov/planetary/earth/imagery?" \
           f"lon={long}&lat={lat}&dim=0.15&api_key={self.bot.nasa}" \
           f"&date={dt.strftime('%Y-%m-%d')}"
     buf = io.BytesIO()
     async with ctx.typing():
         async with aiohttp.ClientSession() as sess:
             async with sess.get(url) as resp:
                 buf.write(await resp.content.read())
     await ctx.embed(title=f"{lat} {long} {dt.strftime('%Y-%m-%d')}",
                     image=buf)
Exemple #4
0
 async def satellite(self, ctx: aoi.AoiContext,
                     location: gmaps.LocationCoordinates):
     res = await self.wx.lookup_grid(location.lat, location.long)
     radar = res.radar_station[-3:]
     if radar in self.sat_cache:
         diff = (datetime.now() - self.sat_cache[radar][0]).seconds
         if diff < 30 * 60:
             img = self.sat_cache[radar][1]
             buf = io.BytesIO()
             img.save(buf, format="png")
             return await ctx.embed(
                 image=buf,
                 footer=f"Cached from {diff // 60}m{diff % 60:2} ago")
         del self.sat_cache[radar]
     urls = [
         f"https://radar.weather.gov/ridge/Overlays/Topo/Short/{radar}_Topo_Short.jpg",
         f"https://radar.weather.gov/ridge/RadarImg/N0R/{radar}_N0R_0.gif",
         f"https://radar.weather.gov/ridge/Overlays/County/Short/{radar}_County_Short.gif",
         f"https://radar.weather.gov/ridge/Overlays/Rivers/Short/{radar}_Rivers_Short.gif",
         f"https://radar.weather.gov/ridge/Overlays/Highways/Short/{radar}_Highways_Short.gif",
         f"https://radar.weather.gov/ridge/Overlays/Cities/Short/{radar}_City_Short.gif",
         f"https://radar.weather.gov/ridge/Warnings/Short/{radar}_Warnings_0.gif",
         f"https://radar.weather.gov/ridge/Legend/N0R/{radar}_N0R_Legend_0.gif"
     ]
     imgs = []
     async with ctx.typing():
         async with aiohttp.ClientSession() as sess:
             for url in urls:
                 async with sess.get(url) as resp:
                     buf = io.BytesIO()
                     buf.write(await resp.content.read())
                     buf.seek(0)
                     imgs.append(Image.open(buf, "png").convert("RGBA"))
     composite = reduce(lambda i1, i2: Image.alpha_composite(i1, i2), imgs)
     self.sat_cache[radar] = (datetime.now(), composite)
     buf = io.BytesIO()
     composite.save(fp=buf, format="png")
     await ctx.embed(image=buf)