Make the unsubscribe script universal

master
Nikola Forró 4 years ago
parent 966c28d8f8
commit 63ac656eb5

@ -0,0 +1,62 @@
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()

@ -1,41 +0,0 @@
import configparser
import sys
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 unsubscribe(access_token, access_token_secret):
handler = tweepy.OAuthHandler(consumer_key, consumer_secret)
handler.set_access_token(access_token, access_token_secret)
twitivity = Twitivity(tweepy.API(handler), environment_name)
try:
twitivity.check_subscription()
except TwitivityError:
return
twitivity.unsubscribe()
def main():
try:
unsubscribe(*sys.argv[1:3])
except TypeError:
print('Not enough arguments!')
except TwitivityError as e:
print(str(e))
else:
print('Success.')
if __name__ == '__main__':
main()
Loading…
Cancel
Save