Skip to content

Commit

Permalink
Fix for #43 report errors clearly (#48)
Browse files Browse the repository at this point in the history
* Fix artetv collection when --keep-bonuses is false

* Fix #43 Report errors

- Refactor log system with levels ERROR, INFO, TRACE and DEBUG
- INFO, TRACE and DEBUG message are written only on log file when provided
- Fatal errors like missing parameter or invalid paths are reported and the program is stopped
  • Loading branch information
simulot committed May 4, 2020
1 parent cf5fada commit 42287ff
Show file tree
Hide file tree
Showing 28 changed files with 443 additions and 674 deletions.
21 changes: 13 additions & 8 deletions README.md
Expand Up @@ -59,12 +59,12 @@ tar -czvf aspiratv_0.8.0_Linux_x86_64.tar.gz
```
Usage of aspiratv:
--config string Configuration file name. (default "config.json")
--debug Debug mode.
-d, --destination string Destination path.
--force Force media download.
--headless Headless mode. Progression bars are not displayed.
-b, --keep-bonuses Download bonuses when true (default true)
--log string Give the log file name. When empty, no log.
--log string Give the log file name.
-l, --log-level string Log level (INFO,TRACE,ERROR,DEBUG) (default "ERROR")
-a, --max-aged int Retrieve media younger than MaxAgedDays.
-m, --max-tasks int Maximum concurrent downloads at a time. (default 8)
-p, --provider string Provider to be used with download command. Possible values : artetv,francetv,gulli
Expand All @@ -74,7 +74,7 @@ Le programme fonctionne selon deux modilités :
## Pour surveiller la mise à disposition de nouveaux épisodes d'une émission
Dans ce mode, le fichiers de configuration `config.json` placé dans le même répertoire que le programe est lu pour pour interroger les différents serveur.

### -headless
### --headless
L'option `--headless` désactive les barres de progressions et produit une log sur la console.


Expand All @@ -97,15 +97,20 @@ Cette commande cherchera les épisodes de la série "Les Dalton" sur france tél
### --max-tasks NUM
Précise le nombre maximal de téléchargements simultanés possible. La valeur par défaut est le nombre de processeurs de la machine.

### --debug
Ajoute au fichier de log des informations utiles au débugage.
### --log-level, -l
Indique le niveau de détail du fichier de log. Les options possibles sont:
* INFO
* TRACE
* DEBUG

### --force
Télécharge toutes les émissions correspondant à la liste de recherche, même si elles ont été déjà téléchargées.

### --log LOG_FILE

L'option `-log` redirige les messages d'erreur dans le fichier indiqué.
L'option `--log` indique le fichier dans lequel les messages d'erreur sont écrits.

### --force
Télécharge toutes les émissions correspondant à la liste de recherche, même si elles ont été déjà téléchargées.


### --keep-bonuses
Télécharge les vidéos associées au show demandé.
Expand Down
29 changes: 10 additions & 19 deletions cmd/aspiratv/config.go
Expand Up @@ -14,7 +14,10 @@ import (
)

func (a *app) Initialize() {
a.ReadConfig(a.Config.ConfigFile)
err := a.ReadConfig(a.Config.ConfigFile)
if err != nil {
a.logger.Fatal().Printf("[Initialize] %s", err)
}

// Check ans normalize configuration file
a.Config.Check()
Expand All @@ -28,19 +31,15 @@ func (a *app) Initialize() {
}
b, err := cmd.Output()
if err != nil {
log.Fatal("Missing ffmpeg on your system, it's required to download video files.")
a.logger.Fatal().Printf("[Initialize] Can't determine ffmpeg path: %s", err)
}
a.ffmpeg = strings.Trim(strings.Trim(string(b), "\r\n"), "\n")
if a.Config.Debug {
log.Printf("FFMPEG path: %q", a.ffmpeg)
}
a.logger.Trace().Printf("[Initialize] FFMPEG path: %q", a.ffmpeg)

// Get FFMPEG version
cmd = exec.Command(a.ffmpeg, "-version")
b, err = cmd.Output()
if a.Config.Debug {
log.Printf("FFMPEG version: %q", string(b))
}
a.logger.Debug().Printf("[Initialize] FFMPEG version: %q", string(b))
}

type ProviderConfig struct {
Expand Down Expand Up @@ -83,7 +82,7 @@ var defaultConfig = &config{

// WriteConfig create a JSON file with the current configuration
func WriteConfig() {
f, err := os.Create("confing.json")
f, err := os.Create("config.json")
if err != nil {
log.Fatalf("Can't write configuration file: %v", err)
}
Expand All @@ -96,6 +95,7 @@ func WriteConfig() {

// ReadConfig read the JSON configuration file
func (a *app) ReadConfig(configFile string) error {
a.logger.Trace().Printf("[ReadConfig] opening '%s'", configFile)
f, err := os.Open(configFile)
if err != nil {
return fmt.Errorf("Can't open configuration file: %v", err)
Expand All @@ -109,15 +109,6 @@ func (a *app) ReadConfig(configFile string) error {
return nil
}

// // ReadConfigOrDie create a stub of config.json when it is missing from disk
// func ReadConfigOrDie(conf *Config) {
// err := ReadConfig(conf.ConfigFile, conf)
// if err != nil {
// log.Fatalf("Fatal: %v", err)
// }

// }

// Check the configuration or die
func (c *config) Check() {

Expand All @@ -131,7 +122,7 @@ func (c *config) Check() {
m.Show = strings.ToLower(m.Show)
m.Title = strings.ToLower(m.Title)
if _, ok := c.Destinations[m.Destination]; !ok {
log.Fatalf("Destination %q is not defined into section Destination of %q", m.Destination, c.ConfigFile)
log.Fatalf("Destination %q for show %q is not defined into section Destination of %q", m.Destination, m.Show, c.ConfigFile)
}
}

Expand Down
58 changes: 19 additions & 39 deletions cmd/aspiratv/download.go
Expand Up @@ -2,7 +2,6 @@ package main

import (
"context"
"log"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -65,19 +64,14 @@ func (a *app) DownloadShow(ctx context.Context, p providers.Provider, m *provide

defer func() {
if dlErr != nil || ctx.Err() != nil {
log.Printf("[%s] Cancelling download of %q.", p.Name(), itemName)
a.logger.Info().Printf("[%s] download of %q is cancelled.", p.Name(), itemName)
for _, f := range files {
log.Printf("[%s] Removing %q.", p.Name(), f)
a.logger.Debug().Printf("[%s] Removing %q.", p.Name(), f)
err := os.Remove(f)
if err != nil {
log.Printf("[%s] Error %s", p.Name(), err)
a.logger.Error().Printf("[%s] Error %s", p.Name(), err)
}
}
} else {
if a.Config.Headless || a.Config.Debug {
log.Printf("[%s] Media %q downloaded", p.Name(), filepath.Base(itemName))
}

}
cancel()
}()
Expand All @@ -87,7 +81,7 @@ func (a *app) DownloadShow(ctx context.Context, p providers.Provider, m *provide
err := p.GetMediaDetails(ctx, m) // Side effect: Episode number can be determined at this point.
url := m.Metadata.GetMediaInfo().URL
if err != nil || len(url) == 0 {
log.Printf("[%s] Can't get url from %s.", p.Name(), filepath.Base(m.Metadata.GetMediaPath(a.Config.Destinations[m.Match.Destination])))
a.logger.Error().Printf("[%s] Can't get url from %s.", p.Name(), filepath.Base(m.Metadata.GetMediaPath(a.Config.Destinations[m.Match.Destination])))
return
}

Expand All @@ -102,48 +96,36 @@ func (a *app) DownloadShow(ctx context.Context, p providers.Provider, m *provide
fn := m.Metadata.GetMediaPath(a.Config.Destinations[m.Match.Destination])
itemName = filepath.Base(fn)

if a.Config.Headless || a.Config.Debug {
log.Printf("[%s] Start downloading media %q", p.Name(), fn)
}
a.logger.Trace().Printf("[%s] Start downloading media %q", p.Name(), fn)

if !a.Config.Headless {
pgr = a.NewDownloadBar(pc, filepath.Base(fn), id)
defer pgr.bar.SetTotal(1, true)
}
if a.Config.Debug {
log.Printf("[%s] Stream url: %q", p.Name(), url)
}

if a.Config.Debug {
log.Printf("[%s] Downloading into file: %q", p.Name(), fn)
}
a.logger.Debug().Printf("[%s] Stream url: %q", p.Name(), url)

err = os.MkdirAll(filepath.Dir(fn), 0777)
if err != nil {
log.Println(err)
a.logger.Error().Printf("Can't create destination folder: %s", err)
return
}

info := m.Metadata.GetMediaInfo()
if a.Config.Debug {
log.Printf("[%s] Download started %q", p.Name(), filepath.Base(fn))
}

files = append(files, fn)
dlErr = download.Download(ctx, url, fn, info, download.WithProgress(pgr), download.WithDebug(a.Config.Debug))
dlErr = download.Download(ctx, a.logger, url, fn, info, download.WithProgress(pgr))

if dlErr != nil {
log.Printf("[%s] Download exits with error:\n%s", p.Name(), dlErr)
a.logger.Error().Printf("[%s] Download exits with error:\n%s", p.Name(), dlErr)
return
}

if ctx.Err() != nil {
log.Printf("[%s] Download exits with error:\n%s", p.Name(), ctx.Err())
a.logger.Error().Printf("[%s] Download exits with error:\n%s", p.Name(), dlErr)
return
}

if a.Config.Headless || a.Config.Debug {
log.Printf("[%s] %q downloaded.", p.Name(), filepath.Base(fn))
}
a.logger.Info().Printf("[%s] Media %q downloaded.", p.Name(), filepath.Base(fn))
return
}

Expand Down Expand Up @@ -177,7 +159,7 @@ func (a *app) DownloadInfo(ctx context.Context, p providers.Provider, destinatio
if !nfoExists && err == nil {
err = m.Metadata.WriteNFO(nfoPath)
if err != nil {
log.Println(err)
a.logger.Error().Printf("WriteNFO: %s", err)
}
*downloadedFiles = append(*downloadedFiles, nfoPath)
a.DowloadImages(ctx, p, nfoPath, info.Thumb, downloadedFiles)
Expand All @@ -189,7 +171,7 @@ func (a *app) DownloadInfo(ctx context.Context, p providers.Provider, destinatio
if !nfoExists && err == nil {
info.SeasonInfo.WriteNFO(nfoPath)
if err != nil {
log.Println(err)
a.logger.Error().Printf("WriteNFO: %s", err)
}
*downloadedFiles = append(*downloadedFiles, nfoPath)

Expand All @@ -202,7 +184,7 @@ func (a *app) DownloadInfo(ctx context.Context, p providers.Provider, destinatio
if !nfoExists && err == nil {
info.TVShow.WriteNFO(nfoPath)
if err != nil {
log.Println(err)
a.logger.Error().Printf("WriteNFO: %s", err)
}
*downloadedFiles = append(*downloadedFiles, nfoPath)

Expand All @@ -220,14 +202,14 @@ func (a *app) DowloadImages(ctx context.Context, p providers.Provider, destinati

err := os.MkdirAll(filepath.Dir(destination), 0777)
if err != nil {
log.Printf("[DONWLOAD] Can't create %s :%s", destination, err)
a.logger.Error().Printf("[%s] Can't create %s", p.Name(), err)
return
}

for _, thumb := range thumbs {
var base string
if ctx.Err() != nil {
log.Printf("[%s] Cancelling %s", p.Name(), ctx.Err())
a.logger.Debug().Printf("[%s] Cancelling %s", p.Name(), ctx.Err())
return
}

Expand All @@ -248,11 +230,9 @@ func (a *app) DowloadImages(ctx context.Context, p providers.Provider, destinati
err := a.DownloadImage(ctx, thumb.URL, thumbName, downloadedFiles)

if err != nil {
log.Printf("[%s] Can't get thumbnail from %q: %s", p.Name(), thumb.URL, err)
a.logger.Error().Printf("[%s] Can't get thumbnail from %q: %s", p.Name(), thumb.URL, err)
continue
}
if a.Config.Headless || a.Config.Debug {
log.Printf("[%s] thumbnail %q downloaded.", p.Name(), thumbName)
}
a.logger.Debug().Printf("[%s] thumbnail %q downloaded.", p.Name(), thumbName)
}
}

0 comments on commit 42287ff

Please sign in to comment.