append metadata when downloading episodes

This commit is contained in:
2026-02-13 14:23:11 +03:00
parent 39d8286eb9
commit 7df08812bd
4 changed files with 84 additions and 5 deletions

26
main.go
View File

@@ -23,6 +23,7 @@ type Podcast struct {
Feed string `json:"feed" db:"feed"`
Language string `json:"language" db:"language"`
Link string `json:"link" db:"link"`
Image string `json:"image" db:"image"`
CreatedAt time.Time `json:"createdAt" db:"created_at"`
}
@@ -33,6 +34,7 @@ type Episode struct {
Guid string `json:"guid" db:"guid"`
Url string `json:"url" db:"url"`
PodcastId int64 `json:"podcastId" db:"podcast_id"`
Number int `json:"number" db:"number"`
CreatedAt time.Time `json:"createdAt" db:"created_at"`
}
@@ -72,6 +74,7 @@ type RSSFeed struct {
Description string `xml:"description"`
Language string `xml:"language"`
Link string `xml:"link"`
Image string `xml:"image>url"`
Items []RSSFeedItem `xml:"item"`
} `xml:"channel"`
}
@@ -126,12 +129,13 @@ func getPodcasts(db *sqlx.DB) ([]*Podcast, error) {
}
func createPodcast(db *sqlx.DB, feed RSSFeed, feedUrl string) error {
_, err := db.NamedExec("INSERT INTO podcasts (name, description, feed, language, link) VALUES (:name, :description, :feed, :language, :link)", map[string]any{
_, err := db.NamedExec("INSERT INTO podcasts (name, description, feed, language, link, image) VALUES (:name, :description, :feed, :language, :link, :image)", map[string]any{
"name": feed.Channel.Title,
"description": feed.Channel.Description,
"feed": feedUrl,
"language": feed.Channel.Language,
"link": feed.Channel.Link,
"image": feed.Channel.Image,
})
return err
}
@@ -150,11 +154,12 @@ func getEpisodeByGuid(db *sqlx.DB, guid string) (*Episode, error) {
return episode, nil
}
func addEpisode(db *sqlx.DB, episode RSSFeedItem, podcastId int64) error {
_, err := db.NamedExec("INSERT INTO episodes (title, pubdate, guid, url, podcast_id) VALUES (:title, :pubdate, :guid, :url, :podcast_id)", map[string]any{
func addEpisode(db *sqlx.DB, episode RSSFeedItem, episodeNumber int, podcastId int64) error {
_, err := db.NamedExec("INSERT INTO episodes (title, pubdate, guid, url, number, podcast_id) VALUES (:title, :pubdate, :guid, :url, :number, :podcast_id)", map[string]any{
"title": episode.Title,
"pubdate": episode.PubDate.Time,
"url": episode.Enclosure.URL,
"number": episodeNumber,
"guid": episode.Guid,
"podcast_id": podcastId,
})
@@ -220,6 +225,7 @@ func main() {
feed varchar not null,
language varchar not null,
link varchar not null,
image varchar not null,
created_at datetime default current_timestamp
);
@@ -229,6 +235,7 @@ func main() {
pubdate datetime not null,
guid varchar not null,
url varchar not null,
number integer not null,
created_at datetime default current_timestamp,
podcast_id integer not null,
FOREIGN KEY (podcast_id) REFERENCES podcasts(id)
@@ -258,12 +265,16 @@ func main() {
continue
}
if err := addEpisode(db, newestEpisode, podcast.ID); err != nil {
episodeNumber := len(feed.Channel.Items) - 1
if err := addEpisode(db, newestEpisode, episodeNumber, podcast.ID); err != nil {
log.Printf("failed to add new episode [%s]: %v\n", podcast.Name, err)
continue
}
f, err := os.OpenFile(path.Join(podcastsDirPath, podcast.Name, newestEpisode.Title+".mp3"), os.O_CREATE|os.O_RDWR, 0644)
episodeFilePath := path.Join(podcastsDirPath, podcast.Name, fmt.Sprintf("Episode %d.mp3", episodeNumber))
f, err := os.OpenFile(episodeFilePath, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
log.Printf("failed to create file for newest episode: %v\n", err)
continue
@@ -274,6 +285,11 @@ func main() {
continue
}
if err := appendPodcastMetadata(episodeFilePath, newestEpisode, episodeNumber, *podcast); err != nil {
log.Printf("failed to append episode metadata: %v\n", err)
continue
}
f.Close()
}
}