Skip to content

Commit

Permalink
Merge pull request #11 from nlif-m/dev
Browse files Browse the repository at this point in the history
Merging dev
  • Loading branch information
nlif-m committed Jun 16, 2023
2 parents 2bc117f + ad32c09 commit 4aeed97
Show file tree
Hide file tree
Showing 16 changed files with 513 additions and 292 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ jobs:
with:
go-version: 1.19
- name: Build
run: make crossplatform
run: make
4 changes: 1 addition & 3 deletions .github/workflows/release-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ jobs:
with:
go-version: 1.19
- name: Build
run: make crossplatform
run: make
- name: Release
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
files: |
atomgen_linux_amd64
atomgen_linux_arm
atomgen_windows_amd64.exe
16 changes: 8 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ atomgen: vet test
GOOS=${GOOS} GOARCH=${GOARCH} go build ${GOFLAGS} -o "$@"

vet: fmt
go vet
go vet ./...

fmt:
go fmt
go fmt ./...

test:
go test
go test ./...

linux_amd64: vet test
GOOS=linux GOARCH=amd64 go build ${GOFLAGS} -o "${PROGRAM_NAME}_linux_amd64"

windows_amd64: vet test
GOOS=windows GOARCH=amd64 go build ${GOFLAGS} -o "${PROGRAM_NAME}_windows_amd64.exe"
# windows_amd64: vet test
# GOOS=windows GOARCH=amd64 go build ${GOFLAGS} -o "${PROGRAM_NAME}_windows_amd64.exe"

linux_arm: vet test
GOOS=linux GOARCH=arm go build ${GOFLAGS} -o "${PROGRAM_NAME}_linux_arm"
# linux_arm: vet test
# GOOS=linux GOARCH=arm go build ${GOFLAGS} -o "${PROGRAM_NAME}_linux_arm"

crossplatform: linux_amd64 linux_arm windows_amd64
# crossplatform: linux_amd64 linux_arm windows_amd64
12 changes: 7 additions & 5 deletions atom.go → atom/atom.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package atom

