rss-dl/main.go

99 lines
2.5 KiB
Go

package main
import (
"encoding/json"
"fmt"
"path/filepath"
"strings"
. "./fileio"
. "./helpers"
. "./structs"
"github.com/mmcdole/gofeed"
)
func main() {
GetConfig()
fp := gofeed.NewParser()
LogInfo("Downloading " + Config.FeedURL)
feed, _ := fp.ParseURL(Config.FeedURL)
outputDir := Config.OutputPath + "/" + ToCleanString(feed.Title)
InitOutputDirectory(outputDir)
feedInfoPath := outputDir + "/feed_details.json"
LogInfo("Writing feed details as JSON to " + feedInfoPath)
WriteToFile(feedInfoPath, GrabFeedDetailsJSON(feed))
feedImagePath := outputDir + "/image" + filepath.Ext(feed.Image.URL)
DownloadFile(feedImagePath, feed.Image.URL)
for _, item := range feed.Items {
itemOutputFilename := ToCleanString(
item.PublishedParsed.Format("20060102") + "_" + item.Title)
itemOutputDir := outputDir + "/" + itemOutputFilename
if CheckIfExists(itemOutputDir) {
fmt.Println("Item '" + item.Title + "' already downloaded, skipping")
continue
}
LogInfo("Downloading feed item '" + item.Title + "' to " + itemOutputDir)
InitOutputDirectory(itemOutputDir)
itemDetailsPath := itemOutputDir + "/details.json"
LogInfo("Writing details to " + itemDetailsPath)
WriteToFile(
itemDetailsPath,
GrabFeedItemJSON(item))
itemImagePath := itemOutputDir + "/image" + filepath.Ext(item.Image.URL)
LogInfo("Downloading image to " + itemImagePath)
DownloadFile(
itemImagePath,
item.Image.URL)
for _, enclosure := range item.Enclosures {
filename := strings.Split(filepath.Base(enclosure.URL), "?")[0]
LogInfo("Downloading attachment '" + filename + "'")
DownloadFile(
itemOutputDir+"/"+filename,
enclosure.URL)
}
}
}
// GrabFeedDetailsJSON - Returns a feed summary json sring
func GrabFeedDetailsJSON(feed *gofeed.Feed) string {
feedParsed := &FeedDetails{
Title: feed.Title,
Description: feed.Description,
Categories: feed.Categories,
Language: feed.Language,
Link: feed.Link,
FeedLink: feed.FeedLink,
Updated: feed.Updated,
}
feedDetails, _ := json.Marshal(feedParsed)
return string(feedDetails)
}
// GrabFeedItemJSON - Returns a feed summary json sring
func GrabFeedItemJSON(item *gofeed.Item) string {
itemParsed := &ItemDetails{
Title: item.Title,
Description: item.Description,
Content: item.Content,
Link: item.Link,
Updated: item.Updated,
Published: item.Published,
GUID: item.GUID,
Categories: item.Categories,
}
itemDetails, _ := json.Marshal(itemParsed)
return string(itemDetails)
}