def alarm_end(alarm_time: datetime.datetime, alarm: dict, news_call: bool, weather_call: bool) -> None: """ This function runs when an alarm has gone off. The notification and log dictionaries are created including the necessary alarm data. If the user has checked news and weather checkboxes then news and weather information will be played. Covid Data Nwews will be plaayed on every alarm """ # Alarm Go off notification created alarm_notification = ({ 'timestamp': time.strftime('%H:%M:%S'), 'type': 'Alarm', 'title': ' Alarm scheduled for ' + str(alarm_time) + ' has gone off.', 'description': '' }) alarm_log = ({ 'timestamp': time.strftime('%H:%M:%S'), 'type': 'alarm', 'description': 'Alarm scheduled for ' + str(alarm_time) + ' has gone off.', 'error': '' }) new_notification(alarm_notification) info_log(alarm_log) tts("Alarm " + str(alarm_time) + "has gone off") if news_call == True: get_news(True) if weather_call == True: get_weather(True) get_covid_data(True)
def voiceit_verification(groupId): audio_length = 5 try: tts('started verification process', 'startedVerification') os.system('mpg321 ttsfiles/startedVerification') os.system("mpg321 ttsfiles/countdown.mp3") record.record(audio_length, "verify.wav") phrase = file_to_text("verify.wav") identification_response = voiceit.identify(groupId, phrase, 'verify.wav') if identification_response['responseCode'] == 'SUCC': print(identification_response) uid = identification_response['userId'] db_response = model.get_user(uid) if len(db_response) > 1: name = db_response[1] s = "First step completed, {0} was successfully identified. Please wait for the complete verification".format( name) else: s = "First step completed, user was successfully identified. Please wait for the complete verification" tts(s, 'response') os.system('mpg321 ttsfiles/response.mp3') verification_response = voiceit.verify(uid, phrase, 'verify.wav') if verification_response['responseCode'] == 'SUCC': print(verification_responses) confidence = verification_response['confidence'] st = "successfully verified user with confidence: {0}".format( confidence) tts(st, 'response') else: msg = verification_response['message'] if 'grp_' in msg: position = msg.index("grp_") gid = msg[position:position + 36] st = msg.replace(gid, "") else: st = msg tts(st, 'response') os.system('mpg321 ttsfiles/response.mp3') else: msg = identification_response['message'] if 'grp_' in msg: position = msg.index("grp_") gid = msg[position:position + 36] st = msg.replace(gid, "") else: st = msg tts(st, 'response') os.system('mpg321 ttsfiles/response.mp3') except Exception as e: return str(e)
def get_weather(tts_enabled: bool) -> None: """ This function fetches the weather data from openweathermap using api key provided in "config.json" and stores the data in "weather.json" New Notification and info log is created each time new data is fetched """ # location location = 'New Delhi' api_key = get_api_key() url = 'https://api.openweathermap.org/data/2.5/weather?q={} \ &appid={}&units=metric'.format(location, api_key) new_weather = requests.get(url).json() with open('assets/weather.json', 'w') as weather_file: json.dump(new_weather, weather_file, indent=2) weather_notification = ({ 'timestamp': time.strftime('%H:%M:%S'), 'type': 'Weather', 'title': 'Current temperature in ' + new_weather['name'] + ' is ' + str(new_weather['main']['temp']) + "\n Minimum Temperature is " + str(new_weather['main']['temp_min']) + "\n and " + "\n Maximum Temperature is " + str(new_weather['main']['temp_max']), 'description': '' }) weather_log = ({ 'timestamp': time.strftime('%H:%M:%S'), 'type': 'weather', 'description': 'Current Weather in ' + new_weather['name'] + ' has been updated.', 'error': '' }) new_notification(weather_notification) info_log(weather_log) if tts_enabled: try: tts('Current temperature in ' + new_weather['name'] + 'is' + str(new_weather['main']['temp']) + "Minimum Temperature is" + str(new_weather['main']['temp_min']) + "and" + "Maximum Temperature is" + str(new_weather['main']['temp_max'])) except RuntimeError: error_log(RuntimeError) with open('assets/weather.json', 'w') as weather_file: json.dump(new_weather, weather_file, indent=2)
def get_covid_data(tts_enabled: bool): """ This function fetches data from api.coronavirus.data.gov using get method in requests module, creates a dictionary and store it in "covid_notifications.json" file. New Notification and info log is created each time new data is found """ url = "https://api.covid19india.org/v4/data-2020-12-04.json" data = requests.get(url).json() new_covid_data = data['DL'] with open('assets/covid_notifications.json', 'w') as covid_file: json.dump(new_covid_data, covid_file, indent=2) new_covid_notification = ({ 'timestamp': time.strftime('%H:%M:%S'), 'type': 'Covid News', 'title': "Covid News for Delhi" + " with" + " Total confirmed cases " + str(new_covid_data['total']['confirmed']) + " Todays confirmed cases " + str(new_covid_data['delta']['confirmed']) }) covid_logs = ({ 'timestamp': time.strftime('%H:%M:%S'), 'type': 'Covid News', 'description': "New Covid Data for Delhi", 'error': '' }) new_notification(new_covid_notification) info_log(covid_logs) if tts_enabled: try: tts("Covid News for Delhi" + " with " + " Total confirmed cases " + str(new_covid_data['total']['confirmed']) + " Todays confirmed cases " + str(new_covid_data['delta']['confirmed'])) except RuntimeError: error_log(RuntimeError) with open('assets/covid_notifications.json', 'w') as covid_file: json.dump(new_covid_data, covid_file, indent=2)
def get_news(tts_enbled: bool) -> None: """ This function fetches the weather data from newssapi using api key provided in "config.json" and stores the data in "news.json" New Notification and info log is created each time new data is fetched """ api_key = get_api_key() country = 'in' url = 'https://newsapi.org/v2/top-headlines?country={}&apiKey={}' \ .format(country, api_key) new_news = requests.get(url).json() with open('assets/news.json', 'w') as news_file: json.dump(new_news, news_file, indent=2) for i in range(3): news_notification = ({ 'timestamp': time.strftime('%H:%M:%S'), 'type': 'News', 'title': new_news['articles'][i]['title'], 'description': '' }) news_log = ({ 'timestamp': time.strftime('%H:%M:%S'), 'type': 'news', 'description': 'New news articles' + new_news['articles'][i]['title'], 'error': '' }) new_notification(news_notification) info_log(news_log) if (tts_enbled): try: tts('New news story.' + new_news['articles'][i]['title']) except RuntimeError: error_log(RuntimeError) with open('assets/news.json', 'w') as news_file: json.dump(new_news, news_file, indent=2)
def voiceit_enrollment(uid, phrases): iterations = 3 audio_length = 5 tts("please say your name", 'name') os.system("mpg321 ttsfiles/name.mp3") record.record(audio_length, "name.wav") name = file_to_text("name.wav") print(name) model.save_user(name=name, voiceit_id=uid) model.add_user_to_group(uid, groupId=1) voiceit.add_user_to_group("grp_bc595294f94341568dc66dd7a09790bc", uid) print("user id: ", uid) print("supported phrases: \n", pretty_list(phrases)) count = 1 while count <= iterations: #speak("Please say the selected phrase, starting recording in 3 ... 2 ... 1. ... Go. ") os.system("mpg321 ttsfiles/countdown.mp3") record.record(audio_length, "test.wav") phrase = file_to_text("test.wav") res = voiceit.enroll_user(uid, phrase, "test.wav") print("response message: ", res['message']) print(phrase) print("") if res['responseCode'] == 'SUCC': st = "attempt {0} of enrollment successful".format(count) tts(st, 'response') count += 1 else: msg = res['message'] if 'grp_' in msg: position = msg.index("grp_") gid = msg[position:position + 36] st = msg.replace(gid, "") else: st = msg tts(st, 'response') os.system("mpg321 ttsfiles/response.mp3") os.system("mpg321 ttsfiles/EnrollmentSuccess.mp3")
phrases = reg['voiceitPhrases'] print("user id: ", uid) print("supported phrases: \n", pretty_list(phrases)) count = 1 while count <= iterations: os.system("mpg321 ttsfiles/countdown.mp3") record.record(5, "test.wav") phrase = file_to_text("test.wav") res = voiceit.enroll_user(uid, phrase, "test.wav") print("response message: ",res['message']) print(phrase) print("") if res['responseCode'] == 'SUCC': tts(res['message'], 'response', count) count += 1 os.system("mpg321 ttsfiles/EnrollmentSuccess.mp3") elif value == 'verify': print("") record.record(5,'identify.wav') res = voiceit.verify("usr_0d2f73a90e134498890109f9241624a1", file_to_text("identify.wav"), "identify.wav") print(res) print("") elif value == 'stop': sys.exit() except sr.UnknownValueError: print("Oops! Didn't catch that") except sr.RequestError as e:
#print("start by saying: register") while True: with m as source: audio = r.listen(source) try: # recognize speech using Google Speech Recognition print("say something") value = r.recognize_google(audio) print(value) if (value == "register"): #response = handler.create_profile() os.system('mpg321 ttsfiles/record.mp3') reg = start_voiceit_registration() uid = reg['voiceitUserId'] tts("please say your name", 'name') os.system('mpg321 ttsfiles/name.mp3') record.record(5, "name.wav") name = file_to_text("name.wav") print(name) model.save_user(name=name, voiceit_id=uid) model.add_user_to_group(uid) voiceit.add_user_to_group( "grp_bc595294f94341568dc66dd7a09790bc", uid) phrases = reg['voiceitPhrases'] print("user id: ", uid) print("supported phrases: \n", pretty_list(phrases)) count = 1 while count <= iterations: os.system("mpg321 ttsfiles/countdown.mp3") record.record(5, "test.wav")
def screenshot_comment(comments: list, directory_name: str): """The browser takes a screenshot of the comment Args: link (str): The link to the post's comment directoryname (str): The directory to save the screenshot in filename (str): The name of the file to save as id (str): The id of the comment to move to on the page Returns: None """ browser = headless() title = comments.pop(0) post, post_link = title.split("|||") post_title, post_author = post.split(" submitted by ") tts(post_title, directory_name, ".title") screenshot_title(browser, post_link, directory_name, ".title", None) file = open("{}/.description.txt".format(directory_name), "w") file.write(post_title + "\n" + post_link) file.close() # youtube description file = open("{}/.ytdescription.txt".format(directory_name), "w") file.write("{}\n\nPosted by u/{}".format(post_title, post_author)) file.close() f = open("{}/.comments.txt".format(directory_name), "a") iteration = 0 for comment in tqdm(comments): if iteration == 20: browser.close() browser = headless() comment_number, comment_text, comment_link, comment_id = comment.split("|||") id = "t1_{}".format(comment_id) file_name = "comment{}".format(comment_number) browser.get(comment_link) nsfw_check(browser) try: browser.find_element_by_css_selector('.PiO8QDmoJoOL2sDjJAk4C.j9NixHqtN2j8SKHcdJ0om').click() except NoSuchElementException: pass if id: try: element = browser.find_element_by_id(id) except NoSuchElementException: time.sleep(10) element = browser.find_element_by_id(id) browser.execute_script("arguments[0].scrollIntoView(alignToTop=false);", element) browser.save_screenshot("{}/pictures/{}.png".format(directory_name, file_name)) tts(comment_text, directory_name, file_name) f.write("{}|||{}\n".format(comment_number, comment_link)) iteration = iteration + 1 f.close() browser.quit()