Skip to content

Commit

Permalink
Customizable configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
matcornic committed Jan 31, 2016
1 parent ceceacd commit 6bce64a
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 22 deletions.
36 changes: 28 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
Subify is a tool to download subtitles for your favorite TV shows and movies.
It is directly able to open the video with your default player, once the subtitle is downloaded.

Subify uses [SubDB Web API](http://thesubdb.com/) and [OpenSubtitles API](http://trac.opensubtitles.org/projects/opensubtitles/wiki) to get subtitles. It also considers that you use a default player interpreting srt subtitles when the video file name is the same than the srt file (ex: [VLC](http://www.videolan.org/vlc/)).
Subify combines [SubDB Web API](http://thesubdb.com/) and [OpenSubtitles API](http://trac.opensubtitles.org/projects/opensubtitles/wiki) to get the best subtitles fr your video. It also considers that you use a default player interpreting srt subtitles when the video file name is the same than the srt file (ex: [VLC](http://www.videolan.org/vlc/)).

Subify gets the best match from several APIs in this order
Subify gets the best match from several APIs in this order. This default behavior can easily be changed. See the documentation below

1. SubDB
2. OpenSubtitles
Expand All @@ -14,7 +14,7 @@ Download the [last version of Subify](https://github.com/matcornic/subify/releas

If you use Golang, you can get Subify and its binary directly with :
```shell
go get -v github.com/matcornic/subify
go get -u github.com/matcornic/subify
```

On Mac OS, you can also create a Service Automator, in order to [add a "Subify" option in the Finder menu for your videos](https://github.com/matcornic/subify/wiki/Adding-a-Subify-option-in-the-Finder-menu-for-your-videos).
Expand Down Expand Up @@ -51,7 +51,7 @@ Available Commands:
list List information about something (ex: languages)
Flags:
--config string Config file (default is $HOME/.subify.json). Edit to change default behaviour
--config string Config file (default is $HOME/.subify.|json|yaml|toml). Edit to change default behaviour
--dev Instanciate development sandbox instead of production variables
-v, --verbose Print more information while executing
Expand All @@ -76,7 +76,7 @@ Flags:
-o, --open Once the subtitle is downloaded, open the video with your default video player (OSX: "open", Windows: "start", Linux/Other: "xdg-open")
Global Flags:
--config string Config file (default is $HOME/.subify.json). Edit to change default behavior
--config string Config file (default is $HOME/.subify.|json|yaml|toml). Edit to change default behavior
--dev Instanciate development sandbox instead of production variables
-v, --verbose Print more information while executing
```
Expand All @@ -95,7 +95,7 @@ Aliases:
Global Flags:
--all Shows all languages
--config string Config file (default is $HOME/.subify.json). Edit to change default behaviour
--config string Config file (default is $HOME/.subify.|json|yaml|toml). Edit to change default behaviour
--dev Instanciate development sandbox instead of production variables
-v, --verbose Print more information while executing
```
Expand All @@ -106,11 +106,27 @@ Usage:
subify list apis [flags]
Global Flags:
--config string Config file (default is $HOME/.subify.json). Edit to change default behaviour
--config string Config file (default is $HOME/.subify.|json|yaml|toml). Edit to change default behaviour
--dev Instanciate development sandbox instead of production variables
-v, --verbose Print more information while executing
```

## Overriding default configuration

Default configuration can be overridden. Instead of passing the same parameters again and again to the command, you can write a `JSON/YAML/TOML` in your home folder (`$HOME/.subify.|json|yaml|toml`). Here is an example with a `.subify.toml` file :

```toml

# Root is for all commands
[root]
verbose = false # Turn on to print more information by default
dev = false # Don't turn on, just for development purpose

# download for the download/dl command
[download]
languages = "en" # Searching for theses languages. Can be a list like : "fr,es,en"
apis = "SubDB,OpenSubtitles" # Searching from these sites
```

## Release Notes
* **0.2.0** Not released yet
Expand All @@ -120,6 +136,7 @@ Global Flags:
* Vendoring (with glide)
* List of available apis
* Usage of APIs is customizable (can order Subdb search before OpenSubtitles for ex)
* customizable default configuration with a conf file (for example to change the default language for all downloads)
* **0.1.0** Jan 15, 2016
* Implement first init

Expand All @@ -140,9 +157,12 @@ Subify uses vendoring with Glide to manage dependencies. Don't forget to set `GO
## TODO
1. Auto update command
2. Upload command to contribute to SubDB/OpenSubtitles database
3. Doc on default configuration (for example to change the default language for all downloads)
4. Add Addic7ed API (better quality of translations, but no real API)
5. Localization/Internationalization
6. Proper logging (stdout + stderr + verbose + debug)
7. Wercker/Travis
8. Generate GoDoc
9. Extract SubDB API in separate projet

## License
Subify is released under the Apache 2.0 license. See LICENSE.txt
14 changes: 10 additions & 4 deletions cmd/dl.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package cmd

import (
"strings"

"github.com/matcornic/subify/common/utils"
"github.com/matcornic/subify/subtitles"
"github.com/skratchdot/open-golang/open"
"github.com/spf13/cobra"
logger "github.com/spf13/jwalterweatherman"
"github.com/spf13/viper"
)

var apis []string
var languages []string
var openVideo bool

// dlCmd represents the dl command
Expand All @@ -26,6 +27,8 @@ Give the path of your video as first parameter and let's go !`,
videoPath := args[0]
utils.VerbosePrintln(logger.INFO, "Given video file is "+videoPath)

apis := strings.Split(viper.GetString("download.apis"), ",")
languages := strings.Split(viper.GetString("download.languages"), ",")
err := subtitles.Download(videoPath, apis, languages)
if err != nil {
utils.ExitPrintError(err, "Sadly, we could not download any subtitle for you. Try another time or contribute to the apis. See 'subify upload -h'")
Expand All @@ -38,10 +41,13 @@ Give the path of your video as first parameter and let's go !`,
}

func init() {
dlCmd.Flags().StringSliceVarP(&languages, "languages", "l", []string{"en"}, "Languages of the subtitle separate by a comma (First to match is downloaded). Available languages at 'subify list languages'")
dlCmd.Flags().StringSliceVarP(&apis, "apis", "a", []string{"SubDB", "OpenSubtitles"}, "Overwrite default searching APIs behavior, hence the subtitles are downloaded. Available APIs at 'subify list apis'")
dlCmd.Flags().StringP("languages", "l", "en", "Languages of the subtitle separate by a comma (First to match is downloaded). Available languages at 'subify list languages'")
dlCmd.Flags().StringP("apis", "a", "SubDB,OpenSubtitles", "Overwrite default searching APIs behavior, hence the subtitles are downloaded. Available APIs at 'subify list apis'")
dlCmd.Flags().BoolVarP(&openVideo, "open", "o", false,
"Once the subtitle is downloaded, open the video with your default video player"+
` (OSX: "open", Windows: "start", Linux/Other: "xdg-open")`)
viper.BindPFlag("download.languages", dlCmd.Flags().Lookup("languages"))
viper.BindPFlag("download.apis", dlCmd.Flags().Lookup("apis"))

RootCmd.AddCommand(dlCmd)
}
17 changes: 10 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package cmd

import (
"fmt"

"github.com/matcornic/subify/common/config"
"github.com/matcornic/subify/common/utils"
"github.com/spf13/cobra"
logger "github.com/spf13/jwalterweatherman"
"github.com/spf13/viper"
)

Expand All @@ -15,7 +16,9 @@ var RootCmd = &cobra.Command{
Long: `Tool to handle subtitles for your best TV Shows and movies
http://github.com/matcornic/subify`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// Assertions
// Overwrite conf from config files
config.Dev = viper.GetBool("root.dev")
config.Verbose = viper.GetBool("root.verbose")
utils.InitLoggingConf()
},
}
Expand All @@ -32,12 +35,13 @@ func init() {
cobra.OnInitialize(initConfig)
// Default configuration can be overridden
RootCmd.PersistentFlags().StringVar(&config.ConfigFile, "config", "",
"Config file (default is $HOME/.subify.json). Edit to change default behavior")
"Config file (default is $HOME/.subify.yaml|json|toml). Edit to change default behavior")
RootCmd.PersistentFlags().BoolVarP(&config.Verbose, "verbose", "v", false,
"Print more information while executing")
RootCmd.PersistentFlags().BoolVarP(&config.Dev, "dev", "", false,
"Instanciate development sandbox instead of production variables")

viper.BindPFlag("root.dev", RootCmd.PersistentFlags().Lookup("dev"))
viper.BindPFlag("root.verbose", RootCmd.PersistentFlags().Lookup("verbose"))
}

const (
Expand All @@ -53,13 +57,12 @@ func initConfig() {

viper.SetConfigName(subifyConfigFile) // name of config file (without extension)
viper.AddConfigPath(subifyConfigPath) // adding home directory as first search path
viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
err := viper.ReadInConfig()
if err == nil {
utils.VerbosePrintln(logger.INFO, "Using config file:"+viper.ConfigFileUsed())
fmt.Println("Using config file:" + viper.ConfigFileUsed())
} else {
logger.WARN.Println("Could not read config file (", viper.ConfigFileUsed(), "): ", err)
fmt.Println("Cant read config file:" + viper.ConfigFileUsed())
}
}
2 changes: 1 addition & 1 deletion subtitles/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func InitAPIs(apiAliases []string) (apis Clients) {
for _, alias := range apiAliases {
for _, availableAPI := range DefaultAPIs {
for _, availableAliases := range availableAPI.GetAliases() {
if strings.ToLower(alias) == strings.ToLower(availableAliases) {
if strings.ToLower(strings.TrimSpace(alias)) == strings.ToLower(strings.TrimSpace(availableAliases)) {
apis = append(apis, availableAPI)
}
}
Expand Down
2 changes: 1 addition & 1 deletion subtitles/subdb/subdb.api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

const (
userAgent = "SubDB/1.0 (Subify/0.1; http://github.com/matcornic/subify)"
devURL = "http://sandbox.thesubd.com/"
devURL = "http://sandbox.thesubdb.com/"
prodURL = "http://api.thesubdb.com/"
)

Expand Down
3 changes: 3 additions & 0 deletions subtitles/subdb/subdb.services.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ func getHashOfVideo(filename string) (string, error) {
func buildURL(hash string, language string) string {
baseURL := prodURL
if config.Dev {
fmt.Println("Dev mode")
baseURL = devURL
} else {
fmt.Println("Prod mode")
}
opt := options{
Action: "download",
Expand Down
2 changes: 1 addition & 1 deletion subtitles/subtitles.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ browselang:
}

if err != nil {
return fmt.Errorf("No %v subtitle found, even after searching in all APIs for all given languages", strings.Join(l.GetDescriptions(), ", nor "))
return fmt.Errorf("No %v subtitle found, even after searching in all APIs (%v)", strings.Join(l.GetDescriptions(), ", nor "), a.String())
}

return nil
Expand Down

0 comments on commit 6bce64a

Please sign in to comment.