Check-in [ded0b18764]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview

SHA1 Hash:ded0b1876455a4236d4f46a0cbf54d269caa0263
Date: 2009-11-13 01:21:03
User: dmitry
Comment:Refactor code. Remove Twitter JSON work-around, and instead initialize a slice with lengh=20 to get unmarshalled tweets.

Tags And Properties
Changes

Changes to src/Makefile

@@ -11,6 +11,6 @@
 
 twitter.6: twitter.go
 	6g twitter.go
 
 clean:
-	rm -rf *.6 gotweet
+	rm *.6 gotweet

Changes to src/main.go

@@ -59,36 +59,31 @@
 	tw := twitter.NewTwitter(*username, *password);
 
 	switch flag.Arg(0) {
 	case "@", "mentions":
 		requireLogin();
-		fmt.Println(checkForError(tw.GetMentions()));
+		os.Stdout.WriteString(checkForError(tw.Mentions()));
 	case "", "friends":
 		requireLogin();
-		fmt.Println(checkForError(tw.GetFriendsTimeline()));
+		os.Stdout.WriteString(checkForError(tw.FriendsTimeline()));
 	case "u", "user":
 		requireLogin();
-		fmt.Println(checkForError(tw.GetUserTimeline()));
+		os.Stdout.WriteString(checkForError(tw.UserTimeline()));
 	case "public":
-		fmt.Println(checkForError(tw.GetPublicTimeline()))
+		os.Stdout.WriteString(checkForError(tw.PublicTimeline()))
 	case "p", "post":
 		requireLogin();
 		s := "";
 		for i := 1; i < flag.NArg(); i++ {
 			if i > 1 {
 				s += " "
 			}
 			s += flag.Arg(i);
 		}
-		err := tw.Post(s);
+		err := tw.UpdateStatus(s);
 		if err != nil {
 			fmt.Fprintf(os.Stderr, "%s\n", err);
 			os.Exit(1);
 		}
-	default:
-		requireLogin();
-		fmt.Println(checkForError(tw.GetFriendsTimeline()));
-		os.Exit(1);
 	}
 	os.Exit(0);
-
 }

Changes to src/twitter.go

@@ -43,14 +43,10 @@
 	Text	string;
 	Source	string;
 	User	User;
 }
 
-type Timeline struct {
-	Tweets []Tweet;
-}
-
 const (
 	mentionsURL		= "http://twitter.com/statuses/mentions.json";
 	friendsTimelineURL	= "http://twitter.com/statuses/friends_timeline.json";
 	userTimelineURL		= "http://twitter.com/statuses/user_timeline";	// no .json!
 	publicTimelineURL	= "http://twitter.com/statuses/public_timeline.json";
@@ -62,21 +58,11 @@
 	acc.Username = user;
 	acc.Password = pwd;
 	return &Twitter{acc};
 }
 
-func unmarshalTwitterJSON(s string) Timeline {
-	// Twitter sends JSON with top-level array, but Go's json can't unmarshall it.
-	// Workaround: put it under 'tweets'.
-	s = `{"tweets" : ` + s + `}`;
-
-	var timeline Timeline;
-	json.Unmarshal(s, &timeline);
-	return timeline;
-}
-
-func (t *Twitter) getTimelineForURL(url string) (out string, err os.Error) {
+func (t *Twitter) getTimeline(url string) (out string, err os.Error) {
 	var r *http.Response;
 	r, err = http_auth.Get(url, t.Account.Username, t.Account.Password);
 
 	if err != nil {
 		return
@@ -88,14 +74,15 @@
 	}
 
 	b, _ := io.ReadAll(r.Body);
 	r.Body.Close();
 
-	timeline := unmarshalTwitterJSON(string(b));
+	tweets := make([]Tweet, 20);
+	json.Unmarshal(string(b), tweets);
 
 	re, _ := regexp.Compile("<a[^>]*>(.*)</a>");
-	for _, t := range timeline.Tweets {
+	for _, t := range tweets {
 		// Source may be a link: <a href="...">source</a>
 		// Extract text of the link with regexp.
 		matches := re.MatchStrings(t.Source);
 		if matches != nil && len(matches) > 0 {
 			t.Source = matches[1]
@@ -107,11 +94,11 @@
 		}
 	}
 	return;
 }
 
-func (t *Twitter) postToURL(url, s string) (our string, err os.Error) {
+func (t *Twitter) post(url, s string) (our string, err os.Error) {
 	bb := &bytes.Buffer{};
 	bb.Write(strings.Bytes(s));
 
 	var r *http.Response;
 	r, err = http_auth.Post(url, t.Account.Username, t.Account.Password,
@@ -131,26 +118,26 @@
 
 	return string(b), nil;
 }
 
 
-func (t *Twitter) GetMentions() (string, os.Error) {
-	return t.getTimelineForURL(mentionsURL)
+func (t *Twitter) Mentions() (string, os.Error) {
+	return t.getTimeline(mentionsURL)
+}
+
+func (t *Twitter) FriendsTimeline() (string, os.Error) {
+	return t.getTimeline(friendsTimelineURL)
 }
 
-func (t *Twitter) GetFriendsTimeline() (string, os.Error) {
-	return t.getTimelineForURL(friendsTimelineURL)
+func (t *Twitter) UserTimeline() (string, os.Error) {
+	return t.getTimeline(userTimelineURL + "/" + t.Account.Username + ".json")
 }
 
-func (t *Twitter) GetUserTimeline() (string, os.Error) {
-	return t.getTimelineForURL(userTimelineURL + "/" + t.Account.Username + ".json")
+func (t *Twitter) PublicTimeline() (string, os.Error) {
+	return t.getTimeline(publicTimelineURL)
 }
 
-func (t *Twitter) GetPublicTimeline() (string, os.Error) {
-	return t.getTimelineForURL(publicTimelineURL)
-}
-
-func (t *Twitter) Post(s string) os.Error {
+func (t *Twitter) UpdateStatus(s string) os.Error {
 	s = "status=" + http.URLEscape(s);
-	_, err := t.postToURL(updateURL, s);
+	_, err := t.post(updateURL, s);
 	return err;
 }



Mac journal software
How to write a journal