logo

scripts

A bunch of scripts, some to be moved to their own repository
commit: e9d102899cc50fc41a366284579302740542a8da
parent: 51b9b1b7b0362410d93bfcdb449de77fa156bc04
Author: lanodan <lanodan.delta@free.fr>
Date:   Tue,  3 Mar 2015 18:59:17 +0100

Uh I suck at commiting

Diffstat:

Dtimelapse.sh2--
Atwitter/common.py19+++++++++++++++++++
Atwitter/config.py.example11+++++++++++
Atwitter/direct_messages.py17+++++++++++++++++
Atwitter/mentions.py35+++++++++++++++++++++++++++++++++++
Atwitter/replies.py11+++++++++++
Mtwitter/stream.py76+++++++++++++++++++++++++++++++++++++++++++++-------------------------------
Mtwitter/tweet.py4++--
Aunicode.py313+++++++++++++
9 files changed, 153 insertions(+), 35 deletions(-)

diff --git a/timelapse.sh b/timelapse.sh @@ -1,2 +0,0 @@ -#!/bin/sh -fswebcam /seagate/net/the-delta/www/test/webcam.png && cp /seagate/net/the-delta/www/test/webcam.png /seagate/net/the-delta/www/test/webcam-$(date +%F-%H:%M:%S).png diff --git a/twitter/common.py b/twitter/common.py @@ -0,0 +1,19 @@ +from TwitterAPI import TwitterAPI + +f = { + 'default': '\033[1m %s %s %s “\033[3m%s\033[0m”', + 'favorite': '\033[1;33m %s %s %s “\033[3m%s\033[0m”', + 'retweet': '\033[1;32m %s %s %s “\033[3m%s\033[0m”', + 'follow': '\033[1m %s %s %s \033[0m', + 'tweet': '%(id)s <\033[1m%(screen_name)s> — %(name)s \033[0m\n\t%(text)s' +} + +friend_list_garbage = 'Twitter gave me friend list, I think it’s garbage, since I want the timeline' + +def api(consumer_key, consumer_secret, access_token_key, access_token_secret): + return TwitterAPI( + consumer_key, + consumer_secret, + access_token_key, + access_token_secret + ) diff --git a/twitter/config.py.example b/twitter/config.py.example @@ -0,0 +1,11 @@ +credentials = { + 'consumer_secret': 'rNQWXzfCJehXL8FKHdVNk7BwWWEjEjq9oKT3KH2DCsN0bLXMrm', + 'consumer_key': 'r2BQmLAFCzY8zbLcsxQM3xqxM' + 'users': { + 0: { + 'access_token_key': None, + 'access_token_secret': None, + 'screen_name': 'lanodan' + } + } +} diff --git a/twitter/direct_messages.py b/twitter/direct_messages.py @@ -0,0 +1,17 @@ +#!/usr/bin/python3 +# -*- encoding: utf-8 -*- +from TwitterAPI import TwitterAPI, TwitterOAuth, TwitterRestPager + +o = TwitterOAuth.read_file('credentials.txt') +api = TwitterAPI( + o.consumer_key, + o.consumer_secret, + o.access_token_key, + o.access_token_secret) + +def get_direct_messages(): + r = api.request('direct_messages/sent') + print(r) + +if __name__ == "__main_": + get_direct_messages() diff --git a/twitter/mentions.py b/twitter/mentions.py @@ -0,0 +1,35 @@ +#!/usr/bin/python3 +# -*- encoding: utf-8 -*- +from html import unescape +from time import sleep +import common +import config + + +def get_mentions(since_id): + r = api.request('statuses/mentions_timeline', {'since_id' : since_id}) + for item in r: + try: + print(common.f['tweet'] % {'id': item['id'], 'screen_name': item['user']['screen_name'], 'name': item['user']['name'], 'text': unescape(item['text'])}) + except KeyError: + print(item) + try: + return item['id'] + except: + return since_id + +if __name__ == "__main__": + user = dict() + username = list(config.credentials['users'])[0] + user = config.credentials['users'][username] + user['name'] = username + username = None + api = common.api(config.credentials['consumer_key'], config.credentials['consumer_secret'], user['access_token_key'], user['access_token_secret']) + since_id = '571110938467921921' # Garbage id need to save it to a file maybe json + while True: + old_id = since_id + since_id = get_mentions(since_id) + if old_id != since_id: + print('\033[1mSince_id:\033[0m', since_id) + + sleep(60) diff --git a/twitter/replies.py b/twitter/replies.py @@ -0,0 +1,11 @@ +#!/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) diff --git a/twitter/stream.py b/twitter/stream.py @@ -1,37 +1,51 @@ #!/usr/bin/python3 # -*- encoding: utf-8 -*- -from TwitterAPI import TwitterAPI, TwitterOAuth, TwitterRestPager import html, sys +import config -o = TwitterOAuth.read_file('credentials.txt') -api = TwitterAPI( - o.consumer_key, - o.consumer_secret, - o.access_token_key, - o.access_token_secret) +#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: - print(item['id'], ' <\033[1m', item['user']['screen_name'], '\033[0m> — ', item['user']['name'], sep='') - 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: - 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: +def main(user): + import common + api = common.api(config.credentials['consumer_key'], config.credentials['consumer_secret'], user['access_token_key'], user['access_token_secret']) + + r = api.request('user', {'replies': 'all'}) + for item in r: + if 'text' in item: + print(common.f['tweet'] % {'id': item['id'], 'screen_name': item['user']['screen_name'], 'name': item['user']['name'], 'text': html.unescape(item['text'])}) + elif 'friends' in item: + print(common.friend_list_garbage) + elif 'event' in item: + try: + if item['event'] in ('favorite', 'favorited_retweet'): + print(common.f['favorite'] % { item['source']['screen_name'], item['event'], item['target_object']['user']['screen_name'], item['target_object']['text']}) + elif item['event'] in ('retweet', 'retweeted_retweet'): + print(common.f['retweet'] % { item['source']['screen_name'], item['event'], item['target_object']['user']['screen_name'], item['target_object']['text']}) + elif item['event'] == 'follow': + print(common.f['follow'] % { item['source']['screen_name'], item['event'], item['target']['user']['screen_name']}) + else: + print(common.f['default'] % { item['source']['screen_name'], item['event'], item['target_object']['user']['screen_name'], item['target_object']['text']}) + except: + print(item) + print(sys.exc_info()) + else: print(item) - print(sys.exc_info()) - else: - print(item) + +user = dict() +username = list(config.credentials['users'])[0] +#if __name__ == "__main__": +# try: +# username = sys.argv[0] +# except KeyError: +# print('Protip: you can specify an user account, anyway using first account') +# username = list(config.credentials['users'])[0] + +user = config.credentials['users'][username] +user['name'] = username +username = None +main(user) diff --git a/twitter/tweet.py b/twitter/tweet.py @@ -110,5 +110,5 @@ def parse_command(status): if __name__ == "__main__": while 1: - status = input('>> ') - parse_command(status) + prompt = input('>>') + parse_command(prompt) diff --git a/unicode.py3 b/unicode.py3 @@ -0,0 +1,13 @@ +#!/usr/bin/python3 +# -*- encoding: utf-8 -*- + +unicodeChars = chr(1) + +for i in range(2, 1114111): + try: + unicodeChars = str(unicodeChars + chr(i)) + print(i) + except UnicodeEncodeError: + print('UnicodeEncodeError') + +print(unicodeChars)