Created
February 25, 2015 16:34
-
-
Save praswicaksono/9341230a39edeade78c9 to your computer and use it in GitHub Desktop.
Search Tweet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "github.com/ChimeraCoder/anaconda" | |
| "net/url" | |
| "encoding/csv" | |
| "os" | |
| ) | |
| func main() { | |
| // Set filename | |
| csvfile, err := os.Create("output.csv") | |
| if err != nil { | |
| fmt.Println("Error:", err) | |
| return | |
| } | |
| defer csvfile.Close() | |
| writer := csv.NewWriter(csvfile) | |
| // Set twitter auth details | |
| anaconda.SetConsumerKey("consumer-key") | |
| anaconda.SetConsumerSecret("consumer-secret") | |
| api := anaconda.NewTwitterApi("access-token", "access-token-secret") | |
| // Initialization | |
| MaxId := "0" | |
| LastSavedMaxId := "0" | |
| v := url.Values{} | |
| v.Set("max_id", MaxId) | |
| // Infinity loops ;) | |
| for { | |
| // Set parameter | |
| v.Set("count", "100") | |
| v.Set("lang", "en") | |
| // Lets query | |
| searchResult, _ := api.GetSearch("golang -RT since:2015-02-18 until:2015-02-26", v) | |
| // Iterate tweet if any | |
| for _ , tweet := range searchResult.Statuses { | |
| // Prepare array for tweet text and date | |
| record := []string{tweet.Text, tweet.CreatedAt} | |
| // Write to file | |
| err := writer.Write(record) | |
| // Is there error? | |
| if err != nil { | |
| fmt.Println("Error:", err) | |
| return | |
| } | |
| // Always set max_id param to latest tweet id | |
| v.Set("max_id", tweet.IdStr) | |
| } | |
| // Check if data from given range has been fetched | |
| if LastSavedMaxId == v.Get("max_id") { | |
| return | |
| } | |
| // Save last max_id | |
| LastSavedMaxId = v.Get("max_id") | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment