Futura-Py / weather

Coming soon
MIT License
4 stars 1 forks source link

feat: Basic CLI and OpenWeatherMap setup #4

Closed im-coder-lg closed 1 year ago

im-coder-lg commented 1 year ago

Our first order of business, fellas!

P.S. to test, use VS Code. P. P. S. now we can test the API to make sure our request always works.

im-coder-lg commented 1 year ago

P. P. P. S. just can now be used as our scriptrunner!

not-nef commented 1 year ago

isnt os.getenv for getting an environment variable? do you expect all devs to get an api token?

sumeshir26 commented 1 year ago

@im-coder-lg OpenWeatherMap's API is reallay liberal so I feel there is no reason not to keep a common key. You are allowed one request/second which is pretty good IMO for now

im-coder-lg commented 1 year ago

I am SCARED of leaking that into open-source space right now. Plus, getting the API key for every individual is always easy, and also - we ought to look into it right now. I felt scared to death seeing the public key out there - right now, having .envs will work. It's just something in my formulated plan, except the old commit.

im-coder-lg commented 1 year ago

do you expect all devs to get an api token?

sorta? Right now, I need to think carefully. 2000 calls ain't gonna work always, right?

Moosems commented 1 year ago

I am SCARED of leaking that into open-source space right now. Plus, getting the API key for every individual is always easy, and also - we ought to look into it right now. I felt scared to death seeing the public key out there - right now, having .envs will work. It's just something in my formulated plan, except the old commit.

It's just a weather api, if its free no harm, right?

im-coder-lg commented 1 year ago

Well, the API is for free use in this world, right? There is a possibility of someone finding this API key and using it for their own work. I can't end up finding my account just got billed, right? It could be just straight-up dangerous for me. Plus, we will have to help them get an API key for themselves, and perhaps fuel them to have ideas for this project/their own project. We can think of new ways for it later - right now, we don't want these hindrances.

I'm just being paranoid, this is my "panic mode".

# edit: signing off
Moosems commented 1 year ago

