def index_base_info(request): """ Trias Blockchain BaseInfo :param request: """ transactions_history = OrderedDict() data = {} try: # 24-hour trading rate and 7-day trading history transactionRate = 0 for i in range(7): nowtime = time.time() end = int( time.mktime( datetime.datetime.fromtimestamp( nowtime).date().timetuple()) - 86400 * i + 86400) # i days ago timestamp start = int( time.mktime( datetime.datetime.fromtimestamp( nowtime).date().timetuple()) - 86400 * i + 1) # i+1 days ago timestamp x = time.strftime("%m/%d", time.localtime(start)) tx_count = TransactionInfo.objects.filter( Q(timestamp__lte=end) & Q(timestamp__gte=start)).count() if i == 0: transactionRate = round(tx_count / 24, 2) transactions_history[x] = tx_count data['transactionsHistory'] = transactions_history data['transactionRate'] = transactionRate data['addresses'] = Address.objects.count() data['transactions'] = TransactionInfo.objects.count() data['lastBlock'] = Block.objects.last( ).number if Block.objects.exists() else 0 data['lastBlockFees'] = 'None' data['lastTransactionFees'] = 'None' data['totalDifficulty'] = 'None' # The number of active addresses in the last 7 days addr_end = int(time.time()) addr_satrt = int(time.time() - 86400 * 7) data['unconfirmed'] = Address.objects.filter( Q(time__lte=addr_end) & Q(time__gte=addr_satrt)).count() # Address balance ranking richList = [] addresses = Address.objects.all().order_by('-time', '-id') if addresses.exists(): richList = list(addresses.values('address', 'balance', 'time'))[:8] for addr in richList: addr['time'] = stamp2datetime(addr['time']) data['richList'] = richList except Exception as e: logger.error(e) return JsonResponse({"code": 200, "return_data": data})
def address_info(request): """ Address Detail Info :param address: address id :return: """ address = request.GET.get('address') if not address: return JsonResponse({"code": 201, "message": "Need Address ID"}) try: data = list(Address.objects.filter(address=address).values())[0] data['time'] = stamp2datetime(data['time']) data['txCount'] = TransactionInfo.objects.filter( Q(source=address) | Q(to=address)).count() sent = 0 received = 0 sent_list = TransactionInfo.objects.filter(source=address).values_list( 'value', flat=True) for sent_value in sent_list: sent += int(sent_value) received_list = TransactionInfo.objects.filter(to=address).values_list( 'value', flat=True) for rec_value in received_list: received += int(rec_value) data['sent'] = sent data['received'] = received except Exception as e: logger.error(e) return JsonResponse({"code": 201, "message": "Address Error"}) return JsonResponse({"code": 200, "return_data": data})
def nullify_session(self, errmsg): # Sets Variables to ensure error handling logger.info(errmsg) self.content = [] self.remaining = 0 self.limit = 0 self.error = errmsg logger.error('Nullifying Session...')
def func_wrapper(*args, **kwargs): try: return func(*args, **kwargs) except SQLAlchemyError as sqlE: logger.error(sqlE) return sqlE except Exception as e: logger.error(e) return e
def server_error(error): name = getattr(error, 'name', 'UNKNOWN') code = getattr(error, 'code', 0) description = getattr(error, 'description', None) or \ getattr(error, 'msg', 'UNKNOWN') error = {'name': name, 'code': code, 'description': description} logger.error('ERROR: {}|{}'.format(code, name)) logger.error('ERROR AT: {}'.format(request.path)) return render_template('error.html', error=error), int(code)
def address_transactions(request): """ Transactions Of Address :param address: :param size: :param page: :return: """ size = request.GET.get("size", 3) page = request.GET.get("page", 1) address = request.GET.get("address") if not address: return JsonResponse({"code": 201, "message": "Need Address ID"}) try: size = int(size) page = int(page) if size <= 0 or page <= 0: size = 3 page = 1 except Exception as e: size = 3 page = 1 try: transactions = TransactionInfo.objects.filter( Q(source=address) | Q(to=address)).order_by('-blockNumber') pag = Paginator(transactions, size) if page > pag.num_pages: page = 1 data = list( pag.page(page).object_list.values('hash', 'gasPrice', 'source', 'value', 'to', 'gasUsed', 'blockNumber')) for item in data: item['time'] = stamp2datetime( Block.objects.get(number=item['blockNumber']).timestamp) item['fees'] = item['gasPrice'] * item['gasUsed'] item['confirmations'] = Block.objects.last().number - \ item['blockNumber'] except Exception as e: logger.error(e) return JsonResponse({"code": 201, "message": "ERROR"}) return JsonResponse({ "code": 200, "total_size": len(transactions), "page": page, "total_page": pag.num_pages, "return_data": data })
def block_transactions(request): """ Transactions Of Block :param size: per page count :param page: current page :param block_hash: :param sort: :return: """ size = request.GET.get("size", 3) page = request.GET.get("page", 1) block_hash = request.GET.get("block_hash") sort = request.GET.get("sort", '-id') if not block_hash: return JsonResponse({"code": 201, "message": "Need Block Hash"}) try: size = int(size) page = int(page) if size <= 0 or page <= 0: size = 3 page = 1 except Exception as e: size = 3 page = 1 try: total_data = TransactionInfo.objects.filter( blockHash=block_hash).order_by(sort) if total_data.count() == 0: return JsonResponse({ "code": 200, "total_size": 0, "return_data": [] }) pag = Paginator(total_data, size) if page > pag.num_pages: page = 1 data = list(pag.page(page).object_list.values()) except Exception as e: logger.error(e) return JsonResponse({"code": 201, "message": "ERROR"}) return JsonResponse({ "code": 200, "total_size": len(total_data), "page": page, "total_page": pag.num_pages, "return_data": data })
def get_repo_data(): #TODO: Clean this shit up folders = [f for f in repo.content if f.path.endswith('-code')] repo_json = {'folders': {}} errors = [] for folder in folders: message_data = None files_by_group = defaultdict(dict) folder_name = folder.name for file_data in folder.raw_data: if file_data['name'].endswith('.name'): folder_name = file_data['name'].replace('.name', '') continue if file_data['name'] == 'message.html': message_data = file_data continue chunks = file_data['name'].split('_') if len(chunks) != 2: logger.error('Skipping Gist. Could not parse: ' + str(chunks)) errors.append('Could not Parse: {}'.format(str(chunks))) continue file_group, file_name = chunks files_by_group[file_group][file_name] = {'name': file_name.split('.')[0], 'id': file_name.replace('.','-'), 'data': file_data } folder_json = {'name': folder_name, 'contents': files_by_group } repo_json['folders'][folder.name] = folder_json if message_data: repo_json['folders'][folder.name]['message'] = message_data logger.info('Gettings Repo. Rate: {}/{}'.format(repo.remaining, repo.limit)) if hasattr(repo, 'error'): errors.append(repo.error.data['message']) if not repo_json['folders']: errors.append('repository did not return valid data') if errors: repo_json['error'] = errors return repo_json
def all_transactions(request): """ Transactions List :param size: per page count :param page: current page :return: """ size = request.GET.get("size", 50) page = request.GET.get("page", 1) try: size = int(size) page = int(page) if size <= 0 or page <= 0: size = 50 page = 1 except Exception as e: size = 50 page = 1 try: total_data = TransactionInfo.objects.all().order_by( '-blockNumber', '-timestamp') pag = Paginator(total_data, size) if page > pag.num_pages: page = 1 data = list( pag.page(page).object_list.values('hash', 'source', 'to', 'value', 'blockNumber', 'blockHash', 'tx_str', 'type')) for item in data: number = item['blockNumber'] item['time'] = stamp2datetime( Block.objects.get(number=number).timestamp) except Exception as e: logger.error(e) return JsonResponse({"code": 201, "message": "ERROR"}) return JsonResponse({ "code": 200, "total_size": total_data.count(), "page": page, "total_page": pag.num_pages, "return_data": data })
def __init__(self): logger.info('Initializing Gist Session') self._session = Github(GithubRepoWrapper.OAUTH) try: self._session.get_user().id except Exception as errmsg: logger.error('GitWrapper Error:') self.nullify_session(errmsg) else: self.update_rates() self._user = self._session.get_user() self.update_content() self.limit = self._session.get_rate_limit().rate.limit logger.info('Rate Limit: {}/{}'.format(self.remaining, self.limit)) if self.limit < 1: self.nullify_session('Rate Limit Exceeded')
def search_db(pattern=None, field=None): """ Searches db_index.json If looking up by href, which is the key, a dictionary look up is used, else it uses list comprehensian + regex search """ if field == 'href': return db_json.get(pattern) try: results = [ member for member in db_json.values() if re.search(pattern, member.get(field)) ] except Exception as errmsg: logger.error('search_db query error: {}|{}'.format(pattern, field)) return [] else: return results
def all_blocks(request): """ Blocks List :param size: per page count :param page: current page :return: """ size = request.GET.get("size", 50) page = request.GET.get("page", 1) try: size = int(size) page = int(page) if size <= 0 or page <= 0: size = 50 page = 1 except Exception as e: size = 50 page = 1 try: total_data = Block.objects.all().order_by('-number') pag = Paginator(total_data, size) if page > pag.num_pages: page = 1 data = list( pag.page(page).object_list.values('hash', 'number', 'transactionsCount', 'size', 'blockReward', 'timestamp')) for item in data: item['time'] = stamp2datetime(item['timestamp']) item['avgFee'] = 0.00332 except Exception as e: logger.error(e) return JsonResponse({"code": 201, "message": "ERROR"}) return JsonResponse({ "code": 200, "total_size": total_data.count(), "page": page, "total_page": pag.num_pages, "return_data": data })
def index_latest_blocks(request): """ Latest Blocks 20 Numbers :param request: :return: """ data = [] try: blocks = Block.objects.all().order_by('-number')[:20] if blocks.exists(): data = list( blocks.values('number', 'size', 'timestamp', 'hash', 'blockReward')) for item in data: item['time'] = stamp2datetime(item['timestamp']) except Exception as e: logger.error(e) return JsonResponse({"code": 200, "size": len(data), "return_data": data})
def prioritize_match(results=None, raw_query=None, field=None): """If Search query matches title exactly (no fuzzy or space forgiveness) result is pushed up to top of list """ try: exact_query = '^{query}$'.format(query=raw_query) pattern = re.compile(exact_query, re.IGNORECASE) except Exception as errmsg: logger.error( 'prioritize: Could not compile raw_query: {}'.format(exact_query)) return results prioritized_results = [] for member in results: if re.match(pattern, member.get(field)): prioritized_results.insert(0, member) logger.debug('Priority Match Found: {}'.format(pattern)) else: prioritized_results.append(member) # import pdb; pdb.set_trace() return prioritized_results
def address_info(request): """ Address Detail Info :param address: address id :return: """ address = request.GET.get('address') if not address: return JsonResponse({"code": 201, "message": "Need Address ID"}) try: data = list(Address.objects.filter(address=address).values())[0] data['time'] = stamp2datetime(data['time']) data['txCount'] = TransactionInfo.objects.filter( Q(source=address) | Q(to=address)).count() sent = 0 received = 0 sent_list = TransactionInfo.objects.filter(source=address).values_list( 'value', flat=True) for sent_value in sent_list: sent += int(sent_value) received_list = TransactionInfo.objects.filter(to=address).values_list( 'value', flat=True) for rec_value in received_list: received += int(rec_value) data['sent'] = sent data['received'] = received except Exception as e: logger.error(e) return JsonResponse({"code": 201, "message": "Address Error"}) # try: # url = 'http://' + jc.eth_ip + '/api/' # chain = simple_request(url, params={"method":"personal_listAccounts","params":"aa"}) # data['chain'] = chain.get('chain_id', '') # except Exception as e: # logger.error(e) return JsonResponse({"code": 200, "return_data": data})
def index_recent_transactions(request): """ Recent Transactions 20 Numbers :param request: :return: """ data = [] try: transactions = TransactionInfo.objects.all().order_by( '-blockNumber')[:20] if transactions.exists(): data = list( transactions.values('source', 'to', 'hash', 'blockNumber')) for item in data: block_number = item['blockNumber'] item['time'] = stamp2datetime( Block.objects.get(number=block_number).timestamp) except Exception as e: logger.error(e) return JsonResponse({"code": 200, "size": len(data), "return_data": data})
def block_info(request): """ Block Detail Info :param block_hash: current block hash :return: """ block_hash = request.GET.get('block_hash') if not block_hash: return JsonResponse({"code": 201, "message": "Need Block Hash"}) try: block_info = Block.objects.filter(hash=block_hash).values('number', 'transactionsCount', 'timestamp', 'size', 'difficulty', 'nonce', 'parentHash', 'miner', 'gasLimit', 'gasUsed', 'blockReward') if block_info.exists(): block_info = list(block_info)[0] block_info['time'] = stamp2datetime(block_info['timestamp']) number = block_info['number'] else: block_info = url_data(url, "eth_getBlockByHash", [block_hash, True])['result'] block_info['time'] = stamp2datetime(hex2int(block_info['timestamp'])) block_info['transactionsCount'] = len(block_info['transactions']) number = hex2int(block_info['number']) block_info['number'] = number block_info['blockReward'] = 3 * 10 ** 18 block_info['confirmations'] = IndexInfo.objects.last().lastBlock - number if block_info['confirmations'] > 0: next_block_number = number + 1 block_info['nextHash'] = url_data(url, "eth_getBlockByNumber", [hex(next_block_number), True])['result'][ 'hash'] else: block_info['nextHash'] = 'N/A' except Exception as e: logger.error(e) return JsonResponse({"code": 201, "message": "ERROR"}) return JsonResponse({"code": 200, "return_data": block_info})
def transaction_info(request): """ Transaction Detail Info :param tx_hash: current transaction hash :return: """ tx_hash = request.GET.get('tx_hash') if not tx_hash: return JsonResponse({"code": 201, "message": "Need Transaction Hash"}) data = [] try: data = list(TransactionInfo.objects.filter(hash=tx_hash).values())[0] number = data['blockNumber'] block = Block.objects.get(number=number) data['gasLimit'] = block.gasLimit data['time'] = stamp2datetime(block.timestamp) data['confirmations'] = IndexInfo.objects.last().lastBlock - number except Exception as e: logger.error(e) return JsonResponse({"code": 200, "return_data": data})
def get_current_price(stock_symbol): # south korea ticker symbol starts with a number while that of United states starts with an alphabet x = re.match("[0-9]", stock_symbol) country = "south korea" if x else "united states" try: current_price_raw = investpy.stocks.get_stock_recent_data( stock_symbol, country, as_json=True, order='descending', interval='Daily') except Exception as e: logger.error("{}, get_current_price() error : ".format( datetime.datetime.now().strftime('%H:%M:%S')) + str(e)) return handle_errors(e) current_price_dict = json.loads(current_price_raw) current_price = current_price_dict['recent'][0]['close'] return current_price
def get_best_entry_match(entry, target_year): """Figures out the best match based on the target year. The best match is self is content is the first to exists or has been updated if That doesn't apply For example is the years entry are: {'years': {'2015': 'exists', '2016': 'unchanged', '2017': 'updated', '2017.1': 'unchanged'}} Target > Best Match: - 2015 > 2015 - 2016 > 2015 - 2017 > 2017 - 2017.1 > 2017 """ years_dict = entry['years'] if target_year not in years_dict: return None if years_dict[target_year] == 'exists' or years_dict[ target_year] == 'updated': return target_year possible_years = [ year for year, status in years_dict.items() if year < target_year and ( (status == 'exists') or (status == 'updated')) ] if not possible_years: logger.error('Could not Find best match for: {}:{}'.format( entry['href'], target_year)) raise Exception('Could not find template match f') else: last_possible_year = sorted(possible_years)[-1] return last_possible_year
def block_info(request): """ Block Detail Info :param block_hash: current block hash :return: """ block_hash = request.GET.get('block_hash') if not block_hash: return JsonResponse({"code": 201, "message": "Need Block Hash"}) try: block_info = Block.objects.filter(hash=block_hash).values( 'number', 'transactionsCount', 'timestamp', 'size', 'difficulty', 'nonce', 'parentHash', 'miner', 'gasLimit', 'gasUsed', 'blockReward') if not block_info.exists(): return JsonResponse({ "code": 201, "message": "The block doesn't exist" }) block_info = list(block_info)[0] block_info['time'] = stamp2datetime(block_info['timestamp']) number = block_info['number'] block_info['confirmations'] = Block.objects.last().number - number if block_info['confirmations'] > 0: next_block_number = number + 1 block_info['nextHash'] = Block.objects.get(number=number + 1).hash else: block_info['nextHash'] = 'N/A' except Exception as e: logger.error(e) return JsonResponse({"code": 201, "message": "ERROR"}) return JsonResponse({"code": 200, "return_data": block_info})
def serach(request): """ Search From number/blockHash/txHash/address :param key: tags :return: """ key = request.GET.get('key') if not key: return JsonResponse({"code": 201, "message": 'Need a key'}) try: # search block by block number isBlock = Block.objects.filter(number=key) if isBlock.exists(): return JsonResponse({ "code": 200, "data_type": "block", "block_hash": isBlock[0].hash }) logger.warning("Search Block Number Error") # search block by block hash isBlock = Block.objects.filter(hash=key) if isBlock.exists(): return JsonResponse({ "code": 200, "data_type": "block", "block_hash": key }) logger.warning("Search Block Hash Error") # search transaction by tx hash isTx = TransactionInfo.objects.filter(hash=key) if isTx.exists(): return JsonResponse({ "code": 200, "data_type": "transaction", "tx_hash": key }) logger.error("Search Transaction Error") # search address by tx hash isAddress = Address.objects.filter(address=key) if isAddress.exists(): return JsonResponse({ "code": 200, "data_type": "address", "address": key }) logger.error("Search Address Error") except Exception as e: logger.error(e) return JsonResponse({"code": 201, "message": 'Error key'})
def func_wrapper(*args, **kwargs): try: return func(*args, **kwargs) except ValidationError as e: logger.error(e) return e
def serach(request): """ Search From number/blockHash/txHash/address :param key: tags :return: """ key = request.GET.get('key') if not key: return JsonResponse({"code": 201, "message": 'Need a key'}) # try search block by block number try: if int(key) > IndexInfo.objects.last().lastBlock: return JsonResponse({"code": 201, "message": 'Error Block Number'}) isBlock = Block.objects.filter(number=key) if isBlock.exists(): return JsonResponse({ "code": 200, "data_type": "block", "block_hash": isBlock[0].hash }) hash = url_data(url, "eth_getBlockByNumber", [hex(int(key)), True])['result']['hash'] return JsonResponse({ "code": 200, "data_type": "block", "block_hash": hash }) except: logger.error("Search Block Number Error") # try search block by block hash try: isBlock = Block.objects.filter(hash=key) if isBlock.exists(): return JsonResponse({ "code": 200, "data_type": "block", "block_hash": key }) else: hash = url_data(url, "eth_getBlockByHash", [key, True])['result']['hash'] return JsonResponse({ "code": 200, "data_type": "block", "block_hash": hash }) except: logger.error("Search Block Hash Error") # try search transaction by tx hash try: isTx = TransactionInfo.objects.filter(hash=key) if isTx.exists(): return JsonResponse({ "code": 200, "data_type": "transaction", "tx_hash": key }) isAddress = Address.objects.filter(address=key) if isAddress.exists(): return JsonResponse({ "code": 200, "data_type": "address", "address": key }) except: logger.error("Search Transaction Or Address Error") return JsonResponse({"code": 201, "message": 'Error key'})