Example #1
0
def vote():
    """

    /vote_up?post_id=123
    """
    # Lese GET-Parameter
    method = request.args.get("method")
    post_id = request.args.get("post_id")

    if post_id == "" or method == "":
        return render_template(
            "quick_info.html",
            info_text="Ungültige Post ID oder Zugriffsmethode!")

    # Hole Post aus DB
    db_handler = DB_Handler()
    (code, data) = db_handler.get_post_by_pid(mysql, post_id)

    if code == -1:
        return render_template(
            "quick_info.html",
            info_text="Wir konnten leider keinen Post mit der ID " +
            str(post_id) + " finden!")

    method_labels = {"upvote": "Upvote", "downvote": "Downvote"}
    csrf_seq = generate_verification_token(8)

    # Gebe Seite mit vollständigem Post zurück
    # Präsentiere zufällige Zeichenfolge, die eingegeben werden muss, um CSRF-Attacken zu unterbinden
    return render_template("vote.html",
                           post=data,
                           csrf_seq=csrf_seq,
                           method=method,
                           method_label=method_labels[method])
Example #2
0
def handle_login():
    login_email = request.form["login-email"]
    login_password = request.form["login-password"]

    # Check for correct parameter format
    if not check_email(login_email) or not check_password(login_password):
        return render_template(
            "error.html", message="Please enter a valid E-mail and password.")

    db_handler = DB_Handler()
    user_record = db_handler.check_for_user_existence(mysql, login_email)

    if user_record == None:
        return render_template("login_failed.html")

    # Check for password equality
    if not check_password_hash(user_record["password"], str(login_password)):
        return render_template("login_failed.html")

    session[SESSIONV_LOGGED_IN] = True
    session[SESSIONV_USER] = login_email
    session[SESSIONV_UID] = user_record["uid"]
    session[SESSIONV_USERNAME] = user_record["username"]

    # Set admin=True if the stored role_id of the user is equal to the configured board admin role_id

    if check_config_entry("role_id-board-administrator",
                          str(user_record["role_id"]),
                          check_values=True):
        session[SESSIONV_ADMIN] = True

    return render_template("login_success.html", user=user_record["username"])
Example #3
0
class TwitterAccessor():
    def __init__(self, keyfile="twitter_keys.txt"):
        with open(keyfile, "r") as f:
            consumer_key = f.readline().strip()
            consumer_secret = f.readline().strip()
            access_token = f.readline().strip()
            access_token_secret = f.readline().strip()
        auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
        auth.set_access_token(access_token, access_token_secret)
        self.api = tweepy.API(auth)
        self.dbhandler = DB_Handler()

    def clean_tweet(self, tweet):
        text = re.sub(' https://t.co/[A-Za-z0-9]{10}', '', tweet.text)
        try:
            images = [
                t['media_url'] for t in tweet.entities['media']
                if t['type'] == 'photo'
            ]
        except:
            images = []
        time_posted = tweet.created_at  #datetime.strptime(tweet.created_at, "%a %b %d %H:%M:%S %z %Y")
        return {'time': time_posted, 'text': text, 'images': images}

    def get_clean_tweets_in_date_range(self, start, end):
        tweets = tweepy.Cursor(self.api.user_timeline).items()
        return [
            self.clean_tweet(t) for t in tweets
            if self.clean_tweet(t)['time'] > start
            and self.clean_tweet(t)['time'] < end
        ]

    def get_tweets_in_date_range(self, start, end):
        tweets = tweepy.Cursor(self.api.user_timeline).items()
        return [
            t for t in tweets if self.clean_tweet(t)['time'] > start
            and self.clean_tweet(t)['time'] < end
        ]

    def get_my_info(self):
        return self.api.me()

    def get_friend_names(self):
        friends = []
        for friend in tweepy.Cursor(self.api.friends).items():
            friends.append(str(friend.name))
        return friends

    def get_my_tweets(self):
        tweets = tweepy.Cursor(self.api.user_timeline).items()
        return [t for t in tweets]

    def add_to_database(self, tweet):
        self.dbhandler.add_media('t',
                                 time_posted=tweet['time'],
                                 text=tweet['text'],
                                 image=None)  # update this (!)
Example #4
0
 def __init__(self, keyfile="twitter_keys.txt"):
     with open(keyfile, "r") as f:
         consumer_key = f.readline().strip()
         consumer_secret = f.readline().strip()
         access_token = f.readline().strip()
         access_token_secret = f.readline().strip()
     auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
     auth.set_access_token(access_token, access_token_secret)
     self.api = tweepy.API(auth)
     self.dbhandler = DB_Handler()
Example #5
0
def login():
    """
    tbd
    """
    # Falls bereits eingeloggt
    try:
        session["logged_in"]
        return render_template("Du bist bereits eingeloggt!")
    except:
        pass

    # Hole Daten aus Loginform
    if request.method == "GET":
        return_info = {}
        return_info["invalid_method"] = "GET"

        return prepare_info_json(url_for("post_user"),
                                 "GET ist unzulässig für den Login",
                                 return_info)

    login_email = request.form["login_email"]
    login_password = request.form["login_password"]

    if login_email == "" or login_password == "":
        return prepare_info_json(url_for("post_user"),
                                 "Es wurden Felder beim Login leergelassen")

    # Hashe Passwort
    login_password_hashed = generate_password_hash(login_password)

    # Suche nach User in DB
    db_handler = DB_Handler()
    (code, data) = db_handler.check_for_existence(mysql, login_email)
    if code == -1:
        return render_template(
            "quick_info.html",
            info_text="Passwort und/oder Benutzername sind inkorrekt!")
    app.logger.debug("user found=" + data["email"] + ":" + data["password"])

    # Überprüfe gehashte Passwörter
    if not check_password_hash(data["password"], login_password):
        return render_template(
            "quick_info.html",
            info_text="Benutzername und/oder Passwort sind inkorrekt!")
    else:
        # Setze Sessionvariable
        session["logged_in"] = True
        session["user"] = login_email
        session["uid"] = data["uid"]
        session["role_id"] = data["role_id"]
        return render_template(
            "quick_info.html",
            info_text="Du wurdest eingeloggt. Willkommen zurück, " +
            login_email)
Example #6
0
def main():
    # def on_press(key):s
    #     listener.stop()
    #     if key == KeyCode.from_char('s'):
    #         db_handler = DB_Handler(data_dir="H:\Data_Collector\Data", scene_dir="test1")
    #         db_handler.colecting_data()
    # with Listener(on_press=on_press) as listener:
    #     listener.join()1

    db_handler = DB_Handler(data_dir="H:\Data_Collector\Data",
                            scene_dir="set_1")
    db_handler.colecting_data(db_set="training",
                              for_window_name="World of Warcraft")
Example #7
0
def finish_vote():
    """
    tbd
    """
    # Hole GET-Parameter
    csrf_token = request.args.get("csrf_token")
    post_id = request.args.get("post_id")
    method = request.args.get("method")
    input_csrf = request.form["vote_code"]

    try:
        post_id_int = int(post_id)
    except:
        return render_template(
            "quick_info.html",
            info_text="Deine Anfrage war ungültig. Bitte versuche es erneut!")

    if input_csrf == "" or csrf_token == "" or post_id == "" or post_id_int < 0 or not method in [
            "upvote", "downvote"
    ]:
        return render_template(
            "quick_info.html",
            info_text="Deine Anfrage war ungültig. Bitte versuche es erneut!")
    # Überprüfe csrf_seq
    if not csrf_token == input_csrf:
        return render_template(
            "quick_info.html",
            info_text=
            "Der eingegebene Code war leider falsch. Bitte versuche es erneut!"
        )

    # Persistiere Vote
    db_handler = DB_Handler()

    if method == "upvote":
        success = db_handler.do_upvote(mysql, post_id)
        if success == -1:
            return render_template(
                "quick_info.html",
                info_text="Etwas ist schiefgelaufen! Versuche es erneut!")
        return render_template("quick_info.html",
                               info_text="Upvote erfolgreich!")

    elif method == "downvote":
        success = db_handler.do_downvote(mysql, post_id)
        if success == -1:
            return render_template(
                "quick_info.html",
                info_text="Etwas ist schiefgelaufen! Versuche es erneut!")
        return render_template("quick_info.html",
                               info_text="Downvote erfolgreich!")
Example #8
0
def index():
    try:
        session[SESSIONV_LOGGED_IN]
    except:
        # If the user is not logged in and the board is configured to only display the forum to logged in users
        if config_handler.check_config_entry("board-access-mode",
                                             "login-required",
                                             check_values=True):
            return redirect("login")

    board_content = []

    db_handler = DB_Handler()
    section_list = db_handler.get_section_list(mysql)

    for section in section_list:
        """
        Multiple temps will be appended to the board_content object. 
        Each temp object is a dict that will consist of the title of 
        the section and contain a list under the "subsection_list" key that will
        contain a list of all the subsections of the current section.
        """
        temp = {}
        temp["title"] = str(section[1])
        temp["section_id"] = section[0]
        all_subsections = db_handler.get_subsections_for_section_id(
            mysql, int(section[0]))
        subsection_list = []

        for subsection in all_subsections:
            # Dictionary that contains the meta data for a subsection
            ss_dict = {}
            ss_dict["subsection_id"] = subsection[0]
            ss_dict["subsection_name"] = subsection[1]
            ss_dict["subsection_desc"] = subsection[2]

            subsection_list.append(ss_dict)

        temp["subsection_list"] = subsection_list
        print(temp)

        # For each iteration for every section append the section and its subsections to the board content that is displayed on the index page
        board_content.append(temp)

    return render_template("board.html", section_list=board_content)
Example #9
0
def send_verification_email(reg_email):
    """
        Sende Bestätigungsmail mit Verification Token.
    """

    app.logger.debug("Sende Bestätigungsmail.")

    # Hole Verification Token aus der DB
    db_handler = DB_Handler()
    (success, token) = db_handler.get_token_for_user(mysql, reg_email)

    if success == -1:
        return -1

    # Sende Bestätigungsemail mit Token
    sender = "*****@*****.**"
    password = "******"

    msg = MIMEMultipart()
    msg["From"] = sender
    msg["To"] = reg_email
    msg["Subject"] = "Bestätige deinen Account bei Tralala!"

    msg.attach(MIMEText(u"Hallo " + reg_email + "!</br>" \
                                                "Benutze den folgenden Link, um deinen Account zu bestaetigen. Du musst diesen in die Adresszeile deines Browsers kopieren.</br></br>" \
                                                "<a href=\"localhost:5000" + url_for(
        "confirm") + "?token=" + token + "\">" + "localhost:5000" + url_for(
        "confirm") + "?token=" + token + "</a>",
                        "html"))

    try:
        server = smtplib.SMTP("mail.gmx.net", 587)
        server.starttls()
        server.login(sender, password)

        server.sendmail(sender, reg_email, msg.as_string())
        server.quit()
    except Exception as e:
        app.logger.error("Fehler beim Senden der Bestätigungsmail...\n" +
                         str(e))
        return -1

    app.logger.debug("Bestätigungsemail gesendet an '" + reg_email + "' ...")
    return 1
Example #10
0
def confirm():
    """
    tbd
    """
    # Suche nach Email basierend auf Token
    token = request.args.get("token")

    db_handler = DB_Handler()
    (success, email) = db_handler.get_user_for_token(mysql, token)
    if success == -1:
        app.logger.error("Es konnte kein User for das Token '" + token +
                         "' zurückgeliefert werden (ungültiges Token)")
        return render_template(
            "quick_info.html",
            info_text="Der Benutzer konnte nicht bestätigt werden!")
    if success == 2:
        app.logger.debug("Benutzer ist bereits bestätigt!")
        return render_template(
            "quick_info.html",
            info_text="Der Benutzer wurde bereits bestätigt!")
    if success == 1:
        app.logger.debug("Account bestätigt für Benutzer '" + email +
                         "' für Token '" + token + "'")

    # Setze Token auf Defaultwert und setze verified auf 1
    success = db_handler.user_successful_verify(mysql, email)

    if success == -1:
        app.logger.error(
            "User konnte nicht bestätigt werden (Benutzer konnte in der DB nicht bestätigt werden)"
        )
        return render_template(
            "quick_info.html",
            info_text="Der Benutzer konnte nicht bestätigt werden!")
    if success == 1:
        app.logger.debug("User wurde bestätigt")
        return render_template(
            "quick_info.html",
            info_text=
            "Der Benutzer wurde erfolgreich bestätigt. Du kannst dich nun einloggen."
        )
Example #11
0
def index():
    """
    Startseite
    Hier muss ebenfalls die Darstellung aller Posts aus der DB behandelt werden
    """

    db_handler = DB_Handler()
    (code, data) = db_handler.get_all_posts(mysql)

    if code == -1:
        return render_template("index.html",
                               error_message="Keine Posts gefunden!")

    colors = ["red", "blue", "green", "yellow"]

    post_list = []

    for row in data:
        color_key = random.randint(0, 3)
        upvotes = int(row[5])
        downvotes = -int(row[6])  # Vorzeichenwechsel
        total_votes = upvotes + downvotes

        html_trans = ""
        html_trans += "<div class=\"" + colors[color_key] + "\">"
        html_trans += "<div id=\"usr\">" + str(row[1]) + " | " + str(
            row[2]) + " | <b>Votes: " + str(total_votes) + "</b></div>"
        html_trans += "<p>" + row[3] + "<p>"
        html_trans += "</br></br>"
        html_trans += "<div><b>" + row[4] + "</b>&nbsp;&nbsp;&nbsp;"
        html_trans += "<a class=\"post_vote_up\" href=\"" + url_for(
            "vote") + "?method=upvote&post_id=" + str(
                row[0]) + "\">+</a>&nbsp;&nbsp;&nbsp;"
        html_trans += "<a class=\"post_vote_down\" href=\"" + url_for(
            "vote") + "?method=downvote&post_id=" + str(
                row[0]) + "\">-</a>&nbsp;&nbsp;&nbsp;</div>"
        html_trans += "</div>"
        post_list.append(html_trans)

    return render_template("index.html", post_list=post_list)
Example #12
0
def post_message():
    try:
        session[
            "logged_in"]  # Nur eingeloggte Benutzer dürfen Nachrichten posten
    except:
        return render_template(
            "quick_info.html",
            info_text="Du musst eingeloggt sein, um eine Nachricht zu posten!")

    # Post sanitizen
    message = request.form["post_message"]
    hashtags = request.form["post_hashtags"]

    if message == "":
        return render_template(
            "quick_info.html",
            info_text=
            "Leider konnte deine Nachricht nicht gepostet werden, da du keine Nachricht angegeben hast. Versuche es bitte erneut!"
        )

    # Post in DB schreiben
    db_handler = DB_Handler()
    success = db_handler.post_message_to_db(mysql, session["uid"], None,
                                            message[:279], hashtags)

    if success == -1:
        return render_template(
            "quick_info.html",
            info_text=
            "Deine Nachricht konnte nicht geposted werden. Versuche es erneut!"
        )
    elif success == 1:
        return render_template(
            "quick_info.html",
            info_text=
            "Deine Nachricht wurde geposted. Du kannst sie auf der Postseite nun sehen!"
        )

    return "post message uid=" + str(
        session["uid"]) + " message=" + message + " hashtags=" + hashtags
Example #13
0
class test:
    def __init__(self):
        print ">>> {}".format("Talk to me!")

        self.DBH = DB_Handler()

    def example(self):
        # print(">>> Getting all the phrases")

        phrases = self.DBH.get_all_phrases()

        #-1 because len doesn't start counting from 0..
        print(phrases[randint(0, len(phrases) - 1)][1])
Example #14
0
def admin_dashboard():
    """
    tbd
    """
    # Benutzerdaten holen
    db_handler = DB_Handler()
    (code, data) = db_handler.get_all_users(mysql)

    if not code == 1:
        return render_template(
            "admin.html",
            error=
            "Admin Dashboard konnte nicht geladen werden. Versuche es später noch einmal."
        )

    admin_table = ""
    admin_table += "<table>"
    admin_table += "<tr>" \
                   "<th>ID</th>" \
                   "<th>E-Mail</th>" \
                   "<th>Benutzer löschen</th>" \
                   "<th>Rolle ändern</th>" \
                   "</tr>"

    for row in data:
        admin_table += "<tr>"
        admin_table += "<td>" + str(row[1]) + "</td>"
        admin_table += "<td>" + str(row[0]) + "</td>"
        admin_table += "<td>" + "Benutzer löschen" + "</td>"
        admin_table += "<td>" + "Rolle ändern" + "</td>"
        admin_table += "</tr>"

    admin_table += "</table>"

    return render_template("admin.html", dashboard=admin_table)

    # In Dashboard eintragen

    return render_template("admin.html")
Example #15
0
    def __init__(self, user_id='12354', dbs_path='./dbs', verbose=True):
        """ Starts a new bot
            db_name: usually will be an chat_id
            """

        self.user_id = str(user_id)
        self.dbs_path = dbs_path
        self.verbose = verbose

        # Starts a new handler for the DB corresponding to this user.
        self.dbh = DB_Handler(dbs_path=self.dbs_path,
                              db_name=self.user_id,
                              verbose=self.verbose)

        # Instantiate Sentence_parser object for natural lenguaje processing
        self.sp = Sentence_parser()

        # Object States, for acount Bot States
        self.state = States()

        # Load the private data if were found
        self.load_personal_info()

        return None
Example #16
0
def handle_register():
    reg_email = clean_text(request.form["reg-email"])
    reg_username = clean_text(request.form["reg-username"])
    reg_password = clean_text(request.form["reg-password"])
    reg_password_confirm = clean_text(request.form["reg-password-confirm"])

    # If the e-mail format was incorrect.
    if not check_email(reg_email):
        return render_template("register_failed.html",
                               message="Please enter a valid E-Mail address.")

    # If the username format was incorrect.
    if not check_text(reg_username):
        return render_template("register_failed.html",
                               message="Please enter a valid username.")

    # If the passwords did not match
    if not reg_password == reg_password_confirm:
        return render_template(
            "register_failed.html",
            message="The passwords did not match. Please try again.")

    # If the password format (i.e. strength) was incorrect.
    if not check_password(reg_password) or not check_password(
            reg_password_confirm):
        return render_template(
            "register_failed.html",
            message=
            "Please enter a valid password. You password must at least contain of 6 characters."
        )

    db_handler = DB_Handler()

    user_data = {}
    user_data["username"] = reg_username
    user_data["email"] = reg_email
    user_data["password"] = generate_password_hash(reg_password)

    user_added, e = db_handler.add_new_user(mysql, user_data)

    # Catch internal error
    if user_added is False:
        logger.error("Error at registering new user " + str(reg_email) +
                     " with exception:\n" + str(e))
        return render_template(
            "register_failed.html",
            message=
            "The new user could not be added due to a internal error. Please try again. If the problem continues to exists, please get in touch with a support member."
        )

    # If the user is already registered
    if user_added is None:
        return render_template(
            "register_failed.html",
            message="The user already exists. You can login <a href=\"" +
            url_for("login") + "\">here</a>.")

    logger.success("New registration: " + str(reg_email))
    return render_template(
        "register_success.html",
        message=
        "Your registration has been successful. You can login <b><a href=\"" +
        url_for("login") + "\">here</a></b>.")
Example #17
0
def register_new_account(mysql, email, pw_hash, verification_token):
    db_handler = DB_Handler()
    success = db_handler.add_new_user(mysql, email, pw_hash,
                                      verification_token)

    return success
