-
Notifications
You must be signed in to change notification settings - Fork 0
/
podcasts.go
184 lines (154 loc) · 4.6 KB
/
podcasts.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
package admin
import (
"encoding/xml"
"fmt"
"log"
"net/http"
"net/http/httputil"
"strconv"
"github.com/gorilla/mux"
"github.com/gorilla/schema"
"github.com/podcreep/server/cron"
"github.com/podcreep/server/rss"
"github.com/podcreep/server/store"
"github.com/podcreep/server/util"
)
var (
// http.Client we'll use to make HTTP requests.
httpClient = &http.Client{}
)
func handlePodcastsList(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
log.Printf("loading podcasts...\n")
podcasts, err := store.LoadPodcasts(ctx)
if err != nil {
return err
}
return render(w, "podcast/list.html", map[string]interface{}{
"Podcasts": podcasts,
})
}
func handlePodcastsAdd(w http.ResponseWriter, r *http.Request) error {
if r.Method == "GET" {
return render(w, "podcast/add.html", nil)
}
ctx := r.Context()
// It's a POST, so first, grab the URL of the RSS feed.
r.ParseForm()
url := r.Form.Get("url")
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return fmt.Errorf("error creating request: %sL %v", url, err)
}
log.Printf("Fetching RSS URL: %s\n", url)
req.Header["User-Agent"] = []string{util.GetUserAgent()}
// Fetch the RSS feed via a HTTP request.
resp, err := httpClient.Do(req)
if err != nil {
return fmt.Errorf("error fetching URL: %s: %v", url, err)
}
log.Printf("Fetched %d bytes, status %d %s, type %s\n", resp.ContentLength, resp.StatusCode, resp.Status, resp.Header.Get("Content-Type"))
if resp.StatusCode != 200 {
reqDump, _ := httputil.DumpRequest(req, true)
respDump, _ := httputil.DumpResponse(resp, true)
return fmt.Errorf("error fetching URL: %s status=%d\n%s\n\n%s", url, resp.StatusCode, string(reqDump), string(respDump))
}
// Unmarshal the RSS feed into an object we can query.
var feed rss.Feed
if err := xml.NewDecoder(resp.Body).Decode(&feed); err != nil {
return fmt.Errorf("error unmarshalling response: %w", err)
}
podcast := store.Podcast{
Title: feed.Channel.Title,
Description: feed.Channel.Description,
ImageURL: feed.Channel.Image.URL,
FeedURL: feed.Channel.Link.Href,
}
id, err := store.SavePodcast(ctx, &podcast)
if err != nil {
return fmt.Errorf("error saving podcast: %w", err)
}
http.Redirect(w, r, fmt.Sprintf("/admin/podcasts/%d", id), http.StatusFound)
return nil
}
func handlePodcastsEditGet(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
vars := mux.Vars(r)
podcastID, err := strconv.ParseInt(vars["id"], 10, 0)
if err != nil {
return httpError(err.Error(), http.StatusBadRequest)
}
podcast, err := store.LoadPodcast(ctx, podcastID)
if err != nil {
return httpError(err.Error(), http.StatusNotFound)
}
episodes, err := store.LoadEpisodes(ctx, podcastID, 25)
if err != nil {
return err
}
return render(w, "podcast/edit.html", map[string]interface{}{
"Podcast": podcast,
"Episodes": episodes,
})
}
func handlePodcastsEditPost(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
if err := r.ParseForm(); err != nil {
return httpError(fmt.Sprintf("error parsing form: %v", err), http.StatusBadRequest)
}
var podcast *store.Podcast
sid := r.Form.Get("id")
if sid != "" {
// TODO: load podcast
} else {
podcast = &store.Podcast{}
}
if err := schema.NewDecoder().Decode(podcast, r.PostForm); err != nil {
return httpError(fmt.Sprintf("error decoding form: %v", err), http.StatusBadRequest)
}
log.Printf("Saving: %v\n", podcast)
id, err := store.SavePodcast(ctx, podcast)
if err != nil {
return err
}
podcast.ID = id
// TODO: fetch episodes
return render(w, "podcast/edit.html", map[string]interface{}{
"Podcast": podcast,
})
}
func handlePodcastsDelete(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
vars := mux.Vars(r)
podcastID, err := strconv.ParseInt(vars["id"], 10, 0)
if err != nil {
return httpError(err.Error(), http.StatusBadRequest)
}
podcast, err := store.LoadPodcast(ctx, podcastID)
if err != nil {
return httpError(err.Error(), http.StatusNotFound)
}
log.Printf("Deleting: %v\n", podcast)
return store.DeletePodcast(ctx, podcast)
}
func handlePodcastsRefreshPost(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
vars := mux.Vars(r)
podcastID, err := strconv.ParseInt(vars["id"], 10, 0)
if err != nil {
return httpError(err.Error(), http.StatusBadRequest)
}
podcast, err := store.LoadPodcast(ctx, podcastID)
if err != nil {
return httpError(err.Error(), http.StatusNotFound)
}
flags := rss.ForceUpdate
if r.URL.Query().Get("iconOnly") == "1" {
flags |= rss.IconOnly
}
_, err = cron.UpdatePodcast(ctx, podcast, flags)
if err != nil {
return err
}
return nil
}