import tldextract import json import firebase_admin from firebase_admin import credentials, firestore from config import PROJECT # initialize firebase sdk CREDENTIALS = credentials.ApplicationDefault() firebase_admin.initialize_app(CREDENTIALS, { 'projectId': PROJECT, }) # get firestore client db = firestore.client() # Keyword Array filled in by daya in Firestore keywords = [] fallback = 'Sorry, I cannot think of a reply for that.' result = '' query = 'greenpeace t-shirt' try: search_result_list = list(search(query, tld="com", num=100, pause=3)) for search in search_result_list: page = requests.get(search)
def upload_average(data): db = firestore.client() db.collection("general_information").document("daily_average").set(data)
def init_firebase(token): cred = credentials.Certificate(token) firebase_admin.initialize_app(cred) db = firestore.client() return db
def schedule_test_request(): cornell_email = os.environ.get("cornell_email") cornell_netid = cornell_email[:cornell_email.find('@')] # Use the application default credentials cred = credentials.Certificate('if-no-corona-1851477ee5bc.json') firebase_admin.initialize_app(cred) db = firestore.client() user_ref = db.collection(u'users').document(cornell_netid) user_doc = user_ref.get() first_name = user_doc.get('firstName') last_name = user_doc.get('lastName') college = user_doc.get('college') date_of_birth = user_doc.get('dateOfBirth') # print(str(date_of_birth)) # print(type(date_of_birth)) addr_template = "https://register.cayugahealth.com/patient-registration/employee?employer=Cornell-Surveillance&hideInsurance=1&hideEmergencyContact=1&sourceSystemPatientId=%s&firstName=%s&lastName=%s" addr = addr_template % (cornell_netid, first_name, last_name) with webdriver.Chrome() as driver: # First page driver.get(addr) time.sleep(1) button_wrapper = driver.find_element_by_class_name('button-solid') shadow_section = expand_shadow_element(driver, button_wrapper) next_button = shadow_section.find_element_by_class_name( 'button-native') ActionChains(driver).move_to_element(next_button).click( next_button).perform() # Second page time.sleep(1) #have_registered_form = driver.find_element_by_xpath("//ion-item[@class='item-interactive']") have_registered_form = driver.find_element_by_xpath( "//ion-select[@name='hasPreviouslyRegistered']") ActionChains(driver).move_to_element(have_registered_form).click( have_registered_form).perform() ion_item = have_registered_form.find_element_by_class_name( 'ion-activated') time.sleep(1) yes_option = ion_item ActionChains(driver).move_to_element(yes_option).click( yes_option).perform() #have_registered_shadow = expand_shadow_element(driver, have_registered_form) #yn_dropdown_button = have_registered_shadow. #yn_dropdown_button.click() print("================") # select_yn = shadow_dropdown.find_elements_by_xpath() # shadow_select_yn = expand_shadow_element(driver, select_yn) # next_button = fetch_only_element(driver, "//ion-button[@value='...']") # next_button.click() # prev_registered_button = fetch_only_element(driver, "//button[@value='']") # prev_registered_button.click() time.sleep(10)
def get(self): print(g.args) if firebase_admin._DEFAULT_APP_NAME not in firebase_admin._apps: cred = credentials.Certificate('serviceAccountKey.json') firebase_admin.initialize_app(cred) db = firestore.client() users_ref = db.collection(u'users') if 'username' in g.args.keys(): query = users_ref.document(g.args['username']) data = query.get().to_dict() if data == None: respond = {'status': 1} return respond, 200, None print(data) body = { 'username': query.id, 'current_certification': data['current_certification'], 'first_name': data['first_name'], 'last_name': data['last_name'], 'isSupervisor': data['supervisor_approved'], 'messenger_ID': data['messenger_uid'], 'program_uid': data['program_uid'], 'specialist_level': data['specialist_level'], 'verify_token': data['verify_token'] } respond = {'status': 0, 'body': body} return respond, 200, None if 'verify_token' in g.args.keys(): query = users_ref.where(u'verify_token', u'==', g.args['verify_token']).stream() docs = [] for doc in query: docs.append((doc.id, doc.to_dict())) if docs == []: respond = {'status': 1} return respond, 404, None print(docs) id = docs[0][0] data = docs[0][1] body = { 'username': id, 'current_certification': data['current_certification'], 'first_name': data['first_name'], 'last_name': data['last_name'], 'isSupervisor': data['supervisor_approved'], 'messenger_ID': data['messenger_uid'], 'program_uid': data['program_uid'], 'specialist_level': data['specialist_level'], 'verify_token': data['verify_token'] } respond = {'status': 0, 'body': body} return respond, 200, None if 'messenger_ID' in g.args.keys(): query = users_ref.where(u'messenger_uid', u'==', g.args['messenger_ID']).stream() docs = [] for doc in query: docs.append((doc.id, doc.to_dict())) if docs == []: respond = {'status': 1} return respond, 404, None print(docs) id = docs[0][0] data = docs[0][1] body = { 'username': id, 'current_certification': data['current_certification'], 'first_name': data['first_name'], 'last_name': data['last_name'], 'isSupervisor': data['supervisor_approved'], 'messenger_ID': data['messenger_uid'], 'program_uid': data['program_uid'], 'specialist_level': data['specialist_level'], 'verify_token': data['verify_token'] } respond = {'status': 0, 'body': body} return respond, 200, None if 'name' in g.args.keys(): try: first_name, last_name = g.args['name'].split('_') except ValueError: respond = {'status': 2} return respond, 200, None query = users_ref.where(u'first_name', u'==', first_name).where(u'last_name', u'==', last_name).stream() docs = [] for doc in query: docs.append((doc.id, doc.to_dict())) if docs == []: respond = {'status': 1} return respond, 404, None print(docs) id = docs[0][0] data = docs[0][1] body = { 'username': id, 'current_certification': data['current_certification'], 'first_name': data['first_name'], 'last_name': data['last_name'], 'isSupervisor': data['supervisor_approved'], 'messenger_ID': data['messenger_uid'], 'program_uid': data['program_uid'], 'specialist_level': data['specialist_level'], 'verify_token': data['verify_token'] } respond = {'status': 0, 'body': body} return respond, 200, None
def __init__(self): self.app = initialize_app(credentials.Certificate(os.path.dirname(__file__) + r'\static\auth\firebase_auth.json'),{'storageBucket':'trader-3870e.appspot.com'},str(uuid.uuid4())) self.fb = firestore.client(app=self.app) self.bucket = storage.bucket(app=self.app)
name = input("Enter Equipment Name >> ") owner = None while not owner: owner = input("Enter Ownership Here >> ") d2[t] = (name, owner) collect_data(ref, tags, d2) def do_highlight(self, arg): """test highlight""" ls = arg.split() for l in ls: a = cv2.imread(l, cv2.IMREAD_GRAYSCALE) cv2.imwrite('highlighted_' + l, highlight_image(a)) def do_exit(self, arg): sys.exit() if __name__ == '__main__': if not len(firebase_admin._apps): cred = credentials.Certificate("technica2019-firebase-adminsdk-2gyis-fdf3974fd6.json") app = firebase_admin.initialize_app(cred, { 'projectId': 'technica2019', }) else: app = firebase_admin.get_app() ref = firestore.client() CommandLineTool().cmdloop()
def __init__(self) -> None: self.client = firestore.client() self.collection = self.client.collection("stocks")
def __init__(self): _cert_path = os.path.join(os.getcwd(), "sutdieeeweb.json") cred = credentials.Certificate(_cert_path) _db = firebase_admin.initialize_app(cred) self.client = firestore.client(_db)
def sendDataToFirebase(): db = firestore.client() doc_ref = db.collection("system-monitor").document(get_host_ip()) doc_ref.set(doc)
def __init__(self) -> None: self.client = firestore.client() self.db = self.client.collection('portfolio')
def __init__(self, mapper, creds_path, project_id=None, custom_token=None, api_key=None, validator=None, read_only=False, name=None, namespace=None ): self.mapper = mapper self.validator = validator self.read_only = read_only self.api_key = api_key self.namespace = namespace self.expires = None self.data_adapter = FirestoreDataAdapter() # Use a service account options = {"httpTimeout": 5} app = None if custom_token is not None: # from device client self.user = self.sign_in_with_custom_token(custom_token) self.update_expires() self.creds = CustomToken(self.user["idToken"], project_id) app_name = name if name else firebase_admin._DEFAULT_APP_NAME try: app = firebase_admin.get_app(name=app_name) except ValueError as e: # ugly hack to account for different behaviour of google libs try: app = firebase_admin.initialize_app(self.creds, name=app_name, options=options) except Exception as e: self.creds = self.creds.get_credential() app = firebase_admin.initialize_app(self.creds, name=app_name, options=options) elif creds_path is not None: # in dev with service creds try: self.creds = credentials.Certificate(creds_path) app = firebase_admin.initialize_app(self.creds, options=options) except ValueError as e: print("already initaialised") else: # in app engine environment already_initialised = firebase_admin._DEFAULT_APP_NAME in firebase_admin._apps if not already_initialised: app = firebase_admin.initialize_app(options=options) self.db = firestore.client(app=app) self.app = app
def __init__(self): self.db = firestore.client()
def get_database_client(firestore_API_key: str): cred = credentials.Certificate(firestore_API_key) firebase_admin.initialize_app(cred) return firestore.client()
def deleteSmarthome(collection, userId): db = firestore.client() db.collection(collection).document(userId).delete() return print("delete" + collection + " smarthome in the database")
def __init__(self, cred, col_name): #firebase_admin.initialize_app(credentials.Certificate(cred)) self.col_ref = firestore.client().collection(col_name) self.doc_ref = None
def get_conn(): if 'conn' in g and g.conn is not None: return g.conn else: g.conn = firestore.client() return g.conn
def extractFromFirstProjectOfFirebase(cred): app = firebase_admin.initialize_app(cred) db = firestore.client() delete_app(app) return db
# Server Routes for Flask import datetime import time import requests import flask from firebase_admin import auth, firestore from firebase_admin import exceptions from flask import Blueprint, render_template, redirect, url_for, request, flash, session import json from .python_firebase.firebase_connect import firebase_app server_bp = Blueprint('server_bp', __name__) client = firestore.client(app=firebase_app) @server_bp.route('/login') def login(): return render_template('login.html', navigation=dynamic_nav()) @server_bp.route('/login', methods=['POST']) def login_post(): from .python_firebase.firebase_custom import fireAuth email = request.form.get('email') password = request.form.get('password') try: user = fireAuth.sign_in_with_email_and_password(email, password)
from firebase_admin import credentials, firestore from flask_cors import CORS import firebase_admin import json, os cred = credentials.Certificate(json.loads(os.environ["PROJECT_AUTH"])) firebase_admin.initialize_app(cred) fireClient = firestore.client() from flask import Flask, render_template, session, redirect, request from flask_socketio import SocketIO socketio = SocketIO(logger=True) from socraticos.blueprints import users, groups, chat, auth def create_app(): app = Flask(__name__) # TODO: CHANGE THIS!!! app.secret_key = "DEVELOPMENT" CORS(app) app.register_blueprint(users.users, url_prefix="/users") app.register_blueprint(groups.groups, url_prefix="/groups") app.register_blueprint(auth.auth, url_prefix="/auth") @app.before_request def log_request_info(): app.logger.debug('JSON: %s', request.get_json()) # Redirect to API documentation
def __init__(self): # Use a service account cred = credentials.Certificate('seek-525ee-2cc702803f50.json') firebase_admin.initialize_app(cred) self.db = firestore.client()
def doctor(pid, did): import datetime import pandas as pd import numpy as np import firebase_admin from firebase_admin import credentials from firebase_admin import firestore from firebase_admin import storage import pyrebase from datetime import date, timedelta import urllib.request, json import time from matplotlib import pyplot as plt import matplotlib.dates as mdates import os import csv from IPython.display import display from Model import trainData import random # from google.cloud import storage from matplotlib.patches import Ellipse import seaborn as sns import matplotlib.patches as mpatches # signal processing from scipy import signal from scipy.ndimage import label from scipy.stats import zscore from scipy.interpolate import interp1d from scipy.integrate import trapz # misc import warnings # generate pdf from reportlab.pdfgen import canvas from reportlab.lib.colors import Color, lightblue, black # In[97]: if not firebase_admin._apps: cred = credentials.Certificate("serene-firebase-adminsdk.json") app = firebase_admin.initialize_app( cred, { 'storageBucket': 'serene-2dfd6.appspot.com', }, name='[DEFAULT]') else: app = firebase_admin.get_app() db = firestore.client() # In[98]: today = datetime.datetime.now() timestamp = today.strftime("%Y-%m-%d") bucket = storage.bucket(app=app) # ## Get data from storage and get list of dates (2 weeks) # In[99]: # get a a list of date between start and end date userID = pid doctorID = did duration = 15 # two weeks dates = [] for x in range(0, duration): today = date.today() # revert to original # yesterday = today - datetime.timedelta(days=1) start_date = (today - timedelta(days=duration - x)).isoformat() dates.append(start_date) # In[100]: df = pd.DataFrame() notAvailableDates = [] # loop through the storage and get the data sleep = [] for x in range(0, len(dates)): # Sleep blob = bucket.blob(userID + "/fitbitData/" + dates[x] + "/" + dates[x] + "-sleep.json") # download the file u = blob.generate_signed_url(datetime.timedelta(seconds=300), method='GET') try: with urllib.request.urlopen(u) as url: data = json.loads(url.read().decode()) sleepMinutes = data['summary']["totalMinutesAsleep"] except: notAvailableDates.append(dates[x]) pass # Activity (Steps) blob = bucket.blob(userID + "/fitbitData/" + dates[x] + "/" + dates[x] + "-activity.json") # download the file u = blob.generate_signed_url(datetime.timedelta(seconds=300), method='GET') try: with urllib.request.urlopen(u) as url: data = json.loads(url.read().decode()) steps = data['summary']["steps"] except: notAvailableDates.append(dates[x]) pass # heartrate heart_rate = {} blob = bucket.blob(userID + "/fitbitData/" + dates[x] + "/" + dates[x] + "-heartrate.json") u = blob.generate_signed_url(datetime.timedelta(seconds=300), method='GET') try: with urllib.request.urlopen(u) as url: data = json.loads(url.read().decode()) df_heartrate = pd.DataFrame( data['activities-heart-intraday']['dataset']) df_heartrate.time.apply(str) df_heartrate['time'] = pd.to_datetime(df_heartrate['time']) df_heartrate['hour'] = df_heartrate['time'].apply( lambda time: time.strftime('%H')) df_heartrate.drop(['time'], axis=1, inplace=True) heart_rate = df_heartrate.groupby(["hour"], as_index=False).mean() heart_rate['sleepMin'] = sleepMinutes heart_rate['TotalSteps'] = steps heart_rate['date'] = dates[x] heart_rate = heart_rate.astype({"hour": int}) except: notAvailableDates.append(dates[x]) pass # append dataframe df = df.append(heart_rate, ignore_index=True) # In[101]: notAvailableDates notSyncedDates = pd.DataFrame() notSyncedDates['date'] = notAvailableDates # In[102]: notSyncedDates = notSyncedDates.drop_duplicates() # In[103]: notSyncedDates # ### Get user location # In[104]: # get location from database loc_df = pd.DataFrame() locID = [] locations = db.collection(u'PatientLocations').where( u'patientID', u'==', userID).stream() for location in locations: loc1 = location.to_dict() locID.append(location.id) loc_df = loc_df.append(pd.DataFrame(loc1, index=[0]), ignore_index=True) loc_df['id'] = locID # In[105]: loc_df.drop(['anxietyLevel', 'lat', 'lng', 'patientID'], axis=1, inplace=True) # In[106]: loc_df.time.apply(str) loc_df['time'] = pd.to_datetime(loc_df['time']) loc_df['date'] = pd.to_datetime(loc_df['time'], format='%Y:%M:%D').dt.date loc_df['hour'] = loc_df['time'].apply(lambda time: time.strftime('%H')) loc_df.drop(['time'], axis=1, inplace=True) loc_df.hour = loc_df.hour.astype(int) loc_df.date = loc_df.date.astype(str) df.date = df.date.astype(str) # In[107]: dfinal = pd.merge(left=df, right=loc_df, how='left', left_on=['hour', 'date'], right_on=['hour', 'date']).ffill() # ### Test data into model # In[108]: # test model train_df = dfinal.rename(columns={'value': 'Heartrate'}) # In[109]: Labeled_df = pd.DataFrame() Labeled_df = trainData(train_df) # In[110]: Labeled_df.drop(['lon'], axis=1, inplace=True) # In[111]: Labeled_df['name'].fillna("Not given", inplace=True) Labeled_df['id'].fillna("Not given", inplace=True) Labeled_df['anxiety_assigned'].fillna('Not given', inplace=True) # In[112]: # Update firebase with the user anxiety level for row in Labeled_df.itertuples(): if row.id != 'Not given': if row.Label == 'Low' or row.Label == 'LowA': anxietyLevel = '1' elif row.Label == 'Meduim': anxietyLevel = '2' else: anxietyLevel = '3' if row.anxiety_assigned == False or row.anxiety_assigned == 'Not given': doc_ref = db.collection(u'PatientLocations').document(row.id) doc_ref.update({ u'anxietyLevel': anxietyLevel, u'anxiety_assigned': True }) # ### Show the places with highest anxiety level # In[113]: # Show the highest level df_high = pd.DataFrame() df_high = Labeled_df[Labeled_df.Label == 'High'] # # Get patient information # In[114]: docDf = pd.DataFrame() doc_ref = db.collection(u'Patient').document(userID) doc = doc_ref.get().to_dict() docDf = docDf.append(pd.DataFrame(doc, index=[0]), ignore_index=True) # In[115]: age1 = docDf['age'].values name1 = docDf['name'].values emp1 = docDf['employmentStatus'].values mar1 = docDf['maritalStatus'].values income1 = docDf['monthlyIncome'].values chronicD1 = docDf['chronicDiseases'].values smoke1 = docDf['smokeCigarettes'].values gad1 = docDf['GAD-7ScaleScore'].values gender1 = docDf['gender'].values age = age1[0] name = name1[0] emp = emp1[0] mar = mar1[0] income = income1[0] chronicD = chronicD1[0] smoke = smoke1[0] gad = gad1[0] gender = gender1[0] # ## Storage intiliazation # In[116]: firebaseConfig = { "apiKey": "AIzaSyBoxoXwFm9TuFysjQYag0GB1NEPyBINlTU", "authDomain": "serene-2dfd6.firebaseapp.com", "databaseURL": "https://serene-2dfd6.firebaseio.com", "projectId": "serene-2dfd6", "storageBucket": "serene-2dfd6.appspot.com", "messagingSenderId": "461213981433", "appId": "1:461213981433:web:62428e3664182b3e58e028", "measurementId": "G-J66VP2Y3CR" } firebase = pyrebase.initialize_app(firebaseConfig) storage = firebase.storage() # # HR # In[117]: sns.set(rc={'axes.facecolor': '#fcfeff'}) # In[118]: # Take the highest heartrate in a day dfhr = pd.DataFrame() hr = [] hrr = 0 rowCount = 1 for x in range(0, len(dates)): rowCount = 0 for row in df.itertuples(): if (row.date == dates[x]): if (row.value > rowCount): rowCount = row.value hr.append(rowCount) dfhr['date'] = dates dfhr['hr'] = hr # In[119]: plt.figure(figsize=(20, 7)) plt.plot(dfhr['date'], dfhr['hr'], color="#a03e3e", linewidth=4) plt.xlabel("Date", fontsize=16) plt.ylabel("Amplitude (bpm)") plt.tick_params(axis='x', rotation=70) plt.tight_layout() plt.savefig('hr.png', dpi=None) # plt.show() # plt.draw() # # Steps # In[120]: dfstep = pd.DataFrame() avgSteps = [] totalsteps = 0 rowCount = 1 for x in range(0, len(dates)): for row in Labeled_df.itertuples(): if (row.date == dates[x]): rowCount += 1 totalsteps += row.TotalSteps avgSteps.append(totalsteps / rowCount) dfstep['date'] = dates dfstep['Steps'] = avgSteps # In[121]: # Plot Steps plt.figure(figsize=(20, 7)) # plt.fill_between(dfstep['date'], dfstep['Steps'], color="#ffd6b0", linewidth=1) plt.plot(dfstep['date'], dfstep['Steps'], color="#ff5900", linewidth=4, marker='o', markerfacecolor='#ffd6b0') plt.xlabel("Date", fontsize=16) plt.ylabel("Total steps") plt.tick_params(axis='x', rotation=70) plt.tight_layout() plt.savefig('steps.png', dpi=None) # plt.show() # plt.draw() # # Sleep # In[122]: dfsleep = pd.DataFrame() sleeps = [] totalsleep = 0 rowCount = 1 for x in range(0, len(dates)): for row in df.itertuples(): if (row.date == dates[x]): totalsleep = row.sleepMin sleeps.append(totalsleep / 60) dfsleep['date'] = dates dfsleep['sleep'] = sleeps # In[123]: figs = dfsleep.plot.bar(x='date', y='sleep', rot=70, width=0.25, color='#3629a6', capstyle='round').get_figure() figs.set_size_inches(20, 10) plt.xlabel('Date') plt.ylabel('Sleep (hr)') # plt.show() # plt.draw() plt.tight_layout() figs.savefig('sleep.png', dpi=None) # # AL # In[124]: # Change Label values to num, to represent them in a barchart nums = [] for row in Labeled_df.itertuples(): if row.Label == 'Low' or row.Label == 'LowA': nums.append(1) elif row.Label == 'Meduim': nums.append(2) else: nums.append(3) Labeled_df['numLabel'] = nums # In[125]: # Get anxiety level by day and store it in a new data frame plot_df = pd.DataFrame() avgAnxiety = [] totalAnxiety = 0 rowCount = 1 for x in range(0, len(dates)): totalAnxiety = 0 rowCount = 1 for row in Labeled_df.itertuples(): if (row.date == dates[x]): rowCount += 1 totalAnxiety += row.numLabel avgAnxiety.append(totalAnxiety / rowCount) plot_df['date'] = dates plot_df['Anxiety'] = avgAnxiety # In[126]: fig, ax = plt.subplots() # Draw the stem and circle c1 = '#9dd6f5' c2 = '#4ba0d1' c3 = '#23495f' bar_width = 0.25 for t, y in zip(plot_df["date"], plot_df["Anxiety"]): c = "" if (y <= 1): c = c1 elif (1 < y <= 2): c = c2 elif (y > 2): c = c3 plt.bar([t, t], [0, y], bar_width, color=c) colors = [[c1, c1], [c2, c2], [c3, c3]] categories = ['Low', 'Meduim', 'High'] # create dict legend_dict = dict(zip(categories, colors)) # create patches patchList = [] for key in legend_dict: data_key = mpatches.Patch(facecolor=legend_dict[key][0], edgecolor=legend_dict[key][1], label=key) patchList.append(data_key) ax.legend(handles=patchList, ncol=len(categories), fontsize=12) plt.tick_params(axis='x', rotation=70) # Start the graph at 0 ax.set_ylim(0, 3) fig.set_size_inches(15.5, 10) plt.tight_layout() plt.xlabel('Date') ax.yaxis.set_label_coords(-0.02, 0.48) # volo insert # plt.show() fig.savefig('AL.png', dpi=None) # # Location Analysis # In[127]: # get location from database new_loc = pd.DataFrame() locID = [] locations = db.collection(u'PatientLocations').where( u'patientID', u'==', userID).stream() for location in locations: loc = location.to_dict() locID.append(location.id) new_loc = new_loc.append(pd.DataFrame(loc, index=[0]), ignore_index=True) new_loc['id'] = locID # In[128]: new_loc.time.apply(str) new_loc['time'] = pd.to_datetime(new_loc['time']) new_loc['date'] = pd.to_datetime(new_loc['time'], format='%Y:%M:%D').dt.date new_loc.drop(['time'], axis=1, inplace=True) new_loc.date = new_loc.date.astype(str) # In[129]: new_loc = new_loc[(new_loc.date >= dates[0]) & (new_loc.date <= dates[len(dates) - 1])] # In[130]: names = [] Name = "" for row in new_loc.itertuples(): Name = row.nearestLoc names.append(Name) # In[131]: new_name = pd.DataFrame() new_name['name'] = names # In[132]: new_name = new_name.drop_duplicates() new_name.dropna() # In[133]: fnames = [] fName = "" for row in new_name.itertuples(): fName = row.name fnames.append(fName) # In[134]: analysis = pd.DataFrame() count = 0 i = 0 # label = "" locationName = "" near = '' nearLocs = [] counts = [] # labels = [] locationNames = [] for x in range(0, len(fnames)): count = 0 locName = fnames[i] for row in new_loc.itertuples(): if (locName == row.nearestLoc): if (row.anxietyLevel == '3'): count += 1 # label = row.anxietyLevel locationName = row.name near = row.nearestLoc i += 1 counts.append(count) # labels.append(label) locationNames.append(locationName) nearLocs.append(near) analysis['Location'] = locationNames analysis['Frequency'] = counts # analysis ['Anxiety Level'] = labels analysis['Nearest Location'] = nearLocs # In[135]: # newA = analysis.drop(analysis[analysis['Number of occurrences'] == 0].index, inplace=True) # In[136]: import six import arabic_reshaper from bidi.algorithm import get_display from reportlab.pdfbase import pdfmetrics from reportlab.pdfbase.ttfonts import TTFont from reportlab.lib import colors # In[137]: def render_mpl_table(data, col_width=5.0, row_height=0.625, font_size=14, header_color='#23495f', row_colors=['#e1eff7', 'w'], edge_color='#23495f', bbox=[0, 0, 1, 1], header_columns=0, ax=None, **kwargs): if ax is None: size = (np.array(data.shape[::-1]) + np.array([0, 1])) * np.array( [col_width, row_height]) fig, ax = plt.subplots(figsize=size) ax.axis('off') mpl_table = ax.table(cellText=data.values, bbox=bbox, colLabels=data.columns, cellLoc='center', **kwargs) mpl_table.auto_set_font_size(False) mpl_table.set_fontsize(font_size) for k, cell in six.iteritems(mpl_table._cells): cell.set_edgecolor(edge_color) if k[0] == 0 or k[1] < header_columns: cell.set_text_props(weight='bold', color='w') cell.set_facecolor(header_color) else: cell.set_facecolor(row_colors[k[0] % len(row_colors)]) cell.alignment = 'center' fig.savefig('Location.png', dpi=100) return ax # In[138]: if (len(analysis) > 0): for ind, row in analysis.iterrows(): analysis.loc[ind, 'Nearest Location'] = get_display( arabic_reshaper.reshape(analysis.loc[ind, 'Nearest Location'])) # In[139]: if (len(analysis) > 0): render_mpl_table(analysis, header_columns=0, col_width=4) # # improvement # In[153]: # get yesterday improvement today_al = float("{:.2f}".format(plot_df['Anxiety'].mean())) improvement = -1 # In[165]: try: doc_ref = db.collection(u'Patient').document(userID) doc = doc_ref.get().to_dict() prev = float("{:.2f}".format(doc['anxiety_level'])) # calculate the improvement improvement = float("{:.2f}".format(((prev - today_al) / 3) * 100)) except: improvement = 404 # ## Generate doctor report pdf and store it in database # # In[143]: pdf = canvas.Canvas('Doctor.pdf') pdf.setTitle('Patient report') pdf.drawImage("serene .png", 150, 730, width=300, height=130, mask='auto') pdf.setFillColor(colors.HexColor('#e1eff7')) pdf.roundRect(57, 400, 485, 200, 4, fill=1, stroke=0) pdf.setFont("Helvetica-Bold", 20) pdf.setFillColor(colors.HexColor('#23495f')) pdf.drawString(100, 570, "Patient Information") pdf.setFont("Helvetica-Bold", 15) pdf.drawString(150, 540, "Name: ") pdf.drawString(150, 520, "Age: ") pdf.drawString(150, 500, "Employment Status: ") pdf.drawString(150, 480, "Martial Status: ") pdf.drawString(150, 460, "Monthly Income: ") pdf.drawString(150, 440, "Chronic Diseases: ") pdf.drawString(150, 420, "Cigarette Smoker: ") pdf.drawString(150, 410, "Gender: ") pdf.setFont("Helvetica", 15) pdf.setFillColor(black) pdf.drawString(210, 540, name) pdf.drawString(210, 520, age) pdf.drawString(310, 500, emp) pdf.drawString(260, 480, mar) pdf.drawString(290, 460, income) pdf.drawString(290, 440, chronicD) pdf.drawString(290, 420, smoke) pdf.drawString(220, 410, gender) pdf.setFillColor(colors.HexColor('#bfbfbf')) pdf.roundRect(370, 560, 125, 30, 4, fill=1, stroke=0) pdf.setFillColorRGB(1, 1, 1) pdf.drawString(375, 570, "GAD-7 Score = ") pdf.setFont("Helvetica-Bold", 15) pdf.drawString(480, 570, gad) pdf.setFillColor(colors.HexColor('#e1eff7')) pdf.roundRect(57, 160, 485, 200, 4, fill=1, stroke=0) pdf.setFont("Helvetica-Bold", 16) pdf.setFillColor(colors.HexColor('#23495f')) pdf.drawString( 115, 330, "Report Duration From: (" + dates[0] + " To: " + dates[len(dates) - 1] + ")") pdf.setFont("Helvetica-Bold", 14) pdf.drawString(250, 300, "Improvements: ") pdf.setFont("Helvetica-Bold", 20) if (improvement == 404): pdf.drawString(290, 260, "--") else: pdf.drawString(280, 260, str(improvement) + '%') pdf.showPage() pdf.drawImage("serene .png", 150, 730, width=300, height=130, mask='auto') pdf.setFont("Helvetica-Bold", 20) pdf.setFillColor(colors.HexColor('#808080')) pdf.drawString(100, 650, "Anxiety Level") pdf.drawImage("AL.png", 57, 400, width=485, height=200) pdf.drawString(100, 330, "Heart Rate") pdf.drawImage("hr.png", 57, 100, width=485, height=200) pdf.showPage() pdf.drawImage("serene .png", 150, 730, width=300, height=130, mask='auto') pdf.setFont("Helvetica-Bold", 20) pdf.setFillColor(colors.HexColor('#808080')) pdf.drawString(100, 650, "Activity Rate") pdf.drawImage("steps.png", 57, 400, width=485, height=200) pdf.drawString(100, 350, "Sleep Analysis") pdf.drawImage("sleep.png", 57, 100, width=485, height=200) pdf.showPage() pdf.drawImage("serene .png", 150, 730, width=300, height=130, mask='auto') pdf.setFont("Helvetica-Bold", 20) pdf.setFillColor(colors.HexColor('#808080')) pdf.drawString(100, 650, "Locations With Highest Level of Anxiety") if (len(analysis) > 0): pdf.drawImage("Location.png", 30, 200, width=570, height=100) else: pdf.setFont("Helvetica", 15) pdf.setFillColor(colors.HexColor('#23495f')) t = pdf.beginText(130, 550) text = [ name + " condition was stable through this period,", "no locations with high anxiety level were detected." ] for line in text: t.textLine(line) pdf.drawText(t) if (len(notSyncedDates) != 0): pdf.setFont("Helvetica", 12) pdf.setFillColor(colors.HexColor('#d40027')) pdf.drawString( 75, 100, "Note: Below dates are missing, because they were not synced correctly:" ) i = 70 for row in notSyncedDates.itertuples(): pdf.drawString(85, i, '- ' + row.date) i = i - 20 pdf.save() # In[144]: # new method doct = storage.child(userID + "/DoctorReport/doctorReport").put('Doctor.pdf') # In[145]: linkDF = pd.DataFrame() linkDF = linkDF.append(pd.DataFrame(doct, index=[0]), ignore_index=True) # In[146]: token1 = linkDF['downloadTokens'].values token = token1[0] link = storage.child(userID + "/DoctorReport/doctorReport").get_url(token) # In[149]: from datetime import datetime # In[150]: date = datetime.now() # In[161]: # store the data random_id = random.randint(0, 1000) ID = "Doctor" + userID + str(random_id) doc_rec = db.collection(u'DoctorReports').document(str(ID)) doc_rec.set({ u'doctorId': doctorID, u'emailsent': True, u'patientId': userID, u'reportTime': date, u'reportUrl': link, u'improvement': improvement }) doc_rec = db.collection(u'Patient').document(userID) doc_rec.update({u'anxiety_level': today_al}) # In[152]: os.remove("hr.png") os.remove("sleep.png") os.remove("AL.png") os.remove("steps.png") os.remove("Doctor.pdf") if (len(analysis) > 0): os.remove("Location.png") return {"Doctor": "Yaaay"}
from rest_framework.decorators import api_view from rest_framework.response import Response from rest_framework import status import base64 from Crypto.PublicKey import RSA from Cryptodome.Cipher import PKCS1_OAEP from Cryptodome.Hash import SHA512 from firebase_admin import initialize_app, firestore, credentials import os # For this to work, make sure that environment variable GOOGLE_APPLICATION_CREDENTIALS is set properly. BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) CREDS_ADDRESS=BASE_DIR+'/views/creds.json' cred=credentials.Certificate(CREDS_ADDRESS) fb_app = initialize_app(credential=cred) store = firestore.client(app=fb_app) #----------------- #Getdata function #----------------- db = firestore.client() def getdata(phnumber, update_probability=False): privatekey = db.collection(u'Profile').document(u'' + phnumber).get().to_dict()['PrivateKey'] rsakey = RSA.importKey(base64.b64decode(privatekey)) # rsakey = PKCS1_OAEP.new(rsakey, hashAlgo=SHA256, mgfunc=lambda x,y: pss.MGF1(x,y, SHA256)) rsakey = PKCS1_OAEP.new(rsakey, SHA512) def decrypt_location(loc, rsakey): # global rsakey b64_decoded_message = base64.b64decode(loc) decrypted = rsakey.decrypt(b64_decoded_message) return decrypted
def searchTweets(query): db = firestore.client() maxCount = 100 max_id = -1 count = 0 obj = { query: { "regioes": { "Norte": { "tristeza": 0, "alegria": 0, "amor": 0, "raiva": 0 }, "Nordeste": { "tristeza": 0, "alegria": 0, "amor": 0, "raiva": 0 }, "Centro-Oeste": { "tristeza": 0, "alegria": 0, "amor": 0, "raiva": 0 }, "Sul": { "tristeza": 0, "alegria": 0, "amor": 0, "raiva": 0 }, "Sudeste": { "tristeza": 0, "alegria": 0, "amor": 0, "raiva": 0 } } } } other_obj = { "regioes": { "Norte": { "tristeza": 0, "alegria": 0, "amor": 0, "raiva": 0, "count": 0 }, "Nordeste": { "tristeza": 0, "alegria": 0, "amor": 0, "raiva": 0, "count": 0 }, "Centro-Oeste": { "tristeza": 0, "alegria": 0, "amor": 0, "raiva": 0, "count": 0 }, "Sul": { "tristeza": 0, "alegria": 0, "amor": 0, "raiva": 0, "count": 0 }, "Sudeste": { "tristeza": 0, "alegria": 0, "amor": 0, "raiva": 0, "count": 0 } } } users_ref = db.collection(query) docs = users_ref.stream() jsonT = "" for doc in docs: jsonT = doc.to_dict()["porcentagem"] if jsonT == "": while count < maxCount: if max_id <= 0: searched_tweets = api.search(q=query + " -filter:retweets", lang="pt-br", tweet_mode='extended', count=maxCount * 5) else: searched_tweets = api.search(q=query + " -filter:retweets", lang="pt-br", tweet_mode='extended', count=maxCount * 5, max_id=str(max_id - 1)) if not searched_tweets: print("tem nada aq mona") break else: for tweet in searched_tweets: if (tweet.place is not None) and (count < maxCount): text = json.dumps( tweet._json['full_text'], sort_keys=True, indent=4, ensure_ascii=False).encode('utf8').decode() finalText = text.split(" ") text = "" for aux in finalText: if not '@' in aux and not 'https://' in aux: text += aux + " " count += 1 text = Cucco.replace_emojis(text) text = text.replace('"', '') municipio = (json.dumps( tweet._json['place']['full_name'], sort_keys=True, indent=4, ensure_ascii=False).encode('utf8') ).decode().split(",")[0].replace('"', "") try: if municipio == 'Sao Paulo': municipio = 'São Paulo' regiao = regioes.getRegion( ufbr.get_cidade(municipio).codigo) em = classify(text) other_obj["regioes"][regiao][em] += 1 other_obj["regioes"][regiao]["count"] += 1 pass except Exception as identifier: count -= 1 pass max_id = searched_tweets[-1].id arr_reg = ["Norte", "Nordeste", "Centro-Oeste", "Sul", "Sudeste"] arr_emo = ["tristeza", "alegria", "amor", "raiva"] for i in arr_reg: for j in arr_emo: total = other_obj["regioes"][i]["count"] if total == 0: obj[query]["regioes"][i][j] = 0 else: obj[query]["regioes"][i][j] = round( (other_obj["regioes"][i][j] / total) * 100, 2) db.collection(query).add({ "tweets_classificados": json.dumps(other_obj), "porcentagem": json.dumps(obj) }) objs = [obj, other_obj] return objs else: users_ref = db.collection(query) docs = users_ref.stream() jsonP = "" for doc in docs: jsonP = doc.to_dict()["porcentagem"] jsonT = doc.to_dict()["tweets_classificados"] arr = [json.loads(jsonP), json.loads(jsonT)] return arr
def upload_data(data): db = firestore.client() db.collection("daily_data").document(str(datetime.date.today())).set( format_data(data))
def __init__(self): self.robot = rosebot.RoseBot() cred = credentials.Certificate('serviceAccountKey.json') firebase_admin.initialize_app(cred) self.db = firestore.client() self.addCommandListener()
def periodicDBInsert(key): insert_SQL = '''INSERT INTO personEvent(fecha, tipo_marcado, id_sensor) VALUES(?, ?, ?)''' db = sqlite3.connect( '/home/pi/projects/personCounter/firebaseClient/local.db') c = db.cursor() global DB_INTERVAL global FALLA #/////////////////// global missed_events try: print("conectando a la DB...") cred = credentials.Certificate(key) firebase_admin.initialize_app(cred) dbFs = firestore.client() FAULT.off() FALLA = False except: FAULT.on() FALLA = True return # for sqlite while True: if eventQueue.empty() and not missed_events: print("no hay eventos!") else: print("insertando eventos...") # for event in events: # pushToLocalDB(db, event) # creando doc events = [] if not eventQueue.empty(): print("eventos nuevos en cola: ", eventQueue.qsize()) events = queue_get_all(eventQueue) eventQueue.task_done() try: print("eventos perdidos en cola: ", len(missed_events)) total_events = events + missed_events print("accediendo a coleccion...") doc_data = { 'marcados': total_events, 'id_evento': 1, } ###### events_sqlite = [] for event in total_events: events_sqlite.append( (event['fecha'], event['tipo_marcado'], event['id_sensor'])) c.executemany(insert_SQL, events_sqlite) print('ingresando datos a db local...') db.commit() ###### print('ingresando datos a db remota...') doc_ref = dbFs.collection(u'marcados_eventos').document( unicode(datetime.now())) doc_ref.set(doc_data) ################## events = [] missed_events = [] FAULT.off() FALLA = False print('actualizacion de db finalizada!') except Exception: print(Exception.message) print('salvando datos...') missed_events = events FAULT.on() FALLA = True #c.executemany(insert_SQL, events2) #db.commit() #select_last_events(db) events = [] time.sleep(DB_INTERVAL)
import os, firebase_admin from abc import ABCMeta, abstractclassmethod from firebase_admin import firestore, credentials path = os.path.dirname(os.path.abspath(__file__)).replace( '/features', '/utils') db = None try: cred = credentials.Certificate(path + '/gerajobsfirebase.json') app = firebase_admin.initialize_app(credential=cred) db = firestore.client(app=app) except Exception as e: print(str(e)) class Collection: @classmethod def add(self, data): pass @classmethod def where(self, *args, **kwargs): return Where() @classmethod def get(self): pass @classmethod def stream(self): pass
class MyFirebase(): wak = 'AIzaSyBL6nY4funOgC_Yiw0no4zOdixr0kWAOlM' db = firestore.client() def sign_up(self, email, password): #send email to firebase #firebase will return localid(userid), authtoken(idtoken), refreshtoken signup_url = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key=" + self.wak signup_payload = { "email": email, "password": password, "returnSecureToken": True } sign_up_request = requests.post(signup_url, data=signup_payload) print(sign_up_request.ok) print(sign_up_request.content.decode()) sign_up_data = json.loads(sign_up_request.content.decode()) if sign_up_request.ok == True: refresh_token = sign_up_data['refreshToken'] localId = sign_up_data['localId'] idToken = sign_up_data['idToken'] # Save refreshToken to a file with open("refresh_token.txt", "w") as f: f.write(refresh_token) # save local id to a variable in main app class App.get_running_app().local_id = localId App.get_running_app().id_token = idToken #create new key and add initial info: initial_data = {'avatar': 'fridge.png'} self.db.collection(localId).document(u'initial').set(initial_data) #send back to login screen App.get_running_app().root.current = 'login_screen' if sign_up_request.ok == False: error_data = json.loads(sign_up_request.content.decode()) error_message = error_data["error"]['message'] App.get_running_app().root.ids['sign_up_screen'].ids[ 'invalid_sign_up'].text = error_message pass def sign_in(self, email, password): """Called if a user tried to sign up and their email already existed.""" signin_url = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=" + self.wak signin_payload = { "email": email, "password": password, "returnSecureToken": True } signin_request = requests.post(signin_url, data=signin_payload) sign_up_data = json.loads(signin_request.content.decode()) app = App.get_running_app() if signin_request.ok == True: refresh_token = sign_up_data['refreshToken'] localId = sign_up_data['localId'] print(localId) idToken = sign_up_data['idToken'] # Save localId to a variable in main app class # Save idToken to a variable in main app class app.local_id = localId app.id_token = idToken app.signed_in() App.get_running_app().root.current = 'list_screen' print('true!') elif signin_request.ok == False: error_data = json.loads(signin_request.content.decode()) error_message = error_data["error"]['message'] app.root.ids['login_screen'].ids[ 'wrong_login'].text = error_message.replace("_", " ") def get_database_data(self, loginId): grocery_items = {} item_number = 0 docs = self.db.collection(loginId).stream() for doc in docs: if doc.id != 'initial': d = doc.to_dict() temp = {item_number: d} grocery_items.update(temp) item_number += 1 return grocery_items
def firestore_init(): cred = credentials.Certificate(os.environ['FIREBASE_AUTH_KEY']) firebase_admin.initialize_app(cred) return firestore.client()
import google.cloud.exceptions from tweepy.streaming import StreamListener from tweepy import OAuthHandler from tweepy import Stream import json from Sastrawi.Stemmer.StemmerFactory import StemmerFactory from collections import Counter akun = ['548904824', '255409050', '480224156', '63433517', '82552414', '61379637', '79994423', '47251716', '260043508'] # ['@IndosatCare','@Telkomsel','@myXLCare','@triindonesia','@myXL','@IM3Ooredoo','@AXISgsm','@ask_AXIS','@simPATI'] kata_kunci = ['lambat', 'lelet', 'lola', 'lemot', 'koneksi', 'gsm', '3g', '4g', 'hsdpa', 'edge', 'jaring', 'ganggu'] cred = credentials.Certificate('kunci2.json') firebase_admin.initialize_app(cred) db = firestore.client() tweet_ref = db.collection('Tweet') kata_ref = db.collection("kata_kunci") last_ref = db.collection("lasttweet") factory = StemmerFactory() stemmer = factory.create_stemmer() def tweetstruct(user, text, t): data = { 'username': user, 'text': text, 'time': t, } return data
def get_survey_firestore_client(): app = firebase_admin.get_app("surveys") return firestore.client(app)