Skip to content

Commit

Permalink
feat: code quality (#7)
Browse files Browse the repository at this point in the history
* feat: code quality

* fixes
  • Loading branch information
dni authored Aug 2, 2024
1 parent 20203e1 commit ad9d1ec
Show file tree
Hide file tree
Showing 19 changed files with 2,827 additions and 88 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: lint
on:
push:
branches:
- main
pull_request:

jobs:
lint:
uses: lnbits/lnbits/.github/workflows/lint.yml@dev
15 changes: 7 additions & 8 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
on:
push:
tags:
- "v[0-9]+.[0-9]+.[0-9]+"
- 'v[0-9]+.[0-9]+.[0-9]+'

jobs:

release:
runs-on: ubuntu-latest
steps:
Expand Down Expand Up @@ -34,12 +33,12 @@ jobs:
- name: Create pull request in extensions repo
env:
GH_TOKEN: ${{ secrets.EXT_GITHUB }}
repo_name: "${{ github.event.repository.name }}"
tag: "${{ github.ref_name }}"
branch: "update-${{ github.event.repository.name }}-${{ github.ref_name }}"
title: "[UPDATE] ${{ github.event.repository.name }} to ${{ github.ref_name }}"
body: "https://github.com/lnbits/${{ github.event.repository.name }}/releases/${{ github.ref_name }}"
archive: "https://github.com/lnbits/${{ github.event.repository.name }}/archive/refs/tags/${{ github.ref_name }}.zip"
repo_name: '${{ github.event.repository.name }}'
tag: '${{ github.ref_name }}'
branch: 'update-${{ github.event.repository.name }}-${{ github.ref_name }}'
title: '[UPDATE] ${{ github.event.repository.name }} to ${{ github.ref_name }}'
body: 'https://github.com/lnbits/${{ github.event.repository.name }}/releases/${{ github.ref_name }}'
archive: 'https://github.com/lnbits/${{ github.event.repository.name }}/archive/refs/tags/${{ github.ref_name }}.zip'
run: |
cd lnbits-extensions
git checkout -b $branch
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
__pycache__
node_modules
.mypy_cache
.venv
12 changes: 12 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"semi": false,
"arrowParens": "avoid",
"insertPragma": false,
"printWidth": 80,
"proseWrap": "preserve",
"singleQuote": true,
"trailingComma": "none",
"useTabs": false,
"bracketSameLine": false,
"bracketSpacing": false
}
47 changes: 47 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
all: format check

format: prettier black ruff

check: mypy pyright checkblack checkruff checkprettier

prettier:
poetry run ./node_modules/.bin/prettier --write .
pyright:
poetry run ./node_modules/.bin/pyright

mypy:
poetry run mypy .

black:
poetry run black .

ruff:
poetry run ruff check . --fix

checkruff:
poetry run ruff check .

checkprettier:
poetry run ./node_modules/.bin/prettier --check .

checkblack:
poetry run black --check .

checkeditorconfig:
editorconfig-checker

test:
PYTHONUNBUFFERED=1 \
DEBUG=true \
poetry run pytest
install-pre-commit-hook:
@echo "Installing pre-commit hook to git"
@echo "Uninstall the hook with poetry run pre-commit uninstall"
poetry run pre-commit install

pre-commit:
poetry run pre-commit run --all-files


checkbundle:
@echo "skipping checkbundle"
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Stream Alerts - <small>[LNbits](https://github.com/lnbits/lnbits) extension</small>

<small>For more about LNBits extension check [this tutorial](https://github.com/lnbits/lnbits/wiki/LNbits-Extensions)</small>

<h2>Integrate Bitcoin Donations into your livestream alerts</h2>
Expand All @@ -12,22 +13,22 @@ At the moment, the only service that has an open API to work with is Streamlabs,

1. Log into [Streamlabs](https://streamlabs.com/login?r=https://streamlabs.com/dashboard).
1. Navigate to the API settings page to register an App:
![image](https://user-images.githubusercontent.com/28876473/127759145-710d53b6-3c19-4815-812a-9a6279d1b8bb.png)
![image](https://user-images.githubusercontent.com/28876473/127759182-da8a27cb-bb59-48fa-868e-c8892080ae98.png)
![image](https://user-images.githubusercontent.com/28876473/127759201-7c28e9f1-6286-42be-a38e-1c377a86976b.png)
![image](https://user-images.githubusercontent.com/28876473/127759145-710d53b6-3c19-4815-812a-9a6279d1b8bb.png)
![image](https://user-images.githubusercontent.com/28876473/127759182-da8a27cb-bb59-48fa-868e-c8892080ae98.png)
![image](https://user-images.githubusercontent.com/28876473/127759201-7c28e9f1-6286-42be-a38e-1c377a86976b.png)
1. Fill out the form with anything it will accept as valid. Most fields can be gibberish, as the application is not supposed to ever move past the "testing" stage and is for your personal use only.
In the "Whitelist Users" field, input the username of a Twitch account you control. While this feature is *technically* limited to Twitch, you can use the alerts overlay for donations on YouTube and Facebook as well.
For now, simply set the "Redirect URI" to `http://localhost`, you will change this soon.
Then, hit create:
![image](https://user-images.githubusercontent.com/28876473/127759264-ae91539a-5694-4096-a478-80eb02b7b594.png)
In the "Whitelist Users" field, input the username of a Twitch account you control. While this feature is _technically_ limited to Twitch, you can use the alerts overlay for donations on YouTube and Facebook as well.
For now, simply set the "Redirect URI" to `http://localhost`, you will change this soon.
Then, hit create:
![image](https://user-images.githubusercontent.com/28876473/127759264-ae91539a-5694-4096-a478-80eb02b7b594.png)
1. In LNbits, enable the Stream Alerts extension and optionally the SatsPayServer (to observe donations directly) and Onchain Wallet (watch-only) (to accept on-chain donations) extenions:
![image](https://user-images.githubusercontent.com/28876473/127759486-0e3420c2-c498-4bf9-932e-0abfa17bd478.png)
![image](https://user-images.githubusercontent.com/28876473/127759486-0e3420c2-c498-4bf9-932e-0abfa17bd478.png)
1. Create a "NEW SERVICE" using the button. Fill in all the information (you get your Client ID and Secret from the Streamlabs App page):
![image](https://user-images.githubusercontent.com/28876473/127759512-8e8b4e90-2a64-422a-bf0a-5508d0630bed.png)
![image](https://user-images.githubusercontent.com/28876473/127759526-7f2a4980-39ea-4e58-8af0-c9fb381e5524.png)
![image](https://user-images.githubusercontent.com/28876473/127759512-8e8b4e90-2a64-422a-bf0a-5508d0630bed.png)
![image](https://user-images.githubusercontent.com/28876473/127759526-7f2a4980-39ea-4e58-8af0-c9fb381e5524.png)
1. Right-click and copy the "Redirect URI for Streamlabs" link (you might have to refresh the page for the text to turn into a link) and input it into the "Redirect URI" field for your Streamelements App, and hit "Save Settings":
![image](https://user-images.githubusercontent.com/28876473/127759570-52d34c07-6857-467b-bcb3-54e10679aedb.png)
![image](https://user-images.githubusercontent.com/28876473/127759604-b3c8270b-bd02-44df-a525-9d85af337d14.png)
![image](https://user-images.githubusercontent.com/28876473/127759570-52d34c07-6857-467b-bcb3-54e10679aedb.png)
![image](https://user-images.githubusercontent.com/28876473/127759604-b3c8270b-bd02-44df-a525-9d85af337d14.png)
1. You can now authenticate your app on LNbits by clicking on this button and following the instructions. Make sure to log in with the Twitch account you entered in the "Whitelist Users" field:
![image](https://user-images.githubusercontent.com/28876473/127759642-a3787a6a-3cab-4c44-a2d4-ab45fbbe3fab.png)
![image](https://user-images.githubusercontent.com/28876473/127759681-7289e7f6-0ff1-4988-944f-484040f6b9c7.png)
Expand Down
17 changes: 6 additions & 11 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from fastapi import APIRouter

from lnbits.db import Database
from lnbits.helpers import template_renderer

db = Database("ext_streamalerts")
from .crud import db
from .views import streamalerts_generic_router
from .views_api import streamalerts_api_router

streamalerts_ext: APIRouter = APIRouter(prefix="/streamalerts", tags=["streamalerts"])
streamalerts_ext.include_router(streamalerts_generic_router)
streamalerts_ext.include_router(streamalerts_api_router)

streamalerts_static_files = [
{
Expand All @@ -14,10 +15,4 @@
}
]


def streamalerts_renderer():
return template_renderer(["streamalerts/templates"])


from .views import * # noqa: F401,F403
from .views_api import * # noqa: F401,F403
__all__ = ["db", "streamalerts_ext", "streamalerts_static_files"]
10 changes: 5 additions & 5 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Stream Alerts",
"short_description": "Bitcoin donations in stream alerts",
"tile": "/streamalerts/static/image/streamalerts.png",
"contributors": ["Fittiboy"],
"min_lnbits_version": "0.11.0"
"name": "Stream Alerts",
"short_description": "Bitcoin donations in stream alerts",
"tile": "/streamalerts/static/image/streamalerts.png",
"contributors": ["Fittiboy"],
"min_lnbits_version": "0.11.0"
}
18 changes: 9 additions & 9 deletions crud.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from typing import List, Optional
from typing import Optional

import httpx

from lnbits.core.crud import get_wallet
from lnbits.db import SQLITE
from lnbits.db import SQLITE, Database
from lnbits.helpers import urlsafe_short_hash

from . import db
from .models import CreateService, Donation, Service

db = Database("ext_streamalerts")


async def get_service_redirect_uri(request, service_id):
"""Return the service's redirect URI, to be given to the third party API"""
Expand Down Expand Up @@ -40,7 +40,7 @@ async def get_charge_details(service_id):


async def create_donation(
id: str,
donation_id: str,
wallet: str,
cur_code: str,
sats: int,
Expand All @@ -66,10 +66,10 @@ async def create_donation(
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(id, wallet, name, message, cur_code, sats, amount, service, posted),
(donation_id, wallet, name, message, cur_code, sats, amount, service, posted),
)

donation = await get_donation(id)
donation = await get_donation(donation_id)
assert donation, "Newly created donation couldn't be retrieved"
return donation

Expand Down Expand Up @@ -120,7 +120,7 @@ async def create_service(data: CreateService) -> Service:
returning = "" if db.type == SQLITE else "RETURNING ID"
method = db.execute if db.type == SQLITE else db.fetchone

result = await (method)(
result = await method(
f"""
INSERT INTO streamalerts.Services (
twitchuser,
Expand Down Expand Up @@ -226,7 +226,7 @@ async def service_add_token(service_id, token):
return True


async def delete_service(service_id: int) -> List[str]:
async def delete_service(service_id: int) -> list[str]:
"""Delete a Service and all corresponding Donations"""
rows = await db.fetchall(
"SELECT * FROM streamalerts.Donations WHERE service = ?", (service_id,)
Expand Down
6 changes: 3 additions & 3 deletions helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import httpx

from lnbits.app import settings
from lnbits.settings import settings

from .models import ChargeStatus

Expand All @@ -27,11 +26,12 @@ async def get_charge_status(charge_id: str, api_key: str) -> ChargeStatus:
r.raise_for_status()
return ChargeStatus.parse_obj(r.json())


async def delete_charge(charge_id: str, api_key: str):
async with httpx.AsyncClient() as client:
headers = {"X-API-KEY": api_key}
r = await client.delete(
url=f"http://{settings.host}:{settings.port}/satspay/api/v1/charge/{charge_id}",
headers=headers,
)
r.raise_for_status()
r.raise_for_status()
14 changes: 7 additions & 7 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"repos": [
{
"id": "streamalerts",
"organisation": "lnbits",
"repository": "streamalerts"
}
]
"repos": [
{
"id": "streamalerts",
"organisation": "lnbits",
"repository": "streamalerts"
}
]
}
59 changes: 59 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "streamalerts",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"prettier": "^3.2.5",
"pyright": "^1.1.358"
}
}
Loading

0 comments on commit ad9d1ec

Please sign in to comment.