def index(): '''This module is what connects the modules and then shows this on the webpapge''' #getting the information from the form on the webpage\ #when the user submits their alarm the alarm alarm = str(request.args.get("alarm")) #the label for the alarm name = request.args.get("two") #whether they want a news update in the alarm or not inclnews = request.args.get("news") #whether they want a weather update in the alarm or not inclweather = request.args.get("weather") #if the user decides to delete an alarm delete = request.args.get("alarm_item") #if the user decides to delete a notification rem_notif = request.args.get("notif") #passing the delete info to the remove function if delete: remove_alarm(delete, True) if rem_notif: remove_notification(rem_notif) #using the covid function to get the latest stats from the api covid = get_covid() #using the news function to get the latest news from the news api articles = news() #using the weather function to get the latest weather from the weather api weather = weathers() #setting a blank variable to be used in the alarm function calls temp = '' #using if statements to decide what to call the alarm with dependin on user inputs if inclnews == 'news' and inclweather == 'weather': pick_news = True pick_weather = True #calling the add alarm function to add the details the user inputted to the json file alarmj = add_alarm(alarm, name, weather, articles, covid, pick_news, pick_weather) elif inclweather: pick_news = False pick_weather = True alarmj = add_alarm(alarm, name, weather, temp, covid, pick_news, pick_weather) elif inclnews: pick_news = True pick_weather = False alarmj = add_alarm(alarm, name, temp, articles, covid, pick_news, pick_weather) else: pick_news = False pick_weather = False alarmj = add_alarm(alarm, name, temp, temp, covid, pick_news, pick_weather) if name is None: #checking and refreshing the alarms to the latest weather, news and covid info alarmj = refresh(covid, articles, weather) #putting all the updates (covid,news,weather) into one dictionary\ #to be passed to the html as notifications organised = org_func(covid, weather, articles) #rendering all this informaion to the html template as a website return render_template('template.html', notifications=organised, alarms=alarmj,\ image='icons8-clock-64.png')
def schedule_event(): '''This is the main function where the alarms and notifications are utilised. In this function, the alarms are not only created but also cancelled, as well as the notifications.''' ttsstring = '' s.run(blocking=False) alarm_time = request.args.get("alarm") alarm_title = request.args.get("two") alarm_news = request.args.get("news") alarm_weather = request.args.get("weather") alarm_name = request.args.get("alarm_item") notif_name = request.args.get("notif") current_time = now.strftime("%H:%M") if alarm_name: try: s.cancel(events[alarm_name]) events.pop(alarm_name, None) except ValueError: print('exception occured') finally: alarms[:] = [x for x in alarms if x.get('title') != alarm_name] alarms[:] = [x for x in alarms if x.get('title') != alarm_name] if notif_name: notifications[:] = [ x for x in notifications if x.get('title') != notif_name ] if alarm_time: alarmsplit = alarm_time.split("T") alarm_time = alarmsplit[1] if alarm_time is not None: contentstring = 'Alarm will go off at ' + str(alarm_time) + '.' ttsstring += str(alarm_title) + ' has gone off at ' + str( alarm_time) + '!' global INCREMENT INCREMENT += 1 if alarm_news is not None: contentstring += 'With a News/COVID briefing.' covidstring = '' newsstring = newsbrief() coviddict = get_covid() covidstring += coviddict['title'] + coviddict['content'] ttsstring += newsstring ttsstring += covidstring if alarm_weather is not None: contentstring += ' With a Weather briefing.' weatherstring = weatherbrief() ttsstring += weatherstring alarms.append({ 'value': INCREMENT, 'title': alarm_title, 'content': contentstring }) if alarm_time: delay = time_conversions.hhmm_to_seconds(alarm_time) -\ time_conversions.hhmm_to_seconds(current_time) events[alarm_title] = s.enter(int(delay), INCREMENT, announcements, [ ttsstring, ]) INCREMENT -= 1 return hello()
def remove_alarm(title, status=False): '''This function opens the json file for the alarms and then it loops through the file until the title is the same and it removes that alarm''' with open('./json_files/alarms.json') as jfile: data = json.load(jfile) #getting the alarms from the json file temp = data['alarms'] #the number of alarms in the file length = len(temp) #looping through each alarm in the list for i in range(length): #then checkign each alarm title agaist the input title, #and if they are the same this will remove it if temp[i]["title"] == title: temp.pop(i) log("alarm for " + title + " has been removed") break #re-wrtiting the updated alarms to the json file with open('./json_files/alarms.json', 'w') as jfile: json.dump(data, jfile, indent=4) length = len(lst) if status: #looping through the scheduler list for i in range(length): try: #canceling the events in the list s.cancel(lst[i]) #logging that this has been done log('alarm ' + title + ' removed from scheduler') except: log('alarm' + title + 'could not be removed from the scheduler') #removing the item from the list lst.clear() if not status: refresh(get_covid(), news(), weathers())
def send_covid(self, **kwargs): chat_id = kwargs['chat_id'] self.bot.send_message(chat_id, covid.get_covid())
def test_get_covid(): assert get_covid() != "error getting covid info, api call error"
def addnotifications(): '''Adds notifications in every hour.''' notifications = get_news() notifications.append(get_weather()) notifications.append(get_covid())
from covid import get_covid now = datetime.now() s = sched.scheduler(time.time, time.sleep) app = Flask(__name__) alarms = [] notifications = [] events = {} INCREMENT = 1 logging.basicConfig( filename='logger.log', level=logging.NOTSET, format='%(asctime)s %(levelname)s %(name)s %(threadName)s : %(message)s') notifications = get_news() notifications.append(get_weather()) notifications.append(get_covid()) def announcements(announcement: str): '''This function facilitates the usage of the pyttsx3 package, which enables the use of text to speech.''' engine = pyttsx3.init() engine.say(announcement) engine.runAndWait() def notifrefresh(): '''Refreshes notifications every hour.''' s.run(blocking=False) s.enter(3600, 1, addnotifications) notifrefresh()