|
| 1 | +import requests |
| 2 | +from smtplib import SMTP |
| 3 | +import os |
| 4 | +from dotenv import load_dotenv |
| 5 | + |
| 6 | +load_dotenv() |
| 7 | + |
| 8 | +def function(): |
| 9 | + """Go to Manage your Google(or any mail) account, and then headover to Security.\nTurn OFF the options 'Two Step Verification' and 'Use your phone to sign in' in the Signing in to Google section.\nTurn ON the Less secure apps section. |
| 10 | + """ |
| 11 | + return 0 |
| 12 | +print(function.__doc__) |
| 13 | +MY_MAIL= os.getenv('MAIL') |
| 14 | +MY_PASSWORD= os.getenv('PASSWORD') |
| 15 | +RECIEVER_MAIL = input('Send mail to (mail id): ') |
| 16 | +CITY = input('Enter your City: ') |
| 17 | + |
| 18 | +API_KEY = os.getenv('API') |
| 19 | + |
| 20 | +API_END_POINT ='https://nominatim.openstreetmap.org/search.php' |
| 21 | +PARAMETER_LOCATION ={ |
| 22 | + 'city' : CITY, |
| 23 | + 'format': 'json', |
| 24 | +} |
| 25 | +response_location = requests.get(url = API_END_POINT, params=PARAMETER_LOCATION) |
| 26 | +data_location = response_location .json() |
| 27 | +LAT = data_location [0]['lat'] |
| 28 | +LONG = data_location [0]['lon'] |
| 29 | +PARAMETER= { |
| 30 | + "lat": LAT, |
| 31 | + "lon": LONG, |
| 32 | + "appid" : API_KEY, |
| 33 | + "exclude" : "current,minutely,daily", |
| 34 | +} |
| 35 | +api = requests.get(url="http://api.openweathermap.org/data/2.5/onecall",params=PARAMETER) |
| 36 | +data = api.json() |
| 37 | + |
| 38 | +bring_umbrella = False |
| 39 | + |
| 40 | +for i in range(0,12): |
| 41 | + hourly_condition = data['hourly'][i]['weather'][0]['id'] |
| 42 | + if(hourly_condition<700): |
| 43 | + bring_umbrella = True |
| 44 | + |
| 45 | +if (bring_umbrella == True): |
| 46 | + MESSAGE=f"Subject: Rain Rain \n\nIt's going to rain today. Bring Umbrella " |
| 47 | + |
| 48 | +else: |
| 49 | + MESSAGE = f"Subject: Sunny Day\n\nMay be a sunny day. Carry sunglasses. " |
| 50 | + |
| 51 | +with SMTP("smtp.gmail.com") as connection: |
| 52 | + connection.starttls() |
| 53 | + connection.login(user=MY_MAIL, password=MY_PASSWORD) |
| 54 | + connection.sendmail(from_addr=MY_MAIL, to_addrs=RECIEVER_MAIL, |
| 55 | + msg=MESSAGE) |
| 56 | + print('Mail Sent') |
0 commit comments