130 lines
4.0 KiB
Python
Executable File
130 lines
4.0 KiB
Python
Executable File
#!/usr/bin/python
|
|
#
|
|
# Custom twitter client... mostly for learning Python
|
|
|
|
import sys, twitter, Tkinter, Pmw, ConfigParser, os
|
|
|
|
|
|
class TwitWindow:
|
|
|
|
""" 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 = 10
|
|
|
|
self.labels = []
|
|
self.texts = []
|
|
|
|
|
|
def create_window(self):
|
|
self.tkroot = Tkinter.Tk()
|
|
self.tkroot.title("MyTwitter")
|
|
Pmw.initialise(self.tkroot)
|
|
|
|
# Create the scrolled frame that will hold the tweets
|
|
self.tweet_view = Pmw.ScrolledFrame(self.tkroot, hscrollmode='none', horizflex='elastic')
|
|
self.tweet_view.pack(expand=Tkinter.YES, fill=Tkinter.BOTH)
|
|
|
|
# Bind scrollwheel to move the tweets. Might as well allow scrollwheel
|
|
# from anywhere, so we'll bind to tkroot
|
|
self.tkroot.bind("<Button-4>", self.scroll_wheel);
|
|
self.tkroot.bind("<Button-5>", self.scroll_wheel);
|
|
|
|
# Create labels and text widgets
|
|
for i in range(0, self.num_entries):
|
|
self.labels.append(Tkinter.Label(self.tweet_view.interior()))
|
|
self.labels[i].pack(expand=False, fill=Tkinter.X)
|
|
self.texts.append(Tkinter.Text(self.tweet_view.interior()))
|
|
self.texts[i].pack(expand=False, fill=Tkinter.X)
|
|
self.labels[i].config(bg="#07c", fg="white")
|
|
self.texts[i].config(bg="#eff", fg="black", wrap=Tkinter.WORD)
|
|
|
|
# Create an update box at the bottom of the window
|
|
self.update_box = Tkinter.Frame(self.tkroot)
|
|
self.update_box.pack(expand=False, fill=Tkinter.X)
|
|
self.update_entry = Tkinter.Entry(self.update_box)
|
|
self.update_entry.pack(expand=Tkinter.YES, fill=Tkinter.X, side=Tkinter.LEFT)
|
|
self.update_button = Tkinter.Button(self.update_box, text="Update", command=self.update_status)
|
|
self.update_button.pack(expand=Tkinter.NO, side=Tkinter.RIGHT)
|
|
|
|
# Init the twitter API and start up the main loop
|
|
self.api = twitter.Api(username=self.username, password=self.password)
|
|
self.update_window()
|
|
self.tkroot.mainloop()
|
|
|
|
|
|
def update_window(self) :
|
|
statuses = self.api.GetFriendsTimeline(self.username)
|
|
for i in range(0, self.num_entries):
|
|
self.texts[i].delete(0.0, Tkinter.END) # Clear the old text
|
|
|
|
if i < len(statuses):
|
|
# Update the label with the user's name and screen name
|
|
user = statuses[i].user
|
|
labeltext = user.name + " (" + user.screen_name + ")"
|
|
self.labels[i].config(text=labeltext)
|
|
|
|
# Display the text of the tweet
|
|
self.texts[i].insert(Tkinter.END, statuses[i].text)
|
|
|
|
self.timer = self.tkroot.after(300000, self.update_window)
|
|
|
|
|
|
def update_status(self):
|
|
text = self.update_entry.text()
|
|
self.update_entry.delete(0, Tkinter.END)
|
|
self.api.PostUpdate(text)
|
|
UpdateWindow()
|
|
|
|
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');
|
|
|
|
|
|
### 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
|
|
|
|
|
|
# Do the action!
|
|
# arg = ""
|
|
# if len(sys.argv) > 1:
|
|
# arg = sys.argv[1]
|
|
|
|
# actions = {
|
|
# "show_lists": print_lists,
|
|
# "list": print_statuses_list,
|
|
# }
|
|
|
|
# actions.get(arg, print_statuses)()
|
|
|
|
|
|
# main
|
|
win = TwitWindow()
|
|
win.create_window()
|