Пример #1
0
def assign_work(table_name):
    height = config.block_init_height
    thread_pool = []
    db_lock = threading.Lock()
    while True:
        try:
            url = "https://www.blockchain.com/btc/block-height/" + str(height)
            r = requests.get(url)
            soup = BeautifulSoup(r.content, features='lxml')
            tr = soup.find_all("tr")
            block_hash = tr[2].find_all("td")[1].text
            block_time = tr[5].find_all("td")[1].string
        except Exception as e:
            print(e,height)
            continue

        if utils.date_to_timestamp(block_time) > utils.date_to_timestamp(config.block_stop_time):
            if thread_pool.__len__() < config.thread_pool_max_size:
                thread = threading.Thread(target=get_block_tx, args=[block_hash,height, table_name, db_lock])
                thread.start()
                thread_pool.append(thread)
                print(str(height) + " "+block_time)
                height = height -1
            else:
                for thread in thread_pool:
                    if not thread.isAlive():
                        thread_pool.remove(thread)
        else:
            print("current block time = " + block_time + ", stop block time = " + config.block_stop_time)
            return
Пример #2
0
def update_tx(tx, raw_cache, block_cache):
    url = 'https://www.blockchain.com/btc/tx/' + tx[0]
    r = requests.get(url)
    soup = BeautifulSoup(r.content, features='lxml')
    button = soup.find(id=re.compile("tx-*"))
    try:
        if button.find_all('button')[0].string != 'Unconfirmed Transaction!':
            raw_cache.add_item(tx[0], 0, False)
            row = soup.find_all("tr")
            index = 6
            if row.__len__() == 18:
                index = 7
            block_time = row[index].find_all("td")[1].text.split('(')[1].lstrip().split(' +')[0]
            block_time = utils.date_to_timestamp(block_time)

            tx_ = dict()
            tx_['hash'] = tx[0]
            tx_['size'] = tx[1]
            tx_['receive_time'] = tx[6]
            tx_['block_time'] = block_time
            tx_['total_input'] = tx[2]
            tx_['total_output'] = tx[3]
            tx_['fees'] = tx[4]
            tx_['fee_rate'] = tx[5]

            block_cache.add_item(tx_['hash'], tx_, True)

    except Exception as e:
        print(str(e))
        print(tx[0])
Пример #3
0
    def update_new_round(self, button):
        if not self._input_bounty.input_text:
            self._plugin._modal.show_error('please enter a round bounty')
            return

        round_info = (
            utils.date_to_timestamp(self._calendar_start._datetime),  # start
            utils.diff_seconds(self._start_datetime,
                               self._end_datetime),  # duration
            60 * 60 * 24 * 7,  # review
            self._plugin._web3.to_wei(self._input_bounty.input_text)  # bounty
        )
        self._menu_select_winners.select_winners(1, round_info)
Пример #4
0
    def create_tournament(self,
                          content,
                          bounty,
                          entryfee,
                          round_bounty,
                          start_datetime,
                          end_datetime,
                          review_length=60 * 60 * 24 * 7):
        start = utils.date_to_timestamp(start_datetime)
        duration = utils.diff_seconds(start_datetime, end_datetime)

        t_details = (content, bounty, entryfee)
        r_details = (start, duration, review_length, round_bounty)

        fn = self._contract.functions.createTournament(t_details, r_details)
        return self._plugin._web3.send_tx(fn)
Пример #5
0
def get_new_tx_info(hash_str, raw_cache, block_cache):
    url = 'https://www.blockchain.com/btc/tx/' + hash_str
    r = requests.get(url)
    soup = BeautifulSoup(r.content, features='lxml')
    button = soup.find(id=re.compile("tx-*"))
    try:
        is_confirmed = button.find_all('button')[0].string != 'Unconfirmed Transaction!'

        row = soup.find_all("tr")
        size = row[3].find_all("td")[1].string.split("(")[0]
        # print('size = ' + size)

        receive_time = row[5].find_all("td")[1].string.lstrip()
        # print(receive_time)

        receive_time = utils.date_to_timestamp(receive_time)

        total_input_index = 8

        block_time = 0

        if is_confirmed:
            block_time = row[6].find_all("td")[1].text.split('(')[1].lstrip().split(' +')[0]
            block_time = utils.date_to_timestamp(block_time)
            # print(blockinfo)
            total_input_index = 10

        if row.__len__() == 16 or row.__len__() == 18:
            total_input_index = total_input_index + 1

        total_input = row[total_input_index].find_all("td")[1].find("span").string.split(' ')[0]
        total_input = utils.format_value(total_input)
        # print(total_input)

        total_output = row[total_input_index + 1].find_all("td")[1].find("span").string.split(' ')[0]
        total_output = utils.format_value(total_output)
        # print(total_output)

        fees = row[total_input_index + 2].find_all("td")[1].find("span").string.split(' ')[0]
        fees = utils.format_value(fees)
        # print(fees)

        feerate = row[total_input_index + 3].find_all("td")[1].string.lstrip().rstrip().split(' ')[0]
        feerate = utils.format_value(feerate)
        # print(feerate)

        tx = dict()
        tx['hash'] = hash_str
        tx['size'] = size
        tx['receive_time'] = float(receive_time)
        tx['block_time'] = float(block_time)
        tx['total_input'] = float(total_input)
        tx['total_output'] = float(total_output)
        tx['fees'] = float(fees)
        tx['fee_rate'] = float(feerate)

        if is_confirmed:
            block_cache.add_item(tx['hash'], tx, True)
        else:
            raw_cache.add_item(tx['hash'], tx, True)
    except Exception as e:
        print(str(e))
Пример #6
0
def get_block_tx_info(hash_str):
    url = 'https://www.blockchain.com/btc/tx/' + hash_str
    while True:
        try:
            r = requests.get(url)
            break
        except Exception as e:
            print(e)
    soup = BeautifulSoup(r.content, features='lxml')
    try:

        row = soup.find_all("tr")
        size = row[3].find_all("td")[1].string.split("(")[0]
        # print('size = ' + size)

        weight = row[4].find_all("td")[1].string
        # print(weight)

        receive_time = row[5].find_all("td")[1].string.lstrip()
        # print(receive_time)

        receive_time = utils.date_to_timestamp(receive_time)

        lock_time = -1

        block_time_index = 6
        total_input_index = 10

        if row.__len__() == 18:
            lock_time = row[6].find_all('td')[1].string.split('\n')[2].lstrip()
            block_time_index = block_time_index + 1
            total_input_index = total_input_index + 1

        block_time = row[block_time_index].find_all("td")[1].text.split('(')[1].lstrip().split(' +')[0]
        block_time = utils.date_to_timestamp(block_time)

        confirmations = row[block_time_index + 1].find_all("td")[1].string

        total_input = row[total_input_index].find_all("td")[1].find("span").string.split(' ')[0]
        total_input = utils.format_value(total_input)
        # print(total_input)

        total_output = row[total_input_index + 1].find_all("td")[1].find("span").string.split(' ')[0]
        total_output = utils.format_value(total_output)
        # print(total_output)

        fees = row[total_input_index + 2].find_all("td")[1].find("span").string.split(' ')[0]
        fees = utils.format_value(fees)
        # print(fees)

        fee_rate = row[total_input_index + 3].find_all("td")[1].string.lstrip().rstrip().split(' ')[0]
        fee_rate = utils.format_value(fee_rate)
        # print(fee_rate)

        fee_wrate = row[total_input_index + 4].find_all("td")[1].string.lstrip().rstrip().split(' ')[0]
        fee_wrate = utils.format_value(fee_wrate)

        transacted = row[total_input_index + 5].find_all("td")[1].text.lstrip().rstrip().split(' ')[0]
        transacted = utils.format_value(transacted)

        tx = dict()
        tx['hash'] = hash_str
        tx['size'] = int(size)
        tx['weight'] = int(weight)
        tx['lock_time'] = int(lock_time)
        tx['confirmations'] = int(confirmations)
        tx['receive_time'] = float(receive_time)
        tx['block_time'] = float(block_time)
        tx['total_input'] = float(total_input)
        tx['total_output'] = float(total_output)
        tx['fees'] = float(fees)
        tx['fee_rate'] = float(fee_rate)
        tx['fee_wrate'] = float(fee_wrate)
        tx['transacted'] = float(transacted)

        return tx
    except Exception as e:
        print(hash_str)
        print(str(e))
Пример #7
0
 def get(self, request):
     portfolios = UserPortfolio.objects.filter(
         user=request.user).order_by('date')
     user_data = [[date_to_timestamp(portfolio.date), portfolio.balance]
                  for portfolio in portfolios]
     return JsonResponse({'portfolio': user_data})
Пример #8
0
def merge_dataset(
    adl,
    obs,
    start_date,
    end_date,
    length=60,
    on_update=None,
    on_att="id",
    use_day_period=False,
):
    first_minute = date_to_timestamp(start_date)
    last_minute = date_to_timestamp(end_date)
    n_sens = max(obs[on_att]) + 1

    timestamps = []
    activities = []
    sensors = []
    periods = []
    for i, s in tqdm(enumerate(range(first_minute, last_minute + 1, length))):
        if on_update:
            on_update(
                i / ((last_minute - first_minute - 1) / length) * 100 + 1,
                "A" if (last_minute - first_minute) / 86400 < 15 else "B",
            )
        e = s + length - 1

        # Trova i sensori attivi al tempo s
        q = obs.query("@e >= start_time and end_time >= @s")
        sl = q[on_att].tolist()
        active_sensors = "".join("1" if x in sl else "0" for x in range(n_sens))

        # Trova l'attività al tempo s
        q = adl.query("@e >= start_time and end_time >= @s")
        if q.shape[0] == 0:
            # Se la configurazione precedente è uguale a quella attuale
            # probabilmente l'attività è la stessa
            if len(sensors) > 0 and active_sensors == sensors[-1]:
                activity = activities[-1]
            # Stato che indica 'nessuna attività'
            else:
                activity = max(adl["activity"]) + 1
        else:
            activity = q.iloc[0]["activity"]

        # Calcola il periodo della giornata
        period = day_period(s)
        if use_day_period:
            active_sensors = active_sensors + str(period)

        timestamps.append(s)
        activities.append(activity)
        sensors.append(active_sensors)
        periods.append(period)

    result = pd.DataFrame(
        columns=["timestamp", "activity", "sensors", "period"],
        data={
            "timestamp": timestamps,
            "activity": activities,
            "sensors": sensors,
            "period": periods,
        },
    )

    return result