예제 #1
0
파일: main.py 프로젝트: amosoz/anyway
def load_discussions(identifiers):
    from anyway.models import DiscussionMarker
    from flask_sqlalchemy import SQLAlchemy
    from anyway.utilities import init_flask

    app = init_flask()
    db = SQLAlchemy(app)

    identifiers = identifiers or sys.stdin

    for identifier in identifiers:
        identifier = identifier.strip()
        m = re.match(r'\((\d+\.\d+),\s*(\d+\.\d+)\)', identifier)
        if not m:
            logging.error("Failed processing: " + identifier)
            continue
        (latitude, longitude) = m.group(1, 2)
        marker = DiscussionMarker.parse({
            'latitude': latitude,
            'longitude': longitude,
            'title': identifier,
            'identifier': identifier
        })
        try:
            db.session.add(marker)
            db.session.commit()
            logging.info("Added:  " + identifier)
        except Exception as e:
            db.session.rollback()
            logging.warn("Failed: " + identifier + ": " + e)
예제 #2
0
파일: main.py 프로젝트: AlonJoshua/anyway
def load_discussions(identifiers):
    from anyway.models import DiscussionMarker
    from flask_sqlalchemy import SQLAlchemy
    from anyway.utilities import init_flask

    app = init_flask()
    db = SQLAlchemy(app)

    identifiers = identifiers or sys.stdin

    for identifier in identifiers:
        identifier = identifier.strip()
        m = re.match(r"\((\d+\.\d+),\s*(\d+\.\d+)\)", identifier)
        if not m:
            logging.error("Failed processing: " + identifier)
            continue
        (latitude, longitude) = m.group(1, 2)
        marker = DiscussionMarker.parse({
            "latitude": latitude,
            "longitude": longitude,
            "title": identifier,
            "identifier": identifier,
        })
        try:
            db.session.add(marker)
            db.session.commit()
            logging.info("Added:  " + identifier)
        except Exception as e:
            db.session.rollback()
            logging.warn("Failed: " + identifier + ": " + e)
예제 #3
0
def mda_twitter():
    app = init_flask()
    db = SQLAlchemy(app)

    TWITTER_CONSUMER_KEY = os.environ.get('TWITTER_CONSUMER_KEY')
    TWITTER_CONSUMER_SECRET = os.environ.get('TWITTER_CONSUMER_SECRET')
    TWITTER_ACCESS_KEY = os.environ.get('TWITTER_ACCESS_KEY')
    TWITTER_ACCESS_SECRET = os.environ.get('TWITTER_ACCESS_SECRET')

    GOOGLE_MAPS_API_KEY = os.environ.get('GOOGLE_MAPS_KEY')

    twitter_user = '******'

    latest_tweet_id = get_latest_tweet_id_from_db(db)

    # the next id to be used for the first new row
    next_sequential_id = get_latest_sequential_id_from_db(db) + 1

    # check if there are any MDA tweets in the DB
    if latest_tweet_id:
        mda_tweets = get_user_tweets(twitter_user, latest_tweet_id,
                                     TWITTER_CONSUMER_KEY,
                                     TWITTER_CONSUMER_SECRET,
                                     TWITTER_ACCESS_KEY, TWITTER_ACCESS_SECRET,
                                     GOOGLE_MAPS_API_KEY)
    else:
        mda_tweets = get_user_tweets(twitter_user, 'no_tweets',
                                     TWITTER_CONSUMER_KEY,
                                     TWITTER_CONSUMER_SECRET,
                                     TWITTER_ACCESS_KEY, TWITTER_ACCESS_SECRET,
                                     GOOGLE_MAPS_API_KEY)
    if mda_tweets is None:
        return
    mda_tweets = mda_tweets.loc[:, [
        'id', 'accident', 'author', 'date', 'description', 'lat', 'link',
        'lon', 'title', 'source', 'location', 'city', 'intersection', 'road1',
        'road2', 'street', 'geo_extracted_address', 'geo_extracted_city',
        'geo_extracted_district', 'geo_extracted_intersection',
        'geo_extracted_road_no', 'geo_extracted_street', 'resolution',
        'street2'
    ]]

    for row in mda_tweets.itertuples(index=False):
        (tweet_id, accident, author, date, description, lat, link, lon, title,
         source, location, city, intersection, road1, road2, street,
         geo_extracted_address, geo_extracted_city, geo_extracted_district,
         geo_extracted_intersection, geo_extracted_road_no,
         geo_extracted_street, resolution, street2) = row

        insert_mda_tweet(db, tweet_id, title, link, date, author, description,
                         location, lat, lon, road1, road2, intersection, city,
                         street, street2, resolution, geo_extracted_street,
                         geo_extracted_road_no, geo_extracted_intersection,
                         geo_extracted_city, geo_extracted_address,
                         geo_extracted_district, accident, source,
                         next_sequential_id)

        next_sequential_id += 1
예제 #4
0
def insert_waze_alerts(waze_alerts):
    """
    insert new waze alerts to db
    :param waze_alerts_df: DataFrame contains waze alerts
    """

    app = init_flask()
    db = SQLAlchemy(app)

    db.session.bulk_insert_mappings(WazeAlert, waze_alerts)
    db.session.commit()
예제 #5
0
def insert_waze_traffic_jams(waze_traffic_jams):
    """
    insert new waze traffic jams to db
    :param waze_traffic_jams_df: DataFrame contains waze traffic jams
    """

    app = init_flask()
    db = SQLAlchemy(app)

    db.session.bulk_insert_mappings(WazeTrafficJams, waze_traffic_jams)
    db.session.commit()
예제 #6
0
def run_migrations_online():
    """Run migrations in 'online' mode.

    In this scenario we need to create an Engine
    and associate a connection with the context.

    """
    from anyway.utilities import init_flask
    from flask_sqlalchemy import SQLAlchemy
    app = init_flask()
    connectable = SQLAlchemy(app).engine

    with connectable.connect() as connection:
        context.configure(connection=connection,
                          target_metadata=target_metadata)

        with context.begin_transaction():
            context.run_migrations()
예제 #7
0
파일: env.py 프로젝트: amosoz/anyway
def run_migrations_online():
    """Run migrations in 'online' mode.

    In this scenario we need to create an Engine
    and associate a connection with the context.

    """
    from anyway.utilities import init_flask
    from flask_sqlalchemy import SQLAlchemy
    app = init_flask()
    connectable = SQLAlchemy(app).engine

    with connectable.connect() as connection:
        context.configure(
            connection=connection,
            target_metadata=target_metadata
        )

        with context.begin_transaction():
            context.run_migrations()
예제 #8
0
def init_db() -> "DBAdapter":
    app = init_flask()
    db = SQLAlchemy(app)
    return DBAdapter(db)
예제 #9
0
from anyway.utilities import init_flask
from flask_sqlalchemy import SQLAlchemy

app = init_flask()
db = SQLAlchemy(app)


def get_description(ind):
    description = db.session.execute(
        'SELECT description FROM news_flash WHERE id=:id', {
            'id': ind
        }).fetchone()
    return description


def insert_new_flash_news(id_flash, title, link, date_parsed, author,
                          description, location, lat, lon, road1, road2,
                          intersection, city, street, accident, source):
    db.session.execute(
        'INSERT INTO news_flash (id,title, link, date, author, description, location, lat, lon, '
        'road1, road2, intersection, city, street, accident, source) VALUES \
                       (:id, :title, :link, :date, :author, :description, :location, :lat, :lon, :road1, :road2, :intersection, :city, :street, :accident, :source)',
        {
            'id': id_flash,
            'title': title,
            'link': link,
            'date': date_parsed,
            'author': author,
            'description': description,
            'location': location,
            'lat': lat,
예제 #10
0
class FlexRootApi(flask_restx.Api):
    def __init__(self, inner_app, **kwargs):
        self.render_root_method = None
        super().__init__(inner_app, **kwargs)

    def render_root(self):
        if self.render_root_method is not None:
            return self.render_root_method()
        else:
            self.abort(HTTPStatus.NOT_FOUND)

    def set_render_root_function(self, func):
        self.render_root_method = func


app = utilities.init_flask()
db = SQLAlchemy(app)
api = FlexRootApi(app, doc="/swagger")


def get_cors_config() -> dict:
    cors_site_list = BE_CONST.ANYWAY_CORS_SITE_LIST_PROD
    if SERVER_ENV == "dev":
        cors_site_list = BE_CONST.ANYWAY_CORS_SITE_LIST_DEV

    return {
        r"/location-subscription": {
            "origins": "*"
        },
        r"/report-problem": {
            "origins": "*"
예제 #11
0

SUBTYPE_ACCIDENT_WITH_PEDESTRIAN = 1
LOCATION_ACCURACY_PRECISE = True
LOCATION_ACCURACY_PRECISE_INT = 1
INJURED_TYPE_PEDESTRIAN = 1
YISHUV_SYMBOL_NOT_EXIST = -1
CONTENT_ENCODING = 'utf-8'
HEBREW_ENCODING = 'cp1255'
ANYWAY_UI_FORMAT_MAP_ONLY = "https://www.anyway.co.il/?zoom=17&start_date={start_date}&end_date={end_date}&lat={latitude}&lon={longitude}&show_fatal=1&show_severe=1&show_light=1&approx={location_approx}&accurate={location_accurate}&show_markers=1&show_discussions=0&show_urban=3&show_intersection=3&show_lane=3&show_day=7&show_holiday=0&show_time=24&start_time=25&end_time=25&weather=0&road=0&separation=0&surface=0&acctype={acc_type}&controlmeasure=0&district=0&case_type=0&show_rsa=0&age_groups=1,2,3,4&map_only=true"
ANYWAY_UI_FORMAT_WITH_FILTERS = "https://www.anyway.co.il/?zoom=17&start_date={start_date}&end_date={end_date}&lat={latitude}&lon={longitude}&show_fatal=1&show_severe=1&show_light=1&approx={location_approx}&accurate={location_accurate}&show_markers=1&show_discussions=0&show_urban=3&show_intersection=3&show_lane=3&show_day=7&show_holiday=0&show_time=24&start_time=25&end_time=25&weather=0&road=0&separation=0&surface=0&acctype={acc_type}&controlmeasure=0&district=0&case_type=0&show_rsa=0&age_groups=1,2,3,4"
DATE_INPUT_FORMAT = '%d-%m-%Y'
DATE_URL_FORMAT = '%Y-%m-%d'


app = init_flask()
db = SQLAlchemy(app)

def get_bounding_box(latitude, longitude, distance_in_km):

    latitude = math.radians(latitude)
    longitude = math.radians(longitude)

    radius = 6371
    # Radius of the parallel at given latitude
    parallel_radius = radius*math.cos(latitude)

    lat_min = latitude - distance_in_km/radius
    lat_max = latitude + distance_in_km/radius
    lon_min = longitude - distance_in_km/parallel_radius
    lon_max = longitude + distance_in_km/parallel_radius