-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
episodes.go
223 lines (179 loc) · 5.33 KB
/
episodes.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
package pocketcasts
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
)
// GetPodcastEpisodes returns a list of episodes for a given podcast.
func (acon *AuthedConnection) GetPodcastEpisodes(UUID PodcastUUID) (*PodcastEpisodes, error) {
req, err := http.NewRequest("GET", "https://cache.pocketcasts.com/podcast/full/"+string(UUID)+"/0/2/1000", nil)
if err != nil {
return nil, err
}
// Fetch Request
resp, err := acon.Client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("request error: %s", resp.Status)
}
dec := json.NewDecoder(resp.Body)
out := &PodcastEpisodes{}
err = dec.Decode(out)
if err != nil {
return nil, err
}
return out, nil
}
type PodcastEpisodeUUID string
type PodcastEpisodes struct {
EpisodeFrequency string `json:"episode_frequency"`
EstimatedNextEpisodeAt time.Time `json:"estimated_next_episode_at"`
HasSeasons bool `json:"has_seasons"`
SeasonCount int `json:"season_count"`
EpisodeCount int `json:"episode_count"`
HasMoreEpisodes bool `json:"has_more_episodes"`
Podcast struct {
URL string `json:"url"`
Title string `json:"title"`
Author string `json:"author"`
Description string `json:"description"`
Category string `json:"category"`
Audio bool `json:"audio"`
ShowType string `json:"show_type"`
UUID PodcastUUID `json:"uuid"`
Episodes []struct {
Title string `json:"title"`
URL string `json:"url"`
FileType string `json:"file_type"`
FileSize int `json:"file_size"`
Duration int `json:"duration"`
Published time.Time `json:"published"`
Type string `json:"type"`
Season int `json:"season,omitempty"`
Number int `json:"number,omitempty"`
UUID PodcastEpisodeUUID `json:"uuid"`
} `json:"episodes"`
} `json:"podcast"`
}
// GetPodcastEpisodeStatuses returns a list of episode statuses for a given podcast.
func (acon *AuthedConnection) GetPodcastEpisodeStatuses(UUID PodcastUUID) (*PodcastEpisodeStatuses, error) {
type reqPodcastEpisodeStatuses struct {
UUID PodcastUUID `json:"uuid"`
}
sReq := reqPodcastEpisodeStatuses{
UUID: UUID,
}
reqJSON, err := json.Marshal(sReq)
if err != nil {
return nil, err
}
body := bytes.NewBuffer(reqJSON)
req, err := http.NewRequest("POST", "https://api.pocketcasts.com/user/podcast/episodes", body)
if err != nil {
return nil, err
}
// Fetch Request
resp, err := acon.Client.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("request error: %s", resp.Status)
}
dec := json.NewDecoder(resp.Body)
out := &PodcastEpisodeStatuses{}
err = dec.Decode(out)
if err != nil {
return nil, err
}
return out, nil
}
type EpisodePlayingStatus int
const (
StatusUnplayed EpisodePlayingStatus = 1
StatusStarted EpisodePlayingStatus = 2
StatusFinished EpisodePlayingStatus = 3
)
type PodcastEpisodeStatuses struct {
Episodes []struct {
UUID PodcastEpisodeUUID `json:"uuid"`
PlayingStatus EpisodePlayingStatus `json:"playingStatus"`
PlayedUpTo int `json:"playedUpTo"`
IsDeleted bool `json:"isDeleted"`
Starred bool `json:"starred"`
Duration int `json:"duration"`
} `json:"episodes"`
}
// UpdateEpisodeStatus updates the status of a given episode.
func (acon *AuthedConnection) UpdateEpisodeStatus(episodeUUID PodcastEpisodeUUID, podcastUUID PodcastUUID, status EpisodePlayingStatus) error {
type reqStatusUpdate struct {
UUID PodcastEpisodeUUID `json:"uuid"`
Podcast PodcastUUID `json:"podcast"`
Status EpisodePlayingStatus `json:"status"`
}
statusReq := reqStatusUpdate{
UUID: episodeUUID,
Podcast: podcastUUID,
Status: status,
}
reqJSON, err := json.Marshal(statusReq)
if err != nil {
return err
}
body := bytes.NewBuffer(reqJSON)
req, err := http.NewRequest("POST", "https://api.pocketcasts.com/sync/update_episode", body)
if err != nil {
return err
}
// Fetch Request
resp, err := acon.Client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("request error: %s", resp.Status)
}
return nil
}
// UpdateEpisodeArchive updates the archive aka "deleted" status of a given episode.
func (acon *AuthedConnection) UpdateEpisodeArchive(episodeUUID PodcastEpisodeUUID, podcastUUID PodcastUUID, archive bool) error {
type reqArchiveUpdatePodcast struct {
UUID PodcastEpisodeUUID `json:"uuid"`
Podcast PodcastUUID `json:"podcast"`
}
type reqArchiveUpdate struct {
Episodes []reqArchiveUpdatePodcast `json:"episodes"`
Archive bool `json:"archive"`
}
archiveReq := reqArchiveUpdate{
Episodes: []reqArchiveUpdatePodcast{
{episodeUUID, podcastUUID},
},
Archive: archive,
}
reqJSON, err := json.Marshal(archiveReq)
if err != nil {
return err
}
body := bytes.NewBuffer(reqJSON)
req, err := http.NewRequest("POST", "https://api.pocketcasts.com/sync/update_episodes_archive", body)
if err != nil {
return err
}
// Fetch Request
resp, err := acon.Client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("request error: %s", resp.Status)
}
return nil
}