~kf5jwc/sms-printer

5be96bf4f52a694579a9119b84c93c4744d18a7e — Kyle Jones 2 years ago
Initial
6 files changed, 136 insertions(+), 0 deletions(-)

A .gitignore
A Pipfile
A Pipfile.lock
A sms_printer/__init__.py
A sms_printer/__main__.py
A sms_printer/application.py
A  => .gitignore +1 -0
@@ 1,1 @@
__pycache__

A  => Pipfile +17 -0
@@ 1,17 @@
[[source]]

verify_ssl = true
url = "https://pypi.python.org/simple"
name = "pypi"


[packages]

gunicorn = "*"
flask = "*"
pyxdg = "*"


[requires]

python_version = "3.5"

A  => Pipfile.lock +75 -0
@@ 1,75 @@
{
    "_meta": {
        "hash": {
            "sha256": "bfcaad7809714f0cbf96c6a651c72eb65508c28a78725146a6148cb1591da98f"
        },
        "pipfile-spec": 6,
        "requires": {
            "python_version": "3.5"
        },
        "sources": [
            {
                "name": "pypi",
                "url": "https://pypi.python.org/simple",
                "verify_ssl": true
            }
        ]
    },
    "default": {
        "click": {
            "hashes": [
                "sha256:29f99fc6125fbc931b758dc053b3114e55c77a6e4c6c3a2674a2dc986016381d",
                "sha256:f15516df478d5a56180fbf80e68f206010e6d160fc39fa508b65e035fd75130b"
            ],
            "version": "==6.7"
        },
        "flask": {
            "hashes": [
                "sha256:2271c0070dbcb5275fad4a82e29f23ab92682dc45f9dfbc22c02ba9b9322ce48",
                "sha256:a080b744b7e345ccfcbc77954861cb05b3c63786e93f2b3875e0913d44b43f05"
            ],
            "version": "==1.0.2"
        },
        "gunicorn": {
            "hashes": [
                "sha256:7ef2b828b335ed58e3b64ffa84caceb0a7dd7c5ca12f217241350dec36a1d5dc",
                "sha256:bc59005979efb6d2dd7d5ba72d99f8a8422862ad17ff3a16e900684630dd2a10"
            ],
            "version": "==19.8.1"
        },
        "itsdangerous": {
            "hashes": [
                "sha256:cbb3fcf8d3e33df861709ecaf89d9e6629cff0a217bc2848f1b41cd30d360519"
            ],
            "version": "==0.24"
        },
        "jinja2": {
            "hashes": [
                "sha256:74c935a1b8bb9a3947c50a54766a969d4846290e1e788ea44c1392163723c3bd",
                "sha256:f84be1bb0040caca4cea721fcbbbbd61f9be9464ca236387158b0feea01914a4"
            ],
            "version": "==2.10"
        },
        "markupsafe": {
            "hashes": [
                "sha256:a6be69091dac236ea9c6bc7d012beab42010fa914c459791d627dad4910eb665"
            ],
            "version": "==1.0"
        },
        "pyxdg": {
            "hashes": [
                "sha256:1948ff8e2db02156c0cccd2529b43c0cff56ebaa71f5f021bbd755bc1419190e",
                "sha256:fe2928d3f532ed32b39c32a482b54136fe766d19936afc96c8f00645f9da1a06"
            ],
            "version": "==0.26"
        },
        "werkzeug": {
            "hashes": [
                "sha256:c3fd7a7d41976d9f44db327260e263132466836cef6f91512889ed60ad26557c",
                "sha256:d5da73735293558eb1651ee2fddc4d0dedcfa06538b8813a2e20011583c9e49b"
            ],
            "version": "==0.14.1"
        }
    },
    "develop": {}
}

A  => sms_printer/__init__.py +3 -0
@@ 1,3 @@
"This is a small script to accept SMS from bandwidth.com and print it locally"

from .application import APP as application

A  => sms_printer/__main__.py +3 -0
@@ 1,3 @@
from . import application

application.run()

A  => sms_printer/application.py +37 -0
@@ 1,37 @@
import os
from subprocess import CalledProcessError, run
from tempfile import NamedTemporaryFile

from flask import Flask, Response, json, request
from xdg import BaseDirectory

APP = Flask(__name__)
ARCHIVE = os.getenv('ARCHIVE_DIR', BaseDirectory.save_data_path(__name__))
ALLOWED_SENDERS = [
    num for num in os.getenv('ALLOWED_SENDERS', '').split(',') if num
]


@APP.route("/", methods=['GET'])
def main():
    return Response(status=204)


@APP.route("/sms-printer", methods=['POST'])
def sms_printer():
    for sms in request.get_json(force=True):
        if ALLOWED_SENDERS and sms['message']['from'] not in ALLOWED_SENDERS:
            return Response(status=401)

        with open(os.path.join(ARCHIVE, sms['message']['id']), 'w+') as f:
            json.dump(sms, f)

        with NamedTemporaryFile(mode='w+') as f:
            print("{}".format(sms['message']['text']), file=f)
            f.flush()
            try:
                run(['lp', f.name], check=True, timeout=10)
            except CalledProcessError:
                return Response(status=500)

    return Response(status=200)