Created
December 9, 2019 12:04
-
-
Save evd0kim/8f5f473edc25e677d30b4766e78c7e60 to your computer and use it in GitHub Desktop.
sparko-aiohttp
This file contains hidden or 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 aiohttp | |
import asyncio | |
import logging | |
from datetime import datetime | |
from base64 import b64decode, b64encode | |
import os | |
import sys | |
import secrets | |
import json | |
module_logger = logging.getLogger(__name__) | |
handler = logging.StreamHandler(stream=sys.stdout) | |
formatter = logging.Formatter(fmt="%(asctime)s %(levelname)s [%(name)s:%(funcName)s] %(message)s", datefmt="%d %b %H:%M:%S") | |
handler.setFormatter(formatter) | |
module_logger.addHandler(handler) | |
module_logger.setLevel(logging.INFO) | |
async def do_pay(session: aiohttp.ClientSession, url :str, invoice :str): | |
TOKEN = 'YOUR TOKEN' | |
async with session.post(url, data=json.dumps({'method': 'waitpay', 'params': [invoice]}), headers={'X-Access': TOKEN}, ssl = False) as response: | |
data = json.loads(await response.text()) | |
module_logger.warning("-> request %s" % url) | |
if response.status == 400: | |
module_logger.warning("-> error %s" % data["message"]) | |
return {'paid': False} | |
elif response.status == 200: | |
if data['status'] == 'complete': | |
module_logger.warning("-> paid %s" % data) | |
return {'paid': True} | |
else: | |
return {'paid': False} | |
else: | |
return {'paid': False} | |
async def main(num: int, amount: int): | |
SPARKO_HOST = 'HOST:PORT' | |
ln_url = "https://{}/rpc".format(SPARKO_HOST) | |
invoices = ['lnbtc...', 'lnbtc...'] | |
async with aiohttp.ClientSession() as session: | |
pay_tasks = [do_pay(session, ln_url, invoice) for invoice in invoices] | |
results = await asyncio.gather(*pay_tasks) | |
module_logger.info(results) | |
if __name__ == '__main__': | |
asyncio.run(main(num, amount)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment