Skip to content
Permalink
Browse files

Fixing spelling and moving some logic

  • Loading branch information...
s32x committed Dec 15, 2018
1 parent 9e12f84 commit 0e029f4adfb51fbd3fa8e025aa416d58b9371964
Showing with 44 additions and 76 deletions.
  1. +13 −8 common/common.go
  2. +0 −3 common/httpclient.go
  3. +5 −15 common/log/log.go
  4. +19 −40 common/video.go
  5. +1 −1 crunchyroll/episode.go
  6. +1 −1 glide.lock
  7. +5 −8 main.go
@@ -8,9 +8,13 @@ import (

var illegalChars = []string{"\\", "/", ":", "*", "?", "\"", "<", ">", "|"}

// Rename renames the source to the desired destination
// file name and recursively retries i times if there
// are any issues
const pathSep = string(os.PathSeparator)

// Delete removes a file from the system
func Delete(a ...string) error { return os.Remove(strings.Join(a, pathSep)) }

// Rename renames the source to the desired destination file name and
// recursively retries i times if there are any issues
func Rename(src, dst string, i int) error {
if err := os.Rename(src, dst); err != nil {
if i > 0 {
@@ -21,18 +25,19 @@ func Rename(src, dst string, i int) error {
return nil
}

// GenerateEpisodeFilename constructs an episode filename
// and returns the filename fully sanitized
// GenerateEpisodeFilename constructs an episode filename and returns the
// filename fully sanitized
func GenerateEpisodeFilename(show string, season int, episode float64, desc string) string {
ep := fmt.Sprintf("%g", episode)
if episode < 10 {
ep = "0" + ep // Prefix a zero to episode
}
return CleanFilename(fmt.Sprintf("%s - S%sE%s - %s", show, fmt.Sprintf("%02d", season), ep, desc))
return CleanFilename(fmt.Sprintf("%s - S%sE%s - %s", show,
fmt.Sprintf("%02d", season), ep, desc))
}

// CleanFilename cleans the filename of any illegal file
// characters to prevent write errors
// CleanFilename cleans the filename of any illegal file characters to prevent
// write errors
func CleanFilename(name string) string {
for _, bad := range illegalChars {
name = strings.Replace(name, bad, "", -1)
@@ -45,9 +45,6 @@ func NewHTTPClient() (*HTTPClient, error) {
// randomUA retrieves a list of user-agents and returns a
// one randomly selected from the list
func randomUA() string {
// Seed the random number generator
rand.Seed(time.Now().UnixNano())

// Retrieve the bytes of the user-agent list
useragents, err := httpclient.GetString(uaList)
if err != nil {
@@ -5,26 +5,16 @@ import "github.com/fatih/color"
const prefix = "[anirip] "

// Cyan logs important information in Cyan
func Cyan(format string, a ...interface{}) {
color.Cyan(prefix+format, a...)
}
func Cyan(format string, a ...interface{}) { color.Cyan(prefix+format, a...) }

// Info logs basic information
func Info(format string, a ...interface{}) {
color.White(prefix+format, a...)
}
func Info(format string, a ...interface{}) { color.White(prefix+format, a...) }

// Warn logs warnings
func Warn(format string, a ...interface{}) {
color.Yellow(prefix+format, a...)
}
func Warn(format string, a ...interface{}) { color.Yellow(prefix+format, a...) }

// Success logs a success message
func Success(format string, a ...interface{}) {
color.Green(prefix+format, a...)
}
func Success(format string, a ...interface{}) { color.Green(prefix+format, a...) }

// Error logs errors
func Error(err error) {
color.Red(prefix + err.Error())
}
func Error(err error) { color.Red(prefix + err.Error()) }
@@ -1,13 +1,10 @@
package common /* import "s32x.com/anirip/common" */

import (
"os"
"os/exec"
"path/filepath"
)

const pathSep = string(os.PathSeparator)

type VideoProcessor struct{ tempDir string }

// NewVideoProcessor generates a new VideoProcessor that
@@ -18,83 +15,65 @@ func NewVideoProcessor(tempDir string) *VideoProcessor {

// DumpHLS dumps an HLS Stream to the temporary directory
func (p *VideoProcessor) DumpHLS(url string) error {
// Remove a previous incomplete episode file
os.Remove(p.tempDir + pathSep + "incomplete.episode.mkv")
// Delete a previous incomplete episode file
Delete(p.tempDir, "incomplete.episode.mkv")

// Generate and execute the ffmpeg dump command
cmd := exec.Command(
findAbsoluteBinary("ffmpeg"),
"-i", url,
"-c", "copy",
"incomplete.episode.mkv")
"-c", "copy", "incomplete.episode.mkv")
cmd.Dir = p.tempDir
if err := cmd.Run(); err != nil {
return err
}

// Rename the file since it's no longer incomplete
// and return
return Rename(p.tempDir+pathSep+"incomplete.episode.mkv", p.tempDir+pathSep+"episode.mkv", 10)
}

// CleanMKV cleans the MKV metadata using mkclean
func (p *VideoProcessor) CleanMKV() error {
// Rename the episode file before cleaning
if err := Rename(p.tempDir+pathSep+"episode.mkv", p.tempDir+pathSep+"dirty.episode.mkv", 10); err != nil {
return err
}

// Generate and execute the mkclean clean command
// which will create a new copy of the file
cmd := exec.Command(
findAbsoluteBinary("mkclean"),
"dirty.episode.mkv", "episode.mkv")
cmd.Dir = p.tempDir
if err := cmd.Run(); err != nil {
return err
}

// Remove the old dirty (uncleaned) file and return
return os.Remove(p.tempDir + pathSep + "dirty.episode.mkv")
return Rename(p.tempDir+pathSep+"incomplete.episode.mkv",
p.tempDir+pathSep+"episode.mkv", 10)
}

// MergeSubtitles merges the VIDEO.mkv and the VIDEO.ass
func (p *VideoProcessor) MergeSubtitles(audioLang, subtitleLang string) error {
os.Remove(p.tempDir + pathSep + "unmerged.episode.mkv")
if err := Rename(p.tempDir+pathSep+"episode.mkv", p.tempDir+pathSep+"unmerged.episode.mkv", 10); err != nil {
Delete(p.tempDir, "unmerged.episode.mkv")
if err := Rename(p.tempDir+pathSep+"episode.mkv",
p.tempDir+pathSep+"unmerged.episode.mkv", 10); err != nil {
return err
}
cmd := new(exec.Cmd)
if subtitleLang == "" {
cmd = exec.Command(findAbsoluteBinary("ffmpeg"),
cmd = exec.Command(
findAbsoluteBinary("ffmpeg"),
"-i", "unmerged.episode.mkv",
"-c:v", "copy",
"-c:a", "copy",
"-metadata:s:a:0", "language="+audioLang, // sets audio language to passed audioLang
"-metadata:s:a:0", "language="+audioLang,
"-y", "episode.mkv")
} else {
cmd = exec.Command(findAbsoluteBinary("ffmpeg"),
cmd = exec.Command(
findAbsoluteBinary("ffmpeg"),
"-i", "unmerged.episode.mkv",
"-f", "ass",
"-i", "subtitles.episode.ass",
"-c:v", "copy",
"-c:a", "copy",
"-metadata:s:a:0", "language="+audioLang, // sets audio language to passed audioLang
"-metadata:s:s:0", "language="+subtitleLang, // sets subtitle language to subtitleLang
"-metadata:s:a:0", "language="+audioLang,
"-metadata:s:s:0", "language="+subtitleLang,
"-disposition:s:0", "default",
"-y", "episode.mkv")
}
cmd.Dir = p.tempDir
if err := cmd.Run(); err != nil {
return err
}
os.Remove(p.tempDir + pathSep + "subtitles.episode.ass")
os.Remove(p.tempDir + pathSep + "unmerged.episode.mkv")
Delete(p.tempDir, "subtitles.episode.ass")
Delete(p.tempDir, "unmerged.episode.mkv")
return nil
}

// findAbsoluteBinary attempts to search, find, and
// return the absolute path of the desired binary
// findAbsoluteBinary attempts to search, find, and return the absolute path of
// the desired binary
func findAbsoluteBinary(name string) string {
path, err := exec.LookPath(name)
if err != nil {
@@ -90,7 +90,7 @@ func (e *Episode) GetEpisodeInfo(client *common.HTTPClient, quality string) erro
return common.NewError("There was an error retrieving the manifest", err)
}

// Gets the xml string from the recieved xml response body
// Gets the xml string from the received xml response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return common.NewError("There was an error reading the xml response", err)

Some generated files are not rendered by default. Learn more.

13 main.go
@@ -1,7 +1,9 @@
package main /* import "s32x.com/anirip" */

import (
"math/rand"
"os"
"time"

"s32x.com/anirip/common"
"s32x.com/anirip/common/log"
@@ -26,6 +28,8 @@ var (
)

func main() {
// Seed the random number generator
rand.Seed(time.Now().UnixNano())
log.Cyan("v1.5.2(12/8/2018) - by Steven Wolfe <steven@swolfe.me>")
args := os.Args

@@ -118,13 +122,6 @@ func download(showURL, user, pass, quality, subLang string) {
continue
}

// Cleans the MKVs metadata for better reading by clients
log.Info("Cleaning MKV...")
if err := vp.CleanMKV(); err != nil {
log.Error(err)
continue
}

// Moves the episode to the appropriate season sub-directory
if err := common.Rename(tempDir+string(os.PathSeparator)+"episode.mkv",
show.GetTitle()+string(os.PathSeparator)+seasonMap[season.GetNumber()]+
@@ -134,7 +131,7 @@ func download(showURL, user, pass, quality, subLang string) {
log.Success("Downloading and merging completed successfully!")
}
}
log.Cyan("Completed downloading episodes form %s", show.GetTitle())
log.Cyan("Completed downloading episodes from %s", show.GetTitle())
log.Info("Cleaning up temporary directory...")
os.RemoveAll(tempDir)
}

0 comments on commit 0e029f4

Please sign in to comment.
You can’t perform that action at this time.