This repository has been archived by the owner on Nov 16, 2021. It is now read-only.
/
watchhistory.go
212 lines (172 loc) · 5.19 KB
/
watchhistory.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package google
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"runtime"
"strconv"
"sync"
"time"
"github.com/pylls/datatrack/database"
"github.com/pylls/datatrack/model"
)
// WFromTakeout parses a history file (JSON) as inside a Google Takeout.
func WFromTakeout(historyFile io.Reader) (err error) {
var watchhistory WatchHistory
jsoncontent, err := ioutil.ReadAll(historyFile)
if err != nil {
return err
}
err = json.Unmarshal(jsoncontent, &watchhistory.Videos)
if err != nil {
return
}
return insertWatchHistory(watchhistory)
}
// Insert the watch history into the datatrack.
func insertWatchHistory(history WatchHistory) (err error) {
wg := new(sync.WaitGroup)
logChan := make(chan Video, len(history.Videos))
parsedChan := make(chan ParsedVideo, len(history.Videos))
// start workers, one per CPU
for i := 0; i < runtime.NumCPU(); i++ {
wg.Add(1)
go watchWorker(logChan, parsedChan, wg)
}
// feed workers
for _, video := range history.Videos {
logChan <- video
}
disclosures := make([]model.Disclosure, 0, 1*len(history.Videos))
disclosed := make([]model.Disclosed, 0, 1*len(history.Videos))
attributes := make([]model.Attribute, 0, 13*len(history.Videos))
// wait for workers to finish
close(logChan)
wg.Wait()
// assemble all parsed locations
close(parsedChan)
for p := range parsedChan {
disclosures = append(disclosures, p.Disclosure)
disclosed = append(disclosed, p.Disclosed)
attributes = append(attributes, p.Attributes...)
}
wg = new(sync.WaitGroup)
errChan := make(chan error, 4)
wg.Add(3)
// send into database
go database.AddAttributes(attributes, wg, errChan)
go database.AddDisclosures(disclosures, wg, errChan)
go database.AddDiscloseds(disclosed, wg, errChan)
wg.Wait()
close(errChan)
for err := range errChan {
return err
}
return nil
}
func watchWorker(locChan chan Video, parsedChan chan ParsedVideo, wg *sync.WaitGroup) {
defer wg.Done()
for video := range locChan {
parsed, err := insertVideo(video)
if err != nil {
panic(err)
}
parsedChan <- parsed
}
}
// Converts the timestamp of when the video was watched and returns a parsed video.
func insertVideo(video Video) (parsed ParsedVideo, err error) {
time, err := time.Parse(time.RFC3339Nano, video.Snippet.PublishedAt)
if err != nil {
panic(err)
}
timeEpochMs := time.Unix() * 1000
parsed, err = createParsedVideo(timeEpochMs, video)
if err != nil {
return
}
return
}
// Creates a video according to the data model by adding attributes and assigning disclosures.
func createParsedVideo(timestamp int64, video Video) (parsedVideo ParsedVideo, err error) {
parsedVideo.Disclosure, err = model.MakeDisclosure(database.Self, org.ID,
strconv.Itoa(int(timestamp)), "", "", "", "")
if err != nil {
return
}
parsedVideo.Attributes = make([]model.Attribute, 13)
parsedVideo.Attributes[0], err = model.MakeAttribute("Video id", "camera",
fmt.Sprintf("%s", video.ContentDetails.VideoID))
if err != nil {
return
}
parsedVideo.Attributes[1], err = model.MakeAttribute("Etag", "tag",
fmt.Sprintf("%s", video.ETag))
if err != nil {
return
}
parsedVideo.Attributes[2], err = model.MakeAttribute("Id", "barcode",
fmt.Sprintf("%s", video.ID))
if err != nil {
return
}
parsedVideo.Attributes[3], err = model.MakeAttribute("Kind", "filter",
fmt.Sprintf("%s", video.Kind))
if err != nil {
return
}
parsedVideo.Attributes[4], err = model.MakeAttribute("Channel id", "barcode",
fmt.Sprintf("%s", video.Snippet.ChannelID))
if err != nil {
return
}
parsedVideo.Attributes[5], err = model.MakeAttribute("Channel Title", "info",
fmt.Sprintf("%s", video.Snippet.ChannelTitle))
if err != nil {
return
}
parsedVideo.Attributes[6], err = model.MakeAttribute("Description", "info",
fmt.Sprintf("%s", video.Snippet.Description))
if err != nil {
return
}
parsedVideo.Attributes[7], err = model.MakeAttribute("Playlist id", "barcode",
fmt.Sprintf("%s", video.Snippet.PlaylistID))
if err != nil {
return
}
parsedVideo.Attributes[8], err = model.MakeAttribute("Position", "sort-numeric-asc",
fmt.Sprintf("%d", video.Snippet.Position))
if err != nil {
return
}
parsedVideo.Attributes[9], err = model.MakeAttribute("Published at", "clock-o",
fmt.Sprintf("%s", video.Snippet.PublishedAt))
if err != nil {
return
}
parsedVideo.Attributes[10], err = model.MakeAttribute("Kind", "filter",
fmt.Sprintf("%s", video.Snippet.ResourceID.Kind))
if err != nil {
return
}
parsedVideo.Attributes[11], err = model.MakeAttribute("Title", "info",
fmt.Sprintf("%s", video.Snippet.Title))
if err != nil {
return
}
parsedVideo.Attributes[12], err = model.MakeAttribute("Status", "lock",
fmt.Sprintf("%s", video.Status))
if err != nil {
return
}
parsedVideo.Disclosed = model.Disclosed{
Disclosure: parsedVideo.Disclosure.ID,
Attribute: []string{parsedVideo.Attributes[0].ID, parsedVideo.Attributes[1].ID, parsedVideo.Attributes[2].ID,
parsedVideo.Attributes[3].ID, parsedVideo.Attributes[4].ID, parsedVideo.Attributes[5].ID,
parsedVideo.Attributes[6].ID, parsedVideo.Attributes[7].ID, parsedVideo.Attributes[8].ID,
parsedVideo.Attributes[9].ID, parsedVideo.Attributes[10].ID, parsedVideo.Attributes[11].ID,
parsedVideo.Attributes[12].ID}}
return
}