· 6 years ago · Nov 19, 2019, 03:58 AM
1#def converter(input):
2import pyttsx3
3from flask import Flask, render_template, request
4import time
5import sched
6import requests
7import json
8import logging
9
10
11alarmlist = []
12notifications = []
13
14app = Flask(__name__)
15
16s = sched.scheduler(time.time, time.sleep)
17
18@app.route('/')
19def input():
20
21 """
22 This function returns a form to a web server that
23 gives the user the ability to enter an input
24 """
25
26 return '<form action="/setalarm" method="get"> \
27 <input type="datetime-local" name="alarm"> \
28 <input name="two"> \
29 <input type="submit"></form>\
30 <form action="/cancel" method="get"> \
31 <input name="cancelalarm"> \
32 <input type="submit" value="cancel"></form>\
33 <form action="/weather"> \
34 <input name="city"> \
35 <input type="submit" value="Weather"></form>\
36 <form action="/news" method="get"> \
37 <input type="submit" value="News"></form>\
38 ' + str(alarmlist)
39 #html code allows user to input time and type of
40 #alarm, the alarm they would like to cancel
41 #and the city they would like to find the
42 #weather of as well as providing a way to
43 #see the news
44
45
46@app.route('/setalarm')
47def setalarm():
48
49 """
50 Function calls startalarm(imputtime)
51 and returns input() to create a loop
52 """
53
54 #requests the alarm input from the
55 #web server
56 alarm = request.args.get('alarm')
57 #calls startalarm
58 startalarm(alarm)
59 return input()
60
61
62
63
64@app.route('/startalarm')
65def startalarm(inputtime):
66
67 """
68 Function takes the argument inputtime
69 This function takes the inputtime given
70 by rhe user and converts it to seconds
71 so the function can set an alarm
72 """
73
74 #changes time to correct format
75 stripttime = time.strptime(inputtime, '%Y-%m-%dT%H:%M')
76 #get current time
77 currenttime = time.time()
78 #turns the inputtime to seconds
79 settime = time.mktime(stripttime)
80 secondsdifference = settime - currenttime
81 alarmname = request.args.get('two')
82 #creates the alarm
83 alarm = s.enter(secondsdifference,1,announcement,(alarmname,))
84 alarmlist.append(alarm)
85 print(alarmlist)
86 #runs scheduler
87 s.run()
88
89
90
91@app.route('/weather')
92def weather():
93
94 """
95 Funtion returns the weather information for the
96 city inputted
97 """
98
99 with open('api-key.json', 'r') as f:
100 json_file = json.load(f)
101 keys = json_file["API-keys"]
102 weatherkey = (keys["weather"])
103 #gets the api key for the weather site
104 base_url = "http://api.openweathermap.org/data/2.5/weather?"
105 api_key = weatherkey
106 city_name = request.args.get('city')
107 #creates the complete url
108 complete_url = base_url + "appid=" + api_key + "&q=" + city_name
109 response = requests.get(complete_url)
110 x = response.json()
111
112
113
114 if x["cod"] != "404":
115 y = x["main"]
116 #sorts the responce to
117 #necessary information
118 current_temperature = y["temp"]
119 current_pressure = y["pressure"]
120 current_humidiy = y["humidity"]
121 z = x["weather"]
122 weather_description = z[0]["description"]
123 # prints following values
124 weatherinfo = (" Temperature (in kelvin unit) = " +
125 str(current_temperature) +
126 "\n atmospheric pressure (in hPa unit) = " +
127 str(current_pressure) +
128 "\n humidity (in percentage) = " + str(current_humidiy) +
129 "\n description = " + str(weather_description))
130
131 return weatherinfo
132
133
134
135@app.route('/news')
136def news():
137
138 """
139 Function to return sorted news headlines
140 and description
141 """
142
143 with open('api-key.json', 'r') as f:
144 json_file = json.load(f)
145 keys = json_file["API-keys"]
146 news = (keys["news"])
147 #requests api key for news website
148 url = ('https://newsapi.org/v2/top-headlines?'
149 'sources=bbc-news&'
150 'apiKey=' + news)
151 #creates news website url
152 response = requests.get(url)
153 x = response.json()
154 sortednews = ""
155 numberofarticles = 0
156 y = x["articles"]
157 #for loop to produce multiple articles
158 for x in y:
159 title = y[numberofarticles]["title"]
160 description = y[numberofarticles]["description"]
161 news = title + ". " +description
162 numberofarticles = numberofarticles + 1
163 sortednews = str(sortednews) + str(news) + "\n"
164 logging.info('weather called')
165 return sortednews
166
167
168@app.route('/cancel')
169def cancel():
170
171 """
172 Funtion to cancel an alarm
173 """
174
175 alarmstats = request.args.get('cancelalarm')
176 print(alarmstats)
177 #for loop loops throught alarmlist
178 #when alarm that matches the alarm
179 #name input by user the alarm is
180 #canceled
181 for x in alarmlist:
182 if x == alarmstats:
183 s.cancel(x)
184 return input()
185
186@app.route('/tts_request')
187def tts_request(announcement):
188
189 """
190 function takes the argument announcement
191 This function using text to speech to
192 announce an alarm
193 """
194 engine = pyttsx3.init()
195 #asks engine to announce alarm
196 engine.say(announcement)
197 engine.runAndWait()
198
199
200
201@app.route('/announcement')
202def announcement(alarmannouncement):
203
204 """
205 Function takes the argument alarmannouncement
206 This function calls tts_request and print
207 the announcement
208 """
209
210 #records notifications
211 notifications.append(alarmannouncement)
212 tts_request(alarmannouncement)
213 print(alarmannouncement)
214 return str(alarmannouncement)
215
216
217
218if __name__ == '__main__':
219 app.run()
220
221
222
223logging.basicConfig(filename='clock.txt',level=logging.DEBUG)
224#logs the events