Skip to content

Commit

Permalink
Use coordinates instead of geo
Browse files Browse the repository at this point in the history
  • Loading branch information
akiomik committed Aug 9, 2022
1 parent ab680f2 commit b508baf
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ get-old-tweets --from 'barackobama' --top
- `favorite_count` (int)
- `reply_count` (int)
- `quote_count` (int)
- `geo` (str)
- `coordinates` (str)
- `lang` (str)
- `source` (str)

Expand Down
10 changes: 5 additions & 5 deletions export/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ func ExportCsv(f *os.File, ch <-chan []Record) <-chan struct{} {
defer close(done)

w := csv.NewWriter(f)
err := w.Write([]string{"id", "username", "created_at", "full_text", "retweet_count", "favorite_count", "reply_count", "quote_count", "geo", "lang", "source"})
err := w.Write([]string{"id", "username", "created_at", "full_text", "retweet_count", "favorite_count", "reply_count", "quote_count", "coordinates", "lang", "source"})
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
panic(err)
}

for records := range ch {
for _, record := range records {
geo := ""
if record.Geo != nil {
geo = record.Geo.String()
coordinates := ""
if record.Coordinates != nil {
coordinates = record.Coordinates.String()
}

row := []string{
Expand All @@ -50,7 +50,7 @@ func ExportCsv(f *os.File, ch <-chan []Record) <-chan struct{} {
strconv.FormatUint(record.FavoriteCount, 10),
strconv.FormatUint(record.ReplyCount, 10),
strconv.FormatUint(record.QuoteCount, 10),
geo,
coordinates,
record.Lang,
record.Source,
}
Expand Down
6 changes: 3 additions & 3 deletions export/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestExportCsvNonEmpty(t *testing.T) {
FavoriteCount: 400,
ReplyCount: 500,
QuoteCount: 600,
Geo: &json.LatLong{40.74118764, -73.9998279},
Coordinates: &json.LongLat{-73.9998279, 40.74118764},
Lang: "en",
},
}
Expand Down Expand Up @@ -126,15 +126,15 @@ func TestExportCsvNonEmpty(t *testing.T) {
return
}

expectedHeader := []string{"id", "username", "created_at", "full_text", "retweet_count", "favorite_count", "reply_count", "quote_count", "geo", "lang", "source"}
expectedHeader := []string{"id", "username", "created_at", "full_text", "retweet_count", "favorite_count", "reply_count", "quote_count", "coordinates", "lang", "source"}
if !reflect.DeepEqual(actualHeader, expectedHeader) {
t.Errorf("Expect to write %v as a header, got %v", expectedHeader, actualHeader)
return
}

expectedRecords := [][]string{
[]string{"1000", "watson1", "2020-09-06T00:01:02+00:00", "To Sherlock Holmes she is always the woman.", "3000", "4000", "5000", "6000", "", "en", ""},
[]string{"100", "watson2", "2020-09-06T00:01:02+00:00", "To Sherlock Holmes she is always the woman.", "300", "400", "500", "600", "40.74118764,-73.9998279", "en", ""},
[]string{"100", "watson2", "2020-09-06T00:01:02+00:00", "To Sherlock Holmes she is always the woman.", "300", "400", "500", "600", "-73.9998279,40.74118764", "en", ""},
[]string{"10", "watson3", "2020-09-06T00:01:02+00:00", "To Sherlock Holmes she is always the woman.", "30", "40", "50", "60", "", "en", ""},
[]string{"1", "watson4", "2020-09-06T00:01:02+00:00", "To Sherlock Holmes she is always the woman.", "3", "4", "5", "6", "", "en", ""},
}
Expand Down
10 changes: 5 additions & 5 deletions export/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Record struct {
FavoriteCount uint64
ReplyCount uint64
QuoteCount uint64
Geo *json.LatLong
Coordinates *json.LongLat
Lang string
Source string
}
Expand Down Expand Up @@ -63,9 +63,9 @@ func NewRecordsFromAdaptive(j *json.Adaptive) []Record {
t := j.GlobalObjects.Tweets[id]
u := j.GlobalObjects.Users[strconv.FormatUint(t.UserId, 10)]

var geo *json.LatLong
if t.Geo != nil {
geo = &t.Geo.Coordinates
var coordinates *json.LongLat
if t.Coordinates != nil {
coordinates = &t.Coordinates.Coordinates
}

return Record{
Expand All @@ -77,7 +77,7 @@ func NewRecordsFromAdaptive(j *json.Adaptive) []Record {
FavoriteCount: t.FavoriteCount,
ReplyCount: t.ReplyCount,
QuoteCount: t.QuoteCount,
Geo: geo,
Coordinates: coordinates,
Lang: t.Lang,
Source: t.Source,
}
Expand Down
8 changes: 4 additions & 4 deletions export/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ func TestNewRecordsFromAdaptive(t *testing.T) {
FavoriteCount: 400,
ReplyCount: 500,
QuoteCount: 600,
Geo: &json.Geo{
Coordinates: &json.Coordinates{
Type: "Point",
Coordinates: [2]float64{40.74118764, -73.9998279},
Coordinates: json.LongLat{-73.9998279, 40.74118764},
},
Lang: "en",
},
Expand Down Expand Up @@ -217,7 +217,7 @@ func TestNewRecordsFromAdaptive(t *testing.T) {
FavoriteCount: 4000,
ReplyCount: 5000,
QuoteCount: 6000,
Geo: nil,
Coordinates: nil,
Lang: "en",
},
Record{
Expand All @@ -229,7 +229,7 @@ func TestNewRecordsFromAdaptive(t *testing.T) {
FavoriteCount: 400,
ReplyCount: 500,
QuoteCount: 600,
Geo: &json.LatLong{40.74118764, -73.9998279},
Coordinates: &json.LongLat{-73.9998279, 40.74118764},
Lang: "en",
},
}
Expand Down
4 changes: 3 additions & 1 deletion twitter/json/tweet.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package json

// https://developer.twitter.com/en/docs/twitter-api/v1/data-dictionary/object-model/tweet

type Tweet struct {
Id uint64 `json:"id"`
UserId uint64 `json:"user_id"`
Expand All @@ -22,7 +24,7 @@ type Tweet struct {
FavoriteCount uint64 `json:"favorite_count"`
ReplyCount uint64 `json:"reply_count"`
QuoteCount uint64 `json:"quote_count"`
Geo *Geo `json:"geo"`
Geo *Geo `json:"geo"` // deprecated
Coordinates *Coordinates `json:"coordinates"`
Place *Place `json:"place"`
Lang string `json:"lang"`
Expand Down

0 comments on commit b508baf

Please sign in to comment.