import argparse import configparser import tweepy from twitivity import Twitivity, TwitivityError config = configparser.ConfigParser() config.read('settings.cfg') consumer_key = config.get('Twitter', 'consumer_key') consumer_secret = config.get('Twitter', 'consumer_secret') environment_name = config.get('Twitter', 'environment_name') def run(command, token, secret, webhook_id=None, webhook_url=None): handler = tweepy.OAuthHandler(consumer_key, consumer_secret) handler.set_access_token(token, secret) twitivity = Twitivity(tweepy.API(handler), environment_name) try: if command == 'check-webhook': result = twitivity.check_webhook() elif command == 'register-webhook': result = twitivity.register_webhook(webhook_url) elif command == 'remove-webhook': result = twitivity.remove_webhook(webhook_id) elif command == 'poke-webhook': result = twitivity.poke_webhook(webhook_id) elif command == 'check-subscription': result = twitivity.check_subscription() elif command == 'subscribe': result = twitivity.subscribe() elif command == 'unsubscribe': result = twitivity.unsubscribe() except TwitivityError as e: return 'ERROR: {0}'.format(str(e)) return 'SUCCESS: {0}'.format(result or '-') def main(): parser = argparse.ArgumentParser() parser.add_argument('--token', metavar='ACCESS_TOKEN', required=True) parser.add_argument('--secret', metavar='ACCESS_TOKEN_SECRET', required=True) parser.add_argument('--webhook-id') parser.add_argument('--webhook-url') parser.add_argument('command', choices=( 'check-webhook', 'register-webhook', 'remove-webhook', 'poke-webhook', 'check-subscription', 'subscribe', 'unsubscribe' )) args = parser.parse_args() print(run(args.command, args.token, args.secret, webhook_id=args.webhook_id, webhook_url=args.webhook_url)) if __name__ == '__main__': main()