Created
December 14, 2017 15:58
-
-
Save jace/bf51dd004a41f40c49e9d222850e31ee to your computer and use it in GitHub Desktop.
Bulletin Babu for #SpeakForMe
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 | |
""" | |
Script to count the emails received by #SpeakForMe | |
""" | |
import os | |
import sys | |
import tweepy | |
import requests | |
import json | |
from collections import OrderedDict | |
keywords = OrderedDict([ | |
('mp', "MPs"), | |
('bank', "Banks"), | |
('mobile', "Mobile service providers"), | |
('gov', "Government services"), | |
('catchall', "Others"), | |
]) | |
consumer_key = "<insert>" | |
consumer_secret = "<insert>" | |
access_token = "<insert>" | |
access_token_secret = "<insert>" | |
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) | |
auth.set_access_token(access_token, access_token_secret) | |
def main(argv): | |
if len(argv) < 2: | |
print "Missing count parameter" | |
return | |
counts = dict([l.strip().split(' ', 1) for l in open(argv[1]).readlines() if l.strip()]) | |
if len(argv) > 2 and os.path.exists(argv[2]): | |
oldcounts = dict([l.strip().split(' ', 1) for l in open(argv[2]).readlines() if l.strip()]) | |
else: | |
oldcounts = {} | |
for key in counts: | |
counts[key] = int(counts[key]) | |
for key in oldcounts: | |
oldcounts[key] = int(oldcounts[key]) | |
status = "Emails from #SpeakForMe to:\n" | |
for key in keywords: | |
if key in counts: | |
status += keywords[key] + ": {:,}".format(counts[key]) | |
if key in oldcounts: | |
status += " ({:+,})".format(counts[key] - oldcounts[key]) | |
status += "\n" | |
total = sum(counts.values()) | |
oldtotal = sum(oldcounts.values()) | |
status += "Total: {:,}".format(total) | |
if oldtotal: | |
status += " ({:+,})".format(total - oldtotal) | |
requests.post("https://hooks.slack.com/services/<insert>", data=json.dumps({"text": status})) | |
api = tweepy.API(auth) | |
api.update_status(status=status) | |
if __name__ == "__main__": | |
main(sys.argv) |
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
# m h dom mon dow command | |
30 * * * * touch count.txt; mv count.txt count.old; for DIR in mp bank mobile gov catchall; do echo $DIR `find mailbox/$DIR -type f | wc -l`; done > count.txt; ./bulletinbabu.py count.txt count.old |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment