Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #def converter(input):
- import pyttsx3
- from flask import Flask, render_template, request
- import time
- import sched
- import requests
- import json
- import logging
- alarmlist = []
- notifications = []
- app = Flask(__name__)
- s = sched.scheduler(time.time, time.sleep)
- @app.route('/')
- def input():
- """
- This function returns a form to a web server that
- gives the user the ability to enter an input
- """
- return '<form action="/setalarm" method="get"> \
- <input type="datetime-local" name="alarm"> \
- <input name="two"> \
- <input type="submit"></form>\
- <form action="/cancel" method="get"> \
- <input name="cancelalarm"> \
- <input type="submit" value="cancel"></form>\
- <form action="/weather"> \
- <input name="city"> \
- <input type="submit" value="Weather"></form>\
- <form action="/news" method="get"> \
- <input type="submit" value="News"></form>\
- ' + str(alarmlist)
- #html code allows user to input time and type of
- #alarm, the alarm they would like to cancel
- #and the city they would like to find the
- #weather of as well as providing a way to
- #see the news
- @app.route('/setalarm')
- def setalarm():
- """
- Function calls startalarm(imputtime)
- and returns input() to create a loop
- """
- #requests the alarm input from the
- #web server
- alarm = request.args.get('alarm')
- #calls startalarm
- startalarm(alarm)
- return input()
- @app.route('/startalarm')
- def startalarm(inputtime):
- """
- Function takes the argument inputtime
- This function takes the inputtime given
- by rhe user and converts it to seconds
- so the function can set an alarm
- """
- #changes time to correct format
- stripttime = time.strptime(inputtime, '%Y-%m-%dT%H:%M')
- #get current time
- currenttime = time.time()
- #turns the inputtime to seconds
- settime = time.mktime(stripttime)
- secondsdifference = settime - currenttime
- alarmname = request.args.get('two')
- #creates the alarm
- alarm = s.enter(secondsdifference,1,announcement,(alarmname,))
- alarmlist.append(alarm)
- print(alarmlist)
- #runs scheduler
- s.run()
- @app.route('/weather')
- def weather():
- """
- Funtion returns the weather information for the
- city inputted
- """
- with open('api-key.json', 'r') as f:
- json_file = json.load(f)
- keys = json_file["API-keys"]
- weatherkey = (keys["weather"])
- #gets the api key for the weather site
- base_url = "http://api.openweathermap.org/data/2.5/weather?"
- api_key = weatherkey
- city_name = request.args.get('city')
- #creates the complete url
- complete_url = base_url + "appid=" + api_key + "&q=" + city_name
- response = requests.get(complete_url)
- x = response.json()
- if x["cod"] != "404":
- y = x["main"]
- #sorts the responce to
- #necessary information
- current_temperature = y["temp"]
- current_pressure = y["pressure"]
- current_humidiy = y["humidity"]
- z = x["weather"]
- weather_description = z[0]["description"]
- # prints following values
- weatherinfo = (" Temperature (in kelvin unit) = " +
- str(current_temperature) +
- "\n atmospheric pressure (in hPa unit) = " +
- str(current_pressure) +
- "\n humidity (in percentage) = " + str(current_humidiy) +
- "\n description = " + str(weather_description))
- return weatherinfo
- @app.route('/news')
- def news():
- """
- Function to return sorted news headlines
- and description
- """
- with open('api-key.json', 'r') as f:
- json_file = json.load(f)
- keys = json_file["API-keys"]
- news = (keys["news"])
- #requests api key for news website
- url = ('https://newsapi.org/v2/top-headlines?'
- 'sources=bbc-news&'
- 'apiKey=' + news)
- #creates news website url
- response = requests.get(url)
- x = response.json()
- sortednews = ""
- numberofarticles = 0
- y = x["articles"]
- #for loop to produce multiple articles
- for x in y:
- title = y[numberofarticles]["title"]
- description = y[numberofarticles]["description"]
- news = title + ". " +description
- numberofarticles = numberofarticles + 1
- sortednews = str(sortednews) + str(news) + "\n"
- logging.info('weather called')
- return sortednews
- @app.route('/cancel')
- def cancel():
- """
- Funtion to cancel an alarm
- """
- alarmstats = request.args.get('cancelalarm')
- print(alarmstats)
- #for loop loops throught alarmlist
- #when alarm that matches the alarm
- #name input by user the alarm is
- #canceled
- for x in alarmlist:
- if x == alarmstats:
- s.cancel(x)
- return input()
- @app.route('/tts_request')
- def tts_request(announcement):
- """
- function takes the argument announcement
- This function using text to speech to
- announce an alarm
- """
- engine = pyttsx3.init()
- #asks engine to announce alarm
- engine.say(announcement)
- engine.runAndWait()
- @app.route('/announcement')
- def announcement(alarmannouncement):
- """
- Function takes the argument alarmannouncement
- This function calls tts_request and print
- the announcement
- """
- #records notifications
- notifications.append(alarmannouncement)
- tts_request(alarmannouncement)
- print(alarmannouncement)
- return str(alarmannouncement)
- if __name__ == '__main__':
- app.run()
- logging.basicConfig(filename='clock.txt',level=logging.DEBUG)
- #logs the events
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement