Bitcoin Transaction Mapping Throws KeyError - python

I have the following piece of code, which seems to run until line 36 recipientlist.append(target["addr"]) and then throws the error KeyError: 'addr'
However 'addr' seems to be in the data so not sure what the issue is
Can someone please help?
import json
import requests
z = 0
i = 0
firstpart = "https://blockchain.info/rawaddr/"
initialinput = '3PaGEcGDjPsNQHAQ4pTmjQuLXWoEwvnr11'
initialreq = firstpart + initialinput
firstjson = (requests.get(initialreq)).json()
graphvizlines = []
addresslist = []
usedaddresslist = []
addresslist.append(initialinput)
usedaddresslist.append(initialinput)
while i < 6:
if z is 1:
initialreq = firstpart + addresslist[i]
firstjson = (requests.get(initialreq)).json()
for transaction in firstjson["txs"]:
payerlist = []
recipientlist = []
print("\n" + transaction["hash"])
for item in transaction["inputs"]:
payerlist.append(item["prev_out"]["addr"])
if item["prev_out"]["addr"] not in addresslist:
addresslist.append(item["prev_out"]["addr"])
for target in transaction["out"]:
recipientlist.append(target["addr"])
if target["addr"] not in addresslist:
addresslist.append(target["addr"])
for payer in payerlist:
for recipient in recipientlist:
a = '"' + payer + '"' + " -> " + '"' + recipient + '"' + ";"
if a not in graphvizlines:
graphvizlines.append(a)
i = i + 1
z = 1
for t in graphvizlines:
print(t)

While addr is in your data, it's not in every inputs element. Check the very last element in txs, you'll see that inputs is:
"inputs": [
{
"sequence": 0,
"witness": "304402203f872bfd7093fcdad6a3735cbd76f276279890b0304e6f23f54c51388cc2a84402203731d7a7f71265f072f6792c8f4d2e805ff8f86bbfbd0b48a187d573c051593001",
"prev_out": {
"spent": true,
"spending_outpoints": [
{
"tx_index": 0,
"n": 0
}
],
"tx_index": 0,
"type": 0,
"value": 1880609,
"n": 1,
"script": "0014292738ed3f9466f8eedd8c49e5bb013088a7052b"
},
"script": ""
}
],
This element lacks the presence of prev_out.addr.
You will need to first check if the addr element exists or wrap your loop in a try/except.
for transaction in firstjson['txs']:
...
for item in transaction['inputs']:
address = item.get('prev_out').get('addr')
if(address == None):
continue
payerlist.append(address)
...
The above would still fail if prev_out didn't exist, so you should confirm what will be in the result and what might be.

Related

Design a JSON structure to create a display

