def create_store(): post_body = json.loads(request.data) name = post_body['name'] coordinate = post_body['coordinate'] store = Store(name=name, coordinate=coordinate) db.session.add(store) db.session.commit() return json.dumps({'success': True, 'data': store.serialize()}), 200
def traverse(addr, map_nccc, session): retailer = requests.get(addr) content_big5 = retailer.content.decode('cp950') soup = BeautifulSoup(content_big5, "html.parser") table = soup.find('table', bgcolor='#fafaf5') all_tr = table.tr.find_all('tr', valign='top') for i, each in enumerate(all_tr): item = each.find_all('td') name = item[0].get_text().strip() main = item[1].get_text().strip() phone = item[2].get_text().strip() address = item[3].get_text().strip() href = 'http://travel.nccc.com.tw' + item[5].a['href'] city, town, sub = detail(href) print(i + 1, name, phone, city, town, address, main, sub) res = session.query(Store).filter_by(name=name, address=address).first() print(res) if res is None: retailer = Store(name=name, phone=phone, address=address, city=city, town=town, main=main, sub=sub) latlon = locator(address) if latlon is not None: folium.Marker(location=latlon, popup=name).add_to(map_nccc) map_nccc.location = latlon map_nccc.save('nccc.html') print('\t' + str(latlon)) retailer.lat = latlon[0] retailer.lon = latlon[1] session.add(retailer) session.commit() next_a = table.previous_element.previous_element.previous_element next_href = 'http://travel.nccc.com.tw' + next_a['href'] print(next_href) if next_a.get_text() == '下一頁': traverse(next_href, map_nccc, session)
def maven(self, port, wsock_port): """ Collects all messages by listening for any incoming data from clients and stores it to a internal rrdb """ # internal messaging sub = nnpy.Socket(nnpy.AF_SP, nnpy.SUB) sub.bind('tcp://127.0.0.1:{}'.format(port)) sub.setsockopt(nnpy.SUB, nnpy.SUB_SUBSCRIBE, '') # messaging to browser (webssocket) wsock = nnpy.Socket(nnpy.AF_SP, nnpy.PUB) wsock.bind('ws://*:{}'.format(wsock_port)) db = Store() i = 0 self.init_batch() while True: msg = sub.recv() self._debug(msg) msgJson = json.loads(msg.decode("utf-8")) client = msgJson['node'] module = msgJson['module'] if module in self.modules.keys(): wsock.send(msg) # add message to batch if client not in self.batch[module]: self.batch[module] = {client: []} self.batch[module][client].append(msg.decode("utf-8")) i += 1 else: print("Unknown module '{}'. Ignored.".format(module)) if i >= 10: # add to db db.write(self.batch) self.init_batch() i = 0
def status(): store = Store() client = ApiClient() start = store.start_point() end = store.end_point() srs = store.get_srs() witnesses = get_witnesses(client) print '' templ = '%-11s' print fire.core.formatting.Bold(templ % 'Start Block') + ': ' + str(start) print fire.core.formatting.Bold(templ % ' End Block') + ': ' + str(end) print '' templ = '%-45s %-40s %-30s %-10s' print fire.core.formatting.Bold( templ % ('SR Address', 'SR NAME', 'OutOfTime Txs', 'Total Blocks')) for sr in srs: oft_cnt = store.get_sr_oft_cnts(sr) if witnesses.has_key(sr): sr_name = witnesses[sr]['url'] else: sr_name = '-' block_cnt = store.get_sr_blocks_cnts(sr) print(templ % (sr, sr_name, oft_cnt, block_cnt))
from db import Store __author__ = 'pahaz' schema = ( ('id', int, 3), ('v', str, 20), ) import os os.system('rm test.db') s = Store('test.db', schema, 5) for x in range(2): s.execute("insert values (2, 'privert')") s.execute("insert values (3, 'poka')") s.execute("insert values (4, 'hohoohoh')") s._memory_dump() z = s.execute('select where id > 2 limit 1') assert z['id'] == 3, 'select limit' z = s.execute('select where id > 2 limit 1 desk') assert z['id'] == 4, 'select limit desk' z = s.execute('select * where id = 2') assert len(z) == 2, 'select * id = 2' z = s.execute('delete where id > 2 limit 1') z = s.execute('select where id > 2')
from flask_cors import CORS, cross_origin app = Flask(__name__, static_url_path='', static_folder='frontend/dist', template_folder='frontend/dist') env = Env() env.read_env() cors = CORS(app) app.config['DEBUG'] = env.bool('DEBUG', default=True) app.config['SECRET_KEY'] = env.str('SECRET_KEY', default=uuid.uuid4().hex) app.config['CORS_HEADERS'] = 'Content-Type' db = Store() # flask routes @app.route('/') def home(): try: context = {'modules': server.modules} except: context = {} return render_template("index.html", **context) @app.route('/data/<module>') @cross_origin() def get_data(module):
def parse(start=-1, end=10000000000): if start < -1: print fire.core.formatting.Error( 'err: start parameter must be greater than 0.') return if end < 0: print fire.core.formatting.Error( 'err: end parameter must be greater than 0.') return if start >= end: print fire.core.formatting.Error( 'err: the value of the start parameter must be less than the end parameter.' ) return store = Store() client = ApiClient() now = client.get_now_blocknumber() db_start = store.start_point() db_end = store.end_point() if start == -1: start = db_end + 1 if end == 10000000000: end = now new_db_start = db_start new_db_end = db_end process_start = 0 process_end = 0 if db_start == -1: process_start = new_db_start = start process_end = new_db_end = end else: if start > db_end + 1: print fire.core.formatting.Error( 'err: the start parameter cannot be greater than %d' % db_end) return else: process_start = start if start < db_start: new_db_start = start if end < db_start - 1: print fire.core.formatting.Error( 'err: the end parameter cannot be less than %d' % db_start) return else: process_end = end if end >= db_end: new_db_end = end size = end - start + 1 util.progress_msg('parsing Blocks from trongrid, from %d to %d' % (process_start, process_end)) with tqdm(total=size, unit='blocks') as pbar: for i in range(start, end + 1): parse_one_block(client, store, i) pbar.update(1) store.update_start_point(new_db_start) store.update_end_point(new_db_end) pbar.close()
def txs(witness, recent_days=0, min_block=0, max_block=0): store = Store() srs = store.get_srs() trx_list = [] def extend_trx(trxs): trx_list.extend(trxs) def traversing_blocks(start, end): for block_number in range(start, end + 1): trxs = store.get_sr_block_oft_trxs(witness, block_number) extend_trx(trxs) db_start = store.start_point() db_end = store.end_point() process_start = db_start process_end = db_end if recent_days == 0 and min_block == 0 and max_block == 0: pass elif recent_days != 0: sec = recent_days * 24 * 60 * 60 blocks = int(sec / 3) start = db_end - blocks if start < db_start: print fire.core.formatting.Error( 'specified time span is too large and needs ' 'to be parsed from block %d and currently %d' % (start, db_start)) return process_start = start process_end = db_end elif recent_days == 0 and min_block != 0 or max_block != 0: if min_block >= max_block: print fire.core.formatting.Error( 'min_block must be less than max_block') return if min_block < db_start or max_block > db_end: print fire.core.formatting.Error( 'The specified time range is incorrect and ' 'must be between %d and %d' % (db_start, db_end)) return process_start = min_block process_end = max_block else: print fire.core.formatting.Error( 'min_block and max_block specify errors') return traversing_blocks(process_start, process_end) start_timestamp = store.get_block_time(process_start) end_timestamp = store.get_block_time(process_end) util.status_msg_div() util.status_msg( 'Time Range:', '%s ~ %s' % (util.timestamp_to_strftime(start_timestamp), util.timestamp_to_strftime(end_timestamp))) util.status_msg('Block Range:', '%d ~ %d' % (process_start, process_end)) util.status_msg_div() templ = '%-70s %-16s %-25s %-20s' print fire.core.formatting.Bold(templ % ('TxId', 'Ret', 'Time', 'FuncID')) for trx in trx_list: txid = trx['txID'] ret = trx['ret'][0]['contractRet'] funcid = trx['raw_data']['contract'][0]['parameter']['value']['data'][ 0:8] timestamp = trx['raw_data']['timestamp'] strftime = util.timestamp_to_strftime(timestamp) print(templ % (txid, ret, strftime, funcid))
def query(recent_days=0, min_block=0, max_block=0): store = Store() srs = store.get_srs() srs_cnts_map = {} def add_srs_cnts(sr, counter): if not srs_cnts_map.has_key(sr): srs_cnts_map[sr] = 0 srs_cnts_map[sr] += counter def traversing_blocks(start, end): for block_number in range(start, end + 1): for sr in srs: cnts = store.get_sr_block_oft_cnts(sr, block_number) add_srs_cnts(sr, cnts) db_start = store.start_point() db_end = store.end_point() db_min_timestamp = 0 db_max_timestamp = 0 if db_start != -1: db_min_timestamp = store.get_block_time(db_start) if db_end != -1: db_max_timestamp = store.get_block_time(db_end) client = ApiClient() witnesses = get_witnesses(client) process_start = db_start process_end = db_end if recent_days == 0 and min_block == 0 and max_block == 0: pass elif recent_days != 0: sec = recent_days * 24 * 60 * 60 blocks = int(sec / 3) start = db_end - blocks if start < db_start: print fire.core.formatting.Error( 'specified blocks span is too large and needs ' 'to be parsed from block %d and currently %d' % (start, db_start)) return process_start = start process_end = db_end elif recent_days == 0 and min_block != 0 or max_block != 0: if min_block >= max_block: print fire.core.formatting.Error( 'min_block must be less than max_block') return if min_block < db_start or max_block > db_end: print fire.core.formatting.Error( 'The specified time range is incorrect and ' 'must be between %d and %d' % (db_start, db_end)) return process_start = min_block process_end = max_block else: print fire.core.formatting.Error( 'min_block and max_block specify errors') return traversing_blocks(process_start, process_end) start_timestamp = store.get_block_time(process_start) end_timestamp = store.get_block_time(process_end) templ = '%-45s %-40s %-15s %-15s' util.status_msg_div() util.status_msg( ' Time Range:', '%s ~ %s' % (util.timestamp_to_strftime(start_timestamp), util.timestamp_to_strftime(end_timestamp))) util.status_msg('TimeStamp Range:', '%d ~ %d' % (start_timestamp, end_timestamp)) util.status_msg(' Block Range:', '%d ~ %d' % (process_start, process_end)) util.status_msg_div() print fire.core.formatting.Bold( templ % ('SR Address', 'SR NAME', 'OutOfTime Txs', 'Total Blocks')) for sr in srs: if witnesses.has_key(sr): sr_name = witnesses[sr]['url'] else: sr_name = '-' if srs_cnts_map.has_key(sr): oft_cnt = srs_cnts_map[sr] block_cnt = store.get_sr_blocks_cnts(sr) print(templ % (sr, sr_name, oft_cnt, block_cnt))
#!/usr/bin/env python from db import DatabaseSession, Coupon, Store if __name__ == '__main__': session = DatabaseSession() store = Store(store_type=1, zip_code='14586') session.add_model(store) store.coupons = [Coupon(brand='Samsung', discount=0.05)] session.commit_changes()