diff --git a/.gitignore b/.gitignore index 21f8ed2..cdc6fa3 100644 --- a/.gitignore +++ b/.gitignore @@ -57,6 +57,4 @@ docs/_build/ target/ .vscode/ -slappd.cfg -slappd.cfg.bak tags diff --git a/.pylintrc b/.pylintrc deleted file mode 100644 index 863c8e6..0000000 --- a/.pylintrc +++ /dev/null @@ -1,3 +0,0 @@ -[TYPECHECK] - -generated-members=tmpl diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..41e7a9c --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1 @@ +include templates/*.j2 diff --git a/README.md b/README.md index ddcb6b6..3d25dfb 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # slappd ### About + Since Untappd does not currently support callbacks or webhooks, I wrote a basic Slack integration that will relay check-ins and badges earned for specified users on your feed to a Slack channel. @@ -10,19 +11,23 @@ on your feed to a Slack channel. This script is designed to be run from crontab, and issues one API call per run. ### Known Issues -* The first time you run the script, it may be a little chatty because it has not -previously seen your feed before. + +* The first time you run the script, it may be a little chatty because it has + not previously seen your feed before. * If you have a lot of Untappd friends, but are only watching a subset of them, -you may miss check-ins if you don't run Slappd regularly. + you may miss check-ins if you don't run Slappd regularly. ### Requirements + * Python 3.5+ (It may run on >= 3.0, but I have not tested.) * Some Python modules (configparser, Jinja2, simplejson, requests) * A way of periodically running this script (at, cron, etc) * Untappd [API access](https://untappd.com/api/register?register=new) * A Slack channel full of beer lovers -### Configuration -* Install the required Python modules via: `pip3 install -r requirements.txt` -* Copy [slappd.cfg.dist](slappd.cfg.dist) to 'slappd.cfg' and edit it to reflect your API information -* Run it from crontab: `*/5 * * * python3.5 /path/to/slappd.py > /dev/null 2>&1` +### Installation & Configuration + +* Install Slappd to a virtualenv via `make install`. +* If this is your first time, create the default config file with `make config` + and then edit it `(~/.config/slappd/slappd.cfg)` to reflect your API information. +* Run it from crontab: `*/5 * * * ~/.virtualenv/slappd/bin/slappd > /dev/null 2>&1` diff --git a/makefile b/makefile new file mode 100644 index 0000000..623f286 --- /dev/null +++ b/makefile @@ -0,0 +1,30 @@ +SHELL = /bin/bash +VIRTUALENV_DIR = ${HOME}/.virtualenv +CFG_DIR = ${HOME}/.config/slappd + +.PHONY: config +config: + mkdir -p ${CFG_DIR} + test -f ${CFG_DIR}/slappd.cfg || cp -p slappd.cfg.dist ${CFG_DIR}/slappd.cfg + +.PHONY: dev +dev: ${VIRTUALENV_DIR}/slappd + source ${VIRTUALENV_DIR}/slappd/bin/activate && \ + pip install -U flake8 pip && \ + pip install --editable . + +.PHONY: install +install: ${VIRTUALENV_DIR}/slappd + source ${VIRTUALENV_DIR}/slappd/bin/activate && \ + pip install -U pip && \ + pip install --upgrade . + +${VIRTUALENV_DIR}/slappd: + mkdir -p ${VIRTUALENV_DIR} + cd ${VIRTUALENV_DIR} && python3 -m venv slappd + +.PHONY: lint +lint: + -flake8 + +.DEFAULT_GOAL := install \ No newline at end of file diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index 2eb7796..0000000 --- a/requirements.txt +++ /dev/null @@ -1,3 +0,0 @@ -Jinja2==2.10 -configparser==3.5.0 -requests==2.18.4 diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..1340c1e --- /dev/null +++ b/setup.cfg @@ -0,0 +1,16 @@ +[flake8] +exclude = + .git, + __pycache__, + build, + dist +max-line-length = 120 + +[isort] +import_heading_firstparty = First Party Imports +import_heading_future = Future Imports +import_heading_localfolder = Local Imports +import_heading_stdlib = Standard Library Imports +import_heading_thirdparty = Third Party Imports +line_length = 119 +skip = setup.py \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..9b1ab08 --- /dev/null +++ b/setup.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python3 + +""" Slappd setup.py. """ + +from setuptools import setup + +setup( + entry_points=''' + [console_scripts] + slappd=slappd.__main__:main + ''', + include_package_data=True, + install_requires=[ + 'Jinja2', + 'configparser', + 'requests' + ], + name='slappd', + packages=['slappd'], + package_data={ + '': ['templates/*.j2'] + }, + version='1.0.0' +) diff --git a/slappd/__init__.py b/slappd/__init__.py new file mode 100644 index 0000000..f0842d3 --- /dev/null +++ b/slappd/__init__.py @@ -0,0 +1 @@ +""" Slappd Package """ diff --git a/slappd.py b/slappd/__main__.py old mode 100755 new mode 100644 similarity index 95% rename from slappd.py rename to slappd/__main__.py index 1e4d527..79e1fc5 --- a/slappd.py +++ b/slappd/__main__.py @@ -23,12 +23,16 @@ SOFTWARE. """ +# Standard Library Imports import os import re import sys + +# First Party Imports from configparser import SafeConfigParser from operator import itemgetter +# Third Party Imports import requests from jinja2 import Environment, FileSystemLoader @@ -49,7 +53,7 @@ def check_for_photos(checkins): def config_load(): """ Load configuration options from file """ - config_file = get_cwd() + '/slappd.cfg' + config_file = get_cfg_path() if not os.path.exists(config_file): sys.exit('Error: Configuration file {} does not exist' .format(config_file)) @@ -59,7 +63,7 @@ def config_load(): def config_update(): """ Updates the config file with any changes that have been made """ - config_file = get_cwd() + '/slappd.cfg' + config_file = get_cfg_path() try: with open(config_file, 'w') as cfg_handle: CONFIG.write(cfg_handle) @@ -93,9 +97,9 @@ def fetch_url(method): CONFIG.get('untappd', 'lastseen')) -def get_cwd(): - """ Return the current working directory """ - return os.path.dirname(os.path.realpath(__file__)) +def get_cfg_path(): + """ Return the path to the config file """ + return os.path.expanduser('~/.config/slappd/slappd.cfg') def slack_message(images=None, msg_type=None, text=None): @@ -133,8 +137,9 @@ def strip_html(text): def main(): """ Where the magic happens """ config_load() + cwd = os.path.dirname(os.path.realpath(__file__)) data = fetch_untappd_activity() - env = Environment(loader=FileSystemLoader(get_cwd() + '/templates')) + env = Environment(loader=FileSystemLoader('{}/templates'.format(cwd))) if data['meta']['code'] == 200: checkins = data['response']['checkins']['items'] diff --git a/templates/check-in.j2 b/slappd/templates/check-in.j2 similarity index 100% rename from templates/check-in.j2 rename to slappd/templates/check-in.j2