Created
December 28, 2016 21:56
-
-
Save blakef/564465bfeadcb1241a01e9ce286479ea to your computer and use it in GitHub Desktop.
Notifies when loot arrives
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from argparse import ArgumentParser | |
import os | |
import time | |
from slackclient import SlackClient | |
def modified(cache, current): | |
modified = [] | |
for key, value in current.iteritems(): | |
if key in cache and cache[key] >= value: | |
continue | |
modified.append(key) | |
cache[key] = value | |
return modified | |
if __name__ == "__main__": | |
parser = ArgumentParser(description="watch some files") | |
parser.add_argument('--seconds', | |
type=int, | |
default=300, help="seconds to check for change") | |
parser.add_argument('path', | |
type=str, | |
default='.', | |
help="path to watch files") | |
args = parser.parse_args() | |
print "Watching: {} for updates every {}s".format( | |
args.path, | |
args.seconds) | |
cache = {} | |
def update(): | |
current = {} | |
for file in os.listdir(args.path): | |
current[file] = os.lstat(os.path.join(args.path, file)).st_mtime | |
return modified(cache, current) | |
update() | |
while True: | |
time.sleep(args.seconds) | |
changes = update() | |
if len(changes) > 0: | |
client = SlackClient(os.environ['SLACK_API_TOKEN']) | |
text = ":moneybag: has arrived: {}".format( | |
",".join(changes) | |
) | |
client.api_call( | |
"chat.postMessage", | |
channel="#loot", | |
username="Money Bot", | |
icon_emoji="banana", | |
text=text | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment