<commit_before>#! /usr/bin/env python3

import json
import urllib.request
import csv
import datetime
from datetime import tzinfo

class BLKAPI(object):
	
	api_url = "http://blockchain.info/address/"
	api_address = "1KMLjhgjGLxZTJT5TSWxV2HCZHBqpDEpa1"
    # Temporary Hardcode
	api_format = "?format=json"
  # Transactions
	blktrans = []

	def initiallogic(self) :
		numtrans = 0
		initialcall = self.call(0)
		self.numberOfTransactions = initialcall["n_tx"]
		self.populatetrans(initialcall["txs"])
		numtrans += 50
		while self.numberOfTransactions > numtrans :
			subsequentcalls = self.call(numtrans)
			self.populatetrans(subsequentcalls["txs"])
			numtrans += 50
			
			
		'''
		for txs in initialcall["txs"]:
			inout = self.getdiff(txs)
			txtime = datetime.datetime.fromtimestamp(int(txs["time"]))
			self.blktrans.append([ txtime.strftime(fmt), self.api_address , txs["hash"] , inout["in"] , inout["out"] ])
		'''

	def populatetrans(self, alltrans):
		# Date Format YYYY-MM-DD HH:MM:SS TZ
		fmt = '%y-%m-%d'
		fmttime = '%H:%M:%S'
		for txs in alltrans:
			inout = self.getdiff(txs)
			txtime = datetime.datetime.fromtimestamp(int(txs["time"]))
			self.blktrans.append([ txtime.strftime(fmt), txtime.strftime(fmttime), self.api_address , txs["hash"] , inout["in"] , inout["out"] ])

	
	def call(self, offset):
		if offset == 0 :
			# No need of Offset api
			api_call = self.api_url + self.api_address + self.api_format
		else:
			api_call = self.api_url + self.api_address + self.api_format + "&offset=" + str(offset)
		print("Trying API Call:\n " + api_call)
		request = urllib.request.Request(api_call)
		response = urllib.request.urlopen(request)
		xjson = json.loads((response.read().decode('utf-8')))
		return xjson
	
	def normalize(self):
		for item in self.blktrans:
			if item[4] == 0 or item[5] == 0 :
				## No real Normalization here. One of these is already 0
				continue;
			elif item[4] > item[5] :
				## Net Positive Transaction
				tempfour = item[4] - item[5]
				## Positive Transaction Zero Leaving in the End
				item[5] = 0
				## Set Adjusted Postive Amount
				item[4] = tempfour
			elif item[5] > item[4] :
				## Net Negative Transaction
				tempfive = item[5] = item[4]
				## Negative Transaction Zero Incoming in the End
				item[4] = 0
				## Set Adjusted Negative Amount
				item[5] = tempfive
		
		return
		
	def bitcoinit(self):
		for item in self.blktrans:
			# Convert to BTC
			item[4] = float(item[4])/100000000
			item[5] = float(item[5])/100000000
		
		return
		
	def getdiff(self, datx):
		# Find the change
		satoshisin = 0
		satoshisout = 0
		for inputaddr in datx["inputs"]:
			if "prev_out" in inputaddr :
				if inputaddr["prev_out"]["addr"] == self.api_address :
					# Correct Address
					satoshisout = satoshisout + inputaddr["prev_out"]["value"]
			else :
				# No Input Transaction; Do Nothing
				continue;
		#print(datx["out"])
		for outputaddr in datx["out"]:
			if "addr" in outputaddr:
				# Object Exists Keep Going
				if outputaddr["addr"] == self.api_address :
					# Correct Address
					# print("Amount Value Up:\n " + str(outputaddr["value"]))
					satoshisin = satoshisin + outputaddr["value"]
			else:
				# Object totally Doesn't. Fuck that bitch!
				continue;
		inout = { "in" : satoshisin , "out" : satoshisout }
		return inout
		
	def writetocsv(self, filename):
		
		## Normalize First
		self.normalize()
		self.bitcoinit()
		
		## Create Output File
		output = open(filename, 'w', newline='', encoding='utf8')
		## Create CSV Writer
		wr = csv.writer(output, quotechar=None)
		
		# Write Headers
		wr.writerow(["Date","Time","Account","Description", "Money In", "Money Out"])
		
		## Write Transactions
		for item in self.blktrans :
			# Print the Transaction Pythonically
			print (item)
			# Print the Transaction CSV Style to File
			wr.writerow(item)<commit_msg>Removing normalize atm to see reduce chance of breaking<commit_after>#! /usr/bin/env python3

import json
import urllib.request
import csv
import datetime
from datetime import tzinfo

class BLKAPI(object):
	
	api_url = "http://blockchain.info/address/"
	api_address = "1KMLjhgjGLxZTJT5TSWxV2HCZHBqpDEpa1"
    # Temporary Hardcode
	api_format = "?format=json"
  # Transactions
	blktrans = []

	def initiallogic(self) :
		numtrans = 0
		initialcall = self.call(0)
		self.numberOfTransactions = initialcall["n_tx"]
		self.populatetrans(initialcall["txs"])
		numtrans += 50
		while self.numberOfTransactions > numtrans :
			subsequentcalls = self.call(numtrans)
			self.populatetrans(subsequentcalls["txs"])
			numtrans += 50
			
			
		'''
		for txs in initialcall["txs"]:
			inout = self.getdiff(txs)
			txtime = datetime.datetime.fromtimestamp(int(txs["time"]))
			self.blktrans.append([ txtime.strftime(fmt), self.api_address , txs["hash"] , inout["in"] , inout["out"] ])
		'''

	def populatetrans(self, alltrans):
		# Date Format YYYY-MM-DD HH:MM:SS TZ
		fmt = '%y-%m-%d'
		fmttime = '%H:%M:%S'
		for txs in alltrans:
			inout = self.getdiff(txs)
			txtime = datetime.datetime.fromtimestamp(int(txs["time"]))
			self.blktrans.append([ txtime.strftime(fmt), txtime.strftime(fmttime), self.api_address , txs["hash"] , inout["in"] , inout["out"] ])

	
	def call(self, offset):
		if offset == 0 :
			# No need of Offset api
			api_call = self.api_url + self.api_address + self.api_format
		else:
			api_call = self.api_url + self.api_address + self.api_format + "&offset=" + str(offset)
		print("Trying API Call:\n " + api_call)
		request = urllib.request.Request(api_call)
		response = urllib.request.urlopen(request)
		xjson = json.loads((response.read().decode('utf-8')))
		return xjson
	
	def normalize(self):
		for item in self.blktrans:
			if item[4] == 0 or item[5] == 0 :
				## No real Normalization here. One of these is already 0
				continue;
			elif item[4] > item[5] :
				## Net Positive Transaction
				tempfour = item[4] - item[5]
				## Positive Transaction Zero Leaving in the End
				item[5] = 0
				## Set Adjusted Postive Amount
				item[4] = tempfour
			elif item[5] > item[4] :
				## Net Negative Transaction
				tempfive = item[5] = item[4]
				## Negative Transaction Zero Incoming in the End
				item[4] = 0
				## Set Adjusted Negative Amount
				item[5] = tempfive
		
		return
		
	def bitcoinit(self):
		for item in self.blktrans:
			# Convert to BTC
			item[4] = float(item[4])/100000000
			item[5] = float(item[5])/100000000
		
		return
		
	def getdiff(self, datx):
		# Find the change
		satoshisin = 0
		satoshisout = 0
		for inputaddr in datx["inputs"]:
			if "prev_out" in inputaddr :
				if inputaddr["prev_out"]["addr"] == self.api_address :
					# Correct Address
					satoshisout = satoshisout + inputaddr["prev_out"]["value"]
			else :
				# No Input Transaction; Do Nothing
				continue;
		#print(datx["out"])
		for outputaddr in datx["out"]:
			if "addr" in outputaddr:
				# Object Exists Keep Going
				if outputaddr["addr"] == self.api_address :
					# Correct Address
					# print("Amount Value Up:\n " + str(outputaddr["value"]))
					satoshisin = satoshisin + outputaddr["value"]
			else:
				# Object totally Doesn't. Fuck that bitch!
				continue;
		inout = { "in" : satoshisin , "out" : satoshisout }
		return inout
		
	def writetocsv(self, filename):
		
		## Normalize First
		##self.normalize()
		self.bitcoinit()
		
		## Create Output File
		output = open(filename, 'w', newline='', encoding='utf8')
		## Create CSV Writer
		wr = csv.writer(output, quotechar=None)
		
		# Write Headers
		wr.writerow(["Date","Time","Account","Description", "Money In", "Money Out"])
		
		## Write Transactions
		for item in self.blktrans :
			# Print the Transaction Pythonically
			print (item)
			# Print the Transaction CSV Style to File
			wr.writerow(item)
{'fraction_non_alphanumeric': 0.1340392561983471, 'fraction_numerical': 0.014721074380165289, 'mean_word_length': 3.5909899229401305, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1572722', 'n_tokens_mistral': 3218, 'n_tokens_neox': 2674, 'n_words': 663}