Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ffreemt committed Oct 10, 2021
0 parents commit 16be144
Show file tree
Hide file tree
Showing 19 changed files with 1,714 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[flake8]
ignore =
D203,
# line too long
E501
per-file-ignores =
# imported but unused
# __init__.py: F401
test_*.py: F401
exclude =
.git,
__pycache__,
docs/source/conf.py,
old,
build,
dist,
.venv
pad*.py
max-complexity = 25
67 changes: 67 additions & 0 deletions .github/workflows/codeql-analysis.yml-
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"

on:
push:
branches: [ master ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ master ]
schedule:
- cron: '16 15 * * 2'

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
language: [ 'python' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
# Learn more:
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed

steps:
- name: Checkout repository
uses: actions/checkout@v2

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl

# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language

#- run: |
# make bootstrap
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
30 changes: 30 additions & 0 deletions .github/workflows/routine-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: tests

on:
push:

jobs:
flake8-pytest:
strategy:
fail-fast: false
matrix:
python-version: [3.7, 3.8]
poetry-version: [1.1.2]
os: [ubuntu-18.04, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- uses: abatilo/[email protected]
with:
poetry-version: ${{ matrix.poetry-version }}
- name: Install packages
run: poetry install # --no-dev
- name: Run flake8
run: |
poetry run flake8 --exit-zero
# poetry run pytest
- name: Run pytest
run: poetry run pytest
143 changes: 143 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/
*.bat
*.swp
links/
# .gitignore
node_modules
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 ffreemt

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# nb2chan
[![tests](https://github.com/ffreemt/nb2chan/actions/workflows/routine-tests.yml/badge.svg)](https://github.com/ffreemt/nb2chan/actions)[![nonebot2](https://img.shields.io/static/v1?label=nonebot&message=v2&color=green)](https://v2.nonebot.dev/)[![cqhttp](https://img.shields.io/static/v1?label=driver&message=cqhttp&color=green)](https://v2.nonebot.dev/guide/cqhttp-guide.html)[![python](https://img.shields.io/static/v1?label=python+&message=3.7%2B&color=blue)](https://img.shields.io/static/v1?label=python+&message=3.7%2B&color=blue)[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)[![PyPI version](https://badge.fury.io/py/nb2chan.svg)](https://badge.fury.io/py/nb2chan)

nonebot2酱推送服务 nb2chan push service

## Install it

```shell
# x pip install nb2chan
# x or poetry add nb2chan
# pip install git+https://github.com/ffreemt/nb2chan
# poetry add git+https://github.com/ffreemt/nb2chan

git clone https://github.com/ffreemt/nb2chan && cd nb2chan && poetry install --nodev

# To upgrade
# x pip install nb2chan -U
# x or poetry add nb2chan@latest
```

## Use it
```python
# bot.py
import nonebot
...
nonebot.init()

driver = nonebot.get_driver()

driver.register_adapter("cqhttp", CQHTTPBot)

nonebot.load_from_toml("pyproject.toml")
nonebot.load_plugin("nb2chan")
...
```

* 目标qq号(例如QQ号 1234)加`nonebot2`机器人qq号好友
* `nonebot2`部署至外网`ip`,例如 `uvicorn --host 0.0.0.0 bot:app` (火墙需放行`nonebot2`的端口)
* 给qq号发消息(浏览器地址栏或`curl/httpie``python reqests/httpx``云函数`/`claudflare worker` etc.):
```bash
http://...:port/nb2chan/?Token=DEMO_TOKEN&qq=1234&msg=hello
```
令牌也可在`headers`里设定,例如
```
curl http://...:port/nb2chan/?qq=1234&msg=hello -H "token: DEMO_TOKEN"
http -v "http://...:port/nb2chan/?qq=1234&msg=hello world" "token: DEMO_TOKEN"
```

## 其他
`nb2chan`
默认令牌
5 changes: 5 additions & 0 deletions nb2chan/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Init."""
__version__ = "0.1.0"
from .nb2chan import nb2chan, app

__all__ = ("nb2chan", "app",)
52 changes: 52 additions & 0 deletions nb2chan/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""Config nb2chan."""
from typing import List, Union

from pydantic import BaseSettings, Field, validator

from logzero import logger


class Settings(BaseSettings):
"""Preset default valid tokens."""

token_list: List[Union[str, int]] = Field(
default_factory=lambda: ["DEMO_TOKEN", "SECRET_TOKEN"]
)

@validator("token_list")
def validate_namelist(cls, v):
res = []
for elm in v:
try:
# may use numerbers
elm = str(elm).strip()
except Exception as exc:
logger.error(exc)
raise

_ = """
if len(elm) < 1:
raise ValueError(
"Empty token not allowed"
)
"""

if len(elm) == 0:
logger.warning(
"This entry [%s] is empty: probably not what you want, but we let it pass.",
elm,
)

res.append(elm)

return res

class Config: # pylint: disable=too-few-public-methods
"""Config."""

env_prefix = "nb2chan_"
# extra = "allow"
env_file = ".env.nb2chan"
env_file_encoding = "utf-8"

logger.info("env_prefix: %s, env_file: %s", env_prefix, env_file)
Loading

0 comments on commit 16be144

Please sign in to comment.