/
tjps.go
180 lines (161 loc) · 3.55 KB
/
tjps.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package tjps
import (
"encoding/json"
"fmt"
"github.com/mrjones/oauth"
"os"
"regexp"
"sort"
"strconv"
"time"
)
const (
TWITTER_TIME_FORMAT = time.RubyDate
TWITTER_RATE_WINDOW = 900
TWITTER_RATE_AMOUNT = 180
TWITTER_REQUEST_WAIT = TWITTER_RATE_WINDOW / TWITTER_RATE_AMOUNT
)
var songRegexp = regexp.MustCompile(`^\s*(.+) - (.+?)\s+\[\d+:\d+\]\s*$`)
type searchResult struct {
Statuses []tweet `json:"statuses"`
}
type tweet struct {
Id int `json:"id"`
CreatedAt string `json:"created_at"`
Text string `json:"text"`
User tweetUser `json:"user"`
}
type tweetUser struct {
ScreenName string `json:"screen_name"`
}
type Song struct {
Title, Artist string
}
type Play struct {
Song Song
At time.Time
}
type PlayCount struct {
Song Song
Count int
}
type PlayCountSorter struct {
PlayCounts []PlayCount
}
func GetPlays(c *oauth.Consumer, token *oauth.AccessToken,
from, until time.Time) (plays []Play,
err error) {
maxIdStr := ""
maxId := 0
untilStr := until.Format("2006-01-02")
lastCall := int64(0)
searching := true
for searching {
time.Sleep(time.Duration((TWITTER_REQUEST_WAIT-
(time.Now().Unix()-lastCall))+1) * time.Second)
fmt.Fprintf(os.Stderr, "Fetching tweets up to id %s\n", maxIdStr)
params := map[string]string{
"q": "from:triplejplays",
"result_type": "recent",
"count": "100",
}
if maxId != 0 {
params["max_id"] = maxIdStr
} else {
params["until"] = untilStr
}
response, err := c.Get(
"https://api.twitter.com/1.1/search/tweets.json", params, token)
if err != nil {
return plays, err
}
lastCall = time.Now().Unix()
res := searchResult{}
decoder := json.NewDecoder(response.Body)
err = decoder.Decode(&res)
if err != nil {
response.Body.Close()
return plays, err
}
if len(res.Statuses) == 0 {
break
}
lastStatusAt := ""
for _, t := range res.Statuses {
lastStatusAt = t.CreatedAt
if maxId == 0 || maxId > t.Id-1 {
maxId = t.Id
maxIdStr = strconv.Itoa(t.Id - 1)
}
if t.User.ScreenName != "triplejplays" {
continue
}
p, err := TweetToPlay(t)
if err != nil {
continue
}
if p.At.Before(from) {
searching = false
break
}
plays = append(plays, p)
}
fmt.Fprintf(os.Stderr, "Last tweet fetched dated %s\n", lastStatusAt)
response.Body.Close()
}
return
}
func ParseSong(s string) (song Song, err error) {
matches := songRegexp.FindStringSubmatch(s)
if matches == nil {
err = fmt.Errorf("Could not extract song from %s", s)
return
}
song.Artist = matches[1]
song.Title = matches[2]
return
}
func TweetToPlay(t tweet) (play Play, err error) {
s, err := ParseSong(t.Text)
if err != nil {
return
}
play.Song = s
at, err := time.Parse(TWITTER_TIME_FORMAT, t.CreatedAt)
if err != nil {
return
}
play.At = at
return
}
func PlaysByCount(plays []Play) (pc []PlayCount) {
titleMap := map[string]*PlayCount{}
for _, p := range plays {
c := titleMap[p.Song.Title]
if c == nil {
c = &PlayCount{
Song: p.Song,
}
titleMap[p.Song.Title] = c
}
c.Count++
}
for _, c := range titleMap {
pc = append(pc, *c)
}
return
}
func SortPlayCounts(playCounts []PlayCount) []PlayCount {
sorter := PlayCountSorter{playCounts}
sort.Sort(sort.Reverse(&sorter))
return sorter.PlayCounts
}
func (p *PlayCountSorter) Len() int {
return len(p.PlayCounts)
}
func (p *PlayCountSorter) Swap(i, j int) {
p.PlayCounts[i], p.PlayCounts[j] = p.PlayCounts[j], p.PlayCounts[i]
}
func (p *PlayCountSorter) Less(i, j int) bool {
return p.PlayCounts[i].Count < p.PlayCounts[j].Count
}