Skip to content

Instantly share code, notes, and snippets.

@jace
Created December 14, 2017 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jace/bf51dd004a41f40c49e9d222850e31ee to your computer and use it in GitHub Desktop.
Save jace/bf51dd004a41f40c49e9d222850e31ee to your computer and use it in GitHub Desktop.
Bulletin Babu for #SpeakForMe
#!/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)
# 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