Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 68 additions & 33 deletions helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from PIL import Image

from sqlalchemy import create_engine
from sqlalchemy import or_
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
Base = declarative_base()
Expand All @@ -22,6 +23,7 @@

DEFAULT_PRICE = 150
INVOICE_MEMO_PREFIX = "DH Channel Explorer for channel_id: "
DEFAULT_CHANNELCOUNT = 10

def debug(message):
sys.stderr.write(message + "\n")
Expand Down Expand Up @@ -53,7 +55,7 @@ def getInfo(self):

return info

def getChannels(self):
def getChannels(self, keyword=None, channelCount=None, page=None):
db_filename = 'sqlite:///' + os.path.join('./graph.db')

engine = create_engine(db_filename, echo=True)
Expand All @@ -62,11 +64,33 @@ def getChannels(self):
Session = sessionmaker()
Session.configure(bind=engine)
s = Session()
channels = s.query(channel.Channel).all()
q = s.query(channel.Channel).order_by(channel.Channel.channel_id)
if keyword:
q = q.filter(or_(channel.Channel.node2_alias.ilike(f"%{keyword}%"), channel.Channel.node2_pub == keyword))
if channelCount and page:
page = page - 1
sliceStart = channelCount * page
sliceEnd = sliceStart + channelCount
q = q.slice(sliceStart, sliceEnd)
channels = q.all()
s.close()

return channels

def getChannelTotalCount(self):
db_filename = 'sqlite:///' + os.path.join('./graph.db')

engine = create_engine(db_filename, echo=True)
Base.metadata.create_all(engine)

Session = sessionmaker()
Session.configure(bind=engine)
s = Session()
count = s.query(channel.Channel).count()
s.close()

return count

#----------------------------------------------------
#インボイス発行
#----------------------------------------------------
Expand Down Expand Up @@ -148,43 +172,54 @@ def checkInvoice(self, payment_hash=""):
resCheckInvoice = {
"paymentStatus": 0,
"lndResponse": lnd_result,
#"capacity": 0,
#"localBalance": 0,
#"remoteBalance": 0,
}
#debug
debug("Payment Status: Not Paid")

return resCheckInvoice

def search(self, keyword):
channels = self.getChannels()
if not keyword:
result = channels
else:
result = {}
j = 0
for i in range(len(channels)):
alias = channels[i].node2_alias
pubKey = channels[i].node2_pub
if keyword.lower() in alias.lower() or keyword == pubKey:
result[j] = channels[i]
j += 1

output = self.convertChannelsToOutput(result)
return output

def convertChannelsToOutput(self, channels):
def search(self, keyword=None, channelCount=None, page=None):

if channelCount or page:
if channelCount:
try:
channelCount = int(channelCount)
if channelCount < 1: raise
except:
channelCount = DEFAULT_CHANNELCOUNT
else:
channelCount = DEFAULT_CHANNELCOUNT

if page:
try:
page = int(page)
if page < 1: raise
except:
page = 1
else:
page = 1

channels = self.getChannels(keyword, channelCount, page)
channelTotal = self.getChannelTotalCount()
output = {}
for i in range(len(channels)):
output[i] = {
"channelId": str(channels[i].channel_id),
"alias": channels[i].node2_alias,
"capacity": channels[i].capacity,
"node1BaseFee": channels[i].node1_base_fee,
"node1FeeRate": channels[i].node1_fee_rate,
"node2PubKey": channels[i].node2_pub,
"node2BaseFee": channels[i].node2_base_fee,
"node2FeeRate": channels[i].node2_fee_rate,
channelsArray = []

for c in channels:
channelsArray.append({
"channelId": str(c.channel_id),
"alias": c.node2_alias,
"capacity": c.capacity,
"node1BaseFee": c.node1_base_fee,
"node1FeeRate": c.node1_fee_rate,
"node2PubKey": c.node2_pub,
"node2BaseFee": c.node2_base_fee,
"node2FeeRate": c.node2_fee_rate,
})

output = {
"channelCount": channelCount,
"channels": channelsArray,
"page": page,
"channelTotal": channelTotal,
}
return output
19 changes: 10 additions & 9 deletions proto.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,23 @@ def req_self():
def req_channels():
print('requesting channels...')

channels = helper.getChannels()
output = helper.convertChannelsToOutput(channels)

return output
# channels = helper.getChannels()
# output = helper.convertChannelsToOutput(channels)
# return output
return helper.search()

# 検索機能
# Search Functionality
@app.route('/search')
@app.route('/search/')
def req_search_blank():
#def req_search_blank():
# Parameter Missing
return helper.search('')

# return helper.search('')
@app.route('/search/<keyword>')
def req_search(keyword):
return helper.search(keyword)
def req_search(keyword=None):
channelCount = request.args.get('channelCount')
page = request.args.get('page')
return helper.search(keyword, channelCount, page)

# インボイス発行
# Issue invoice
Expand Down