def monitor_phrases(): if request.method == "POST": data = request.form phrase = data["phrase"] u_id = request.cookies.get('user_id') ts = time.gmtime() timestamp = time.strftime("%x", ts) try: db.execute( "INSERT INTO stream_phrases (user_id,phrase,created_date) VALUES (:u,:p,:c)", { "u": u_id, "p": phrase, "c": timestamp }) db.commit() except Exception as e: print_err(e) db.rollback() flash("Phrase already exists", "error") result = db.execute("SELECT * from stream_phrases").fetchall() for row in result: print("User: "******"Phrase:", row.phrase, "Created_date:", row.created_date) return render_template('monitor_phrases.html', result=results)
def on_status(self, status): try: tweet_id=status.id tweet = status status = tweet.text print("status: ", status) if status.lower().startswith("rt @"): print("Retweets are ignored") else: print("dealing with phrase") phrases = [row["phrase"] for row in phrase_objects] #get phrase from twitter status selected_phrase = extract_phrase(status, phrases) selected_object = None #fetch phrase id from db for phrase_object in phrase_objects: if phrase_object["phrase"].lower() == selected_phrase.lower(): selected_object = phrase_object break db.create_occurrence(selected_object["id"]) print("occurrence of phrase: ",selected_phrase, " created...") except Exception as e: print_err(e)
def edit_phrase(id): if request.method == "POST": try: data = request.form phrase = data["phrase"] db.execute( "UPDATE stream_phrases SET phrase = :phrase WHERE id = :id", { "phrase": phrase, "id": id }) db.commit() flash("Phrase updated successfully", "success") return redirect('/phrases') except Exception as e: print_err(e) db.rollback() flash("Error updating phrase", "error") phrase = db.execute("SELECT * from stream_phrases WHERE id = :id", { "id": id }).fetchone() print("PHRASE: ", phrase) return render_template('edit_phrase.html', phrase=phrase)