def recognize_profanity(img, filename): # print('[info] initiating text recognition') # start = time() # initialize an array to store the flagged sentences flags = [] # get array of sentences from the tesseract OCR texts = recognize(img) # for each sentence in the array for text in texts: # check the array for its profanity level s, p_cat = sentence_check(str(text)) # if the sentence is classified as profane (p_cat[0] == 1) if p_cat[0] == 1: # append the text to the array of flags flags.append(s) # if there the array of flagged text is not empty if len(flags) > 0: # print('email sent') # send an email to send_email('*****@*****.**', flags, filename)
def send_confirmation_email(): """Send confirmation emails to all the To Process folks. Should look something like: 1. get all records which haven't gotten emails or errors 2. iterate through the records 2a. send the emails 2a1. try: sending the email 2a2. except: error sending the email 2a3. mark the error that was found 2b. mark the records as processed """ print("Scheduler is alive!") experts = airtable.get_all(view='To process') # TODO: remove this when you're satisfied print(experts) for record in experts: email = record['fields']['Email'] name = record['fields']['Name'] try: msg = build_confirmation_msg(name) send_email(email, msg) # verify this works to mark the record as processed: fields = {'Processed': True} airtable.update(record['id'], fields) except Exception as e: print(f"Error sending email: {str(e)}") fields = {'Error': str(e)} airtable.update(record['id'], fields)
def downloadUsingPDFKit(link, path): """ There was no reason to use selenium to download pdfs, instead I found this library called pdfkit pdfkit will convert HTML pages to PDFs, some webpages they have HTML files instead of PDFs, so pdfkit fixes the problem of converting those HTML pages to PDFs and downloading them pdfkit depends on wkhtmltopdf to install pdfkit: run "pip install pdfkit" to install wkhtmltopdf: go to "https://wkhtmltopdf.org/downloads.html" """ try: if platform == "win32" or platform == "cygwin": # IMPORTANT: this path might vary across different windows machines # make sure it matches to the path where tesseract is located config = pdfkit.configuration( wkhtmltopdf= r'C:\Program Files (x86)\wkhtmltopdf\bin\wkhtmltopdf.exe') pdfkit.from_url(link, path, configuration=config) else: pdfkit.from_url(link, path) except Exception as e: print("pdfkit was not able download,", link) print(e) send_email("failed to download " + link + "\nexception text: " + str(e))
def send_status_notification(dev_obj, message_type): if isinstance(dev_obj, Sensor): message = Message.query.filter_by(name=message_type, mess_type='sensor').first().message elif isinstance(dev_obj, Valve) or isinstance(dev_obj, Check): message = Message.query.filter_by(name=message_type, mess_type='valve').first().message else: raise Exception("Wrong Notification Object Type") formatted_message = format_message(message, dev_obj) send_email(formatted_message)
def update_battery_temp(socket_io): battery_notified = False cpu_temp_notified = False battery_counter = 0 min_battery_val = current_app.config.get("SYSTEM_BATTERY_MIN_VOLTAGE") cpu_max_temp = current_app.config.get("SYSTEM_CPU_MAX_TEMPERATURE") while True: with serial.Serial(current_app.config.get("MAIN_SYSTEM_DEVICE_PORT"), 9600, timeout=3) as ser: data = ser.read(8) data = data.decode() if data: message = data.split(',') battery = int(message[0]) / 10 temperature = int(message[1]) / 10 cpu = CPUTemperature() cpu_temperature = round(cpu.temperature * 1.8 + 32, 2) # Convert to Deg F if battery < min_battery_val: battery_counter += 1 if battery_counter >= 5 and not battery_notified: send_email( current_app.config.get("SYSTEM_BATTERY_MESSAGE")) battery_notified = True battery_counter = 0 else: battery_counter = 0 if battery + 1.5 > min_battery_val: battery_notified = False if cpu_temperature > cpu_max_temp: if not cpu_temp_notified: send_email( current_app.config.get( "SYSTEM_CPU_TEMPERATURE_MESSAGE")) cpu_temp_notified = True else: if cpu_temperature < cpu_max_temp - 5: cpu_temp_notified = False socket_io.emit('batteryTemp', { 'battery': battery, 'temperature': temperature, 'cpu_temperature': cpu_temperature }, namespace='/notification')
def event(request): """ List all Events, or create a new Event. """ if request.method == 'GET': e = Event.objects.all() serializer = EventSerializer(e, many=True) return Response(serializer.data) elif request.method == 'POST': try: event_name = request.POST['event'] camera = request.POST['camera'] contact = get_contact(camera) event_print = requests.get(url=event_print_url, params={'cam_id': camera}) frame64 = event_print.text e = Event.objects.create(event=event_name, camera=camera, contact=contact) e.result = send_email(contact, event_name, e.date, camera, frame64) e.save() return Response(status=status.HTTP_200_OK) except KeyError as ex: Response(ex, status=status.HTTP_400_BAD_REQUEST) else: return HttpResponse("Method not allowed", status=405)
def update_battery_temp_test(socket_io): battery_notified = False cpu_temp_notified = False battery_counter = 0 min_battery_val = current_app.config.get("SYSTEM_BATTERY_MIN_VOLTAGE") cpu_max_temp = current_app.config.get("SYSTEM_CPU_MAX_TEMPERATURE") while True: time.sleep(5) message = mock_battery_temp() message = message.split(',') battery = int(message[0]) / 10 temperature = int(message[1]) / 10 cpu_temperature = int(message[2]) / 10 if battery < min_battery_val: battery_counter += 1 if battery_counter >= 5 and not battery_notified: send_email(current_app.config.get("SYSTEM_BATTERY_MESSAGE")) battery_notified = True battery_counter = 0 else: battery_counter = 0 if battery + 1.5 > min_battery_val: battery_notified = False if cpu_temperature > cpu_max_temp: if not cpu_temp_notified: send_email( current_app.config.get("SYSTEM_CPU_TEMPERATURE_MESSAGE")) cpu_temp_notified = True else: if cpu_temperature < cpu_max_temp - 5: cpu_temp_notified = False socket_io.emit('batteryTemp', { 'battery': battery, 'temperature': temperature, 'cpu_temperature': cpu_temperature }, namespace='/notification')
def success(): if request.method == "POST": email = request.form["email_name"] height = request.form["height_name"] if (db.session.query(Data).filter(Data.email == email).count() == 0): data = Data(email, height) db.session.add(data) db.session.commit() average_height = db.session.query(func.avg(Data.height)).scalar() average_height = round(average_height, 1) count = db.session.query(Data.height).count() email_service.send_email(email, height, average_height, count) return render_template("success.html") return render_template( "index.html", text="Seems like we've got something from that email already.")
def getPDFasText(path, ocrEnabled=True): """ path: must be in the format parentFolder + fileName + extension example of valid paths: 1) PDFs/AMBIT ENERGY.pdf 2) PDFs/Energy to Go.pdf returns: The text representation of the given PDF file It reads the PDF file using PyPDF2 library and gets the PDF file as a string If PyPDF2 fails, then it performs OCR on the pdf """ ocrForce = False output = "" try: # pdfReader is the object created from processing the pdf # it contains the pages, which we can use to extract the text pdfReader = PyPDF2.PdfFileReader(open(path, 'rb')) for i in range(pdfReader.numPages): page = pdfReader.getPage(i) output += page.extractText() except Exception as e: print("Error:", e) ocrForce = True # assuming if length is < 50, then the pdf library failed # 50 is just arbitrary # so then we try OCR on it if (len(output) < 50 or output.isspace()) and ocrEnabled or ocrForce: print("PDF reading library failed, running OCR on", path) try: output = ocr(path) except Exception as e: print("error when performing OCR", e) send_email(path + " was failed to read\nexception text: " + str(e)) # if a word doesn't fit in 1 line, then it is split by a colon and continued on the next line # getting rid of that colon output = output.replace('-\n', '') return " ".join(output.split("\n")) # replace new lines with space
def get_gps_data(): moving_email_sent = False stopped_email_sent = True moving_counter = 0 with serial.Serial(current_app.config.get("GPS_SERIAL_PORT"), 9600) as ser: while True: data = ser.read(67) data = data.decode() if data: message = data.split(',') if 'A' not in message: continue n_s_indication = message[4] latitude = format_coordinates(message[3], n_s_indication) w_e_indication = message[6] longitude = format_coordinates(message[5], w_e_indication) speed = float(message[7]) map_url = f"https://www.google.com/maps/search/?api=1&query={latitude},{longitude}" if speed >= current_app.config.get("GROUND_SPEED_MIN"): moving_counter += 1 if not moving_email_sent and moving_counter >= 3: send_email( current_app.config.get("UNIT_IN_MOTION_MESSAGE")) moving_email_sent = True stopped_email_sent = False moving_counter = 0 else: moving_counter = 0 if not stopped_email_sent: send_email( current_app.config.get("UNIT_STOPPED_MESSAGE") + map_url) stopped_email_sent = True moving_email_sent = False time.sleep(30)
def event(request): """ List all Events, or create a new Event. """ external_service_not_responding = 'Action error. Users service or Object detection service is not responding.' success = 'Action service. Event from camera {} was successfully processed' if request.method == 'GET': e = Event.objects.all() serializer = EventSerializer(e, many=True) return Response(serializer.data) elif request.method == 'POST': try: event_name = request.POST['event'] cam_id = request.POST['cam_id'] contact = get_contact() event_print = requests.get(url=event_print_url, params={'cam_id': cam_id}, timeout=5) frame64 = event_print.text cam = requests.get(url=camera_url + '{}/'.format(cam_id), timeout=4).json() e = Event.objects.create(event=event_name, camera=cam_id, contact=contact) e.result = send_email(contact, event_name, cam['id'], cam['model_name'], cam['address'], frame64) e.save() mqtt_client.publish(topic="actions/logs/success", payload=success.format(cam_id)) return Response(status=status.HTTP_200_OK) except KeyError as ex: print(ex) Response(ex, status=status.HTTP_400_BAD_REQUEST) except (requests.exceptions.ConnectTimeout, requests.exceptions.ConnectionError) as ex: print(ex) mqtt_client.publish(topic="actions/logs/success", payload=external_service_not_responding) Response(ex, status=status.HTTP_500_INTERNAL_SERVER_ERROR) else: return HttpResponse("Method not allowed", status=405)
def smtpEmailNotifier(receiver_email): return lambda message: send_email(config.email_receiver, message, config.email_subject, config.smtp)
def get_optimal_lineup(database_session, day=None): """ Get the optimal lineup of the players to choose for tonight :param database_session: SQLAlchemy database session :return: an OptimalLineupDict structure """ if day is None: day = date.today() optimal_lineup = OptimalLineupDict() player_heap = dict() # Look for the hitter entries for fielding_position in OptimalLineupDict.FieldingPositions: query_results = PregameHitterGameEntry.get_daily_entries_by_position(database_session, fielding_position, day) query_results = list(query_results.order_by(desc(PregameHitterGameEntry.predicted_draftkings_points))) query_result_heap = list() for query_result in query_results: heapq.heappush(query_result_heap, (-query_result.predicted_draftkings_points, query_result)) while not optimal_lineup.position_map[fielding_position].is_valid() and len(query_results) > 0: candidate_player = heapq.heappop(query_result_heap)[1] optimal_lineup.add(candidate_player) player_heap[fielding_position] = list() while len(query_result_heap) > 0: player = heapq.heappop(query_result_heap) heapq.heappush(player_heap[fielding_position], player) # Look for pitchers query_results = PregamePitcherGameEntry.get_all_daily_entries(database_session, day) query_results = list(query_results.order_by(desc(PregamePitcherGameEntry.predicted_draftkings_points))) for query_result in query_results: heapq.heappush(query_result_heap, (-query_result.predicted_draftkings_points, query_result)) while not optimal_lineup.position_map["SP"].is_valid() and len(query_results) > 0: candidate_player = heapq.heappop(query_result_heap)[1] optimal_lineup.add(candidate_player) player_heap["SP"] = list() while len(query_result_heap) > 0: player = heapq.heappop(query_result_heap) heapq.heappush(player_heap["SP"], player) # Replace players one by one who are "overpaid" based on predicted points per dollar while (optimal_lineup.get_total_salary() > Draftkings.CONTEST_SALARY and len(player_heap) > 0) or \ not optimal_lineup.is_valid(): worst_position = optimal_lineup.get_worst_position() next_player = heapq.heappop(player_heap[worst_position])[1] optimal_lineup.add(next_player) # Print out all the remaining players in order of their value runner_up_text = "Runner-up players\n" for fielding_position in OptimalLineupDict.FieldingPositions: runner_up_text += fielding_position + "\n" while len(player_heap[fielding_position]) > 0: player = heapq.heappop(player_heap[fielding_position]) runner_up_text += "%s\n" % str(player[1]) runner_up_text += "\n" runner_up_text += "SP\n" while len(player_heap["SP"]) > 0: player = heapq.heappop(player_heap["SP"]) runner_up_text += "%s\n" % str(player[1]) send_email(runner_up_text) print runner_up_text # Commit the prediction to the database lineup_db_entry = LineupEntry() lineup_db_entry.game_date = date.today() lineup_db_entry.game_time = datetime.now().strftime("%H:%M:%S") lineup_db_entry.starting_pitcher_1 = optimal_lineup.position_map["SP"]._position_heap[0][1].rotowire_id lineup_db_entry.starting_pitcher_2 = optimal_lineup.position_map["SP"]._position_heap[1][1].rotowire_id lineup_db_entry.catcher = optimal_lineup.position_map["C"]._player.rotowire_id lineup_db_entry.first_baseman = optimal_lineup.position_map["1B"]._player.rotowire_id lineup_db_entry.second_baseman = optimal_lineup.position_map["2B"]._player.rotowire_id lineup_db_entry.third_baseman = optimal_lineup.position_map["3B"]._player.rotowire_id lineup_db_entry.shortstop = optimal_lineup.position_map["SS"]._player.rotowire_id lineup_db_entry.outfielder_1 = optimal_lineup.position_map["OF"]._position_heap[0][1].rotowire_id lineup_db_entry.outfielder_2 = optimal_lineup.position_map["OF"]._position_heap[1][1].rotowire_id lineup_db_entry.outfielder_3 = optimal_lineup.position_map["OF"]._position_heap[2][1].rotowire_id database_session.add(lineup_db_entry) database_session.commit() return optimal_lineup
if not Path('run_history.txt').is_file(): with open('run_history.txt', 'w') as run_file: run_file.write(datetime.today().strftime('%m/%d/%y %H:%M:%S') + f", {success}") else: with open('run_history.txt', 'a', newline='') as run_file: run_file.write("\n" + datetime.today().strftime('%m/%d/%y %H:%M:%S') + f", {success}") if Path('results_MA/trash.csv').is_file(): os.remove('results_MA/trash.csv') if Path('results_MA/trash_old.csv').is_file(): os.remove('results_MA/trash_old.csv') # The success variable to see how many zipcodes were actually extracted print(f'The number of zipcodes successfully scraped are: {success}') try: # [ACTION REQUIRED] Select which function you want to run scrape() check_unique() except Exception as err: error_traceback = traceback.extract_tb(err.__traceback__) send_email( body= f"Traceback at {datetime.today().strftime('%m/%d/%y %H:%M:%S')} from Scheduler: {error_traceback}" )
def send_events_email(): send_email() return "Email has been sent!"
from mine.stat_miner import * from datetime import date, timedelta, datetime from email_service import send_email import cProfile import traceback try: prefetch_pregame_stats() csv_dict = get_csv_dict("players-" + str(date.today()) + ".csv") update_salaries(csv_dict) predict_daily_points() optimal_lineup = get_optimal_lineup() except Exception as e: print e send_email("The predictor generated an exception: {0}".format(e))
from sql.mlb_database import MlbDatabase from email_service import send_email from mine.stat_miner import LineupMiner try: mlbDatabase = MlbDatabase() lineup_miner = LineupMiner(lineup=None, opposing_pitcher=None, game_date=None, game_time=None, db_path=None, is_home=False) lineup_miner.mine_yesterdays_results() send_email("Mine postgame completed.") except Exception as e: print e send_email("The predictor generated an exception: {0}".format(e))
EMAIL_RECIPIENTS=os.environ['CURRENCY_INSIGHT_EMAIL_RECIPIENT'] if __name__ == '__main__': current=currency_service.get_rate_at_day() weekly=currency_service.get_weekly_change() monthly=currency_service.get_monthly_change() monthly_plot = currency_service.create_monthly_plot() yearly_plot = currency_service.create_yearly_plot() # email_text = email_service.prepare_email_txt( # today=today, # weekly=weekly, # monthly=monthly # ) email_html = email_service.prepare_email_html( base_currency=currency_service.BASE_CURRENCY, target_currency=currency_service.TARGET_CURRENCY, current=current, weekly=weekly, monthly=monthly, monthly_plot=monthly_plot, yearly_plot=yearly_plot ) for recipient in EMAIL_RECIPIENTS.split(','): email_service.send_email( to=recipient, subject=f'{currency_service.BASE_CURRENCY}/{currency_service.TARGET_CURRENCY} insight', text='', html=email_html )
import os from sql.mlb_database import MlbDatabase from mine.rotowire import mine_pregame_stats from mine.draft_kings import Draftkings from datetime import date, timedelta from email_service import send_email import cProfile os.chdir("/home/cameron/workspaces/MlbDatabase/mlb_scrape/Released/mlbscrape_python") databaseSession = MlbDatabase().open_session() #try: #cProfile.run('mine_pregame_stats()') #Draftkings.save_daily_csv() #csv_dict = Draftkings.get_csv_dict() #Draftkings.update_salaries(databaseSession, csv_dict) Draftkings.predict_daily_points(databaseSession, date.today()) optimal_lineup = Draftkings.get_optimal_lineup(databaseSession, date.today()) print optimal_lineup send_email(optimal_lineup.__str__()) """except Exception as e: print e send_email("The predictor generated an exception: {0}".format(e)) """ databaseSession.close()
import os from sql.mlb_database import MlbDatabase from mine.rotowire import mine_pregame_stats from mine.draft_kings import Draftkings from datetime import date, timedelta, datetime from email_service import send_email import cProfile os.chdir("/home/cameron/workspaces/MlbDatabase/mlb_scrape/Released/mlbscrape_python") databaseSession = MlbDatabase().open_session() try: mine_pregame_stats() Draftkings.save_daily_csv() csv_dict = Draftkings.get_csv_dict() Draftkings.update_salaries(databaseSession, csv_dict) Draftkings.predict_daily_points(databaseSession, date.today()) optimal_lineup = Draftkings.get_optimal_lineup(databaseSession, date.today()) print optimal_lineup send_email(optimal_lineup.__str__()) except Exception as e: print e send_email("The predictor generated an exception: {0}".format(e)) databaseSession.close()
def send_message(): status, message = send_email(['*****@*****.**'], 'hey', 'heyyyyy') message = json.loads(message)['message'].title() return jsonify({ 'message': message }), status