logo

scripts

A bunch of scripts, some to be moved to their own repository
commit: 516a22ebd3a0827bd8e8e4f9eaef66317a9905b6
parent: 618ffb98e06ca3e0243de3ef4a052127b81fb6a9
Author: lanodan <lanodan.delta@free.fr>
Date:   Thu, 12 Feb 2015 12:37:53 +0100

Yay I’ve forget to commit fluently

Diffstat:

Mtwitter/search.py28++++++++++++++++------------
Mtwitter/stream.py16++++++++++++----
Mtwitter/tweet.py52++++++++++++++++++++++++++++++++++++++--------------
3 files changed, 66 insertions(+), 30 deletions(-)

diff --git a/twitter/search.py b/twitter/search.py @@ -1,7 +1,6 @@ #!/usr/bin/python3 # -*- encoding: utf-8 -*- from TwitterAPI import TwitterAPI, TwitterOAuth, TwitterRestPager -import sys o = TwitterOAuth.read_file('credentials.txt') api = TwitterAPI( @@ -10,14 +9,19 @@ api = TwitterAPI( o.access_token_key, o.access_token_secret) -try: - r = api.request('search/tweets', {'q': sys.argv[1:]}) - for item in r: - if 'text' in item: - print(item['id'], ' <\033[1m', item['user']['screen_name'], '\033[0m> — ', item['user']['name'], sep='') - else: - print(item) - - print('\nQUOTA: %s' % r.get_rest_quota()) -except: - print(sys.exc_info()) +def search(args): + try: + r = api.request('search/tweets', {'q': args[1:]}) + for item in r: + if 'text' in item: + print(item['id'], ' <\033[1m', item['user']['screen_name'], '\033[0m> — ', item['user']['name'], sep='') + else: + print(item) + + print('QUOTA: ', r.get_rest_quota()) + except: + print(sys.exc_info()) + +if __name__ == "__main__": + import sys + search(sys.argv[1:]) diff --git a/twitter/stream.py b/twitter/stream.py @@ -13,15 +13,23 @@ api = TwitterAPI( r = api.request('user', {'replies': 'all'}) for item in r: if 'text' in item: -# if 'url' in item[''] -# print(item['id'], ' <\033[1m', item['user']['screen_name'], '\033[0m> — ', item['user']['name'], sep='') - print('\t',html.unescape(item['text'])) + try: + print('\t', html.unescape(item['text'].replace(item['entities']['url']['urls']['url'], item['entities']['url']['urls']['expanded_url']))) + except KeyError: + print('\t', html.unescape(item['text'])) elif 'friends' in item: print('Twitter gave me friend list, I think it’s garbage, since I want the timeline') elif 'event' in item: try: - print('\033[1m', item['source']['screen_name'], item['event'], item['target_object']['user']['screen_name'], '\033[0m“', item['target_object']['text'], '”') + if item['event'] in ('favorite', 'favorited_retweet'): + print('\033[1;33m', item['source']['screen_name'], item['event'], item['target_object']['user']['screen_name'], '\033[0m“', item['target_object']['text'], '”') + elif item['event'] in ('retweete', 'retweeted_retweet'): + print('\033[1;32m', item['source']['screen_name'], item['event'], item['target_object']['user']['screen_name'], '\033[0m“', item['target_object']['text'], '”') + elif item['event'] == 'follow': + print('\033[1m', item['source']['screen_name'], item['event'], item['target']['user']['screen_name'], '\033[0m') + else: + print('\033[1m', item['source']['screen_name'], item['event'], item['target_object']['user']['screen_name'], '\033[0m“', item['target_object']['text'], '”') except: print(item) print(sys.exc_info()) diff --git a/twitter/tweet.py b/twitter/tweet.py @@ -10,6 +10,15 @@ 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('Search: /<search|?> <search_string>') + 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>') + try: if sys.argv[1] is not None: print(api.request('statuses/update', {'status': sys.argv[1:]})) @@ -20,21 +29,36 @@ except: try: if status[0] == '/': command = status[1:].split(' ') - 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] == 'tweet': - print('No command is used to tweet, just put it without /tweet') + 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(command[0], 'doesn’t exist\nUsage:') - print('Favorite: /f /fav /favorite') - print('Retweet: /rt /retweet') - print('Favorite & Retweet: /frt /favrt') - print('To tweet just put it without any command. Image uploading not supported yet') + print(status, 'you’ve forget arguments.') + _help() else: if len(status) > 140: print('Tweets have a max of 140 charaters')