Esempio n. 1
0
def update_pol_stock(conn_str=None):
    db = Database(conn_str)
    beers = db.get_pol_beers()
    shops = [pol['id'] for pol in db.get_pol_shops()]

    stock_by_pol = []
    for beer in beers:
        stocks = check_beer(beer['id'])
        for stock in stocks:
            num = stock['stock']
            updated = stock['updated']
            stock_by_pol.append({
                'shop_id': stock['pol_id'],
                'pol_beer_id': beer['id'],
                'stock': num,
                'updated': updated
            })
        shops_with_beer = [int(stock['pol_id']) for stock in stocks]
        for pol_id in shops:
            if pol_id not in shops_with_beer:
                stock_by_pol.append({
                    'shop_id': pol_id,
                    'pol_beer_id': beer['id'],
                    'stock': 0,
                    'updated': datetime.now()
                })
    save_stock(stock_by_pol, db)
    db.add_log('pol_stock')
Esempio n. 2
0
def update_pol_beers(conn_str=None):
    db = Database(conn_str)
    beers_polet, updated = read_pol_beers()

    # ok, unwrap generator
    beers_polet = [b for b in beers_polet]

    breweries_rb = db.get_rb_breweries()
    breweries_pol = get_breweries_polet(beers_polet)

    matched_pol_rb = match_pol_breweries(breweries_pol, breweries_rb)

    matched_beers = []
    for rb_beer_id, pol_breweries in matched_pol_rb.iteritems():
        rb_brewery = find_in_list(breweries_rb, 'id', rb_beer_id)['name']
        rb_beers_for_brewery = db.get_rb_beers_for_brewery(rb_beer_id)
        matched_beers += match_pol_beers(
            rb_brewery,
            rb_beers_for_brewery,
            pol_breweries,
            beers_polet,
            db
        )
    save_pol_beers(matched_beers, db)
    db.add_log('pol_beers', updated)
Esempio n. 3
0
def update_ratebeer(conn_str=None):
    db = Database(conn_str)
    breweries, breweries_updated = read_ratebeer_breweries()
    update_rb_breweries(breweries, db)
    db.add_log('ratebeer_breweries', breweries_updated)

    beers, beers_updated = read_ratebeer_beers()
    update_rb_beers(beers, db)
    db.add_log('ratebeer_beers', beers_updated)
Esempio n. 4
0
def read(conn_str=None):
    db = Database(conn_str)
    r = requests.get(URL)
    r.encoding = 'ISO-8859-1'
    lines = r.text.splitlines()
    parser = get_line_parser(FIELDS)
    shops = [parse_line(line, parser) for line in lines[1:]]
    shop_ids = get_ids()
    for shop in shops:
        shop['Kategori'] = int(shop['Kategori'].replace('Kategori ', ''))
        shop['id'] = shop_ids.get(shop['Butikknavn'], None)
    save_shops(shops, db)
    datetime = get_datetime(shops)
    db.add_log('pol_shops', datetime)
Esempio n. 5
0
def read(conn_str=None):
    db = Database(conn_str)
    r = requests.get(URL)
    r.encoding = "ISO-8859-1"
    lines = r.text.splitlines()
    parser = get_line_parser(FIELDS)
    shops = [parse_line(line, parser) for line in lines[1:]]
    shop_ids = get_ids()
    for shop in shops:
        shop["Kategori"] = int(shop["Kategori"].replace("Kategori ", ""))
        shop["id"] = shop_ids.get(shop["Butikknavn"], None)
    save_shops(shops, db)
    datetime = get_datetime(shops)
    db.add_log("pol_shops", datetime)
Esempio n. 6
0
def read(conn_str=None):
    db = Database(conn_str)
    r = requests.get(URL)
    r.encoding = 'ISO-8859-1'
    lines = r.text.splitlines()
    parser = get_line_parser(FIELDS)
    shops = [parse_line(line, parser) for line in lines[1:]]
    shop_ids = get_ids()
    for shop in shops:
        shop['Kategori'] = int(shop['Kategori'].replace('Kategori ', ''))
        shop['id'] = shop_ids.get(shop['Butikknavn'], None)
    save_shops(shops, db)
    datetime = get_datetime(shops)
    db.add_log('pol_shops', datetime)
