-
Notifications
You must be signed in to change notification settings - Fork 4
/
Article.go
158 lines (140 loc) · 3.09 KB
/
Article.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
package member
import (
"bytes"
"encoding/json"
"net/http"
"strconv"
"strings"
)
func (e Encoding) Year() int {
if v, ok := e.Article.year(); ok {
v, _ := strconv.Atoi(v)
return v
}
return 0
}
func (d DataArticle) year() (string, bool) {
for _, meta := range d.Metas {
if meta.Key == "year" {
return meta.Value, true
}
}
return "", false
}
type DataArticle struct {
Assets []*ArticleAsset
CanonicalTitle string `json:"canonical_title"`
ID int
Metas []struct {
Key string
Value string
}
}
type Encoding struct {
Article *DataArticle
Play *AssetPlay
}
func (e Encoding) Marshal() ([]byte, error) {
return json.MarshalIndent(e, "", " ")
}
func (d DataArticle) Film() (*ArticleAsset, bool) {
for _, asset := range d.Assets {
if asset.LinkedType == "film" {
return asset, true
}
}
return nil, false
}
func (a ArticleSlug) Article() (*DataArticle, error) {
body, err := func() ([]byte, error) {
var s struct {
Query string `json:"query"`
Variables struct {
ArticleUrlSlug ArticleSlug `json:"articleUrlSlug"`
} `json:"variables"`
}
s.Variables.ArticleUrlSlug = a
s.Query = query_article
return json.Marshal(s)
}()
if err != nil {
return nil, err
}
res, err := http.Post(
"https://api.audienceplayer.com/graphql/2/user",
"application/json", bytes.NewReader(body),
)
if err != nil {
return nil, err
}
defer res.Body.Close()
var s struct {
Data struct {
Article DataArticle
}
}
err = json.NewDecoder(res.Body).Decode(&s)
if err != nil {
return nil, err
}
for _, asset := range s.Data.Article.Assets {
asset.article = &s.Data.Article
}
return &s.Data.Article, nil
}
const query_article = `
query($articleUrlSlug: String) {
Article(full_url_slug: $articleUrlSlug) {
... on Article {
assets {
... on Asset {
id
linked_type
}
}
canonical_title
id
metas(output: html) {
... on ArticleMeta {
key
value
}
}
}
}
}
`
type ArticleAsset struct {
ID int
LinkedType string `json:"linked_type"`
article *DataArticle
}
// https://www.cinemember.nl/nl/films/american-hustle
func (a *ArticleSlug) Set(s string) error {
s = strings.TrimPrefix(s, "https://")
s = strings.TrimPrefix(s, "www.")
s = strings.TrimPrefix(s, "cinemember.nl")
s = strings.TrimPrefix(s, "/nl")
s = strings.TrimPrefix(s, "/")
*a = ArticleSlug(s)
return nil
}
func (a ArticleSlug) String() string {
return string(a)
}
type ArticleSlug string
func (Encoding) Show() string {
return ""
}
func (Encoding) Season() int {
return 0
}
func (Encoding) Episode() int {
return 0
}
func (e Encoding) Title() string {
return e.Article.CanonicalTitle
}
func (e *Encoding) Unmarshal(text []byte) error {
return json.Unmarshal(text, e)
}