logo

scripts

Unnamed repository; edit this file 'description' to name the repository.
commit: 51b9b1b7b0362410d93bfcdb449de77fa156bc04
parent: 7fbe4bf9a1b137959a6643d4d6330d5e00ba5a98
Author: lanodan <lanodan.delta@free.fr>
Date:   Mon, 23 Feb 2015 00:22:14 +0100

Okay I’ve done a large refactor

Diffstat:

twitter/tweet.py | 160+++++++++++++++++++++++++++++++++++++++++++++++++------------------------------
1 file changed, 100 insertions(+), 60 deletions(-)

diff --git a/twitter/tweet.py b/twitter/tweet.py @@ -10,65 +10,105 @@ api = TwitterAPI( o.access_token_key, o.access_token_secret) -def _help(): - print('Usage:') - print('Favorite: /<f|fav|favorite> <tweet_id>') - print('Retweet: /<rt|retweet> <tweet_id>') - print('Favorite & Retweet: /<frt|favrt> <tweet_id>') - print('Reply: /<re|reply> <tweet_id> <status>') - print('Delete: /<del|delete|destroy> <tweet_id>') - print('NSFW: /nsfw <status>') - print('Image Upload: /upload <tweet_id>') -# print('Search: /<search|?> <search_string>') I’ll be implemented with curses interface - print('To tweet just put it without any command. Image uploading not supported yet') - print('Protip: To send a direct message prepend MS @<name> or M$ @<name>') +def favorite(id): + """ required argument, id """ + api.request('favorites/create', {'id': id}) -try: - if sys.argv[1] is not None: - print(api.request('statuses/update', {'status': sys.argv[1:]})) - sys.exit(0) -except: - while 1: - status = input('>> ') - try: - if status[0] == '/': - command = status[1:].split(' ') - if command[1] is not None: - if command[0] in ('f', 'fav', 'favorite'): - api.request('favorites/create', {'id':command[1]}) - elif command[0] in ('rt', 'retweet'): - api.request('statuses/retweet/:{}'.format(command[1])) - elif command[0] in ('frt', 'favrt'): - api.request('favorites/create', {'id':command[1]}) - api.request('statuses/retweet/:{}'.format(command[1])) - elif command[0] in ('re', 'reply'): - api.request('statuses/update', {'in_reply_to_status_id': command[1], 'status': status.split(' ', 1)[1]}) - elif command[0] in ('del', 'delete', 'destroy'): - api.request('statuses/destroy/:{}'.format(command[1])) - elif command[0] == 'nsfw': - api.request('statuses/update', {'status': command[1], 'possibly_sensitive': True}) - elif command[0] == 'upload': - file = open(command[1], 'rb') - data = file.read() - r = api.request('media/upload', None, {'media': data}) - media_id = r.json()['media_id'] - api.request('statuses/update', {'status': status.split(' ', 1)[1], 'media_ids': media_id}) - file.close() - data = None - elif command[0] == 'tweet': - print('No command is used to tweet, just put it without /tweet') - else: - print(status, 'doesn’t exist.') - _help() - else: - print(status, 'you’ve forget arguments.') - _help() +def retweet(id): + """ required argument: id """ + api.request('statuses/retweet/:{}'.format(id)) + +def reply(in_reply_to_status_id, status): + """ required arguments: in_reply_to_status_id, status """ + api.request('statuses/update', {'in_reply_to_status_id': in_reply_to_status_id, 'status': status}) + +def delete(id): + """ required argument: id """ + api.request('statuses/destroy/:{}'.format(id)) + +def nsfw(status): + """ required argument: status """ + api.request('statuses/update', {'status': status, 'possibly_sensitive': True}) + +def upload(file_path, status): + """ required argument: file_path, status """ + file = open(file_path, 'rb') + r = api.request('media/upload', None, {'media': file.read()}) + api.request('statuses/update', {'status': status, 'media_ids': r.json()['media_id']}) + file.close() + +def upload_nsfw(file_path, status): + """ required argument: file_path, status """ + file = open(file_path, 'rb') + r = api.request('media/upload', None, {'media': file.read()}) + api.request('statuses/update', {'status': status, 'media_ids': r.json()['media_id'], 'possibly_sensitive': True}) + file.close() + +def follow(target): + if target.isalpha(): + api.request('friendships/create', {'screen_name': target}) + else: + api.request('friendships/create', {'screen_name': target}) + +def unfollow(target): + if target.isalpha(): + api.request('friendships/destroy', {'screen_name': target}) + else: + api.request('friendships/destroy', {'user_id': target}) + +def parse_command(status): + """ Delete: /<del|delete|destroy> <tweet_id> """ + """ Favorite & Retweet: /<frt|favrt> <tweet_id> """ + """ Favorite: /<f|fav|favorite> <tweet_id> """ + """ NSFW: /nsfw <status> """ + """ Reply: /<re|reply> <tweet_id> <status> """ + """ Retweet: /<rt|retweet> <tweet_id> """ + """ Upload: /upload <tweet_id> """ + """ Follow: /follow <screen_name|user_id> """ + """ Unfollow: /unfollow <screen_name|user_id> """ + """ To tweet just put it without any command. Image uploading not supported yet """ + """ Protip: To send a direct message prepend MS @<name> or M$ @<name> """ + if status[0] == '/': + command = status[1:].split(' ') + if command[1] is not None: + if command[0] in ('f', 'fav', 'favorite'): + favorite(id = command[1]) + elif command[0] in ('rt', 'retweet'): + retweet(id = command[1]) + elif command[0] in ('frt', 'favrt'): + favorite(id = command[1]) + retweet(id = command[1]) + elif command[0] in ('re', 'reply'): + reply(in_reply_to_status_id = command[1], status = status.split(' ', 2)[2]) + elif command[0] in ('del', 'delete', 'destroy'): + delete(command[1]) + elif command[0] == 'nsfw': + nsfw(status = command[1]) + elif command[0] == 'upload': + upload(status = status.split(' ', 2)[2], file_path = command[1]) + elif command[0] == 'upload_nsfw': + upload(status = status.split(' ', 2)[2], file_path = command[1]) + elif command[0] == 'follow': + follow(command[1]) + elif command[0] == 'unfollow': + unfollow(command[1]) + elif command[0] == 'tweet': + print('No command is used to tweet, just put it without /tweet') else: - if len(status) > 140: - print('Tweets have a max of 140 charaters') - elif len(status) == 0: - print('Tweets cannot be empty') - else: - print(api.request('statuses/update', {'status': status})) - except: - print(sys.exc_info()) + print(status, 'doesn’t exist.') + _help() + else: + print(status, 'you’ve forget arguments.') + _help() + else: + if len(status) > 140: + print('Tweets have a max of 140 charaters') + elif len(status) == 0: + print('Tweets cannot be empty') + else: + print(api.request('statuses/update', {'status': status})) + +if __name__ == "__main__": + while 1: + status = input('>> ') + parse_command(status)