I am very new to using API's and JSON and so far I have been able to request data from an API. I have made a command that, after you input the command and city, will give you weather information for that city. So far I can't get specific data from the data I receive from the API and it makes a list that looks like this after using print(response.content).
b'{"coord":{"lon":-0.1257,"lat":51.5085},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"base":"stations","main":{"temp":48.18,"feels_like":40.32,"temp_min":46.99,"temp_max":50,"pressure":1012,"humidity":76},"visibility":10000,"wind":{"speed":10.36,"deg":200},"clouds":{"all":100},"dt":1613450531,"sys":{"type":1,"id":1414,"country":"GB","sunrise":1613459549,"sunset":1613495816},"timezone":0,"id":2643743,"name":"London","cod":200}'
I want to make it output the "feels_like" temperature.
import discord
import os
import requests
import json
from discord.ext import commands
from keep_alive import keep_alive
client = discord.Client()
client = commands.Bot(command_prefix="%")
@client.command()
async def weather(ctx, city):
#request
response = requests.get("http://api.openweathermap.org/data/2.5/weather?q=" + city + "&units=imperial&appid=secretkey")
#failed attempts
#json_data = json.loads(response.text)
#weather_data = json_data[0]['main.feels_like']
#status check + output
if (response.status_code == 200):
print("Request successful for " + city + "!")
await ctx.send("Feels like")
#await ctx.send(weather_data)
await ctx.send(response.content)
else:
print("Something went wrong retrieving data from weather api for " + city + ".")
await ctx.send("Couldn't find "+city)
#output
print (response.content)
keep_alive()
client.run(os.getenv('daKey'))