Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
profitware committed Dec 24, 2017
1 parent 9195b02 commit 55f3d65
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
*.class
*.log
target
.ensime*
.DS_Store
2 changes: 2 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
maxColumn = 120
project.excludeFilters = [".ensime_cache"]
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: scala
scala:
- 2.10.4
- 2.11.2
branches:
only:
- master
notifications:
email:
recipients:
- [email protected]
jdk:
- oraclejdk7
- oraclejdk8
12 changes: 12 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Dependencies._

lazy val root = (project in file(".")).settings(
inThisBuild(
List(
organization := "tech.profitware",
scalaVersion := "2.12.3",
version := "0.1.0-SNAPSHOT"
)),
name := "AudioConverter",
libraryDependencies += scalaTest % Test
)
5 changes: 5 additions & 0 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import sbt._

object Dependencies {
lazy val scalaTest = "org.scalatest" %% "scalatest" % "3.0.3"
}
1 change: 1 addition & 0 deletions project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=1.0.2
56 changes: 56 additions & 0 deletions src/main/scala/tech/profitware/audioconverter/Converter.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package tech.profitware.audioconverter

import scala.sys.process._
import java.io.IOException
import java.io.File

trait Conversion {
val sourceExtension: String
val destinationFormat: String
val destinationExtension: String
def isPresent: Boolean
def alertNotInstalled(): Unit
def convertFile(file: File, directory: Directory): Boolean
}

trait FFMpegConversion extends Conversion with Logger {
def isPresent =
try {
("ffmpeg -version" #> new File("/dev/null")).! == 0
} catch {
case ioe: IOException => false
}
def alertNotInstalled() = log("FFMpeg is not installed!")
def convertFile(file: File, directory: Directory) = {
val absoluteFile = file.getAbsoluteFile
val album = absoluteFile.getParentFile.getName
val destionationDirectory =
new File(directory.destinationArtistDirectory.getAbsolutePath, album)
val destinationFile = new File(destionationDirectory, s"${file.getName}.${destinationExtension}").getAbsolutePath

val executableSeq = Seq("ffmpeg", "-i", s"${absoluteFile.getPath}", "-acodec", destinationFormat, destinationFile)
val executable = Process(executableSeq, absoluteFile.getParentFile)

try {
destionationDirectory.mkdirs()
("echo y" #| executable).! == 0
} catch {
case ioe: IOException => false
}
}
}

trait FFMpegFlacToAlacConversion extends FFMpegConversion {
val sourceExtension = "flac"
val destinationFormat = "alac"
val destinationExtension = "m4a"
}

abstract class Converter(val directory: Directory) extends Conversion with Logger {
def convert() =
directory.getSourceDirectoryTree.par
.filter(_.getName().endsWith(s".${sourceExtension}"))
.map(
currentFile => convertFile(currentFile, directory)
)
}
35 changes: 35 additions & 0 deletions src/main/scala/tech/profitware/audioconverter/Directory.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package tech.profitware.audioconverter

import java.io.File

trait DestinationDirectory {
def absoluteDestinationDirectory: File
}

trait MusicDestinationDirectory extends DestinationDirectory {
def absoluteDestinationDirectory = new File(
System.getProperty("user.home"),
"Music"
)
}

abstract class Directory(val sourceDirectory: String) extends DestinationDirectory with Logger {
val absoluteSourceDirectory = new File(sourceDirectory).getAbsoluteFile

log(s"Using ${absoluteSourceDirectory.getAbsolutePath} directory as a source")

val destinationArtistDirectory = new File(
absoluteDestinationDirectory.getAbsolutePath,
absoluteSourceDirectory.getName
)

// Source: https://stackoverflow.com/a/7264833
def getFileTree(f: File): Stream[File] = f #:: {
if (f.isDirectory)
f.listFiles().toStream.flatMap(getFileTree)
else
Stream.empty
}

def getSourceDirectoryTree = getFileTree(absoluteSourceDirectory)
}
9 changes: 9 additions & 0 deletions src/main/scala/tech/profitware/audioconverter/Logger.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package tech.profitware.audioconverter

trait Logger {
def log(s: String): Unit
}

trait SimpleLogger extends Logger {
def log(s: String) = println(s)
}
19 changes: 19 additions & 0 deletions src/main/scala/tech/profitware/audioconverter/Main.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package tech.profitware.audioconverter

import java.io.File

object Main extends App {
val sourceDirectoryFile = new File(if (args.length > 0) args(0) else ".")
val sourceDirectory =
if (sourceDirectoryFile.exists)
sourceDirectoryFile.getAbsolutePath
else "."

val directory = new Directory(sourceDirectory) with MusicDestinationDirectory with SimpleLogger
val converter = new Converter(directory) with FFMpegFlacToAlacConversion with SimpleLogger

if (converter.isPresent)
converter.convert()
else
converter.alertNotInstalled()
}
20 changes: 20 additions & 0 deletions src/test/scala/tech/profitware/audioconverter/DirectorySpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package tech.profitware.audioconverter

import org.scalatest._
import java.io.File

class DirectorySpec extends FlatSpec with Matchers {
val currentDirectory = new Directory(".") with MusicDestinationDirectory with SimpleLogger

"Current directory" should "be absolute" in {
currentDirectory.absoluteSourceDirectory.getAbsolutePath shouldEqual new File(".").getAbsolutePath
}

"Music destination directory" should "end with Music" in {
currentDirectory.absoluteDestinationDirectory.getName shouldEqual "Music"
}

"Artist destination directory" should "be equal to current source directory name" in {
currentDirectory.destinationArtistDirectory.getName shouldEqual new File(".").getName
}
}

0 comments on commit 55f3d65

Please sign in to comment.