logo

scripts

Unnamed repository; edit this file 'description' to name the repository.
commit: f9d63b9cd852a948079cde01f57cd071d88061cb
parent: baad75f5978aa54cb8f733897559ba6e6c07775b
Author: lanodan <lanodan.delta@free.fr>
Date:   Fri,  6 Feb 2015 23:02:10 +0100

Add twitter client

Diffstat:

twitter/README.md | 1+
twitter/stream.py | 29+++++++++++++++++++++++++++++
twitter/tweet.py | 39+++++++++++++++++++++++++++++++++++++++
3 files changed, 69 insertions(+), 0 deletions(-)

diff --git a/twitter/README.md b/twitter/README.md @@ -0,0 +1 @@ + * Uses [TwitterAPI](https://github.com/geduldig/TwitterAPI) diff --git a/twitter/stream.py b/twitter/stream.py @@ -0,0 +1,29 @@ +#!/usr/bin/python3 +# -*- encoding: utf-8 -*- +from TwitterAPI import TwitterAPI, TwitterOAuth, TwitterRestPager +import html, sys + +o = TwitterOAuth.read_file('credentials.txt') +api = TwitterAPI( + o.consumer_key, + o.consumer_secret, + o.access_token_key, + o.access_token_secret) + +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'])) + 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'], '”') + except: + print(item) + print(sys.exc_info()) + else: + print(item) diff --git a/twitter/tweet.py b/twitter/tweet.py @@ -0,0 +1,39 @@ +#!/usr/bin/python3 +# -*- encoding: utf-8 -*- +from TwitterAPI import TwitterAPI, TwitterOAuth, TwitterRestPager +import sys + +o = TwitterOAuth.read_file('credentials.txt') +api = TwitterAPI( + o.consumer_key, + o.consumer_secret, + o.access_token_key, + o.access_token_secret) + +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[0] == 'fav' and command[1] is not None: + api.request('favorites/create', {'id':command[1]}) + elif command[0] == 'rt' and command[1] is not None: + request = 'statuses/retweet/{!s}'.format(command[1]) + print(request) + api.request(request) + else: + print(command[0], 'doesn’t exist') + 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())