From 34667032a2ec1ffa80653a3ae0a19d392e3b41c9 Mon Sep 17 00:00:00 2001 From: aschuma Date: Sat, 27 Jan 2018 23:15:14 +0100 Subject: [PATCH] QA 2 (#3) --- README.md | 59 ++++++++++++++++++++++++++++++++++++++++--------------- tweet.py | 6 +++--- 2 files changed, 46 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 1edc260..e1a634f 100644 --- a/README.md +++ b/README.md @@ -9,31 +9,58 @@ c$$$cc$$$c $$$ $$$$$$c $$ Y$c$$$c$P $$"""" $$"""" ``` # Tweets air quality measurement results of particulate matter (Feinstaub) (http://luftdaten.info) +This is the source code of a Twitter bot that monitors particulate matter (Feinstaub) for a single +sensor of the http://luftdaten.info network. A tweet is published in case the PM10 particulate matter +exceeds a configurable value, e.g. 50 µg/m³. -## config +A tweet might look like this: + ``` +#feinstaub #feinstaubalarm #bot +⚠ PM10: 68.07g/m,³ PM2.5: 41.87µg/m³, 5.50°C, RH:96.00% +http://deutschland.maps.luftdaten.info/#13/48.8066/9.2372 +``` -Set your Twitter credentials in config.py. -Set your sensor meta data in config.py. +This repository provides a cron job written in python 3. After a tweet submission no further tweets +are created for a configurable period. -(see config.py-template for example) -## run as cron +# Installation -add to your crontab: +- Create a twitter account, obtain an access token, see https://developer.twitter.com/en/docs/basics/authentication/guides/access-tokens for further details. +- Make sure python 3 is installed on your dev machine. +- Clone this repository +- Create a virtual environment, e.g. you may obtain more information about those steps here: http://docs.python-guide.org/en/latest/dev/virtualenvs/ + ``` + virtualenv venv -p /usr/bin/python3 + source venv/bin/activate + pip3 install -r requirements.txt + ``` +- Copy the file ```config.py-template``` to ```config.py```, adjust the setup. At least you should alter the following values as starting point. ``` -*/15 * * * * cd /home/pi/projects/aschuma__luftdaten_tweet && venv/bin/python tweet.py > /dev/null 2>&1 -``` - -## virtualenv + # Your PM sensor ID + conf_particle_sensor_id = 4711 + # Your temperature sensor ID + conf_temperature_sensor_id = 4711 + # The URL pointing to the location of your sensor + conf_luftdaten_map_url = "http://deutschland.maps.luftdaten.info/#13/48.8066/9.2372" + # The name of your twitter account + conf_twitter_user_id = 'my-account' + # Credentials provided by twitter + consumer_key = '' + consumer_secret = '' + access_token = '' + access_token_secret = '' + ``` -cp config.py-template config.py -# edit config.py -virtualenv venv -p /usr/bin/python3 -source venv/bin/activate -pip3 install -r requirements.txt -python tweet.py +- Setup a cron job ``` +*/15 * * * * cd /home/pi/projects/aschuma__luftdaten_tweet && venv/bin/python tweet.py > /dev/null 2>&1 +``` + +# Contribution +Please feel free to issue a bug report or create a PR. Any helping hand is welcome. +Be aware that my primary coding language is Java. So currently some code portions might not follow python best practices. diff --git a/tweet.py b/tweet.py index aabf19a..ac5021b 100644 --- a/tweet.py +++ b/tweet.py @@ -90,14 +90,14 @@ def may_tweet_again(): if pm_10_0 is not None and pm_2_50 is not None and float(pm_10_0) > conf_limit_pm_10_0: current_time = strftime("%Y-%m-%d %H:%M:%S UTC", gmtime()) - message = "{} #{}\n⚠ PM10: {}g/m,³ PM2.5: {}µg/m³, {}°C, RH:{}%\n{}\n".format( + message = "{}\n⚠ PM10: {}g/m,³ PM2.5: {}µg/m³, {}°C, RH:{}%\nDetails: {}\nThis is a #{}, code available on github aschuma/air_tweets\n".format( conf_twitter_msg_preamble, - conf_twitter_tag_bot, pm_10_0, pm_2_50, temperature, humidity, - conf_luftdaten_map_url + conf_luftdaten_map_url, + conf_twitter_tag_bot ) print(message)