I'm requesting for help in creating a JSON structure that will help in creating a display in Command Prompt using python. I have a draft JSON structure that is given below. I can change the below structure to best suit the display.
Plan is to use {}.format() to create the display.
[
{
"Acc1": [
{
"clientId": "Acc1",
"Strategy": "MONTHLY_IRON_CONDOR",
"StrategyList": [
"MONTHLY_IRON_CONDOR",
"MONTHLY_IRON_CONDOR",
"MANUAL"
],
"strikeType": "PE",
"CurrentM2M": 1850.0,
"MaxStopLoss": 24195.0,
"MaxProfit": 8065.0
},
{
"clientId": "Acc1",
"Strategy": "MONTHLY_IRON_CONDOR",
"StrategyList": [
"MONTHLY_IRON_CONDOR",
"MONTHLY_IRON_CONDOR",
"MANUAL"
],
"strikeType": "CE",
"CurrentM2M": -8075.0,
"MaxStopLoss": 36060.0,
"MaxProfit": 12020.0
}
]
},
{
"Acc2": [
{
"clientId": "Acc2",
"Strategy": "LONGTERM_STRANGLE",
"StrategyList": [
"LONGTERM_STRANGLE",
"LONGTERM_STRANGLE"
],
"strikeType": "PE",
"CurrentM2M": 405.0,
"MaxStopLoss": 21735.0,
"MaxProfit": 7245.0
},
{
"clientId": "Acc2",
"Strategy": "LONGTERM_STRANGLE",
"StrategyList": [
"LONGTERM_STRANGLE",
"LONGTERM_STRANGLE"
],
"strikeType": "CE",
"CurrentM2M": -3105.0,
"MaxStopLoss": 36015.0,
"MaxProfit": 12005.0
}
]
}
]
Output Structure:
Strategies Acc1 Acc2
P&L | SL | MaxPL P&L | SL | MaxPL
MONTHLY_IRON_CONDOR ----> PE 1850.0 24195.0 8065.0
CE -8075.0 36060.0 12020.0
LONGTERM_STRANGLE ------> PE 405.0 21735.0 7245.0
CE -3105.0 36015.0 12005.0
I've already tried the extract the data best out of the above JSON using the python code given below;
import json
import os
import random
import sys
import time
import pandas as pd
home = os.path.dirname(os.path.dirname(
os.path.dirname(os.path.realpath(__file__))))
sys.path.append(home)
home = os.path.dirname(os.path.dirname(
os.path.dirname(os.path.realpath(__file__))))
tmp_path = str(os.path.join(home, "tmp"))
# Set base path:--------------------------------------------------------
sys.path.append(home)
os.chdir(home)
def clear_screen():
os.system('cls' if os.name == 'nt' else 'clear')
a = 0
while True:
clear_screen()
positionsList = None
if (os.path.isfile(os.path.join(tmp_path, "input"))):
with open(os.path.join(tmp_path, "input"), "r") as f:
positionsList = json.loads(f.read(), strict=False)
clientIdList = []
headerClientId = " Strategies " # This variable will hold the top Header line that will contain dynamic
# number of Accounts
headerNames = " " # This variable will hold the text "P&L | SL | MaxPL" for all
the accounts
monthlyIC = " " # Strategy Specific data for MONTHLY_IRON_CONDOR
longtermStrangle = " " # Strategy Specific data for LONGTERM_STRANGLE
noData = " " # Filler when there is no data for a specific strategy for a given
account
for client in positionsList:
values = list(client.values())[0]
flg = False
for value in values:
for clientId in client.keys():
if flg == False:
clientIdList.append(clientId)
headerClientId = headerClientId + "{} "
headerNames = headerNames + "P&L | SL | MaxPL "
flg = True
availableStrategies = ["MONTHLY_IRON_CONDOR",'INTRADAY_STRANGLE", "LONGTERM_STRANGLE", "PUT_SPREAD"]
unusedStrategies = list(
set(availableStrategies) - set(value['StrategyList'])) # Helps to fill "noData" for these strategies for each
# Account
if Strategy.monthlyIronCondor.value in unusedStrategies and "\n" not in monthlyIC:
monthlyIC = monthlyIC + noData
elif Strategy.longTermStrangle.value in unusedStrategies and "\n" not in longtermStrangle:
longtermStrangle = longtermStrangle + noData
if value['Strategy'] == Strategy.monthlyIronCondor.value:
monthlyIC = monthlyIC + "" + str(value['strikeType']) + " " + str(value['CurrentM2M']) + \
" " + str(value['MaxStopLoss']) + \
" " + str(value['MaxProfit'])
if value['Strategy'] == Strategy.longTermStrangle.value:
longtermStrangle = longtermStrangle + "" + str(value['strikeType']) + " " + str(value['CurrentM2M']) + \
" " + str(value['MaxStopLoss']) + \
" " + str(value['MaxProfit'])
if monthlyIC.replace(' ', '', -1) != '':
monthlyIC = monthlyIC + "\n "
if longtermStrangle.replace(' ', '', -1) != '':
longtermStrangle = longtermStrangle + "\n "
# if longtermStrangle.replace(' ', '', -1) == '':
# longtermStrangle = longtermStrangle + noData
headerClientId = headerClientId.format(clientIdList[0], clientIdList[1])
print(headerClientId, end='\n\r')
print(headerNames, end='\n\r')
print("MONTHLY_IRON_CONDOR ----> " + monthlyIC, end='\n\r')
print("LONGTERM_STRANGLE ------> " + longtermStrangle, end='\n\r')
time.sleep(1)
a += 1

Change Python string

I have a long string that looks like below. This is a small part of it, but the pattern repeat.
The raw data that I get when I read the API with Requests:
...
}
#VER B 160 20201020 "Test Bolag AB (4117)" 20201223
{
#TRANS 3001 {6 "1000050"} -180000 "" "" 0
#TRANS 2611 {6 "1000050"} -45000 "" "" 0
#TRANS 1510 {6 "1000050"} 225000 "" "" 0
}
#VER A 2 20200212 "Test Bolag AB1" 20201223
{
#TRANS 1930 {} -7549 "" "" 0
#TRANS 2641 {} 1209.75 "" "" 0
#TRANS 7990 {} 6339.25 "" "" 0
}
...
The code I've written now:
lst = r.text.split('}\r\n')
for i in range(len(lst)):
tmpstr1 = str(lst[i])
tmpstr2 = tmpstr1.replace(" ",";")
tmpstr3 = tmpstr2.replace("\\r","")
tmpstr4 = tmpstr3.replace("\\n","")
tmpstr5 = re.sub(r'[^A-Za-z0-9;,#-.]', '', tmpstr4)
tmpstr6 = tmpstr5.replace("#VER;","")
tmplst1 = tmpstr6.split('#TRANS;')
tmpstr7 = str(tmplst1)
tmpstr8 = str(tmplst1[0])
tmpstr9 = tmpstr7.replace(";0",tmpstr8)
tmpstr10 = re.sub(r'[^A-Za-z0-9;,-]', '', tmpstr9)
tmpstr11 = tmpstr10.strip()
tmplst2 = tmpstr11.split(',')
tmplst2.pop(0)
lst[i] = str(tmplst2)
print(lst[200])
This is what I get now:
['3001;6;1000050;-180000;;B;160;20201020;Kundfaktura;Test;Bolag;AB;4117;20201223', '2611;6;1000050;-45000;;B;160;20201020;Kundfaktura;Test;Bolag;AB;4117;20201223', '1510;6;1000050;225000;;B;160;20201020;Kundfaktura;Test;Bolag;AB;4117;20201223']
This is what I want to get:
3001;6;1000050;-180000;;B;160;20201020;Kundfaktura;Test Bolag AB;4117;20201223
2611;6;1000050;-45000;;B;160;20201020;Kundfaktura;Test Bolag AB;4117;20201223
1510;6;1000050;225000;;B;160;20201020;Kundfaktura;Test Bolag AB;4117;20201223
Thanks in advance!
Just save them in arrays of an array.
So you would get every entry in an array, which will make it more dynamically for future use.
For this define an array and append after every for loop.