Example #18
0
class MrExerciseBot():
    """ Main class for the bot """
    def __init__(self, user_id='12354', dbs_path='./dbs', verbose=True):
        """ Starts a new bot
            db_name: usually will be an chat_id
            """

        self.user_id = str(user_id)
        self.dbs_path = dbs_path
        self.verbose = verbose

        # Starts a new handler for the DB corresponding to this user.
        self.dbh = DB_Handler(dbs_path=self.dbs_path,
                              db_name=self.user_id,
                              verbose=self.verbose)

        # Instantiate Sentence_parser object for natural lenguaje processing
        self.sp = Sentence_parser()

        # Object States, for acount Bot States
        self.state = States()

        # Load the private data if were found
        self.load_personal_info()

        return None

    def load_personal_info(self):
        """ Loads personal info from user's db
            """

        priv_data = self.dbh.get_private_data()

        if priv_data is None:
            if self.verbose:
                print(
                    " - MrExerciseBot, load_personal_info: there isn't any private data for the user_id: {} yet."
                    .format(self.user_id))

            self.personal_info_d = {
                'name': '',
                'default_sport': '',
                'last_state': States.ON_START,
                'complete': False
            }
        else:
            self.personal_info_d = priv_data
            self.state.set_state(self.personal_info_d['last_state'])

        return None

    def save_personal_info(self):
        """ Saves prersonal info to user's db
            """
        # Remembering the state not the step
        self.personal_info_d['last_state'] = self.state.get_state()[0]

        # Saveing the data
        self.dbh.set_private_data(self.personal_info_d)

        return None

    def on_start(self):
        to_resp_v = []

        self.state.set_state(States.ON_START)
        to_resp_v += self.query()

        return to_resp_v

    def exec_personal_info(self, q=''):
        """ Executes steps for personal info retrival """

        to_resp_v = []

        # Get the state and the i_step
        state, i_step = self.state.get_state()

        if i_step == 0:
            to_resp_v += corpus_d['personal_info'][i_step]
            self.state.step()

        elif i_step == 1:
            name = self.sp.find_name(q)
            if name:
                self.personal_info_d['name'] = name
                to_resp_v += corpus_d['personal_info'][i_step]
                self.state.step()
            else:
                to_resp_v += corpus_d['personal_info_err'][i_step]

        elif i_step == 2:
            default_sport = self.sp.find_exercise(q)

            if default_sport:
                self.personal_info_d['default_sport'] = default_sport
                to_resp_v += corpus_d['personal_info'][i_step]

                self.state.step()
            else:
                to_resp_v += corpus_d['personal_info_err'][i_step]

        elif i_step == 3:
            resp = self.sp.yes_no_question(q)

            if resp == True:
                # Personal info will be saved
                self.personal_info_d['complete'] = True
                self.save_personal_info()

                to_resp_v += corpus_d['personal_info'][i_step]

                self.state.set_state(States.ON_ACTION_SELECT)
                to_resp_v += self.query()

            elif resp == False:
                to_resp_v += corpus_d['personal_info_err'][i_step]
                self.state.step_back()

            else:
                to_resp_v += corpus_d['personal_info_err'][i_step + 1]

        return to_resp_v

    def exec_rec(self, q=''):
        """ Executes steps for record a training"""

        to_resp_v = []

        # Get the state and the i_step
        state, i_step = self.state.get_state()

        if i_step == 0:
            to_resp_v += corpus_d['record'][i_step]
            self.to_record_d = {
                'date': None,
                'activity': self.personal_info_d['default_sport'],
                'time': 0,
                'act_mode': 1
            }
            self.state.step()

        elif i_step == 1:

            use_default = self.sp.yes_no_question(q)

            if use_default == True:
                to_resp_v += corpus_d['record'][i_step + 1]
                self.state.step()
                self.state.step()

            elif use_default == False:
                to_resp_v += corpus_d['record'][i_step]
                self.state.step()

            else:
                to_resp_v += corpus_d['record_err'][i_step]

        elif i_step == 2:
            sport = self.sp.find_exercise(q)

            if sport:
                self.to_record_d['activity'] = sport
                to_resp_v += corpus_d['record'][i_step]
                self.state.step()
            else:
                to_resp_v += corpus_d['record_err'][i_step]

        elif i_step == 3:
            self.to_record_d['time'] = self.sp.find_mins(q)

            if self.to_record_d['time'] > 0:
                to_resp_v += corpus_d['record'][i_step]
                self.state.step()
            else:
                to_resp_v += corpus_d['record_err'][4]

        elif i_step == 4:
            self.to_record_d['act_mode'] = self.sp.find_difficulty(q)
            if 1 <= self.to_record_d['act_mode'] <= 10:
                to_resp_v += [
                    dialog.format(**self.to_record_d)
                    for dialog in corpus_d['record'][i_step]
                ]
                self.state.step()
            else:
                to_resp_v += corpus_d['record_err'][5]

        elif i_step == 5:
            do_save = self.sp.yes_no_question(q)

            if do_save == True:
                self.dbh.add_record(**self.to_record_d)
                self.state.set_state(States.ON_ACTION_SELECT)

                # Personal info will be saved
                self.save_personal_info()

                to_resp_v += corpus_d['record'][i_step]
                to_resp_v += self.query()

            elif do_save == False:
                self.state.set_state(States.ON_REC)

                to_resp_v += corpus_d['record_err'][3]
                to_resp_v += self.query()

            else:
                to_resp_v += corpus_d['record_err'][1]

        return to_resp_v

    def exec_action_select(self, q=''):
        """ Executes action select dialog. """

        to_resp_v = []

        # Get the state and the i_step
        state, i_step = self.state.get_state()

        if i_step == 0:
            to_resp_v += corpus_d['actions'][0]
            self.state.step()

        elif i_step == 1:
            action = self.sp.intension_detector(q)
            if action == 'personal_info':
                self.state.set_state(States.ON_PERSONAL_INFO)
                to_resp_v += self.query(q)

            elif action == 'help':
                self.state.set_state(States.ON_HELP)
                to_resp_v += self.query(q)

            elif action == 'record':
                self.state.set_state(States.ON_REC)
                to_resp_v += self.query(q)

            elif action == 'stats':
                self.state.set_state(States.ON_STATS)
                to_resp_v += self.query(q)

            elif action == 'delete':
                self.state.set_state(States.ON_DELETE)
                to_resp_v += self.query(q)
            else:
                to_resp_v += corpus_d['actions'][-1]

        return to_resp_v

    def exec_delete(self, q=''):
        """ Deletes all dbs for the user """

        to_resp_v = []

        # Get the state and the i_step
        state, i_step = self.state.get_state()

        if i_step == 0:
            to_resp_v += corpus_d['delete'][i_step]
            self.state.step()

        elif i_step == 1:
            do_delete = self.sp.yes_no_question(q)

            if do_delete == True:
                to_resp_v += corpus_d['delete'][i_step]
                self.state.set_state(States.ON_END)
                self.dbh.delete_db()
                to_resp_v += self.query(q)

            elif do_delete == False:
                to_resp_v += corpus_d['delete'][i_step + 1]
                self.state.set_state(States.ON_ACTION_SELECT)
                to_resp_v += self.query(q)

            else:
                to_resp_v += corpus_d['delete_err'][i_step]

        return to_resp_v

    def exec_help(self, q):
        to_resp_v = []

        to_resp_v += corpus_d['help'][0]
        self.state.set_state(States.ON_ACTION_SELECT)

        to_resp_v += self.query(q)

        return to_resp_v

    def exec_stats(self, q):
        to_resp_v = []

        stats_v = self.dbh.get_stats()

        if len(stats_v) == 0:
            to_resp_v += corpus_d['stats'][-1]
        else:
            to_resp_v += corpus_d['stats'][0]
            for stats_d in stats_v:
                to_resp_v.append(corpus_d['stats'][1][0].format(**stats_d))

        self.state.set_state(States.ON_ACTION_SELECT)

        to_resp_v += self.query(q)

        return to_resp_v

    def exec_cancel(self, q):
        to_resp_v = []

        self.load_personal_info()
        if self.personal_info_d['complete']:
            to_resp_v += corpus_d['cancel'][0]
            self.state.set_state(States.ON_ACTION_SELECT)
        else:
            to_resp_v += corpus_d['cancel_err'][0]
            self.state.set_state(States.ON_PERSONAL_INFO)

        to_resp_v += self.query()

        return to_resp_v

    def exec_end(self, q):
        to_resp_v = []

        to_resp_v += corpus_d['end'][0]

        return to_resp_v

    def query(self, q=''):
        to_resp_v = []

        # First of all let's find if there is a cancel state
        if self.sp.intension_detector(q, just_cancel=True) == 'cancel':
            self.state.set_state(States.ON_CANCEL)

        state, i_step = self.state.get_state()
        if state == States.ON_START:
            if self.verbose:
                print(' - MrExerciseBot, query: Selecting ON_START.')

            self.load_personal_info()
            if self.personal_info_d['complete']:
                to_resp_v += corpus_d['on_start'][0]
            else:
                to_resp_v += corpus_d['on_start'][0]
                to_resp_v += corpus_d['on_start'][1]

            if self.personal_info_d['complete']:
                self.state.set_state(States.ON_ACTION_SELECT)
            else:
                self.state.set_state(States.ON_PERSONAL_INFO)

            to_resp_v += self.query(q)

        elif state == States.ON_PERSONAL_INFO:
            if self.verbose:
                print(' - MrExerciseBot, query: Selecting ON_PERSONAL_INFO.')

            to_resp_v += self.exec_personal_info(q)

        elif state == States.ON_ACTION_SELECT:
            if self.verbose:
                print(' - MrExerciseBot, query: Selecting ON_ACTION_SELECT.')

            to_resp_v += self.exec_action_select(q)

        elif state == States.ON_HELP:
            if self.verbose:
                print(' - MrExerciseBot, query: Selecting ON_HELP.')

            to_resp_v += self.exec_help(q)

        elif state == States.ON_DELETE:
            if self.verbose:
                print(' - MrExerciseBot, query: Selecting ON_DELETE.')

            to_resp_v += self.exec_delete(q)

        elif state == States.ON_REC:
            if self.verbose:
                print(' - MrExerciseBot, query: Selecting ON_REC.')

            to_resp_v += self.exec_rec(q)

        elif state == States.ON_STATS:
            if self.verbose:
                print(' - MrExerciseBot, query: Selecting ON_STATS.')

            to_resp_v += self.exec_stats(q)

        elif state == States.ON_CANCEL:
            if self.verbose:
                print(' - MrExerciseBot, query: Selecting ON_CANCEL.')

            to_resp_v += self.exec_cancel(q)

        elif state == States.ON_END:
            if self.verbose:
                print(' - MrExerciseBot, query: Selecting ON_END.')

            to_resp_v += self.exec_end(q)

        else:
            to_resp_v += ['STATE NOT IMPLEMENTED: ' + str(self.state)]

        # Reemplaze all the private data in the response
        for i in range(len(to_resp_v)):
            to_resp_v[i] = to_resp_v[i].format(**self.personal_info_d)


