This repository has been archived on 2019-12-04. You can view files and clone it, but cannot push or open issues or pull requests.
hrafn/mytwitter.py

160 lines
4.7 KiB
Python
Executable File

#!/usr/bin/python
#
# Custom twitter client... mostly for learning Python
import sys, twitter, ConfigParser, os, datetime, dateutil.tz, gtk, gtk.glade, gobject
class MyTwitter():
""" Display Tweets, post to twitter """
def __init__(self):
config = ConfigParser.ConfigParser()
config.read(os.path.expanduser("~/.mytwitter"))
self.username = config.get('global', 'username')
self.password = config.get('global', 'password')
self.num_entries = int(config.get('global', 'entries'))
self.refresh_time = int(config.get('global', 'refreshtime'))
self.labels = []
self.texts = []
# Authenticate with twitter, set up the API object
self.api = twitter.Api(username=self.username, password=self.password)
# Load up all the GUI stuff
self.init_user_interface('./default.glade')
self.init_widgets()
def init_user_interface(self, path_to_skin):
self.widget_tree=gtk.glade.XML(path_to_skin, "window")
self.widget_tree.signal_autoconnect(self)
def init_widgets(self):
self.tweet_box = self.widget_tree.get_widget('tweet_box')
self.update_entry = self.widget_tree.get_widget('update_entry')
self.update_count = self.widget_tree.get_widget('update_count')
# Build us some labels...
for i in range(0, self.num_entries):
self.labels.append(gtk.Label())
self.tweet_box.pack_start(self.labels[i])
self.labels[i].set_alignment(0.0, 0.0)
self.labels[i].set_selectable(True)
self.labels[i].set_line_wrap(True)
self.texts.append(gtk.Label())
self.tweet_box.pack_start(self.texts[i])
self.texts[i].set_alignment(0.0, 0.0)
self.texts[i].set_selectable(True)
self.texts[i].set_line_wrap(True)
self.tweet_box.show_all()
# Timer to update periodically
self.update_window()
gobject.timeout_add(self.refresh_time * 100, self.update_window)
def update_window(self):
timezone = dateutil.tz.gettz()
time_format = "%Y.%m.%d %H:%M:%S %Z"
statuses = self.api.GetFriendsTimeline(self.username, count=self.num_entries)
for i in range(0, self.num_entries):
if i < len(statuses):
# Update the label with the user's name and screen name
user = statuses[i].user
timestamp = datetime.datetime.strptime(statuses[i].created_at, "%a %b %d %H:%M:%S +0000 %Y")
timestamp = timestamp.replace(tzinfo=dateutil.tz.gettz('UTC'))
timestring = timestamp.astimezone(timezone).strftime(time_format)
labeltext = user.name + " (" + user.screen_name + ") " + timestring
self.labels[i].set_text(labeltext)
# Display the text of the tweet
self.texts[i].set_text(statuses[i].text)
def update_status(self):
text = self.update_entry.get_text()
self.update_entry.set_text(none)
self.api.PostUpdate(text)
self.update_window()
# Just calls update_status, here so that things
# that pass an event can be used
def update_status_callback(self, widget):
self.update_status()
# fixme: convert all of the below to wxPython
# def scroll_wheel(self, event):
# if event.num == 4:
# self.tweet_view.yview('scroll', -5, 'units');
# if event.num == 5:
# self.tweet_view.yview('scroll', 5, 'units');
# def page_up(self, event):
# self.tweet_view.yview('scroll', -15, 'units');
# def page_down(self, event):
# self.tweet_view.yview('scroll', 15, 'units');
# def line_up(self, event):
# self.tweet_view.yview('scroll', -5, 'units');
# def line_down(self, event):
# self.tweet_view.yview('scroll', 5, 'units');
def char_counter(self, widget):
new_count = str(len(self.update_entry.get_text())) + "/140"
self.update_count.set_label(new_count)
def gtk_main_quit(self, widget):
gtk.main_quit()
def on_about(self, widget):
print "DEBUG: help->about not yet implemented"
### end class TwitWindow
def print_lists():
data = api.GetUserLists()
for l in data['lists']:
print l.name
def print_statuses_list():
statuses = api.GetListStatuses(sys.argv[2])
print_formatted(statuses)
def print_statuses():
statuses = api.GetFriendsTimeline(username)
print_formatted(statuses)
def print_formatted(statuses):
for s in statuses:
print s.user.name.encode("utf-8"), "(", s.user.screen_name, ") :"
print s.text.encode("utf-8")
print
# main
my_twitter = MyTwitter()
gtk.main()