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

149 lines
4.8 KiB
Python
Raw Normal View History

2010-04-07 03:05:51 +00:00
#!/usr/bin/python
#
# Custom twitter client... mostly for learning Python
import sys, twitter, ConfigParser, os, datetime, dateutil.tz, gtk, gtk.glade, gobject
2010-04-07 03:05:51 +00:00
class MyTwitter():
2010-04-07 03:05:51 +00:00
""" 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')
2010-04-07 18:47:16 +00:00
self.num_entries = int(config.get('global', 'entries'))
if self.num_entries < 20:
self.num_entries = 20
2010-04-07 18:47:16 +00:00
self.refresh_time = int(config.get('global', 'refreshtime'))
if self.refresh_time < 10:
self.refresh_time = 10
2010-04-07 03:05:51 +00:00
self.labels = []
self.texts = []
self.list = None
2010-04-07 03:05:51 +00:00
# Authenticate with twitter, set up the API object
self.api = twitter.Api(username=self.username, password=self.password)
2010-04-07 03:05:51 +00:00
# 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.list_select = self.widget_tree.get_widget('list_select')
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')
2010-04-07 17:21:55 +00:00
# Build us some labels...
2010-04-07 03:05:51 +00:00
for i in range(0, self.num_entries):
## Build the header
self.labels.append(gtk.Label())
label_eb = gtk.EventBox()
label_eb.add(self.labels[i])
label_eb.modify_text(gtk.STATE_NORMAL,gtk.gdk.color_parse("#ffffff"))
label_eb.modify_bg(gtk.STATE_NORMAL,gtk.gdk.color_parse("#8888ff"))
self.tweet_box.pack_start(label_eb)
self.labels[i].set_alignment(0.0, 0.0)
self.labels[i].set_selectable(True)
self.labels[i].set_line_wrap(True)
## Build the text
self.texts.append(gtk.Label())
text_align = gtk.Alignment()
text_align.set_padding(2, 10, 3, 0)
text_align.add(self.texts[i])
self.tweet_box.pack_start(text_align)
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()
# Fill the ComboBox with entries
self.list_select.append_text('@' + self.username)
lists = self.api.GetUserLists()
for l in lists['lists']:
self.list_select.append_text(l.name)
# Timer to update periodically
2010-04-07 03:05:51 +00:00
self.update_window()
gobject.timeout_add(self.refresh_time * 1000, self.update_window)
def update_window(self):
timezone = dateutil.tz.gettz()
time_format = "%Y.%m.%d %H:%M:%S %Z"
if self.list is None or self.list == 'Home':
statuses = self.api.GetFriendsTimeline(self.username, count=self.num_entries)
elif self.list == '@' + self.username:
statuses = self.api.GetReplies()
else:
statuses = self.api.GetListStatuses(self.list)
2010-04-07 03:05:51 +00:00
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
2010-04-09 04:12:39 +00:00
self.labels[i].set_markup(labeltext)
2010-04-07 03:05:51 +00:00
# Display the text of the tweet
2010-04-09 04:12:39 +00:00
self.texts[i].set_markup(statuses[i].text)
2010-04-07 03:05:51 +00:00
def update_window_callback(self, widget):
self.update_window()
def update_status(self):
text = self.update_entry.get_text()
self.update_entry.set_text("")
2010-04-07 03:05:51 +00:00
self.api.PostUpdate(text)
self.update_window()
def update_status_callback(self, widget):
self.update_status()
2010-04-07 03:05:51 +00:00
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"
def on_list_select_changed(self, widget):
self.list = widget.get_active_text()
self.update_window()
2010-04-07 03:05:51 +00:00
### end class TwitWindow
2010-04-07 03:05:51 +00:00
# main
my_twitter = MyTwitter()
gtk.main()