def update_brewery_positions(conn_str=None):
    db = Database(conn_str)
    countries = {'154': 'NO'}

    matches = []
    for rb_id, iso_code in countries.iteritems():
        matches += map_breweries_for_country(db, rb_id, iso_code)
    db.run_upserts(SQL, matches)

    ids = [m['osm_id'] for m in matches]
    delete_sql = '''
        DELETE FROM rb_brewery_position where osm_id NOT IN %s;
    '''
    db.execute_sql(delete_sql, tuple(ids))

    db.add_log('osm')
Esempio n. 8
0
def update_pol_beers(conn_str=None):
    db = Database(conn_str)
    beers_polet, updated = read_pol_beers()

    breweries_rb = db.get_rb_breweries()
    breweries_pol = get_breweries_polet(beers_polet)

    matched_pol_rb = match_pol_breweries(breweries_pol, breweries_rb)

    matched_beers = []
    for rb_beer_id, pol_breweries in matched_pol_rb.iteritems():
        rb_brewery = find_in_list(breweries_rb, 'id', rb_beer_id)['name']
        rb_beers_for_brewery = db.get_rb_beers_for_brewery(rb_beer_id)
        matched_beers += match_pol_beers(rb_brewery, rb_beers_for_brewery,
                                         pol_breweries, beers_polet, db)
    save_pol_beers(matched_beers, db)
    db.add_log('pol_beers', updated)
Esempio n. 9
0
def update_pol_stock(conn_str=None):
    db = Database(conn_str)
    beer_ids = [beer['id'] for beer in db.get_pol_beers()]
    shops = [pol['id'] for pol in db.get_pol_shops()]

    for shop_id in shops:
        stock_for_pol = get_pol_stock_for_pol(shop_id)
        stock_by_pol = []
        for stock in stock_for_pol:
            beer_id = stock['product_id']
            if beer_id in beer_ids:
                stock_by_pol.append({
                    'shop_id': shop_id,
                    'pol_beer_id': beer_id,
                    'stock': stock['stock'],
                    'updated': stock['updated']
                })
        save_stock(stock_by_pol, db)

    db.add_log('pol_stock')
Esempio n. 10
0
async def handle_client(reader, writer):

    # get param in square brackets of client requests
    def get_param(inp):
        param = re.search(r"\[(.+)\]", inp)
        return param.group(1)

    # convert images needed for day menu to bytes
    def prepare_images(day):
        rows = db.get_food_by_day(day)
        img_list = []
        try:
            for row in rows:
                with open(dir_path + "/images/" + row[1], "rb") as img_file:
                    img_list.append(img_file.read())
            return img_list
        except Exception as e:
            print('Something wrong happened!')
            print(e)

    request = None
    data = "ERROR. Invalid Request. Try again."
    try:
        while request != 'quit':
            db = Database()

            request = (await reader.read(255)).decode()

            # handling of client requests
            if request:
                print(f"--------------------------\nCLIENT SAYS: {request}")
                if "GET FOOD BY DAY" in request:
                    day = get_param(request)
                    rows = db.get_food_by_day(day)
                    if len(rows) != 0:
                        data = rows
                elif "GET FOODCOURTS" in request:
                    rows = db.get_foodcourts()
                    if len(rows) != 0:
                        data = rows
                elif "GET PROMOS" in request:
                    rows = db.get_promos()
                    if len(rows) != 0:
                        data = rows
                elif "GET IMAGES" in request:
                    day = get_param(request)
                    rows = prepare_images(day)
                    if rows:
                        data = rows
                elif "LOG PURCHASE" in request:
                    details = get_param(request)
                    if "," in details:
                        log_details = details.split(",")
                        db.add_log(log_details[0], log_details[1])
                        data = "Log successful."

                data_send = pickle.dumps(data)

                print(f"Size of data: {len(data_send)}")
                writer.write(data_send)

            await writer.drain()
            writer.close()

    # client finishes connection
    except ConnectionResetError:
        print("Connection ended.\n")