onest
This commit is contained in:
commit
631859263d
192
november.py
Normal file
192
november.py
Normal file
|
|
@ -0,0 +1,192 @@
|
|||
# november.py
|
||||
# alif radhitya <alif@radhitya.org>
|
||||
# usage:
|
||||
# $ python november.py
|
||||
# option:
|
||||
# 1 is with
|
||||
# 2 is without
|
||||
|
||||
import mysql.connector
|
||||
import time
|
||||
import subprocess
|
||||
import sys
|
||||
import csv
|
||||
import math
|
||||
import os
|
||||
|
||||
from web3 import Web3
|
||||
from mysql.connector import Error
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
source_table = "vitals-experiment"
|
||||
dir_name = "/tmp/"
|
||||
|
||||
file_names = [f"file_{i}_{i*2}jam.csv" for i in range(1, 13)]
|
||||
|
||||
def upload_berkas(filenya):
|
||||
print("uploading to ipfs container")
|
||||
print(f"file {filenya} dulu")
|
||||
start_time = time.perf_counter()
|
||||
try:
|
||||
with open(f"{dir_name}{filenya}", "rb") as f:
|
||||
result = subprocess.check_output(
|
||||
["docker", "exec", "-i", "ipfs_node1", "ipfs", "add"], stdin=f).decode()
|
||||
|
||||
end_time = time.perf_counter()
|
||||
durasi = end_time - start_time
|
||||
cid = result.split()[1]
|
||||
|
||||
print(f"\nIPFS: {cid}")
|
||||
print(f"\n{durasi:.6f} detik")
|
||||
return cid
|
||||
except Exception:
|
||||
return ""
|
||||
|
||||
def to_blockchain(message, amount):
|
||||
if not message:
|
||||
return
|
||||
rpc_url = "http://192.168.1.132:8545"
|
||||
w3 = Web3(Web3.HTTPProvider(rpc_url))
|
||||
|
||||
priv_key = "8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63"
|
||||
if not priv_key:
|
||||
return
|
||||
|
||||
account = w3.eth.account.from_key(priv_key)
|
||||
target_address = "0xf17f52151EbEF6C7334FAD080c5704D77216b732"
|
||||
|
||||
hex_data = w3.to_hex(text=str(message))
|
||||
tx = {
|
||||
'nonce': w3.eth.get_transaction_count(account.address),
|
||||
'to': target_address,
|
||||
'value': w3.to_wei(amount, 'ether'),
|
||||
'gasPrice': w3.eth.gas_price,
|
||||
'data': hex_data,
|
||||
'chainId': 1337
|
||||
}
|
||||
try:
|
||||
tx['gas'] = w3.eth.estimate_gas(tx)
|
||||
signed_tx = w3.eth.account.sign_transaction(tx, priv_key)
|
||||
tx_hash = w3.eth.send_raw_transaction(signed_tx.raw_transaction)
|
||||
print(f"TX Hash: {w3.to_hex(tx_hash)}")
|
||||
|
||||
receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
|
||||
|
||||
print(f"value: {amount} ETH")
|
||||
print(f"gas used: {receipt.gasUsed} units")
|
||||
print(f"total gas fee: {w3.from_wei(receipt.gasUsed * tx['gasPrice'], 'ether'):.18f} ETH")
|
||||
except Exception as e:
|
||||
print(f"x_x: {e}")
|
||||
|
||||
def connect_db():
|
||||
try:
|
||||
mydb = mysql.connector.connect(
|
||||
host="192.168.1.239",
|
||||
user="root",
|
||||
password="root123",
|
||||
database="darsinurse"
|
||||
)
|
||||
if mydb.is_connected():
|
||||
print("mydb is connected ^_^")
|
||||
return mydb, mydb.cursor()
|
||||
except Error as e:
|
||||
print(f"error, debug: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
def catfile(file_path):
|
||||
try:
|
||||
with open(file_path, 'r') as file:
|
||||
return file.read()
|
||||
except Exception:
|
||||
return ""
|
||||
|
||||
def aksi(mycursor):
|
||||
try:
|
||||
mycursor.execute(f"SELECT * FROM `{source_table}`")
|
||||
except Error:
|
||||
print(f"ga ada tablenya")
|
||||
sys.exit(1)
|
||||
|
||||
myresult = mycursor.fetchall()
|
||||
|
||||
fps = [open(f"{dir_name}{name}", "w", newline='') for name in file_names]
|
||||
writers = [csv.writer(fp) for fp in fps]
|
||||
|
||||
try:
|
||||
for row in myresult:
|
||||
waktu = row[3]
|
||||
total_menit = (waktu.hour * 60) + waktu.minute
|
||||
|
||||
start_idx = math.ceil(total_menit / 120) - 1
|
||||
if start_idx < 0: start_idx = 0
|
||||
|
||||
for i in range(max(0, start_idx), 12):
|
||||
writers[i].writerow(row)
|
||||
finally:
|
||||
for fp in fps:
|
||||
fp.close()
|
||||
|
||||
print("mantap selesai fetch database!")
|
||||
|
||||
def option_menu():
|
||||
print("\nselect an option: 1 (with) or 2 (without) or 3 (imagine) or \
|
||||
4 (what is this)")
|
||||
|
||||
def imagine():
|
||||
for name in file_names:
|
||||
content = catfile(f"{dir_name}{name}")
|
||||
if content:
|
||||
print(f"{name}: {content[:50]}...")
|
||||
|
||||
def what_is_this():
|
||||
rpc_url = "http://192.168.1.132:8545"
|
||||
w3 = Web3(Web3.HTTPProvider(rpc_url))
|
||||
tx_hash_input = input("enter tx hash: ").strip()
|
||||
|
||||
try:
|
||||
tx = w3.eth.get_transaction(tx_hash_input)
|
||||
message_raw = tx['input']
|
||||
message_hex_str = w3.to_hex(message_raw)
|
||||
message_string = w3.to_text(message_raw)
|
||||
|
||||
print(f"tx hash: {tx_hash_input}")
|
||||
print(f"\nmessage (hex): {message_hex_str}")
|
||||
print(f"\nmessage (string): {message_string}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"x_x: {e}")
|
||||
|
||||
def handle_choice(choice):
|
||||
if choice == '1':
|
||||
mydb, cursor = connect_db()
|
||||
aksi(cursor)
|
||||
for name in file_names:
|
||||
upload = upload_berkas(name)
|
||||
to_blockchain(upload, 0.001)
|
||||
mydb.close()
|
||||
elif choice == '2':
|
||||
mydb, cursor = connect_db()
|
||||
aksi(cursor)
|
||||
for name in file_names:
|
||||
result = catfile(f"{dir_name}{name}")
|
||||
to_blockchain(result, 0.001)
|
||||
mydb.close()
|
||||
elif choice == '3':
|
||||
imagine()
|
||||
elif choice == '4':
|
||||
what_is_this()
|
||||
else:
|
||||
print("nothing o_o")
|
||||
return True
|
||||
|
||||
def main():
|
||||
while True:
|
||||
option_menu()
|
||||
user_input = input("enter thy choice: ")
|
||||
if not handle_choice(user_input):
|
||||
break
|
||||
|
||||
if __name__ == "__main__":
|
||||
os.system('clear')
|
||||
main()
|
||||
Loading…
Reference in New Issue
Block a user