def before_request(): g.db = MySQLDatabase(app.config['DATABASE_NAME'], host=app.config['DATABASE_HOST'], port=int(app.config['DATABASE_PORT']), user=app.config['DATABASE_USER'], passwd=app.config['DATABASE_PASSWORD']) db_proxy.initialize(g.db) g.db.connect() Text.create_table(fail_silently=True)
def analyze(): html = request.form.get('html', '') analysis_start = time() text, tokens, metrics = analyze_text(html) analysis_time = time() - analysis_start #Text.create(text=text, timestamp=datetime.now().replace(microsecond=0), **metrics) Text.create(timestamp=datetime.now().replace(microsecond=0), analysis_time=analysis_time, character_count=metrics['character_count'], word_count=metrics['word_count'], sentence_count=metrics['sentence_count']) return jsonify({'text': text, 'tokens': tokens, 'metrics': metrics})
def get_url(): """This route get the required info from form number two on profile.""" # saves the user_id to session user_id = session['user_id'] # gets all the requiremts for a text from the form the user submits. url = request.args.get("url") send_out_date = request.args.get("date") phone = request.args.get("phone") # checks if texts is alredy in db. Checks if text alredy is in db, if not it adds it to the db. text = db.session.query(Text).filter(Text.user_id == user_id, Text.phone == phone, Text.send_out_date == send_out_date, Text.url == url).first() # if text is not in db it adds it to the db. if text is None: text = Text(user_id=user_id, url=url, phone=phone, send_out_date=send_out_date) db.session.add(text) db.session.commit() return redirect("/profile") return redirect("/profile")
def get_keyword(): """Gets keyword, phone and date from a form and saves it to the db""" # saves the user_id to session user_id = session['user_id'] # gets all the requiremts for a text from the form the user submits. keyword = request.args.get("keyword") send_out_date = request.args.get("date") phone = request.args.get("phone") # checks if texts is alredy in db. Checks if text alredy is in db, if not it adds it to the db. text = db.session.query(Text).filter(Text.user_id == user_id, Text.phone == phone, Text.send_out_date == send_out_date, Text.keyword == keyword).first() # if text is not in db it adds it to the db. if text is None: text = Text(user_id=user_id, keyword=keyword, phone=phone, send_out_date=send_out_date) db.session.add(text) db.session.commit() flash("Text has been submitted!") return redirect("/profile") return redirect("/profile")
def gTrans(query, destlanguage='en', prompterror=True): log.info( "Using Google translate to determine the unknown translation of %s", query) # No meanings if we don't have a query if query == None: return None # No meanings if our query is blank after stripping HTML out query = utils.striphtml(query) if query.strip() == u"": return None try: # Return the meanings (or lack of them) directly return lookup(query, destlanguage) except urllib2.URLError, e: # The only 'meaning' should be an error telling the user that there was some problem log.exception("Error while trying to obtain Google response") if prompterror: return [[ Word(Text('<span style="color:gray">[Internet Error]</span>')) ]] else: return None
def load_texts(): """Load texts from u.data into database.""" Text.query.delete() for row in open("seed_data/u.text"): # removes all the extra white space from the rows and splits at the pipe (makes a list) row = row.rstrip().split("|") # I was getting this ("ValueError: too many values to unpack") so i slice the unpacking into 2. text_id, user_id, keyword = row[0:3] phone, url, send_out_date, sent = row[3:] text = Text(text_id=text_id, user_id=user_id, keyword=keyword, phone=phone, url=url, send_out_date=send_out_date, sent=sent) # need to have "add" to the session or it won't ever be stored db.session.add(text) # this will commit it db.session.commit() print "Texts"
def send_welcome_text(mobile, user_id): """Sends user welcome text message.""" client.messages.create(body="Wanda warmly welcomes you!", from_="+14159156178", to=mobile) pacific = pytz.timezone('US/Pacific') text = Text(sent_time=datetime.now(tz=pacific).replace(tzinfo=None), user_id=user_id) db.session.add(text) db.session.commit()
def random_items(): data = request.get_json() app_uuid = data["app_uuid"] if not Application.is_valid(app_uuid): abort(401) random_query = Text().select().order_by(fn.Random()) text = random_query.get() item = {} item["id"] = text.uuid item["text"] = text.text app.logger.info("serving item {0}".format(item["id"])) return jsonify({"item": item})
def try_process_file(self): ''' Checks if file at the file path provided by the user can be opened. If no, shows error message. If yes, proceeds with processing the file. ''' if not self.filename: showerror("Open Source File", "No file selected") return elif self.filename: try: # raw_text is one string that contains all the text in the file. self.raw_text = Text.Text(self.filename).text self.process_file() except: showerror("Open Source File", "Failed to read file\n'%s'" % self.filename)
def send_survey(mobile, user_id): """Sends user message with 3 survey questions.""" text = ("Hi there! This is your Wanda check in. " "Please respond to these questions with 2 numbers, separated by commas." "\n\n1. Was your mind wandering right before this text? (yes=1, no=2)" "\n2. On a scale of 1 to 10, how are you feeling?") client.messages.create(body=text, from_="+14159156178", to=mobile) pacific = pytz.timezone('US/Pacific') text = Text(sent_time=datetime.now(tz=pacific).replace(tzinfo=None), user_id=user_id) db.session.add(text) db.session.commit()
def load_texts(): """Loads texts into db.""" i = 0 while i < 112: day = randint(1, 30) hour = randint(8, 19) minute = randint(0, 54) second = randint(0, 59) text = Text(sent_time=datetime( 2018, 4, day, hour, minute, second, 670712), user_id=1) db.session.add(text) text_times.append(datetime(2018, 4, day, hour, minute, second, 670712)) response_times.append(datetime(2018, 4, day, hour, minute + 5, second, 670712)) i += 1 db.session.commit()
log.exception("Error while trying to obtain Google response") if prompterror: return [[ Word(Text('<span style="color:gray">[Internet Error]</span>')) ]] else: return None except ValueError, e: # Not an internet problem log.exception( "Error while interpreting translation response from Google") if prompterror: return [[ Word( Text( '<span style="color:gray">[Error In Google Translate Response]</span>' )) ]] else: return None # This function will send a sample query to Google Translate and return true or false depending on success # It is used to determine connectivity for the Anki session (and thus whether Pinyin Toolkit should use online services or not) def gCheck(destlanguage='en'): try: lookup(u"好", destlanguage) return True except urllib2.URLError: return False except ValueError: