Beispiel #1
0
def create_highway_map(bbox, width, height):

    x1, y1, x2, y2 = bbox.split(",")

    cursor = db.execute_query(
        "SELECT ST_Transform(linestring,3857), tags->'highway' FROM ways WHERE tags?'highway' AND ST_Xmin(ST_Transform(bbox,3857))>"
        + x1 + " AND ST_Xmax(ST_Transform(bbox,3857))<" + x2 +
        " AND ST_Ymin(ST_Transform(bbox,3857))>" + y1 +
        " AND ST_Ymax(ST_Transform(bbox,3857))<" + y2 + ";")

    highway = []
    coordinates = []
    highway_color = {}

    x1, y1, x2, y2 = float(x1), float(y1), float(x2), float(y2)
    width, height = int(width), int(height)
    for row in cursor:
        highway.append(row[1])
        coord_ajout = row[0]
        normalize = [(width - (x2 - point.x) * width / (x2 - x1),
                      ((y2 - point.y) * height / (y2 - y1)))
                     for point in coord_ajout]
        coordinates.append(normalize)

    image_highway = Image(width, height)

    for x, i in enumerate(coordinates):
        image_highway.draw_linestring(i, (0, 0, 0, 1))

    image_highway.save("tuiles/" + bbox + ".png")
    cursor.close()
    db.close_connection()
Beispiel #2
0
def enter_server_loop():
    """
    The main part of the MetaShop Server application that
    consists of maintaining the running state of the server,
    updating the FLANN image index when needed, and spawning
    client servicing threads.
    """
    global main_server, image_processor

    config.create_directories()
    database.open_connection(config.DATABASE_PATHNAME)

    # To insure the existing index isn't being saved
    shared_data.lock.acquire()  # Enter Critical Section
    if os.path.isfile(os.getcwd() + "/matcher.bin"):
        image_matcher = ImageMatcher()
        image_matcher.load(os.getcwd())
        image_processor.update_image_matcher(image_matcher)
    shared_data.lock.release()  # Exit Critical Section

    #main_server = Server("localhost", 32304, image_processor)
    main_server = Server("0.0.0.0", 32304, image_processor)
    main_server.enter_main_loop()

    database.close_connection()
Beispiel #3
0
def create_quiz():
    db_connection = database.create_database_connection()

    try:
        body = request.get_json()

        user_id = extract_field_from_body('user_id', body)
        short_url = extract_field_from_body('short_url', body)
        question = extract_field_from_body('question', body)
        answers_target = extract_field_from_body('answers_target', body)

        quiz_id = database.insert_quiz(db_connection, user_id, short_url,
                                       question, answers_target)

    except:
        return Response("Bad request.",
                        status=400,
                        mimetype='application/json')

    finally:
        database.close_connection(db_connection)

    return Response(json.dumps({"id": quiz_id}),
                    status=201,
                    mimetype='application/json')
Beispiel #4
0
def get_user():

    db_connection = database.create_database_connection()

    try:
        body = request.get_json()

        email = extract_field_from_body('email', body)

        user = database.get_user_by_email(db_connection, email)

        data = {
            'id': user[0],
            'email': user[1],
            'password': user[2],
            'activated': user[3],
            'nickname': user[4],
            'created_date': user[5]
        }

    except:
        return Response("Bad request.", status=400, mimetype='application/json')
    finally:
        database.close_connection(db_connection)

    return Response(json.dumps(data), status=201, mimetype='application/json')
Beispiel #5
0
    def make_query4_onClick(self):
        compared_value = float(self.entry_query4.get())

        connect_db = database.create_connection(self.db)
        regulargrade, midgrade, premiumgrade, diesel = database.query4(
            connect_db, self.db_name, compared_value)
        database.close_connection(connect_db)

        print("\nResultado da Query:\n", regulargrade, midgrade, premiumgrade,
              diesel)
        if regulargrade:
            print("Data em que o combustível Regular ultrapassou",
                  compared_value, ":", regulargrade[0])
        else:
            print("Combustível Regular ainda não ultrapassou o valor",
                  compared_value)
        if midgrade:
            print("Data em que o combustível MidGrade ultrapassou",
                  compared_value, ":", midgrade[0])
        else:
            print("Combustível MidGrade ainda não ultrapassou o valor",
                  compared_value)
        if premiumgrade:
            print("Data em que o combustível Premium ultrapassou",
                  compared_value, ":", premiumgrade[0])
        else:
            print("Combustível Premium ainda não ultrapassou o valor",
                  compared_value)
        if diesel:
            print("Data em que o Diesel ultrapassou", compared_value, ":",
                  diesel[0])
        else:
            print("Diesel ainda não ultrapassou o valor", compared_value)
Beispiel #6
0
    def make_query5_onClick(self):
        compared_value = float(self.entry_query5.get())

        connect_db = database.create_connection(self.db)
        regulargrade, midgrade, premiumgrade, diesel = database.query5(
            connect_db, self.db_name, compared_value)
        database.close_connection(connect_db)

        if regulargrade:
            print("Última data em que o combustível Regular estava abaixo de",
                  compared_value, ":", regulargrade[0])
        else:
            print("Combustível Regular nunca esteve abaixo do valor",
                  compared_value)
        if midgrade:
            print("Última data em que o combustível MidGrade estava abaixo de",
                  compared_value, ":", midgrade[0])
        else:
            print("Combustível MidGrade nunca esteve abaixo do valor",
                  compared_value)
        if premiumgrade:
            print("Última data em que o combustível Premium estava abaixo de",
                  compared_value, ":", premiumgrade[0])
        else:
            print("Combustível Premium nunca esteve abaixo do valor",
                  compared_value)
        if diesel:
            print("Última data em que o Diesel estava abaixo de",
                  compared_value, ":", diesel[0])
        else:
            print("Diesel nunca esteve abaixo do valor", compared_value)
Beispiel #7
0
def search(keywords):
    """
        Serch for a given keyword in a node list
    """
    raw_query =\
        "SELECT tags->'name', ST_X(geom), ST_Y(geom), ST_Z(geom) " +\
        "FROM Nodes " + \
        "WHERE tags->'name' LIKE %s "

    # Pour chaque keyword passé en paramètre, on rajoute un "OR" à la recherche
    complete_query = raw_query + \
        ''.join(list(map(lambda _: " OR tags->'name' LIKE %s ", keywords[1:])))

    # Exec
    cursor = db.execute_query(
        complete_query,
        *keywords
    )

    # Affichage
    print("Searched name \tX_coord\tY_coord\tZ_coord\t".expandtabs(30))
    for row in cursor:
        print('\t'.join([str(el) for el in row]).expandtabs(30))

    cursor.close()
    db.close_connection()
Beispiel #8
0
    def make_query3_onClick(self):
        connect_db = database.create_connection(self.db)
        result_query = database.query3(connect_db, self.db_name)
        database.close_connection(connect_db)

        print("\nResultado da Query:\n", result_query)
        print("Data em que o preço geral do combustível estava mais alto: ",
              result_query[0])
        print("Preço na data em questão: U$", result_query[1])
Beispiel #9
0
def query():
    cursor = db.execute_query(
        "SELECT tags->'highway', ST_Transform(linestring,4326) FROM ways WHERE tags?->'highway' AND ST_Within(ST_Transform(bbox,4326), ST_GeomFromText('POLYGON((5.7 45.1, 5.7 45.2, 5.8 45.2, 5.8 45.1, 5.7 45.1))', 4326));"
    )
    for row in cursor:
        name = row[0]
        polygon = row[1]
        print(name, polygon)
    cursor.close()
    db.close_connection()
Beispiel #10
0
    def make_query10_onClick(self):
        connect_db = database.create_connection(self.db)
        result_query = database.query10(connect_db, self.db_name,
                                        self.entry_query10.get())
        database.close_connection(connect_db)

        fuel, price = utilities.get_most_expensive(result_query[0])
        print("\nResultado da Query:\n", result_query[0])
        print("Combustível mais caro na data", result_query[0][0], ":", fuel,
              "\nPreço: U$", price)
Beispiel #11
0
    def make_query2_onClick(self):
        connect_db = database.create_connection(self.db)
        result_query = database.query2(connect_db, self.db_name)
        database.close_connection(connect_db)

        print("\nResultado da Query:\n", result_query)
        print("Preços na data", result_query[0][0], ": Preço geral - U$",
              result_query[0][1], "Preço Regular - U$", result_query[0][2],
              "Preço MidGrade - U$", result_query[0][3], "Preço Premium - U$",
              result_query[0][4], "Preço Diesel - U$", result_query[0][5])
Beispiel #12
0
def main():
    logging.info("Gordias started")
    try:
        threading.Thread(target=run, kwargs=dict(host='0.0.0.0',
                                                 port=8080)).start()  # Run API
        sync.start_sync()
    except:
        logging.error("Unexpected error: {0}", sys.exc_info()[0])
    finally:
        database.close_connection()
        notnull.dump_matrices()
Beispiel #13
0
    def make_query7_onClick(self):
        connect_db = database.create_connection(self.db)
        result_query = database.query7(connect_db, self.db_name)
        database.close_connection(connect_db)

        print("\nResultado da Query:\n", result_query)
        print("Data em que o Diesel superou combustível premium: ",
              result_query[0])
        print("Preço do Diesel na data em questão: U$", result_query[5])
        print("Preço do combustível Premium na data em questão: U$",
              result_query[4])
Beispiel #14
0
    def make_query6_onClick(self):
        connect_db = database.create_connection(self.db)
        result_query = database.query6(connect_db, self.db_name)
        database.close_connection(connect_db)

        print("\nResultado da Query:\n", result_query)
        print("Data em que o combustível Regular superou o Diesel: ",
              result_query[0])
        print("Preço do combustível Regular na data em questão: U$",
              result_query[2])
        print("Preço do Diesel na data em questão: U$", result_query[5])
Beispiel #15
0
def main():
    config = configparser.ConfigParser()
    config.read('config.ini')
    database_connection = db.create_connection(
        (config['DATABASE']['name'] + '.db'))
    # database_connection = db.create_connection("season2020_lProfessor.db")

    team_info = get_game_info(database_connection)

    snowball = 0
    anti_snowball = 0
    length_limit = 1800
    game_lengths = []
    n_games = 0

    for team in team_info:
        team_str = str(team[0])
        team_dict = eval(team_str)
        team_100 = eval(str(team_dict[0]))
        team_200 = eval(str(team_dict[1]))
        game_length = team[1]
        game_lengths.append(team[1])

        if game_length <= length_limit:
            n_games += 1
            if team_100['win'] == 'Win' and team_100['firstTower'] == True:
                snowball += 1
            if team_200['win'] == 'Win' and team_200['firstTower'] == True:
                snowball += 1

            if team_100['win'] == 'Win' and team_100['firstTower'] == False:
                anti_snowball += 1
            if team_200['win'] == 'Win' and team_200['firstTower'] == False:
                anti_snowball += 1

    print(
        "1st turret + win + <= %.2f minutes: %d, Games analyzed: %d, Ratio %.2f %%"
        % (round(length_limit / 60,
                 2), snowball, n_games, round(snowball / n_games * 100, 2)))
    print(
        "not 1st turret + win + <= %.2f minutes: %d, Games analyzed: %d, Ratio %.2f %%"
        % (round(length_limit / 60, 2), anti_snowball, n_games,
           round(anti_snowball / n_games * 100, 2)))

    print("50th percentile of game length: %.2f, average: %.2f " %
          (round(np.percentile(game_lengths, 50),
                 2), round(np.average(game_lengths), 2)))

    db.close_connection(database_connection)
Beispiel #16
0
def probe_ip(ip):
    whois = IPWhoisResult(ip)

    db.open_connection()
    db.exec_query(select_websrv_result.format(ip=ip))

    db_row = db.cur.fetchone()
    if db_row:
        # Take result from local DB

        whois.contact_name = db_row[3]
        whois.contact_email = db_row[11]
        whois.contact_address = db_row[12]
        whois.geo_ip.organization = db_row[3]
        whois.geo_ip.isp = db_row[4]

        whois.geo_ip.city = db_row[8]
        whois.geo_ip.country = db_row[5]
        whois.geo_ip.region = db_row[9]
        whois.geo_ip.postal_code = db_row[10]
        whois.geo_ip.as_desc = db_row[2]

        whois.geo_ip.coordinates = GeoCoordinates(db_row[6], db_row[7])

    else:
        # Perform queries on remote DBs
        whois.get_from_whois()
        whois.get_from_geoip()

        db.exec_query(add_websrv_result.format(
            ip=ip,
            as_desc=whois.geo_ip.as_desc,
            organization=whois.geo_ip.organization,
            isp=whois.geo_ip.isp,
            country=whois.geo_ip.country,
            region=whois.geo_ip.region,
            city=whois.geo_ip.city,
            postal_code=whois.geo_ip.postal_code,
            contact_email=whois.contact_email,
            contact_address=whois.contact_address,
            lat=whois.geo_ip.coordinates.lat,
            lon=whois.geo_ip.coordinates.lon
        ))
        db.commit()

    db.close_connection()

    return whois
Beispiel #17
0
    def make_query9_onClick(self):
        connect_db = database.create_connection(self.db)
        result_query = database.query9(connect_db, self.db_name)
        database.close_connection(connect_db)

        print("\nResultado da Query:\n", result_query)
        if result_query:
            print("Data em que o combustível midgrade superou o premium: ",
                  result_query[0])
            print("Preço do combustível midgrade na data em questão: U$",
                  result_query[3])
            print("Preço do combustível premium na data em questão: U$",
                  result_query[4])
        else:
            print(
                "O preço do combustível midgrade não superou o do combustível premium até o presente momento"
            )
Beispiel #18
0
def activate_user():

    db_connection = database.create_database_connection()

    try:
        body = request.get_json()

        token = extract_field_from_body('token', body)

        database.activate_user(db_connection, token)

    except:
        return Response("Bad request.", status=400, mimetype='application/json')
    finally:
        database.close_connection(db_connection)

    return Response('User activated.', status=200, mimetype='application/json')
Beispiel #19
0
    def make_adv_query6(self):
        connect_db = database.create_connection(self.db)
        x_value, y_value = database.adv_query6(connect_db, self.db_name)
        database.close_connection(connect_db)

        x_value_formatted = [
            datetime.datetime.strptime(d, "%m/%d/%Y").date() for d in x_value
        ]

        plt.plot(x_value_formatted, y_value[0], label="Premium Grade Prices")
        plt.plot(x_value_formatted, y_value[1], label="Diesel Prices")

        plt.xlabel('Date')
        plt.ylabel('Price of the fuel')
        plt.title('Prices of Disel x Premium fuel, when Premium was cheaper')
        plt.legend()
        plt.show()
Beispiel #20
0
def main():
    logging.root.setLevel(logging.INFO)
    logging.info("Starting up email queue")

    session = database.get_session(config.database)
    provider = providers.get_provider(config.provider)
    processor = queue.QueueProcessor(config, session, provider)

    try:
        while not interrupted:
            logging.debug("checking email queue")
            processor.process()
            time.sleep(config.queue.getfloat('sleep_seconds', 10))
    except Exception as e:
        logging.critical(e.message)
    finally:
        database.close_connection()
Beispiel #21
0
def create_activation_token():

    db_connection = database.create_database_connection()

    try:
        body = request.get_json()

        user_id = extract_field_from_body('user_id', body)
        token = extract_field_from_body('token', body)

        activation_token_id = database.insert_activation_token(
            db_connection, user_id, token)

    except:
        return Response("Bad request.", status=400, mimetype='application/json')
    finally:
        database.close_connection(db_connection)

    return Response(json.dumps({"id": activation_token_id}), status=201, mimetype='application/json')
Beispiel #22
0
def get_quiz(id):

    db_connection = database.create_database_connection()

    try:
        body = request.get_json()

        user_id = extract_field_from_body('user_id', body)

        quiz = database.get_quiz(db_connection, id, user_id)

    except:
        return Response("Bad request.",
                        status=400,
                        mimetype='application/json')
    finally:
        database.close_connection(db_connection)

    return Response(json.dumps(quiz), status=200, mimetype='application/json')
Beispiel #23
0
    def make_adv_query2(self):
        compared_value = self.entry_adv_query2.get()

        connect_db = database.create_connection(self.db)
        x_value, y_value = database.adv_query2(connect_db, self.db_name,
                                               compared_value)
        database.close_connection(connect_db)

        x_value_formatted = [
            datetime.datetime.strptime(d, "%m/%d/%Y").date() for d in x_value
        ]

        plt.plot(x_value_formatted, y_value, label="All Grades Prices")

        plt.xlabel('Date')
        plt.ylabel('Price of the fuel')
        plt.title('Prices of All Grades fuel below a certain value')
        plt.legend()
        plt.show()
Beispiel #24
0
def create_user():

    db_connection = database.create_database_connection()

    try:
        body = request.get_json()

        email = extract_field_from_body('email', body)
        password = extract_field_from_body('password', body)
        nickname = extract_field_from_body('nickname', body)

        user_id = database.insert_user(
            db_connection, email, password, nickname)

    except:
        return Response("Bad request.", status=400, mimetype='application/json')
    finally:
        database.close_connection(db_connection)

    return Response(json.dumps({"id": user_id}), status=201, mimetype='application/json')
Beispiel #25
0
    def make_adv_query1(self):
        connect_db = database.create_connection(self.db)
        x_value, y_value = database.adv_query1(connect_db, self.db_name)
        database.close_connection(connect_db)

        x_value_formatted = [
            datetime.datetime.strptime(d, "%m/%d/%Y").date() for d in x_value
        ]

        plt.plot(x_value_formatted, y_value[0], label="All Grades Prices")
        plt.plot(x_value_formatted, y_value[1], label="Regular Grade Prices")
        plt.plot(x_value_formatted, y_value[2], label="Mid Grade Prices")
        plt.plot(x_value_formatted, y_value[3], label="Premium Grade Prices")
        plt.plot(x_value_formatted, y_value[4], label="Diesel Prices")

        plt.xlabel('Date')
        plt.ylabel('Price of the fuel')
        plt.title('Prices of all fuels along the measured time')
        plt.legend()
        plt.show()
Beispiel #26
0
def create_options():
    db_connection = database.create_database_connection()

    try:
        body = request.get_json()

        texts = extract_field_from_body('texts', body)
        quiz_id = extract_field_from_body('quiz_id', body)

        for text in texts:
            database.insert_option(db_connection, quiz_id, text)

    except:
        return Response("Bad request.",
                        status=400,
                        mimetype='application/json')

    finally:
        database.close_connection(db_connection)

    return Response(json.dumps({}), status=201, mimetype='application/json')
Beispiel #27
0
def answer_quiz(quiz_id):
    db_connection = database.create_database_connection()

    try:
        body = request.get_json()

        user_id = extract_field_from_body('user_id', body)
        option_id = extract_field_from_body('option_id', body)

        database.answer(db_connection, quiz_id, user_id, option_id)

        database.result(db_connection, quiz_id)

    except:
        return Response("Bad request.",
                        status=400,
                        mimetype='application/json')
    finally:
        database.close_connection(db_connection)

    return Response('Answer added.', status=201, mimetype='application/json')
Beispiel #28
0
def poll_and_save():
    """
	Check for new events (and activities) for every saved user and save them to the database 
	"""
    db_session = get_session()
    users = db_session.query(User).all()
    for user in users:
        logging.debug("Polling for {}".format(user))

        #Gather events from WC and save to database, including updating new activities added
        activity_events = weconnect.get_todays_events(user, db_session)
        weconnect.add_events_to_db(activity_events, db_session)
        logging.info("Found {} new events for user {}".format(
            len(activity_events), user.wc_id))

        # Add "progress" based on metaphor
        step_count = 0
        if user.metaphor == CHARGE:
            #get yesterDay
            yesterDay = user.yesterday()
            if yesterDay is not None:
                #get progress from yesterDay, add as starting progress to Fitbit
                step_count = fitbit.update_progress_count(
                    user, yesterDay.computed_progress, db_session)
                logging.info(
                    "Just added {} prelim steps to {}'s account!".format(
                        step_count, user.username))
            else:
                logging.info("CHARGE: Starting fresh from today: {}".format(
                    datetime.now()))

        #Setup new DAY for each user
        newDay = create_today(user, 0, step_count, db_session)

    logging.info("Completed first POLL for {} users at {}.".format(
        len(users), datetime.now()))

    db_session.commit()
    close_connection(db_session)
Beispiel #29
0
    def search(self, bbox):
        """
            Requêtage de la bdd
            :param bbox: Boundary Box pour la recherche
        """
        #"SELECT tags->'highway', ST_AsText(ST_Transform(linestring, %s)) " +\
        raw_query =\
            "SELECT tags->'highway', ST_AsText(linestring) " +\
            "FROM ways " +\
            "WHERE  tags ? 'highway' " +\
            "AND ST_Contains(ST_MakeEnvelope(%s, %s, %s, %s, %s), ST_Transform(linestring, %s) ) "

        # Exec
        (cursor, connection) = db.execute_query(raw_query, *bbox, self.srid,
                                                self.srid)

        res = list(cursor)

        cursor.close()
        db.close_connection(connection)

        return res
Beispiel #30
0
def routes(min_lon,max_lon,min_lat,max_lat,width,height,name):
    conn = database.init_connection()
    cursor = database.execute_query("SELECT tags->'name',id,ST_X((ST_DumpPoints(ST_Transform(linestring,3857))).geom),ST_Y((ST_DumpPoints(ST_Transform(linestring,3857))).geom) \
                                    FROM ways \
                                    WHERE tags ? 'highway' AND ST_Contains(ST_MakeEnvelope(%s,%s,%s,%s,3857),ST_Transform(linestring,3857)) \
                                    GROUP BY id \
                                    ;",min_lon,min_lat,max_lon,max_lat)
                                    
    rows = cursor.fetchall()
    image = drawer.Image(width,height)
    for i in range(1,len(rows)):
        #On présuppose que la taille des tuiles sera 256*256
        lo1 = (rows[i-1][2]-min_lon)/43.48
        lo2 = (rows[i][2]-min_lon)/43.48
        la1 = abs(((rows[i-1][3]-min_lat)/61.65)-256)
        la2 = abs(((rows[i][3]-min_lat)/61.65)-256)
        if rows[i][1] == rows[i-1][1]:
            image.draw_line(lo1,la1,lo2,la2,(0,0,0,255))
    
    image.save(name)
    cursor.close()
    database.close_connection()
def create_edges_full_data_table():
    print('create_edges_full_data_table')
    connection = database.create_connection()
    
    database.run_command('delete from edges_full_data', None, connection, True)
    database.run_command('vacuum', None, connection, True)
    
    sql_query = 'select a.id, a.shape_x_y, b.x from_x, b.y from_y, c.x to_x, c.y to_y \
                from oulu_edges a \
                inner join oulu_nodes b on a.`from` = b.id \
                inner join oulu_nodes c on a.`to` = c.id'
    count = 0
    edges = database.query_all(sql_query, None, connection)
    for edge in edges:
        sequence = 1
        if edge['shape_x_y'] is None:
            
            distance = utility.measure_distance(edge['from_x'], edge['from_y'], edge['to_x'], edge['to_y'])
            if distance <= config.MAX_DISTANCE:
                sql_script = '    insert into "edges_full_data" ("edge_id", "sequence", "from_x", "from_y", "to_x", "to_y", "distance") \
                                values (?, ?, ?, ?, ?, ?, ?)'
                parameter = (edge['id'], sequence, edge['from_x'], edge['from_y'], edge['to_x'], edge['to_y'], distance)
                database.run_command(sql_script, parameter, connection, True)
                sequence += 1
            else:
                splited_dots = split_line(edge['from_x'], edge['from_y'], edge['to_x'], edge['to_y'], config.MAX_DISTANCE)
                for i in range(len(splited_dots)-1):
                    splited_distance = utility.measure_distance(splited_dots[i][0], splited_dots[i][1], splited_dots[i+1][0], splited_dots[i+1][1])
                    sql_script = '    insert into "edges_full_data" ("edge_id", "sequence", "from_x", "from_y", "to_x", "to_y", "distance") \
                                values (?, ?, ?, ?, ?, ?, ?)'
                    parameter = (edge['id'], sequence, splited_dots[i][0], splited_dots[i][1], splited_dots[i+1][0], splited_dots[i+1][1], splited_distance)
                    database.run_command(sql_script, parameter, connection, False)
                    sequence += 1
                database.commit(connection)
        else:
            lines = edge['shape_x_y'].split(' ')
            for i in range(len(lines)-1):
                coord_from = lines[i].split(',')
                coord_to = lines[i+1].split(',')
                distance = sqrt(pow(float(coord_from[0]) - float(coord_to[0]), 2) + pow(float(coord_from[1]) - float(coord_to[1]), 2))
                if distance <= config.MAX_DISTANCE:
                    sql_script = '    insert into "edges_full_data" ("edge_id", "sequence", "from_x", "from_y", "to_x", "to_y", "distance") \
                            values (?, ?, ?, ?, ?, ?, ?)'
                    
                    parameter = (edge['id'], sequence, coord_from[0], coord_from[1], coord_to[0], coord_to[1], distance)
                    database.run_command(sql_script, parameter, connection, False)
                    sequence += 1
                else:
                    splited_dots = split_line(coord_from[0], coord_from[1], coord_to[0], coord_to[1], config.MAX_DISTANCE)
                    for j in range(len(splited_dots)-1):
                        splited_distance = utility.measure_distance(splited_dots[j][0], splited_dots[j][1], splited_dots[j+1][0], splited_dots[j+1][1])
                        sql_script = '    insert into "edges_full_data" ("edge_id", "sequence", "from_x", "from_y", "to_x", "to_y", "distance") \
                                    values (?, ?, ?, ?, ?, ?, ?)'
                        parameter = (edge['id'], sequence, splited_dots[j][0], splited_dots[j][1], splited_dots[j+1][0], splited_dots[j+1][1], splited_distance)
                        database.run_command(sql_script, parameter, connection, False)
                        sequence += 1
                    
            database.commit(connection)
        count += 1
        if count%1000 == 0:
            print(count)
    
    database.close_connection(connection, True)
    print('finish create_edges_full_data_table')
Beispiel #32
0
def parse_chat_to_db(input_data):
    json_data = json.loads(input_data)
    if json_data.get('error'):
        log.error(json_data['error'])
    else:
        database.init_connection()
        #-1 - unknown action
        #0 - user message
        #1 - control field created
        #2 - control field destroyed
        #3 - control field decayed
        #4 - resonator deployed
        #5 - resonator destroyed
        #6 - portal captured
        #7 - link created
        #8 - link destroyed
        #9 - destroyed portal mod
        for result_node in json_data['result']:
            action_type = -1
            message_guid = result_node[0]
            time_stamp = result_node[1]
            plext = result_node[2]["plext"]
            plext_type = plext["plextType"]
            plext_team = plext["team"]
            plext_text = plext["text"]
            markup = plext["markup"]
            user_guid = None
            user_plain = None
            user_team = None
            portal_to = None
            portal_to_adress = None
            portal_to_guid = None
            portal_to_lat = None
            portal_to_lng = None
            portal_to_name = None
            #unused so far
            portal_to_team = None
            portal_from = None
            portal_from_adress = None
            portal_from_guid = None
            portal_from_lat = None
            portal_from_lng = None
            portal_from_name = None
            #unused so far
            portal_from_team = None
            is_secured = 0
            if plext_type == 'PLAYER_GENERATED':
                action_type = 0
                for markup_node in markup:
                    if markup_node[0] == "SECURE":
                        is_secured = 1
                    if markup_node[0] == "SENDER":
                        user_guid = markup_node[1]["guid"]
                        if markup_node[1]["plain"].find(':') > -1:
                            user_plain = markup_node[1]["plain"][:-2]
                        else:
                            user_plain = markup_node[1]["plain"]
                        user_team = markup_node[1]["team"]
                    if markup_node[0] == "TEXT":
                        user_text = markup_node[1]["plain"]
            if plext_type == 'SYSTEM_BROADCAST':
                for markup_node in markup:
                    if markup_node[0] == "PLAYER":
                        user_guid = markup_node[1]["guid"]
                        if markup_node[1]["plain"].find(':') > -1:
                            user_plain = markup_node[1]["plain"][:-2]
                        else:
                            user_plain = markup_node[1]["plain"]
                        user_team = markup_node[1]["team"]
                    if (markup_node[0] == "PORTAL" and portal_from != None):
                        portal_to = markup_node[1]
                        portal_to_adress = markup_node[1]["address"]
                        portal_to_guid = markup_node[1]["guid"]
                        portal_to_lat = markup_node[1]["latE6"]
                        portal_to_lng = markup_node[1]["lngE6"]
                        portal_to_name = markup_node[1]["name"]
                        portal_to_team = markup_node[1]["team"]
                    if (markup_node[0] == "PORTAL" and portal_from == None):
                        portal_from = markup_node[1]
                        portal_from_adress = markup_node[1]["address"]
                        portal_from_guid = markup_node[1]["guid"]
                        portal_from_lat = markup_node[1]["latE6"]
                        portal_from_lng = markup_node[1]["lngE6"]
                        portal_from_name = markup_node[1]["name"]
                        portal_from_team = markup_node[1]["team"]
                if plext_text.find('created a Control Field') > -1:
                    action_type = 1
                if plext_text.find('destroyed a Control Field') > -1:
                    action_type = 2
                if (plext_text.find('Control Field') > -1 and plext_text.find('has decayed') > -1):
                    action_type = 3
                if plext_text.find('deployed an') > -1:
                    action_type = 4
                if plext_text.find('destroyed an') > -1:
                    action_type = 5
                if plext_text.find(' captured ') > -1:
                    action_type = 6
                if plext_text.find(' linked ') > -1:
                    action_type = 7
                if plext_text.find('destroyed the Link ') > -1:
                    action_type = 8
                if (plext_text.find('destroyed') > -1 and plext_text.find('Portal Mod') > -1):
                    action_type = 9
            from datetime import datetime
            full_params = (message_guid, datetime.fromtimestamp(int(time_stamp / 1000)), user_guid, portal_from_guid, portal_to_guid, \
                plext_text.encode('utf8'), int(action_type), plext_type, plext_team, is_secured)
            log.debug(full_params)
            if (user_guid <> None and user_plain <> None and user_team <> None):
                database.insert_player((user_guid, user_plain, user_team))
            if (portal_to <> None):
                database.insert_portal((portal_to_guid, portal_to_adress.encode('utf8'), int(portal_to_lat), int(portal_to_lng), portal_to_name.encode('utf8')))
            if (portal_from <> None):
                database.insert_portal((portal_from_guid, portal_from_adress.encode('utf8'), portal_from_lat, portal_from_lng, portal_from_name.encode('utf8')))
            database.insert_event(full_params)
        database.close_connection()
Beispiel #33
0
def close_connection(exception):
    database.close_connection(exception)