def insert_db(values): connection = dbc.get_connection() with connection.cursor() as cursor: sql = "DROP TABLE IF EXISTS wetter" cursor.execute(sql) sql = "CREATE TABLE wetter (latitude float NOT NULL,longtitude float NOT NULL,timezone VARCHAR(60) NOT NULL, summary VARCHAR(120),icon VARCHAR(25),preciptype VARCHAR(25),temperature float NOT NULL ,time VARCHAR(30),apparentTemperature float, cuurently_humidity float, dewPoint float, windSpeed float, cloudCover float, visibility float, uvIndex float);" cursor.execute(sql) sql = "INSERT INTO wetter VALUES (" for list in values: for value in list: if isinstance(value,basestring): sql = sql + "'" + value + "'," else: sql = sql + str(value) + "," sql = sql[:-1] + "),(" sql = sql[:-2] + ";" cursor.execute(sql) connection.commit()
def scenario_simple_producer(queue_name, sender_name, receiver_name, msg_count): msg_count = int(msg_count) conn = get_connection() sender = Client(conn, sender_name) receiver = Client(conn, receiver_name) queue = Queue(conn, queue_name) for i in range(0, msg_count): print(sender.send_message(queue, content="{0}, {1}".format(i, time.time()), receiver=receiver))
def scenario_simple_consumer(queue_name, sender_name, receiver_name, msg_count): msg_count = int(msg_count) conn = get_connection() sender = Client(conn, sender_name) receiver = Client(conn, receiver_name) queue = Queue(conn, queue_name) msgs = list() for i in range(0, msg_count): raw_msg = receiver.pop(queue, sender=sender) msgs.append(raw_msg[4]) for msg in msgs: splitted_msg = msg.split(', ') (i, delay) = (int(splitted_msg[0]), time.time()-float(splitted_msg[1])) print("Received message {0} after a delay of {1} seconds.".format(i, delay))
def run(): conn = dbconn.get_connection() lastId = dbutil.get_last_blockId(conn) b = Blockchain() for block in b.stream_from(start_block=lastId + 1, full_blocks=True): print(block['block_id']) btxs = list(map(lambda x: x['operations'][0], block['transactions'])) for tx in btxs: if tx[0] == 'comment' and tx[1]['parent_author'] == '': try: meta = json.loads(tx[1]['json_metadata']) if tx[1]['body'].startswith("@@ ", ) == False: article = Article() article.author = tx[1]['author'] article.subject = tx[1]['title'] article.content = tx[1]['body'] article.tag = meta['tags'] html = mistune.markdown(article.content) article.text = BeautifulSoup( html, "html.parser").get_text().strip() article.preview = article.text[0:100] article.hashId = base64.b64encode( md5(article.preview.encode( "UTF-8")).digest())[0:32] article.created = datetime.datetime.strptime( block['timestamp'], '%Y-%m-%dT%H:%M:%S') article.url = "https://steemkr.com/@" + article.author + "/" + tx[ 1]['permlink'] if "image" in meta and len(meta['image']) > 0: article.img = meta['image'][0] try: if detect(article.text) == "ko": print(meta['tags']) dbutil.insert_article(conn, article) except: pass except: pass dbutil.insert_blockId(conn, block_num_from_hash(block['block_id']), block['timestamp'])