Skip to content

Commit cd54cb2

Browse files
committedFeb 8, 2021
Update Day-33
1 parent 3b8db69 commit cd54cb2

6 files changed

+209
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "substantial-wiring",
6+
"metadata": {},
7+
"source": [
8+
"# Day 33"
9+
]
10+
},
11+
{
12+
"cell_type": "markdown",
13+
"id": "bearing-jewel",
14+
"metadata": {},
15+
"source": [
16+
"###### API Endpoints & API Parameters"
17+
]
18+
},
19+
{
20+
"cell_type": "markdown",
21+
"id": "amazing-blond",
22+
"metadata": {},
23+
"source": [
24+
"- An *Application Programming Interface* is a set of commands, functions, protocols and objects that programmers can use to create software or interact with an external system.\n",
25+
"- API Endpoints is the location where data is stored. Its just a URL.\n",
26+
"- API Request involves sending actual request, where your id, query gets authorized.\n",
27+
"- API Parameters allows to give input on request, to get different outcome."
28+
]
29+
},
30+
{
31+
"cell_type": "markdown",
32+
"id": "sharp-single",
33+
"metadata": {},
34+
"source": [
35+
"###### HTTP Status Codes"
36+
]
37+
},
38+
{
39+
"cell_type": "markdown",
40+
"id": "tight-brooks",
41+
"metadata": {},
42+
"source": [
43+
"- 1XX = Hold on something's happening\n",
44+
"- 2XX = Here you go, will get data as expected\n",
45+
"- 3XX = GO away, permission denied\n",
46+
"- 4XX = You screwed up, what you're trying to look doesn't exists\n",
47+
"- 5XX = Server screwed up, maybe server/website is down"
48+
]
49+
},
50+
{
51+
"cell_type": "markdown",
52+
"id": "continuous-graph",
53+
"metadata": {},
54+
"source": [
55+
"```python\n",
56+
"import requests\n",
57+
"response = requests.get(url='http://api.open-notify.org/iss-now.json')\n",
58+
"print(response.status_code)\n",
59+
"response.raise_for_status() # Raises error if not successful\n",
60+
"data = response.json()\n",
61+
"```"
62+
]
63+
},
64+
{
65+
"cell_type": "markdown",
66+
"id": "selected-church",
67+
"metadata": {},
68+
"source": [
69+
"---"
70+
]
71+
}
72+
],
73+
"metadata": {
74+
"kernelspec": {
75+
"display_name": "Python 3",
76+
"language": "python",
77+
"name": "python3"
78+
},
79+
"language_info": {
80+
"codemirror_mode": {
81+
"name": "ipython",
82+
"version": 3
83+
},
84+
"file_extension": ".py",
85+
"mimetype": "text/x-python",
86+
"name": "python",
87+
"nbconvert_exporter": "python",
88+
"pygments_lexer": "ipython3",
89+
"version": "3.9.1"
90+
}
91+
},
92+
"nbformat": 4,
93+
"nbformat_minor": 5
94+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from tkinter import *
2+
import requests
3+
import json
4+
5+
def get_quote():
6+
response = requests.get('https://api.kanye.rest/')
7+
data = response.json()
8+
canvas.itemconfig(quote_text, text=data['quote'])
9+
10+
window = Tk()
11+
window.title("Kanye Says...")
12+
window.config(padx=50, pady=50)
13+
14+
canvas = Canvas(width=300, height=414)
15+
background_img = PhotoImage(file="background.png")
16+
canvas.create_image(150, 207, image=background_img)
17+
quote_text = canvas.create_text(150, 207, text="Kanye Quote Goes HERE", width=250, font=("Arial", 30, "bold"), fill="white")
18+
canvas.grid(row=0, column=0)
19+
20+
kanye_img = PhotoImage(file="kanye.png")
21+
kanye_button = Button(image=kanye_img, highlightthickness=0, command=get_quote)
22+
kanye_button.grid(row=1, column=0)
23+
24+
window.mainloop()

‎Day-33-ISSNotifier/background.png

22.8 KB
Loading

‎Day-33-ISSNotifier/kanye.png

22.7 KB
Loading

‎Day-33-ISSNotifier/main.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import requests
2+
import json
3+
import datetime as dt
4+
import smtplib
5+
import time
6+
7+
my_email = 'notmyemail@yahoo.com'
8+
my_password = 'notmypassword'
9+
10+
MY_LAT = 19.049370
11+
MY_LONG = 73.020462
12+
13+
def convTimezone(time):
14+
hour = int(time.split('T')[1].split(':')[0])
15+
new_hr = (hour + 5)%24
16+
return (f'{new_hr:02}')
17+
18+
def isNight():
19+
parameters = {
20+
'lat':MY_LAT,
21+
'lng':MY_LONG,
22+
'formatted':0
23+
}
24+
response = requests.get('https://api.sunrise-sunset.org/json', params=parameters)
25+
response.raise_for_status()
26+
data = response.json()
27+
sunrise = int(convTimezone(data['results']['sunrise']))
28+
sunset = int(convTimezone(data['results']['sunset']))
29+
current_hr = dt.datetime.now().hour
30+
return sunrise<=current_hr<=sunset
31+
32+
33+
def is_iss_overhead():
34+
response = requests.get(url='http://api.open-notify.org/iss-now.json')
35+
response.raise_for_status()
36+
data = response.json()
37+
iss_position = (
38+
float(data['iss_position']['latitude']),
39+
float(data['iss_position']['longitude'])
40+
)
41+
return MY_LAT-6<iss_position[0]<MY_LAT+6 and MY_LONG-6<iss_position[1]<MY_LONG+6:
42+
43+
def send_letter():
44+
with smtplib.SMTP(host="smtp.gmail.com", port=587) as connection:
45+
connection.starttls()
46+
connection.login(my_email, my_password)
47+
connection.sendmail(
48+
from_addr=my_email,
49+
to_addrs=my_email,
50+
msg=f"Subject:ISS Detected Overhead\n\nLook up in the sky!!"
51+
)
52+
53+
while True:
54+
if is_iss_overhead() and isNight():
55+
send_letter()
56+
time.sleep(60)

‎Day-33-ISSNotifier/sunrise-sunset.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import requests
2+
import json
3+
import datetime as dt
4+
5+
LATITUDE = 19.049370
6+
LONGITUDE = 73.020462
7+
def convTimezone(time):
8+
hour = int(time.split('T')[1].split(':')[0])
9+
minute = int(time.split('T')[1].split(':')[1])
10+
new_hr = hour + 5
11+
new_min = minute + 30
12+
if new_min>=60:
13+
new_min = new_min%60
14+
new_hr += 1
15+
new_hr = new_hr%24
16+
return (f'{new_hr:02}:{new_min:02}')
17+
18+
# response = requests.get(f'https://api.sunrise-sunset.org/json?lat={LATITUDE}&lng={LONGITUDE}')
19+
# Alternate method
20+
parameters = {
21+
'lat':LATITUDE,
22+
'lng':LONGITUDE,
23+
'formatted':0
24+
}
25+
response = requests.get('https://api.sunrise-sunset.org/json', params=parameters)
26+
response.raise_for_status()
27+
data = response.json()
28+
29+
sunrise = convTimezone(data['results']['sunrise'])
30+
sunset = convTimezone(data['results']['sunset'])
31+
32+
33+
print(sunrise)
34+
print(f'{dt.datetime.now().hour}:{dt.datetime.now().minute}')
35+
print(sunset)

0 commit comments

Comments
 (0)
Please sign in to comment.