예제 #1
0
def buscar_conta(connection, id=None, nome=None, info="*"):
    users = None
    if id:
        ConsoleLogger.log("Buscando por ID...")
        users = read_query(connection,
                           f"SELECT {info} FROM USERS WHERE ID = {id};")
    elif nome:
        ConsoleLogger.log("Buscando pelo nome...")
        users = read_query(
            connection,
            f"SELECT {info} FROM USERS WHERE name LIKE '%{nome}%';")
    if users:
        qtd = len(users)
        if qtd > 1:
            print(f"Encontrado: {len(users)} Usuários.")
        else:
            print(f"Encontrado: {len(users)} Usuário.")

        print()
        print(", ".join(settings.info))
        print(f"{'':-^130s}")
        for user in users:
            print(user)
        print(f"{'':-^130s}")
        ConsoleLogger.log("Busca feita com êxito!")
    else:
        ConsoleLogger.log("Usuário não encontrado!")
    def remove_old_profiler():
        con = config.get_connection()

        while 1:
            # wait
            time.sleep(30)

            # Get oldest profilers (10)
            profilers = database.read_query(
                "SELECT username, timestamp FROM profiler ORDER BY timestamp ASC LIMIT 10;",
                (),
                con=con,
                close_con=False)

            for username, timestamp in profilers:
                timestamp = datetime.strptime(timestamp, "%d.%m.%YT%H:%M:%S")

                if timestamp < (datetime.utcnow() -
                                timedelta(hours=config.PROFILER_DELETE_TIME)):
                    # To old --> remove
                    database.commit_query("SET SQL_SAFE_UPDATES = 0;", (),
                                          con=con,
                                          close_con=False)
                    database.commit_query(
                        "DELETE FROM profiler WHERE username=%s;",
                        (username, ),
                        con=con,
                        close_con=False)

                    # Remove interesting Posts
                    database.commit_query(
                        "DELETE FROM interesting_posts WHERE username=%s;",
                        (username, ),
                        con=con,
                        close_con=False)
예제 #3
0
def get_post_category(request):
    author = request.GET.get('author', None)
    permlink = request.GET.get('permlink', None)
    if author is None or permlink is None:
        # Error (No author, permlink is given) --> return Error.js
        return render(
            request,
            'error.js',
            context={"info": "Please give author, permlink and use GET"},
            content_type="application/x-javascript")

    result = database.read_query(
        "SELECT * FROM latest_posts WHERE author=%s AND permlink=%s",
        (author, permlink))
    if len(result) == 0:
        # Error (No post or Connection Error) --> return Error.js
        return render(
            request,
            'error.js',
            context={
                "info":
                "Cannot find a post or cannot create database connection"
            },
            content_type="application/x-javascript")

    author, permlink, category, _ = result[0]
    return render(request,
                  'get_post_category.js',
                  context={"category": category},
                  content_type="application/x-javascript")
예제 #4
0
def get_user_profile():
    ''' AJAX CALL: Get user data'''
    analytics.REMOTE_ADDRS.append(request.remote_addr)
    if "username" not in request.json:
        # Error (No username)
        return jsonify({
            "status": "failed",
            "code": 1,
            "msg": "No username is given"
        })

    username = request.json["username"]
    result = database.read_query("SELECT * FROM profiler WHERE username=%s;",
                                 (username, ))
    if len(result) == 0:
        # Error (No post or Connection Error) --> return Error.js
        return jsonify({
            "status": "failed",
            "code": 2,
            "msg": "Username is unknown"
        })

    username, category, length, _, finished = result[0]

    # get total value
    cat = category.split(' ')
    total = 0
    for c in cat:
        total += float(c)

    # get top 10 profiler categories
    top_cats = []
    while len(top_cats) < 10:
        highest = (0, -1)  # (value, cat_index)
        for index, x in enumerate(cat):
            if highest[1] == -1 or float(x) > highest[0]:
                # first or better
                highest = (float(x), index)

        top_cats.append(highest)
        cat[highest[1]] = 0

    return jsonify({
        "status":
        "succes",
        "top_cats": [{
            "value": x[0],
            "label": config.CATEGORIES[x[1]]
        } for x in top_cats],
        "total":
        total,
        "data_length":
        length,
        "finished":
        "1" in finished
    })
예제 #5
0
    def clean_up(self):
        # TODO: Implement sorting order by timestamp
        query = "SELECT timestamp FROM latest_posts;"
        for item in database.read_query(query, None):
            timestamp = datetime.strptime(item[0], "%d.%m.%YT%H:%M:%S")

            if timestamp < (datetime.utcnow() - timedelta(days=5)):
                result = database.commit_query(
                    "DELETE FROM latest_posts WHERE timestamp=%s;",
                    (item[0], ))
예제 #6
0
def mudar_senha(connection):
    id = input('ID: ')
    user = read_query(connection,
                      f"SELECT name FROM USERS WHERE ID = {id};")[0][0]
    ConsoleLogger.log(f"Mudando senha do usuario: {user}")
    nova_senha = input("Nova senha: ")
    if nova_senha:
        query = f"UPDATE users SET password = '******' WHERE id = {id};"
        execute_query(connection, query)
        ConsoleLogger.log("Senha mudada.")
예제 #7
0
def get_analytics():
    ''' AJAX CALL: get analytics'''
    con = config.get_connection()
    context = {}
    context["status"] = "succes"

    # Get tasks_running
    element = database.read_query(
        "SELECT value_one FROM analytics WHERE name=%s;", ("tasks_running", ),
        con=con,
        close_con=False)[0]  # [(value, )]
    context["tasks_running"] = int(element[0])

    # Get connections
    element = database.read_query(
        "SELECT value_one FROM analytics WHERE name=%s;", ("connections", ),
        con=con,
        close_con=False)[0]  # [(value, )]
    context["connections"] = int(element[0])

    # Get requests
    element = database.read_query(
        "SELECT value_one FROM analytics WHERE name=%s;", ("requests", ),
        con=con,
        close_con=False)[0]  # [(value, )]
    context["requests"] = int(element[0])

    # Get latest_post count
    result = database.read_query("SELECT COUNT(*) FROM latest_posts", (),
                                 con=con,
                                 close_con=False)  # return [(COUNT,)]
    context["count_latest_posts"] = result[0][0]

    # Get profiler count
    result = database.read_query("SELECT COUNT(*) FROM profiler", (),
                                 con=con,
                                 close_con=False)  # return [(COUNT,)]
    context["count_accounts"] = result[0][0]

    con.close()
    return jsonify(context)
    def __init__(self, username: str, start_get_post_thread=True):
        self.username = username
        self.account = Account(username, blockchain_instance=Hive())

        mysql_con = config.get_connection()
        if mysql_con is None:
            print(
                "[INFO] Can't start Latest Post Manager because of an mysql database error!"
            )
            return

        result = database.read_query(
            "SELECT * FROM profiler WHERE username=%s;", (username, ),
            con=mysql_con,
            close_con=False)
        if len(result) == 0:
            # No profiler exists, create one
            self.category = [0 for i in config.CATEGORIES]
            self.data_length = 0
            self.finished = False
            result = database.commit_query(
                "INSERT INTO profiler(username, category, length, timestamp, finished) VALUES (%s, %s, %s, %s, %s);",
                (username, ' '.join(map(str, self.category)), self.data_length,
                 datetime.utcnow().strftime("%d.%m.%YT%H:%M:%S"), False),
                con=mysql_con,
                close_con=False)
            if result <= 0:
                # Error
                print("[WARNING] Can't add Profiler for " + username)
            else:
                # Start analyze Thread, if the profiler existed bevor, this thread already run!
                self.analyze_thread = Thread(target=self.analyze_activity)
                self.analyze_thread.name = "Analyze Activities from " + username
                self.analyze_thread.daemon = True
                self.analyze_thread.start()
        else:
            # Load existent Profiler
            self.update_timestamp()

            self.category = [float(x) for x in result[0][1].split(' ')]
            self.data_length = result[0][2]
            self.finished = "1" in result[0][4]

        mysql_con.close()
        # Start finder thread
        self.find_posts_thread = Thread(target=self.find_interestings)
        self.find_posts_thread.name = "Find interesting Posts for " + username
        self.find_posts_thread.daemon = True
        if start_get_post_thread:
            self.find_posts_thread.start()
예제 #9
0
def get_author_category(request):
    author = request.GET.get('author', None)
    if author is None:
        # Error (No author) --> return Error.js
        return render(request,
                      'error.js',
                      context={"info": "Please give author and use GET"},
                      content_type="application/x-javascript")

    result = database.read_query("SELECT * FROM profiler WHERE username=%s;",
                                 (author, ))
    if len(result) == 0:
        # Error (No post or Connection Error) --> return Error.js
        return render(request,
                      'error.js',
                      context={"info": "Cannot find a profiler"},
                      content_type="application/x-javascript")

    username, category, length, _ = result[0]

    # get total value
    cat = category.split(' ')
    total = 0
    for c in cat:
        total += float(c)

    # get top 10 profiler categories
    top_cats = []
    while len(top_cats) < 10:
        highest = (0, -1)  # (value, cat_index)
        for index, x in enumerate(cat):
            if highest[1] == -1 or float(x) > highest[0]:
                # first or better
                highest = (float(x), index)

        top_cats.append(highest)
        cat[highest[1]] = 0

    return render(request,
                  'get_author_category.js',
                  context={
                      "category": [x[0] for x in top_cats],
                      "total":
                      total,
                      "length":
                      length,
                      "CATEGORIES":
                      " ".join([config.CATEGORIES[x[1]] for x in top_cats])
                  },
                  content_type="application/x-javascript")
예제 #10
0
def get_interesting_posts(request):
    username = request.GET.get('username', None)
    if username is None:
        # Error (No Username is given) --> return Error.js
        return render(
            request,
            'error.js',
            context={"info": "Please enter a 'username' and use GET"},
            content_type="application/x-javascript")

    con = config.get_connection()
    posts = database.read_query(
        "SELECT * FROM interesting_posts WHERE username=%s;", (username, ),
        con=con,
        close_con=False)
    database.commit_query(
        "INSERT INTO tasks(name, timestamp, parameter_one, parameter_two) VALUES (%s, %s, %s, %s);",
        ("profiler", datetime.utcnow().strftime("%d.%m.%YT%H:%M:%S"), username,
         ""),
        con=con,
        close_con=False)

    database.commit_query("SET SQL_SAFE_UPDATES = 0;", (),
                          con=con,
                          close_con=False)

    obj = ""
    length = 0
    for _, author, permlink in posts:
        obj += f"{author}/{permlink};"

        database.commit_query(
            "DELETE FROM interesting_posts WHERE username=%s AND author=%s AND permlink=%s;",
            (username, author, permlink),
            con=con,
            close_con=False)

        length += 1
        if length >= 3:
            # Return only 3
            break

    return render(request,
                  'get_interesting_posts.js',
                  context={"posts": obj[:-1]},
                  content_type="application/x-javascript")
예제 #11
0
def get_interesting_posts():
    ''' Ajax Call: get 3 interesting posts '''
    analytics.REMOTE_ADDRS.append(request.remote_addr)
    if "username" not in request.json:
        # Return error json, if no username is given
        return jsonify({
            "status": "failed",
            "code": 1,
            "message": "No username is given"
        })
    username = request.json["username"]

    # Get 3 posts
    LIMIT = 3
    con = config.get_connection()
    posts = database.read_query(
        "SELECT * FROM interesting_posts WHERE username=%s LIMIT %s;",
        (username, LIMIT),
        con=con,
        close_con=False)

    # Prepare and Delete them
    database.commit_query("SET SQL_SAFE_UPDATES = 0;", (),
                          con=con,
                          close_con=False)
    for index, (_, author, permlink) in enumerate(posts):
        posts[index] = {"author": author, "permlink": permlink}

        database.commit_query(
            "DELETE FROM interesting_posts WHERE username=%s AND author=%s AND permlink=%s;",
            (username, author, permlink),
            con=con,
            close_con=False)

    # Start profiler
    database.commit_query(
        "INSERT INTO tasks(name, timestamp, parameter_one, parameter_two) VALUES (%s, %s, %s, %s);",
        ("profiler", datetime.utcnow().strftime("%d.%m.%YT%H:%M:%S"), username,
         ""),
        con=con,
        close_con=False)

    return jsonify({"status": "succes", "posts": posts})
예제 #12
0
def gen_id(connection):
    """Gerar ID, a partir do ultimo id + 1"""
    id = read_query(connection,
                    "SELECT seq FROM sqlite_sequence WHERE NAME='users';")
    return id[0][0] + 1
예제 #13
0
def task_manager():
    con = config.get_connection()
    while 1:
        # Update analytics
        database.commit_query("UPDATE analytics SET value_one=%s WHERE name=%s", 
                                (len(config.statics.task_list), "tasks_running"), con=con, close_con=False)

        while len(config.statics.task_list) >= config.MAX_TASK_THREADS:
            # Thread limit is reached --> Wait for completing
            time.sleep(0.5)

        # Get first available task
        tasks = database.read_query("SELECT * FROM tasks LIMIT 1;", (), con=con, close_con=False)

        if len(tasks) == 0:
            # If nothing is to do
            time.sleep(0.5)
            continue

        # Get first element and test if it is running
        name, timestamp, p_one, p_two = tasks[0]
        already_running = False
        for _name, _, _p_one, _p_two in config.statics.task_list:
            if name in _name and p_one in _p_one and p_two in _p_two:
                # already running
                already_running = True
                break

        # Delete element
        database.commit_query("SET SQL_SAFE_UPDATES = 0;", (), con=con, close_con=False)
        database.commit_query("DELETE FROM tasks WHERE name=%s AND parameter_one=%s AND parameter_two=%s;",
                                 (name, p_one, p_two), con=con, close_con=False)

        if already_running:
            # abort
            continue

        # Insert in list and Run
        config.statics.task_list.append(tasks[0])
        def run(task):
            name, timestamp, p_one, p_two = task

            if 'profiler' in name:
                p = Profiler(p_one, start_get_post_thread=False)
                p.find_interestings()

            if 'adjust' in name:
                p = Profiler(p_one, start_get_post_thread=False)
                p.adjust(categories_as_strings=p_two.split(','))
            
            if 'set_to_zero' in name:
                p = Profiler(p_one, start_get_post_thread=False)
                p.set_zero(category=p_two)  

            if 'delete_user' in name:
                database.commit_query("DELETE FROM profiler WHERE username=%s;",
                                 (p_one, ))  
                database.commit_query("DELETE FROM interesting_posts WHERE username=%s;",
                                 (p_one, ))           
                

            # delete task
            config.statics.task_list.remove(task)

        # Start thread
        t = Thread(target=run, args=(tasks[0], ))
        t.name = f"T - {name} ({str(p_one)};{str(p_two)})"
        t.daemon = True
        t.start()
예제 #14
0
    def find_interestings(self):
        mysql_con = config.get_connection()

        while self.data_length < config.PROFILER_MIN_DATA:
            # Wait until enough data is availabel
            time.sleep(0.2)

        percentages, top_cats = [], []
        last_data_len = -1
        while 1:
            # 0 Step: Check if post limit is reached
            interesting_posts = database.read_query(
                "SELECT * FROM interesting_posts WHERE username=%s",
                (self.username, ),
                con=mysql_con,
                close_con=False)
            if len(interesting_posts) >= config.MAX_INTERSTING_POSTS:
                break

            # 1 Step: Calc percentages and top cats
            if len(percentages) == 0 or last_data_len != self.data_length:
                # If no percentages are calced or data_length increased
                last_data_len = self.data_length

                percentages = helper.calc_percentages(self.category)
                top_cats = helper.get_top_elements(percentages, 10)

            # 2 Step: get random Posts
            offset = random.randint(0,
                                    config.statics.LATEST_POSTS_START_LIMIT -
                                    50)  # random offset
            posts = database.read_query("SELECT * FROM latest_posts LIMIT " +
                                        str(offset) + ", 50;", (),
                                        con=mysql_con,
                                        close_con=False)

            # 3 Step: prepare posts to compare
            post_percentages = []  # ([top_percentages], author, permlink)
            for author, permlink, category, timestamp in posts:
                category = [float(x) for x in category.split(' ')]

                # Calc percentages and top ones
                percs = helper.calc_percentages(category)
                top_pers = helper.get_top_elements(percs, 10)

                post_percentages.append((top_pers, author, permlink))

            # 4 Step: Compare
            for top_pers, author, permlink in post_percentages:
                score = 0
                diff = 0
                for value, index in top_cats:
                    for p_value, p_index in top_pers:
                        diff += abs(value - p_value)
                        if p_index == index:
                            # Same top Category
                            score += 1

                #if score >= 7:
                if diff <= 2:
                    exists = database.read_query(
                        "SELECT author FROM interesting_posts WHERE username=%s AND author=%s AND permlink=%s;",
                        (self.username, author, permlink),
                        con=mysql_con,
                        close_con=False)

                    if len(exists) > 0:
                        # Already listed --> Next one
                        continue

                    result = database.commit_query(
                        "INSERT INTO interesting_posts(username, author, permlink) VALUES (%s, %s, %s);",
                        (self.username, author, permlink),
                        con=mysql_con,
                        close_con=False)
                    if result < 1:
                        # Error
                        print("[WARNING] Can't insert an interesting post!")
                        time.sleep(5)

            self.update_timestamp()