##        # last_state variable needs to be saved
##        self.save_personal_info()

        return to_resp_v
from threading import Thread


from tqdm import tqdm
from bs4 import BeautifulSoup

from db_handler import DB_Handler
from ocr_handler import OCR_Handler
from ner_handler import NER_Handler
from solr_handler import Solr_Handler

config = configparser.ConfigParser()
config.read('settings.ini')
DATASET_URL = config["DEFAULT"]["dataset_url"]

db_gazete = DB_Handler("gazete")
db_page = DB_Handler("page")
ner = NER_Handler()
#solr = Solr_Handler()

# Queue for multithreading
row_queue = Queue()

def createDirectory(dirPath):
    if not os.path.isdir(dirPath):
        os.mkdir(dirPath)

def write_to_text(paper_json,paper_name):
    dirPath = './Papers'
    filePath = '{}/{}.txt'.format(dirPath,paper_name)
    createDirectory(dirPath)
Example #20
0
    def __init__(self):
        print ">>> {}".format("Talk to me!")

        self.DBH = DB_Handler()
Example #21
0
def main():
    # Training settings
    parser = argparse.ArgumentParser(description='PyTorch MNIST Example')
    parser.add_argument('--batch-size',
                        type=int,
                        default=30,
                        metavar='N',
                        help='input batch size for set_1 (default: 64)')
    parser.add_argument('--test-batch-size',
                        type=int,
                        default=100,
                        metavar='N',
                        help='input batch size for testing (default: 1000)')
    parser.add_argument('--epochs',
                        type=int,
                        default=1000,
                        metavar='N',
                        help='number of epochs to train (default: 14)')
    parser.add_argument('--lr',
                        type=float,
                        default=1.0,
                        metavar='LR',
                        help='learning rate (default: 1.0)')
    parser.add_argument('--gamma',
                        type=float,
                        default=0.7,
                        metavar='M',
                        help='Learning rate step gamma (default: 0.7)')
    parser.add_argument('--no-cuda',
                        action='store_true',
                        default=False,
                        help='disables CUDA set_1')
    parser.add_argument('--seed',
                        type=int,
                        default=1,
                        metavar='S',
                        help='random seed (default: 1)')
    parser.add_argument(
        '--log-interval',
        type=int,
        default=10,
        metavar='N',
        help='how many batches to wait before logging set_1 status')

    parser.add_argument('--save-model',
                        action='store_true',
                        default=True,
                        help='For Saving the current Model')
    args = parser.parse_args()
    use_cuda = torch.cuda.is_available()
    #
    # torch.manual_seed(args.seed)

    device = torch.device(0 if use_cuda else "cpu")

    kwargs = {'num_workers': 4, 'pin_memory': True} if use_cuda else {}
    db_handler = DB_Handler(data_dir="./Data", scene_dir="set_1")
    trainDB_loader = DB_Loader(root_dir="./Data/training/set_1",
                               db=db_handler.read_db(db_set="training"))

    train_loader = torch.utils.data.DataLoader(trainDB_loader,
                                               batch_size=args.batch_size,
                                               shuffle=True,
                                               **kwargs)

    testDB_loader = DB_Loader(root_dir="./Data/testing/set_1",
                              db=db_handler.read_db(db_set="testing"))
    test_loader = torch.utils.data.DataLoader(testDB_loader,
                                              batch_size=args.test_batch_size,
                                              shuffle=True,
                                              **kwargs)

    try:
        model = Net().to(device)
        model.load_state_dict(torch.load("model_test1"))
    except:
        model = Net().to(device)

    optimizer = optim.SGD(model.parameters(), lr=0.005, momentum=0.5)
    # optimizer = optim.Adam(params=model.parameters(), lr=0.9)
    # optimizer = optim.SGD(params=model.parameters(), lr=0.01, momentum=0.7)
    criterion = nn.CrossEntropyLoss()
    scheduler = StepLR(optimizer, step_size=1, gamma=args.gamma)
    for epoch in range(1, args.epochs + 1):
        train(args, model, device, train_loader, criterion, optimizer, epoch)
        test(model, device, test_loader)
        scheduler.step()
        if epoch % 5 == 0:
            torch.save(model.state_dict(), "model_test1")
Example #22
0
def add():
    with DB_Handler() as handler:
        handler.add(request.form["name"])

    return render_template("template.html")