I've got an idea. We set up a server that has it's own api and gets a forecast for major cities so that the key never has to go public and the app just calls the server for some of the major cities that the user has added and the key never goes public (do less than 1,000 calls per day from the main server and the api is free too and now its just about setting up a free server (I'm supposed to get a raspberry pi sometime soon so maybe I could rig up my old one (raspberry pi 2B) to be the main server...) and then calling that server 😃 )

im-coder-lg commented 1 year ago

Who will have to pay for the domain tho...

sumeshir26 commented 1 year ago

@im-coder-lg If you are scared about your account getting billed, we can use my API key. BTW you don't get billed for going over the API limit(I did that once), the request just fails.

im-coder-lg commented 1 year ago

Huh. Well is there a way to keep it hidden from the users, just as a simple thing?

im-coder-lg commented 1 year ago

A plan is formulating inside my brain, I bet it will be insanely cool.

Moosems commented 1 year ago

The reason I say no source folder is because I can set up the build action easier without one and I think it would be so cool to use this on my menubar

im-coder-lg commented 1 year ago

Hmm @Moosems there IS one issue. The domain - how would we achieve that? Would DuckDNS work?

im-coder-lg commented 1 year ago

The reason I say no source folder is because I can set up the build action easier without one and I think it would be so cool to use this on my menubar

I didn't get you here. What do you mean by a source folder? I thought we could use AppImages and Flatpaks... Right?

Moosems commented 1 year ago

I mean that in all other Futura projects there's a src folder but this would require some special functions to get images and whatnot when working with pyinstaller builds and mac application bundles

sumeshir26 commented 1 year ago

No, just put the .spec file inside /src and make workflows cd src

Moosems commented 1 year ago

Hmm @Moosems there IS one issue. The domain - how would we achieve that? Would DuckDNS work?

A flask web app hosted on a raspberry pi doesn't require a domain

Moosems commented 1 year ago

I just have to ensure it NEVER turns off and has no important data on it (like location)

im-coder-lg commented 1 year ago

No, a domain to access the server!

Moosems commented 1 year ago

-_- I know what you're saying, there's also free servers that provide a domain

im-coder-lg commented 1 year ago

Huh, interesting. I want to know more about them.

im-coder-lg commented 1 year ago

Y'know @Moosems I think I have an idea. So, it goes like this:

We offer users our API key(I'll make a new account just for that, to be safe) and tell them that if it sometimes gets overloaded, they can switch to using their own API key. We can provide that option to them, in which case, they can just add it to the program directory(dotenvs :wink:), enable a checkbox to make sure Python switches to that, and I think we'll be good to go.

im-coder-lg commented 1 year ago

image

This is my idea(Excalidraw, if you need the tool)

im-coder-lg commented 1 year ago

gosh @sumeshir26 didn't see the API key coming! We'll keep it for now till I get my OWM account using my GitHub credentials(I can't phrase it, but it's just my GitHub mail and all that).

im-coder-lg commented 1 year ago

Guys, I need some help.

We know that our JSON query looks like this:

image

Code:

{'coord': {'lon': -87.9037, 'lat': 45.7869}, 'weather': [{'id': 804, 'main': 'Clouds', 'description': 'overcast clouds', 'icon': '04n'}], 'base': 'stations', 'main': {'temp': 259.52, 'feels_like': 259.52, 'temp_min': 257.5, 'temp_max': 260.9, 'pressure': 1022, 'humidity': 81}, 'visibility': 10000, 'wind': {'speed': 0.45, 'deg': 325, 'gust': 2.68}, 'clouds': {'all': 100}, 'dt': 1674995542, 'sys': {'type': 2, 'id': 2001298, 'country': 'US', 'sunrise': 1674998226, 'sunset': 1675032728}, 'timezone': -21600, 'id': 5004016, 'name': 'Norway', 'cod': 200}

How can we get the coord or speed or sunrise data, when they seem to be separate datasets? No using PANDAS.

not-nef commented 1 year ago

data["coord"]?

sumeshir26 commented 1 year ago

For the assistant I am kind of working on, I implemented the weather as follows:

#Weather and Location utilites
import requests
import json
import re

from skills.templates import *

def location_city() -> str:
    try:
        answer = (requests.get("http://ipinfo.io/json")).json()['city']
    except:
        answer = False
    return answer

def weather(command:str) -> list:
    API_KEY = "02a48fc85823393c2c20c321febeffb8"

    try:
        split_command = command.split("in")
        city = split_command[1].strip()
    except:
        city = location_city()

    if type(city) == bool:
        frame = template_big_small("Can't reach the internet!", "Check your internet connection")
        return True, frame, "Can't reach the internet"

    response = requests.get("http://api.openweathermap.org/data/2.5/weather?q=" + city + "&appid=" + API_KEY).json()
    print(response)

    try: 
        if response['cod'] == "404":
            frame = template_small_big("Check your city name and try again", f"I couldn't find a place called {city.capitalize()}.", justify="left", anchor="w")
            return True, frame, f"I couldn't find a place called {city.capitalize()}"
    except:
        pass

    frame = template_small(response)
    text = f"Weather in {city} is {response['weather'][0]['description']}"
    print(text)
    return True, frame, text
im-coder-lg commented 1 year ago

I'm not just using that. Dangit my changes are uncommitted. I did this same project on Colab last year, that's when I thought of this. I not only used the API request, but also another module called PyOWM. Turns out, the API gives weird temperature data, hence I used PyOWM for the temperature, stuck with the API for the rest. I used Pandas to convert the datasets inside a JSON field to summon them(cool trick I learnt) and I had my own primitive WeatherPy(too much debug logs, so primitive, but neatened too). Thought of making an app, so here we are, without Pandas DataFrames(heavy packaging, I guess).

Moosems commented 1 year ago

@im-coder-lg I can just use PythonAnywhere, it works just fine for our use case and makes our lives easier, I've been fiddling around with it for a while and I think it'll work

Moosems commented 1 year ago

data["wind"]["speed"] will get you the speed and data["coord"] will get you coordinates, no need for anything big and fancy

im-coder-lg commented 1 year ago

I'll try and tell you guys.

im-coder-lg commented 1 year ago

Y'know what, @Moosems? I think we will use servers for another project. Don't want to sound rude here, or be rude here, but I like my idea better. Giving people the option to switch to their API sounds good, right? @sumeshir26 I am changing it to my official API, don't want to burden you with all the requests.

Moosems commented 1 year ago

No offense taken, lead the way!

sumeshir26 commented 1 year ago

Sure, go ahead!

im-coder-lg commented 1 year ago

I figured it out, but was a bit lazy to add it. It's simple. @Moosems your idea was a bit right, but the weather part is an array. To access the data, I have to put this:

print(data["weather"][0]["main"])

P. S. The temperature data from the API was in Kelvin, I was stupid. Changes coming soon!

im-coder-lg commented 1 year ago

gottem guys

Moosems commented 1 year ago

And @im-coder-lg this is currently just prototype/POC code right? No production style quite yet

im-coder-lg commented 1 year ago

Guys, this is ready for merge.

Screenshot of how I wanted it to work:

image

To-do list(I'll make it official):

Does this look good? Anything I have to remove/change? I need to know, gotta plan availability.

Moosems commented 1 year ago

Never works when I type in the city :\

im-coder-lg commented 1 year ago

Huh. Can you send a screenshot?

im-coder-lg commented 1 year ago

Maybe you didn't enter the city name properly? It happened when I tried searching for NYC once, y'know. The second try worked though.

Moosems commented 1 year ago

It's the TOKEN as an env variable, mac likes to be picky

im-coder-lg commented 1 year ago

Wait, hold on. I put the TOKEN variable as a globally-declared string! Screenshots, please! I gotta know what went wrong.

Moosems commented 1 year ago

Because you put the TOKEN = os.getenv("TOKEN") after it turns into the env var and I can't set it for some reason on my mac

Moosems commented 1 year ago

can't set the env var*

Moosems commented 1 year ago
Screen Shot 2023-01-31 at 8 31 31 AM

I added a print (TOKEN) to OWMCITY

im-coder-lg commented 1 year ago

Oh, now I see what you did there.

And yeah, even I see it now. Odd. Very odd.

im-coder-lg commented 1 year ago

Lemme debug this and see.

im-coder-lg commented 1 year ago

@Moosems fixed :+1:

I think I accidentally added the .env part, which is damn odd...