Skip to content

Commit

Permalink
Merge pull request #1 from aschuma/feature/continous_tweet
Browse files Browse the repository at this point in the history
Enabling quite period
  • Loading branch information
aschuma authored Jan 27, 2018
2 parents 24eabc7 + 9a79e37 commit 80b7a69
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 25 deletions.
17 changes: 11 additions & 6 deletions config.py-template
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@

# lufdaten.info
particle_sensor_id = 4711
temperature_sensor_id = 4712
luftdaten_url = 'http://api.luftdaten.info/v1/sensor/%s/'
luftdaten_map_url = "http://deutschland.maps.luftdaten.info/#13/48.8066/9.2372"
twitter_tags = "#feinstaub"
limit_pm_10_0 = 20
conf_particle_sensor_id = 4711
conf_temperature_sensor_id = 4711
conf_luftdaten_url = 'http://api.luftdaten.info/v1/sensor/%s/'
conf_luftdaten_map_url = "http://deutschland.maps.luftdaten.info/#13/48.8066/9.2372"
conf_luftdaten_graph_url = "http://www.madavi.de/sensor/graph.php?sensor=esp8266-532353-sds011"
conf_twitter_msg_preamble = "#feinstaub"
conf_twitter_tag_bot = "bot"
conf_twitter_user_id = 'my-account'
conf_limit_pm_10_0 = 40
conf_quit_period_in_hours = 6


# tweeter
consumer_key = ''
Expand Down
74 changes: 55 additions & 19 deletions tweet.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import json
import tweepy
import requests
import datetime
from functools import reduce
from time import gmtime, strftime

import requests
import tweepy

from config import *


def lookup_sensors(base_url, sensors):
def lookup_single_sensor_data(url_param):

response = requests.get(url_param)

print(response.json())

if response.ok:
Expand All @@ -27,44 +29,78 @@ def lookup_single_sensor_data(url_param):

def lookup():
(pm_10_0_p, pm_2_50_p), (temperature_p, humidity_p) = lookup_sensors(
luftdaten_url,
[particle_sensor_id, temperature_sensor_id])
conf_luftdaten_url,
[conf_particle_sensor_id, conf_temperature_sensor_id])

if reduce(lambda zero, item: zero and item is not None, [pm_10_0_p, pm_2_50_p, temperature_p, humidity_p], True):
return [pm_10_0_p, pm_2_50_p, temperature_p, humidity_p]
else:
return None


def tweet(msg):
def twitter_api():
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
return api


def tweet(msg):
try:
api.update_status(msg)
twitter_api().update_status(msg)
except tweepy.TweepError as e:
print(e.reason)


pm_10_0, pm_2_50, temperature, humidity = lookup()
def last_bot_tweets():
def print_last_tweets(timeline_filtered):
print("--- last tweets ----")
for item in timeline_filtered:
print(item)
print("--------------------")

def has_bot_marker(timeline_item):
hashtags = [tags['text'] for tags in timeline_item.entities['hashtags']]
return conf_twitter_tag_bot in hashtags

if pm_10_0 is not None and pm_2_50 is not None and float(pm_10_0) > limit_pm_10_0:
try:
timeline = twitter_api().user_timeline(user_id=conf_twitter_user_id, count=50)
timeline_filtered = [item for item in timeline if has_bot_marker(item)]

current_time = strftime("%Y-%m-%d %H:%M:%S UTC", gmtime())
print_last_tweets(timeline_filtered)

message = "{}\nCurrent values: PM10: {}g/m³ PM2.5: {}µg/m³\n{}".format(
twitter_tags,
pm_10_0,
pm_2_50,
luftdaten_map_url
)
return timeline_filtered

except tweepy.TweepError as e:
print(e.reason)

print(message)

tweet(message)
def may_tweet_again():
reference_timestamp = datetime.datetime.now() - datetime.timedelta(hours=conf_quit_period_in_hours)
tweet_ts = [item.created_at for item in last_bot_tweets()]
allowed = len(tweet_ts) == 0 or max(tweet_ts) < reference_timestamp

print("Quit period NOT exceeded", allowed)

return allowed


pm_10_0, pm_2_50, temperature, humidity = lookup()

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(
conf_twitter_msg_preamble,
conf_twitter_tag_bot,
pm_10_0,
pm_2_50,
temperature,
humidity,
conf_luftdaten_map_url
)

print(message)

if may_tweet_again():
tweet(message)

0 comments on commit 80b7a69

Please sign in to comment.