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
2010-04-06 23:05:51 -04:00

120 lines
3.5 KiB
Python
Executable File

#!/usr/bin/python
#
# Custom twitter client... mostly for learning Python
import sys, twitter, Tkinter, Pmw
class TwitWindow:
""" Display Tweets, post to twitter """
def __init__(self):
self.username = "gjalfr"
self.password = "fun4cabbage"
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)
self.tweet_view.bind("<MouseWheel>", 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):
### 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()