Untitled
unknown
plain_text
2 years ago
4.6 kB
4
Indexable
import discord import asyncio from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.options import Options import base64 import time import requests from datetime import datetime import re intents = discord.Intents.all() bot = discord.Bot(intents=intents) def capture_map_screenshot(url): # Define Chrome options with the binary location and headless mode. chrome_options = Options() chrome_options.binary_location = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome' chrome_options.add_argument('--headless') # Enable headless mode. # Initialize the Chrome WebDriver with the defined options. driver = webdriver.Chrome(options=chrome_options) try: driver.get(url) # Wait for a few seconds to ensure the page is fully loaded. #time.sleep(1) # Scroll down to the canvas element to ensure it's in the viewport. canvas_element = driver.find_element(By.XPATH, '//*[@id="map_canvas2"]') driver.execute_script("arguments[0].scrollIntoView();", canvas_element) # Wait for a moment to ensure the canvas is fully rendered. time.sleep(2) # Get the content of the canvas using JavaScript. canvas_content = driver.execute_script( "return document.querySelector('#map_canvas2').toDataURL('image/png');" ) # Save the canvas content as an image. with open('map_screenshot.png', 'wb') as f: f.write(base64.b64decode(canvas_content.split(',')[1])) except Exception as e: print(f"An error occurred: {e}") finally: driver.quit() def get_iss_position(): url = "http://api.open-notify.org/iss-now.json" response = requests.get(url) if response.status_code == 200: data = response.json() timestamp = data["timestamp"] latitude = data["iss_position"]["latitude"] longitude = data["iss_position"]["longitude"] # Convert timestamp to human-readable date and time timestamp_dt = datetime.fromtimestamp(timestamp) short_date_time = timestamp_dt.strftime(f"<t:{timestamp}:f>") relative_time = timestamp_dt.strftime(f"<t:{timestamp}:R>") return f"{latitude}, {longitude} ({short_date_time} / {relative_time})" else: return "Failed to retrieve ISS position." def get_latitude_and_longitude(): result = get_iss_position() pattern = r"(-?\d+\.\d+),\s*(-?\d+\.\d+)" matches = re.search(pattern, result) if matches: latitude = float(matches.group(1)) longitude = float(matches.group(2)) return latitude, longitude else: return None, None def get_country_and_big_city(latitude, longitude, api_key): url = f"https://api.opencagedata.com/geocode/v1/json" params = { "q": f"{latitude},{longitude}", "key": api_key } response = requests.get(url, params=params) if response.status_code == 200: data = response.json() results = data.get("results", []) if results: # Extract the country and big city from the components components = results[0]["components"] country = components.get("country") city = components.get("city") or components.get("town") or components.get("village") or components.get("county") return country, city else: return "Location not found.", None else: return "Failed to retrieve location data.", None @bot.listen() async def on_ready(): print("Bot is ready.") @bot.command(description="See the current ISS location.") async def get_iss(ctx: discord.ApplicationContext): embed = discord.Embed( title="Processing your request...") await ctx.respond(embed=embed) url = "https://astroviewer.net/iss/widgets/orbit-widget.php" capture_map_screenshot(url) # Call the function here to capture the map map_file = "map_screenshot.png" # Use the file name generated by capture_map_screenshot function if map_file: with open(map_file, 'rb') as file: screenshot = discord.File(file, filename=map_file) embed = discord.Embed(title="Here's the current position of the ISS!") embed.description = f"Currently closest to: {big_city, country}. {result}" embed.set_image(url=f"attachment://{screenshot.filename}") await ctx.send(embed=embed, file=screenshot) else: await ctx.send("An error occurred while taking the ISS location.") bot.run("")
Editor is loading...