How to remove bracket from JSON in Flask

I want to remove one fo them bracket from JSON in Flask. I want to get result from database and convert to JSON.
i=0
a = []
for food in sorted_similar_food:
if i==0:
i = i+1
else:
name = get_title_from_index(food[0])
name = str(name)
db_cursor.execute("SELECT * FROM recipe where name = " + "'" + name + "'")
r = [dict((db_cursor.description[i][0], value)
for i, value in enumerate(row)) for row in db_cursor.fetchall()]
a.append( r)
return jsonify({'cursor': a})
AND my result JSON
{ "cursor": [
[
{
"id": 3,
"image": "https://firebasestorage.googleapis.com/v0/b/cricket-17449.appspot.com/o/manti.jpg?alt=media&token=d11a65ec-6486-4b24-a54a-2840ce4fdc",
"ind": "kiyma yumurta sogan un",
"name": "manti",
"recip": "Ge"
}
],
....
]}
There are two brackets. I want to one of them. I should remove one of them.
You start with an empty list a=[]. Inside your loop, you append a list of DB results (r is another list). What you want to do is a.extend(r). That way the elements inside r are appended to a and you end up with just the a list.

Question with extracting data from Json using Python

I am building a bot game for my friends in LINE. I'm a beginning coder. I'm trying to call an object in json which includes a string + integer. I've looked around but nothing seems to fit what I need. What would be the best/simple solution?
My code is amateur, please go easy on me. :P
I'm trying to have Python extract through Json, "Name" + "Stat".
Right now it only extracts "Name" and randomly selects an item. Is there any way to select the item + the stat, display the item and calculate the stat? Thanks.
Python 3:
if text == 'FIGHT':
with open('items.json', 'r') as f:
data = json.load(f)
armor1 = [v for d in data['armor'] for k,v in d.items() if k == 'name']
weapon1 = [v for d in data['weapon'] for k,v in d.items() if k == 'name']
magic1 = [v for d in data['magic'] for k,v in d.items() if k == 'name']
armor2 = random.choice(armor1)
weapon2 = random.choice(weapon1)
magic2 = random.choice(magic1)
calc = add(int(armor2), int(weapon2), int(magic2))
line_bot_api.reply_message(
event.reply_token,
TextSendMessage('Armor = ' + (armor2)),
TextSendMessage('Weapon = ' + (weapon2)),
TextSendMessage('Magic = ' + (magic2)),
TextSendMessage('You have a score of ' + str(calc) + '.'),
TextSendMessage('Waiting for next opponent...')
)
Json:
"armor": [
{
"name":"Cardboard armor 10 DEF" ,
"stats":"10" },
{
"name":"Plastic armor 20 DEF" ,
"stats":"20" },
{
"name":"Rubber armor 30 DEF" ,
"stats":"30" },
{
"name":"Metal armor 40 DEF" ,
"stats":"40" },
{
"name":"Indestructable armor 50 DEF" ,
"stats":"50" }
],
After trying just about everything.. The solution was:
if text == 'FIGHT':
with open('items.json', 'r') as f:
data = json.load(f)
armor2 = random.choice(data['armor'])
weapon2 = random.choice(data['weapon'])
magic2 = random.choice(data['magic'])
calc = add(armor2['stats'], weapon2['stats'], magic2['stats'])
line_bot_api.reply_message(
event.reply_token, [
TextSendMessage('Armor = ' + (armor2['name'])),
TextSendMessage('Weapon = ' + (weapon2['name'])),
TextSendMessage('Magic = ' + (magic2['name'])),
TextSendMessage('Total = ' + str(calc) + '.')
]
)
Thanks to everyone and special thanks to my friend Sae who helped me. :)

python script to download youtube video

On giving youtube video url, I first download video page and extract javascript object between
<script>var ytplayer = ytplayer ..... </script>
I got
{
"args": {
"is_listed": "1",
"account_playback_token": "QUFFLUhqbWdXR1NfQjRiRmNzWVhRVTM0ajlNcnM1alVUd3xBQ3Jtc0tsVi01WFp5VmV2MTU3RnpkYUVkRzVqR1ZTNUI4T2JaQzk1ckxPejdVNkYzUk5zOTdjZnNmb1BYZHNLQ05nblZZbFk2ZWJXNHRPNVFoNVVNc2RjTE1YekdKSGY4dlVhSnlCU1ctNFZJdXBKbWhIRG1TZw==",
"ptk": "RajshriEntertainment",
"focEnabled": "1",
"tag_for_child_directed": false,
"adaptive_fmts": ......,
"probe_url": .....,
"rmktEnabled": "1",
"allow_ratings": "1",
"dbp": "ChoKFk5RNTV5UGs5bDZmSk5wSjQ4a3RiSHcQARABGAI",
"cc3_module": "1",
"no_get_video_log": "1",
"fmt_list": ......,
"title":..........,
"invideo": true,
"sffb": true,
"iurlmq_webp": ,
"cosver": "10_8_4",
"url_encoded_fmt_stream_map": .................,
"max_dynamic_allocation_ad_tag_length": "2040",
"innertube_api_key": "AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8",
"timestamp": "1446586407",
"cc_asr": "1",
"apiary_host_firstparty": "",
"adsense_video_doc_id": "yt_Vd4iNPuRlx4",
"innertube_context_client_version": "1.20151102",
"mpu": true,
"tmi": "1",
"ldpj": "-19",
"fade_out_duration_milliseconds": "1000",
.........
}
}
i found key adaptive_fmts and url_encoded_fmt_stream_map contain multiple url in percent-encoded form.
i take one url from url_encoded_fmt_stream_map it look like this
https://r1---sn-o3o-qxal.googlevideo.com/videoplayback?
ratebypass=yes&
signature=982E413BBE08CA5801420F9696E0F2ED691B99FA.D666D39D1A0AF066F76F12632A10D3B8076076CE&
lmt=1443906393476832&
expire=1446604919&
fexp=9406983%2C9408710%2C9414764%2C9416126%2C9417707%2C9421410%2C9422596%2C9423663&
itag=22&
dur=128.801&
source=youtube&
upn=pk2CEhVBeFM&
sver=3&
key=yt6&
id=o-AK-OlE5NUsbkp51EZY2yKuz5vsSGofgUvrvTtOrhC72e&
sparams=dur%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Cratebypass%2Crequiressl%2Csource%2Cupn%2Cexpire&
mime=video%2Fmp4&
ipbits=0&
pl=21&
ip=x.y.z.a&
initcwndbps=5405000&
requiressl=yes&
mn=sn-o3o-qxal&
mm=31&
ms=au&
mv=m&
mt=1446583222&
itag=22&
type=video/mp4
but when I paste this(above) url in browser nothing happen, I mean not work.
Please help me.
Also
What is difference between adaptive_fmts and url_encoded_fmt_stream_map containing urls?
In python2.7, this works:
import urlparse, urllib2
vid = "vzS1Vkpsi5k"
save_title = "YouTube SpaceX - Booster Number 4 - Thaicom 8 06-06-2016"
url_init = "https://www.youtube.com/get_video_info?video_id=" + vid
resp = urllib2.urlopen(url_init, timeout=10)
data = resp.read()
info = urlparse.parse_qs(data)
title = info['title']
print "length: ", info['length_seconds'][0] + " seconds"
stream_map = info['adaptive_fmts'][0]
vid_info = stream_map.split(",")
mp4_filename = save_title + ".mp4"
for video in vid_info:
item = urlparse.parse_qs(video)
#print 'quality: ', item['quality'][0]
#print 'type: ', item['type'][0]
url_download = item['url'][0]
resp = urllib2.urlopen(url_download)
print resp.headers
length = int(resp.headers['Content-Length'])
my_file = open(mp4_filename, "w+")
done, i = 0, 0
buff = resp.read(1024)
while buff:
my_file.write(buff)
done += 1024
percent = done * 100.0 / length
buff = resp.read(1024)
if not i%1000:
percent = done * 100.0 / length
print str(percent) + "%"
i += 1
break

Categories