import (
"encoding/xml"
Expand All @@ -7,10 +7,11 @@ import (
"os"
"time"

"github.com/nlif-m/atomgen/config"
"golang.org/x/tools/blog/atom"
)

func getMimeType(filePath string) (mimeType string, err error) {
func GetMimeType(filePath string) (mimeType string, err error) {
// TODO: I am sure there something wrong, but what ?
fd, err := os.Open(filePath)
if err != nil {
Expand All @@ -19,7 +20,7 @@ func getMimeType(filePath string) (mimeType string, err error) {
defer fd.Close()

r := io.Reader(fd)
r1 := io.LimitReader(r, detectContentTypeMost)
r1 := io.LimitReader(r, config.DetectContentTypeMost)
head, err := io.ReadAll(r1)

if err != nil {
Expand All @@ -29,7 +30,8 @@ func getMimeType(filePath string) (mimeType string, err error) {
mimeType = http.DetectContentType(head)
return mimeType, nil
}
func newAtomEntry(name string, fileLocation string, mimeType string, length uint, fileModificationTime time.Time, content string) *atom.Entry {

func NewEntry(name string, fileLocation string, mimeType string, length uint, fileModificationTime time.Time, content string) *atom.Entry {
return &atom.Entry{
Title: name,
ID: fileLocation,
Expand All @@ -52,7 +54,7 @@ func newAtomEntry(name string, fileLocation string, mimeType string, length uint
Body: name}}
}

func newAtomFeed(channelTitle string, channelLink string, authorLink string, entries []*atom.Entry) *atom.Feed {
func NewFeed(channelTitle string, channelLink string, authorLink string, entries []*atom.Entry) *atom.Feed {
return &atom.Feed{
XMLName: xml.Name{},
Title: channelTitle,
Expand Down
74 changes: 34 additions & 40 deletions atomgen.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"encoding/json"
"encoding/xml"
"fmt"
"log"
Expand All @@ -14,24 +13,42 @@ import (
"sync"
"time"

aatom "github.com/nlif-m/atomgen/atom"
"github.com/nlif-m/atomgen/config"
"github.com/nlif-m/atomgen/utils"
"github.com/nlif-m/atomgen/ytdlp"

"golang.org/x/tools/blog/atom"
)

type Atomgen struct {
ytdlp Ytdlp
cfg Cfg
ytdlp ytdlp.Ytdlp
cfg config.Cfg
}

func newAtomgen(ytdlp Ytdlp, cfg Cfg) Atomgen {
func newAtomgen(ytdlp ytdlp.Ytdlp, cfg config.Cfg) Atomgen {
return Atomgen{ytdlp, cfg}
}

func (atomgen *Atomgen) fullUpdate() error {
if atomgen.cfg.VideosToDowload != 0 {
err := atomgen.DownloadVideos()
return err
}

if atomgen.cfg.WeeksToDelete != 0 {
err := atomgen.deleteOldFiles()
return err
}

return atomgen.generateAtomFeed()
}
func (atomgen *Atomgen) generateAtomFeed() error {
entries, err := atomgen.getEntries()
if err != nil {
return err
}
atomFeed := newAtomFeed(atomgen.cfg.ChannelTitle, atomgen.cfg.AuthorLink, atomgen.cfg.AuthorLink, entries)
atomFeed := aatom.NewFeed(atomgen.cfg.ChannelTitle, atomgen.cfg.AuthorLink, atomgen.cfg.AuthorLink, entries)

log.Printf("Generated %d entries for '%s'\n", len(entries), atomgen.cfg.AtomFile)
data, err := xml.MarshalIndent(atomFeed, " ", " ")
Expand Down Expand Up @@ -74,20 +91,15 @@ filesLoop:
continue filesLoop
}

mimeType, err := getMimeType(filepath.Join(atomgen.cfg.SrcFolder, file.Name()))
mimeType, err := aatom.GetMimeType(filepath.Join(atomgen.cfg.SrcFolder, file.Name()))
if err != nil {
log.Printf("ERROR: while getting Mimetype of %s%c%s\n%s", atomgen.cfg.SrcFolder, os.PathSeparator, file.Name(), err)
return nil, err
}
content := ""
infoJson, err := atomgen.getInfoJson(Name)
if err == nil {
content = infoJson.Description
}

urlEncodedName := url.PathEscape(Name)
fileLocation, err := url.JoinPath(atomgen.cfg.LocationLink, urlEncodedName)
checkErr(err)
utils.CheckErr(err)
fileInfo, err := file.Info()
if err != nil {
log.Println(err)
Expand All @@ -101,27 +113,11 @@ filesLoop:
return nil, err
}

entries = append(entries, newAtomEntry(Name, fileLocation, mimeType, uint(length), fileModificationTime, content))
entries = append(entries, aatom.NewEntry(Name, fileLocation, mimeType, uint(length), fileModificationTime, fileLocation))
}
return entries, nil
}

func (atomgen *Atomgen) getInfoJson(filename string) (infoJson YtdlpInfoJson, err error) {
filename = strings.Replace(filename, filepath.Ext(filename), YtdlpInfoJsonExtension, 1)
infoJsonFilePath := filepath.Join(atomgen.cfg.SrcFolder, filename)
file, err := os.Open(infoJsonFilePath)
if os.IsExist(err) {
return YtdlpInfoJson{}, fmt.Errorf("WARNING: Info file for '%s' not exist", infoJsonFilePath)
}
err = json.NewDecoder(file).Decode(&infoJson)
if err != nil {
log.Printf("WARNING: failed to decode %s to YtdlpInfoJson\n", infoJsonFilePath)
return YtdlpInfoJson{}, err
}

return infoJson, nil
}

func (atomgen *Atomgen) deleteOldFiles() error {
log.Println("Start deleting old videos")
files, err := os.ReadDir(atomgen.cfg.SrcFolder)
Expand Down Expand Up @@ -153,7 +149,7 @@ func (atomgen *Atomgen) deleteOldFiles() error {
return nil
}

func (atomgen *Atomgen) DownloadURL(URL string) error {
func (atomgen *Atomgen) DownloadURL(URL string, withoutTimeLimit bool) error {
channelName, err := atomgen.ytdlp.GetChannelNameFromURL(URL)
if err != nil {
return err
Expand All @@ -163,18 +159,16 @@ func (atomgen *Atomgen) DownloadURL(URL string) error {
var cmd *exec.Cmd

ytdlpOutputTemplate := filepath.Join(atomgen.cfg.SrcFolder, "%(uploader)s %(title)s.%(ext)s")
cmd = atomgen.ytdlp.newCmdWithArgs(
"--write-info-json",
cmd = atomgen.ytdlp.NewCmdWithArgs(
"--playlist-items", fmt.Sprintf("0:%v", atomgen.cfg.VideosToDowload),
"-x",
"--download-archive", atomgen.cfg.YtdlpDownloadArchive,
"--match-filters", "!is_live",
"-f", "bestaudio",
"--audio-format", atomgen.cfg.DownloadAudioFormat,
"-f", "ba/ba*",
"--audio-format", fmt.Sprintf("%s/best", atomgen.cfg.DownloadAudioFormat),
"-o", ytdlpOutputTemplate,
"--no-simulate", "-O", "Downloading %(title)s")

if !(atomgen.cfg.WeeksToDownload == 0) {
"--no-simulate")
if !withoutTimeLimit && !(atomgen.cfg.WeeksToDownload == 0) {
cmd.Args = append(cmd.Args, "--dateafter", fmt.Sprint("today-", atomgen.cfg.WeeksToDownload, "weeks"))
}
cmd.Args = append(cmd.Args, URL)
Expand All @@ -191,11 +185,11 @@ func (atomgen *Atomgen) DownloadURL(URL string) error {
func (atomgen *Atomgen) DownloadVideos() error {
log.Printf("Start downloading videos to '%s'\n", atomgen.cfg.SrcFolder)
records := atomgen.cfg.Urls
records = Unique(records)
records = utils.Unique(records)
isNotEmpty := func(record string) bool {
return record != ""
}
records = Filter(records, isNotEmpty)
records = utils.Filter(records, isNotEmpty)

var wg sync.WaitGroup

Expand All @@ -209,7 +203,7 @@ func (atomgen *Atomgen) DownloadVideos() error {
go func(URL string) {
defer wg.Done()
limitDownloadBuffer <- 1
atomgen.DownloadURL(URL)
atomgen.DownloadURL(URL, false)
<-limitDownloadBuffer
}(record)
}
Expand Down
Loading

0 comments on commit 4aeed97

Please sign in to comment.