Пример #1
0
func parseSearchResults(elem json.Json) *SearchResult {
	result := new(SearchResult)
	result.Name = elem.Get("from_user").String()
	result.ProfileImageUrl = elem.Get("profile_image_url").String()
	result.Tweet = elem.Get("text").String()
	result.SentAt = elem.Get("created_at").String()
	return result
}
Пример #2
0
/**
 * GetMentions gets all mentions of you using the credentials
 * saved in the Client provided.
 */
func GetMentions(client *Client) (statuses []Status, err os.Error) {
	log.Stdout("Get Mentions\n")
	// get json response from server
	var jsonStrResponse string
	var maxFetchCount = client.MaxFetchCount
	if maxFetchCount == 0 {
		maxFetchCount = DefaultMaxFetchCount
	} // use a default
	var form = map[string][]string{
		"count": []string{strconv.Itoa(maxFetchCount)},
	}

	// send request
	jsonStrResponse, err = send(URLMentions, "GET", form, client, "count="+strconv.Itoa(maxFetchCount))
	if err != nil {
		return nil, err
	}

	// parse json response
	var resultObj json.Json
	resultObj, err = parseJson(jsonStrResponse)
	if err != nil {
		return nil, err
	}

	// create status response
	var i int
	statuses = make([]Status, resultObj.Len())
	log.Stdoutf("result count: %d", resultObj.Len())
	for i = 0; i < len(statuses); i++ {
		var elem = resultObj.Elem(i)
		statuses[i] = *parseStatus(elem)
	}
	return
}
Пример #3
0
/**
 * GetDirectMessages gets all of your direct messages. It uses the credentials
 * saved in the Client provided.
 */
func GetDirectMessages(client *Client) (statuses []Status, err os.Error) {
	log.Stdout("Get Direct Messages\n")
	// get json response from server
	var jsonStrResponse string
	var maxFetchCount = client.MaxFetchCount
	if maxFetchCount == 0 {
		maxFetchCount = DefaultMaxFetchCount
	} // use a default

	// send request
	jsonStrResponse, err = send(URLDirectMessages+"?count="+strconv.Itoa(maxFetchCount), "GET", nil, client, "")
	if err != nil {
		return nil, err
	}

	// parse json response
	var resultObj json.Json
	//log.Stdout("response: %s", jsonStrResponse);
	resultObj, err = parseJson(jsonStrResponse)
	if err != nil {
		return nil, err
	}

	// create status response
	var i int
	statuses = make([]Status, resultObj.Len())
	log.Stdoutf("result count: %d", resultObj.Len())
	for i = 0; i < len(statuses); i++ {
		statuses[i] = *parseDirectMessage(resultObj.Elem(i))
	}
	return
}
Пример #4
0
/**
 * GetUserTimeline gets the user's timeline (provided by screenName) using the credentials
 * saved in the Client provided.
 */
func GetUserTimeline(client *Client, screenName string) (statuses []Status, err os.Error) {
	log.Stdout("Get User's Timeline\n")
	// get json response from server
	var jsonStrResponse string
	var maxFetchCount = client.MaxFetchCount
	if maxFetchCount == 0 {
		maxFetchCount = DefaultMaxFetchCount
	} // use a default

	// send request
	jsonStrResponse, err = send(URLUserTimeline+"?screen_name="+screenName+"&count="+strconv.Itoa(maxFetchCount), "GET", nil, client, "")
	if err != nil {
		return nil, err
	}

	// parse json response
	var resultObj json.Json
	//log.Stdout("response: %s", jsonStrResponse);
	resultObj, err = parseJson(jsonStrResponse)
	if err != nil {
		return nil, err
	}

	// create status response
	var i int
	statuses = make([]Status, resultObj.Len())
	log.Stdoutf("result count: %d", resultObj.Len())
	for i = 0; i < len(statuses); i++ {
		var elem = resultObj.Elem(i)
		//log.Stdoutf("found status: %s\n", elem.Get("text").String());
		statuses[i] = *parseStatus(elem)
	}
	return
}
Пример #5
0
// parseStatus converts a json obj into a twitter status.
func parseStatus(elem json.Json) *Status {
	status := new(Status)
	status.Text = elem.Get("text").String()
	status.SentAt = elem.Get("created_at").String()
	status.Tweeter = *parseUser(elem.Get("user"))
	return status
}
Пример #6
0
/**
 * DoSearch searches all status updates for a particular pattern.
 */
func DoSearch(client *Client, query string) (searchResults []SearchResult, err os.Error) {
	log.Stdout("Do Search\n")
	// get json response from server
	var jsonStrResponse string
	var maxFetchCount = client.MaxFetchCount
	if maxFetchCount == 0 {
		maxFetchCount = DefaultMaxFetchCount
	} // use a default
	query = http.URLEscape(query) // make query safe to travel in http body/url
	var maxFetchCountStr = strconv.Itoa(maxFetchCount)

	// send request
	log.Stdoutf("max fetch count: %s\n", maxFetchCountStr)
	var url = URLSearch + "?q=" + query + "&rpp=" + maxFetchCountStr
	jsonStrResponse, err = send(url, "GET", nil, client, "")
	if err != nil {
		return nil, err
	}

	// parse json response
	var resultObj json.Json
	resultObj, err = parseJson(jsonStrResponse)
	if err != nil {
		return nil, err
	}

	// create status response
	var i int
	resultObj = resultObj.Get("results")
	searchResults = make([]SearchResult, resultObj.Len())
	log.Stdoutf("result count: %d", resultObj.Len())
	for i = 0; i < len(searchResults); i++ {
		var elem = resultObj.Elem(i)
		searchResults[i] = *parseSearchResults(elem)
	}
	return
}
Пример #7
0
/**
 * GetFriendsTimeline gets the same timeline as Home does on the web. It uses the credentials
 * saved in the Client provided.
 */
func GetFriendsTimeline(client *Client) (statuses []Status, err os.Error) {
	log.Stdout("Get Friends Timeline\n")
	jsonStrResponse, err := GetFriendsTimelineJson(client)
	if err != nil {
		return nil, err
	}

	// parse json response
	var resultObj json.Json
	//log.Stdout("response: %s", jsonStrResponse);
	resultObj, err = parseJson(jsonStrResponse)
	if err != nil {
		return nil, err
	}

	// create status response
	var i int
	statuses = make([]Status, resultObj.Len())
	log.Stdoutf("result count: %d", resultObj.Len())
	for i = 0; i < len(statuses); i++ {
		statuses[i] = *parseStatus(resultObj.Elem(i))
	}
	return
}
Пример #8
0
// parseUser converts a json obj into a twitter user
func parseUser(elem json.Json) *User {
	user := new(User)
	user.ScreenName = elem.Get("screen_name").String()
	user.Name = elem.Get("name").String()
	user.Description = elem.Get("description").String()
	user.Url = elem.Get("url").String()
	user.ProfileImageUrl = elem.Get("profile_image_url").String()
	user.Location = elem.Get("location").String()
	user.FriendsCount, _ = strconv.Atoi(elem.Get("friends_count").String())
	user.FollowersCount, _ = strconv.Atoi(elem.Get("followers_count").String())
	user.ProfileBgColour = "#" + elem.Get("profile_background_color").String()
	user.ProfileFgColour = "#" + elem.Get("profile_text_color").String()
	user.ProfileLinkColour = "#" + elem.Get("profile_link_color").String()
	user.ProfileBgImageUrl = elem.Get("profile_background_image_url").String()
	user.ProfileBgImageTile = elem.Get("profile_background_tile").String() == "true"
	return user
}