This repository has been archived by the owner on Nov 20, 2017. It is now read-only.
/
api.go
375 lines (299 loc) · 8.74 KB
/
api.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package main
import (
"bytes"
"context"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/cenkalti/backoff"
"github.com/pkg/errors"
"golang.org/x/net/context/ctxhttp"
)
const (
baseURL = "https://services.zinio.com/newsstandServices/"
httpTimeout = 5 * time.Second
requestTimeout = time.Minute
deviceID = "A6B50079-BE65-44D6-A961-8A184AA81077"
installationUUID = "84A8E36D-DF7F-4D7E-9824-F793AFB93207"
)
var (
decryptionKey = []byte("8D}[" + deviceID[0:4] + "i)|z" + installationUUID[0:4])
)
// MagazineID is a unique identifier of a magazine.
type MagazineID string
// IssueID is a unique identifier of an issue.
type IssueID string
// Session contains session data for single authenticated user.
type Session struct {
login string
password string
profileID string
}
// Magazine represents single magazine with all its issues.
type Magazine struct {
ID MagazineID
Title string
Issues []IssueMetadata
}
// IssueMetadata contains ID and title of an issue.
type IssueMetadata struct {
ID IssueID
Title string
}
// Issue represents single magazine issue.
type Issue struct {
ID IssueID
Title string
Password string
PageCount int
baseURL string
}
type parameters struct {
login string
password string
profileID string
pubID MagazineID
issueID IssueID
}
type page struct {
*bytes.Reader
}
type version struct {
Key string `xml:"key,attr"`
Value string `xml:"value,attr"`
}
type libraryIssueDataRequest struct {
PubID MagazineID `xml:"pubId"`
IssueID IssueID `xml:"issueId"`
}
type zinioServiceRequest struct {
XMLName xml.Name `xml:"zinioServiceRequest"`
RequestHeader struct {
Authorization struct {
Login string `xml:"login"`
Password string `xml:"password"`
} `xml:"authorization"`
Device struct {
ProfileID string `xml:"profileId,omitempty"`
DeviceID string `xml:"deviceId"`
DeviceName string `xml:"deviceName"`
InstallationUUID string `xml:"installationUUID"`
PlatformDescription string `xml:"platformDescription"`
} `xml:"device"`
Application struct {
ApplicationName string `xml:"applicationName"`
Versions []version `xml:"versions>version"`
} `xml:"application"`
} `xml:"requestHeader"`
LibraryIssueDataRequest *libraryIssueDataRequest `xml:"libraryIssueDataRequest,omitempty"`
}
type authenticateUserResponse struct {
ProfileID string `xml:"profileId"`
}
type library struct {
LibraryPublications []struct {
Publication struct {
PubID string `xml:"pubId"`
DisplayName string `xml:"displayName"`
} `xml:"publication"`
LibraryIssue struct {
Issue struct {
IssueID string `xml:"issueId"`
Title string `xml:"title"`
} `xml:"issue"`
} `xml:"libraryIssue"`
} `xml:"libraryPublication"`
}
type issuePackingList struct {
SingleIssue struct {
PubID string `xml:"pubId"`
IssueTitle string `xml:"issueTitle"`
IssueID string `xml:"issueId"`
HostName string `xml:"hostName"`
IssueAssetDir string `xml:"issueAssetDir"`
TrackingCode struct {
Init string `xml:"init,attr"`
Value string `xml:",chardata"`
} `xml:"trackingCode"`
NumberOfPages int `xml:"numberOfPages"`
} `xml:"singleIssue"`
}
type zinioServiceResponse struct {
XMLName xml.Name `xml:"zinioServiceResponse"`
ResponseStatus struct {
Status string `xml:"status"`
ErrorDetail struct {
Code string `xml:"code"`
Message string `xml:"message"`
} `xml:"errorDetail"`
} `xml:"responseStatus"`
AuthenticateUserResponse authenticateUserResponse `xml:"authenticateUserResponse"`
Library library `xml:"library"`
IssuePackingList issuePackingList `xml:"issuePackingList"`
}
func makeRequest(p parameters) zinioServiceRequest {
var req zinioServiceRequest
h := &req.RequestHeader
h.Authorization.Login = p.login
h.Authorization.Password = p.password
h.Device.ProfileID = p.profileID
h.Device.DeviceID = deviceID
h.Device.DeviceName = "Windows 8 Device"
h.Device.InstallationUUID = installationUUID
h.Device.PlatformDescription = "Windows 8"
h.Application.ApplicationName = "ZinioWin8"
h.Application.Versions = []version{{"security", "1.0"}}
if p.pubID != "" && p.issueID != "" {
req.LibraryIssueDataRequest = &libraryIssueDataRequest{
PubID: p.pubID,
IssueID: p.issueID,
}
}
return req
}
func retry(ctx context.Context, query string, p parameters) (*zinioServiceResponse, error) {
ctx, cancel := context.WithTimeout(ctx, requestTimeout)
defer cancel()
var resp *zinioServiceResponse
var err error
op := func() error {
resp, err = post(ctx, query, p)
return err
}
b := backoff.WithContext(backoff.NewConstantBackOff(time.Second), ctx)
if err := backoff.Retry(op, b); err != nil {
return nil, err
}
return resp, nil
}
func post(ctx context.Context, query string, p parameters) (*zinioServiceResponse, error) {
ctx, cancel := context.WithTimeout(ctx, httpTimeout)
defer cancel()
req := makeRequest(p)
b, err := xml.Marshal(req)
if err != nil {
return nil, err
}
r := bytes.NewReader(b)
resp, err := ctxhttp.Post(ctx, http.DefaultClient, baseURL+query, "text/xml", r)
if err != nil {
return nil, err
}
defer resp.Body.Close()
b, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var res zinioServiceResponse
if err = xml.Unmarshal(b, &res); err != nil {
return nil, err
}
message := res.ResponseStatus.ErrorDetail.Message
if message != "" {
return nil, errors.New(message)
}
return &res, nil
}
func decryptPdfPassword(iv64, ciphertext64 string) (string, error) {
iv, err := base64.StdEncoding.DecodeString(iv64)
if err != nil {
return "", err
}
ciphertext, err := base64.StdEncoding.DecodeString(ciphertext64)
if err != nil {
return "", err
}
block, err := aes.NewCipher(decryptionKey)
if err != nil {
return "", err
}
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(ciphertext, ciphertext)
return string(ciphertext[:32]), nil
}
// Login connects to Zinio API server and authenticates user using given email and password.
func Login(ctx context.Context, email, password string) (*Session, error) {
p := parameters{
login: email,
password: password,
}
resp, err := retry(ctx, "authenticateUser", p)
if err != nil {
return nil, errors.Wrapf(err, "failed to authenticate user %s", email)
}
session := Session{
login: email,
password: password,
profileID: resp.AuthenticateUserResponse.ProfileID,
}
return &session, nil
}
// GetMagazines downloads list of all available magazines.
func (session *Session) GetMagazines(ctx context.Context) ([]Magazine, error) {
p := parameters{
login: session.login,
password: session.password,
profileID: session.profileID,
}
resp, err := retry(ctx, "libraryService", p)
if err != nil {
return nil, errors.Wrap(err, "failed to download list of all magazines")
}
magazines := make(map[string]*Magazine)
for _, publication := range resp.Library.LibraryPublications {
pubID := publication.Publication.PubID
mag, ok := magazines[pubID]
if !ok {
mag = &Magazine{ID: MagazineID(pubID), Title: publication.Publication.DisplayName}
magazines[pubID] = mag
}
mag.Issues = append(mag.Issues, IssueMetadata{
ID: IssueID(publication.LibraryIssue.Issue.IssueID),
Title: publication.LibraryIssue.Issue.Title,
})
}
var res []Magazine
for _, mag := range magazines {
res = append(res, *mag)
}
return res, nil
}
// GetIssue downloads metadata for single magazine issue.
func (session *Session) GetIssue(ctx context.Context, magazine MagazineID, issue IssueID) (*Issue, error) {
p := parameters{
login: session.login,
password: session.password,
profileID: session.profileID,
pubID: magazine,
issueID: issue,
}
resp, err := retry(ctx, "issueData", p)
if err != nil {
return nil, errors.Wrapf(err, "failed to download metadata for magazine %s, issue %s", magazine, issue)
}
res := resp.IssuePackingList.SingleIssue
password, err := decryptPdfPassword(res.TrackingCode.Init, res.TrackingCode.Value)
if err != nil {
return nil, errors.Wrapf(err, "failed to decrypt PDF password for magazine %s, issue %s", magazine, issue)
}
isssue := Issue{
ID: IssueID(res.IssueID),
Title: res.IssueTitle,
Password: password,
PageCount: res.NumberOfPages,
baseURL: fmt.Sprintf("https://%s%s", res.HostName, res.IssueAssetDir),
}
return &isssue, nil
}
// GetURL returns download URL for a page.
func (issue Issue) GetURL(page int) (string, error) {
if page < 0 || page >= issue.PageCount {
return "", errors.Errorf("invalid page %d: must be between 0 and %d", page, issue.PageCount)
}
return fmt.Sprintf("%spage%d.pdf", issue.baseURL, page), nil
}