From 7422250c2128645d7f0cb907140f2cd5f47f195b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=ADdac=20Sabat=C3=A9s?= Date: Mon, 6 Jun 2022 21:22:17 +0200 Subject: [PATCH] Init --- .github/workflows/python-publish.yml | 43 ++++++++++++++++ .gitignore | 29 +++++++++++ README.md | 36 +++++++++++++ pibooth_telegram_upload.py | 75 ++++++++++++++++++++++++++++ setup.py | 54 ++++++++++++++++++++ 5 files changed, 237 insertions(+) create mode 100644 .github/workflows/python-publish.yml create mode 100644 .gitignore create mode 100644 README.md create mode 100644 pibooth_telegram_upload.py create mode 100644 setup.py diff --git a/.github/workflows/python-publish.yml b/.github/workflows/python-publish.yml new file mode 100644 index 0000000..9e13ec6 --- /dev/null +++ b/.github/workflows/python-publish.yml @@ -0,0 +1,43 @@ +# This workflows will upload a Python Package using Twine when a release is created +# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries + +name: Upload Python Package + +on: + release: + types: [created] + +permissions: + contents: read + +jobs: + deploy: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + + - name: Set up Python + uses: actions/setup-python@v3 + with: + python-version: '3.x' + + - name: Install build dependencies + run: | + python -m pip install --upgrade pip + pip install setuptools wheel twine + - name: Install pibooth + run: | + sudo apt-get install libsdl* libsmpeg-dev libportmidi-dev + pip install pibooth + - name: Install telegram client + run: | + pip install python-telegram-bot + - name: Build and publish + env: + TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }} + TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }} + run: | + python setup.py sdist bdist_wheel + twine check dist/* && twine upload --skip-existing dist/* diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e936a47 --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +/tmp +.DS_Store + +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST +.vscode/ \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..0440431 --- /dev/null +++ b/README.md @@ -0,0 +1,36 @@ +pibooth-telegram-upload +================= + +[![Python 3.6+](https://img.shields.io/badge/python-3.6+-red.svg)](https://www.python.org/downloads) +[![PyPi package](https://badge.fury.io/py/pibooth-telegram-upload.svg)](https://pypi.org/project/pibooth-telegram-upload) +[![PyPi downloads](https://img.shields.io/pypi/dm/pibooth-telegram-upload?color=purple)](https://pypi.org/project/pibooth-telegram-upload) + +`pibooth-telegram-upload` is a plugin for the [pibooth](https://pypi.org/project/pibooth) application. + +Install +------- + + $ pip3 install pibooth-telegram-upload + +Configuration +------------- + +Below are the new configuration options available in the [pibooth](https://pypi.org/project/pibooth) configuration. **The keys and their default values are automatically added to your configuration after first** [pibooth](https://pypi.org/project/pibooth) **restart.** + +``` {.ini} +[Telegram] + +# Telegram bot token +telegram_token = + +# Chat/Channel id +telegram_chat_id = + +# Message to show when sending the picture +telegram_message = + +``` + +### Note + +Edit the configuration by running the command `pibooth --config`. diff --git a/pibooth_telegram_upload.py b/pibooth_telegram_upload.py new file mode 100644 index 0000000..abf7cb6 --- /dev/null +++ b/pibooth_telegram_upload.py @@ -0,0 +1,75 @@ +# -*- coding: utf-8 -*- + +"""pibooth plugin for uploading pictures to Telegram""" + +import os +import telegram + +try: + from telegram.error import TelegramError +except ImportError: + InstalledAppFlow = None + pass + +import pibooth +from pibooth.utils import LOGGER + + +__version__ = "1.0.0" + +SECTION = "Telegram" + + +@pibooth.hookimpl +def pibooth_configure(cfg): + """Declare the new configuration options""" + cfg.add_option(SECTION, "telegram_token", "", "Telegram Token") + cfg.add_option(SECTION, "telegram_chat_id", "", "Telegram chat ID") + cfg.add_option( + SECTION, + "telegram_message", + "", + "Message sended on every posted photo", + ) + + +@pibooth.hookimpl +def pibooth_startup(app, cfg): + """Verify Telegram credentials""" + telegram_token = cfg.get(SECTION, "telegram_token") + telegram_chat_id = cfg.get(SECTION, "telegram_chat_id") + + if not telegram_token: + LOGGER.error( + "Telegram Token not defined in [" + + SECTION + + "][telegram_token], uploading deactivated" + ) + elif not telegram_chat_id: + LOGGER.error( + "Telegram Chat ID not defined in [" + + SECTION + + "][telegram_chat_id], uploading deactivated" + ) + else: + LOGGER.info("Initializing Telegram client") + app.telegram_client = telegram.Bot(token=telegram_token) + + +@pibooth.hookimpl +def state_processing_exit(app, cfg): + """Upload picture to Telegram chat""" + if hasattr(app, "telegram_client"): + chat_id = cfg.get(SECTION, "telegram_chat_id").strip('"') + upload_path = os.path.basename(app.previous_picture_file) + message = cfg.get(SECTION, "telegram_message") + try: + response = app.telegram_client.send_photo( + chat_id=chat_id, + caption=message, + photo=app.previous_picture_file, + parse_mode=telegram.ParseMode.MARKDOWN, + ) + LOGGER.info("File uploaded to Telegram chat: " + response) + except TelegramError as e: + LOGGER.error(e) diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..cefc14f --- /dev/null +++ b/setup.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import sys +from io import open +import os.path as osp +from setuptools import setup + + +HERE = osp.abspath(osp.dirname(__file__)) +sys.path.insert(0, HERE) +import pibooth_telegram_upload as plugin # nopep8 : import shall be done after adding setup to paths + + +def main(): + setup( + name=plugin.__name__, + version=plugin.__version__, + description=plugin.__doc__, + long_description=open(osp.join(HERE, "README.md"), encoding="utf-8").read(), + long_description_content_type="text/markdown", + classifiers=[ + "Development Status :: 5 - Production/Stable", + "Environment :: Other Environment", + "Intended Audience :: Developers", + "Intended Audience :: End Users/Desktop", + "License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)", + "Operating System :: POSIX :: Linux", + "Programming Language :: Python :: 3.6", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Natural Language :: English", + "Topic :: Multimedia :: Graphics :: Capture :: Digital Camera", + ], + author="Dídac Sabatés", + author_email="sabatesduran@gmail.com", + url="https://github.com/sabatesduran/pibooth-telegram-upload", + download_url="https://github.com/sabatesduran/pibooth-telegram-upload/archive/{}.tar.gz".format( + plugin.__version__ + ), + license="GPLv3", + platforms=["unix", "linux"], + keywords=["Raspberry Pi", "camera", "photobooth", "telegram", "chat", "bot", "channel"], + py_modules=["pibooth_telegram_upload"], + python_requires=">=3.6", + install_requires=["pibooth>=2.0.0", "python-telegram-bot==13.12"], + zip_safe=False, # Don't install the lib as an .egg zipfile + entry_points={"pibooth": ["pibooth_telegram_upload = pibooth_telegram_upload"]}, + ) + + +if __name__ == "__main__": + main()