-
Notifications
You must be signed in to change notification settings - Fork 2
/
tweet.go
69 lines (61 loc) · 2.16 KB
/
tweet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"fmt"
"strconv"
"time"
"github.com/ChimeraCoder/anaconda"
)
const createdAtFormat = "20060102"
// Tweet model
type Tweet struct {
CreatedAt int `json:"created_at"`
CreatedAtFull string `json:"created_at_full"`
FavoriteCount int `json:"favorite_count"`
RetweetCount int `json:"retweet_count"`
Text string `json:"text"`
StatusID string `json:"status_id"`
UserName string `json:"user_name"`
InReplyToScreenName string `json:"in_reply_to_screen_name"`
Hashtags []string `json:"hashtags"`
Lang string `json:"lang"`
TweetClass string `json:"tweet_class"`
}
// TweetFromAnacondaCrawler is a function that transform a tweet from anaconda.Tweet to the Tweet model
func TweetFromAnacondaCrawler(anacondaTweet anaconda.Tweet) Tweet {
//////////////////////
///// convert anaconda.Tweet date
//////////////////////
var date int
anacondaTweetCreatedAtlayout := "Mon Jan 02 15:04:05 -0700 2006"
t, err := time.Parse(anacondaTweetCreatedAtlayout, anacondaTweet.CreatedAt)
if err != nil {
fmt.Println(err)
date = -1
}
date, _ = strconv.Atoi(t.Format(createdAtFormat))
var hashtags []string
for _, entityTag := range anacondaTweet.Entities.Hashtags {
hashtags = append(hashtags, entityTag.Text)
}
// create the tweet
return Tweet{
CreatedAt: date,
CreatedAtFull: anacondaTweet.CreatedAt,
FavoriteCount: anacondaTweet.FavoriteCount,
RetweetCount: anacondaTweet.RetweetCount,
Text: anacondaTweet.FullText,
StatusID: anacondaTweet.IdStr,
InReplyToScreenName: anacondaTweet.InReplyToScreenName,
Hashtags: hashtags,
UserName: anacondaTweet.User.ScreenName,
Lang: anacondaTweet.Lang,
}
}
// TweetsFromAnacondaCrawler is a function that transform a list of tweets from anaconda.Tweet to the Tweet model
func TweetsFromAnacondaCrawler(anacondaTweets []anaconda.Tweet) []Tweet {
var tweets []Tweet
for _, anacondaTweet := range anacondaTweets {
tweets = append(tweets, TweetFromAnacondaCrawler(anacondaTweet))
}
return tweets
}