Last active
February 16, 2019 16:15
-
-
Save vindard/c43921936504ab29d1eab2885d8edf4a to your computer and use it in GitHub Desktop.
Small script to parse the results of `$ lncli listchannels` and return a csv file for my tracking spreadsheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json, csv, requests, sys | |
from subprocess import check_output | |
# Get channels raw data | |
savefile = 'channels.csv' | |
''' | |
channel_source = 'listchannels.txt' | |
with open(channel_source, 'r') as f: | |
all_channels = json.loads(f.read())['channels'] | |
''' | |
listchannels = json.loads(check_output(["lncli","listchannels"]).decode("utf-8")) | |
all_channels = listchannels['channels'] | |
headers = [ | |
'remote_pubkey', | |
'channel_point', | |
'chan_id', | |
'active', | |
'capacity', | |
'local_balance', | |
'remote_balance', | |
] | |
#Unpack channels into a list | |
channels_list = [list(headers)] # 'list(..)' to avoid common memory assignment | |
for chan_as_dict in all_channels: | |
chan_as_list = [] | |
for h in headers: | |
chan_as_list.append(chan_as_dict[h]) | |
channels_list.append(chan_as_list) | |
# Fetch aliases from 1ml.com and add to the list | |
alias_index = 0 # where in channel list 'alias' should be added | |
channels_list[0].insert(alias_index, 'alias') | |
print("Starting alias fetch\n------") | |
for i, chan in enumerate(channels_list[1:]): | |
pubkey_index = headers.index('remote_pubkey') | |
pubkey = chan[pubkey_index] | |
node_info = json.loads(check_output(["lncli", "getnodeinfo", pubkey]).decode("utf-8")) | |
alias = node_info['node']['alias'] | |
''' | |
data = requests.get("https://1ml.com/node/{}/json".format(pubkey)) | |
try: | |
alias = json.loads(data.text)['alias'] | |
except: | |
alias = '[Private channel]' | |
''' | |
print("Adding {} of {} | {} ...".format(i+1, len(channels_list[1:]), alias)) | |
channels_list[i+1].insert(alias_index,alias) | |
# Write channels_list to csv file | |
with open(savefile, 'w') as c: | |
writer = csv.writer(c, delimiter=',') | |
for chan in channels_list: | |
writer.writerow(chan) | |
warn, bold, _end= '\033[93m', '\033[1m', '\033[0m' | |
print("\n{}Results saved to {}'{}'{}".format(bold, warn, savefile, _end)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Script was originally created to be run on any machine using a
listchannels.txt
file (obtained from anlnd
node by running$ lncli listchannels > listchannels.txt
) as input and pinging 1ml.com foralias
info.Script was then modified to instead be run directly on an
lnd
node and to uselncli
calls for all its data input & query requirements.