Created
August 28, 2018 07:30
-
-
Save holgergp/da3fe540d057b35b62617daa806b3de8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env groovy | |
@Grab('org.yaml:snakeyaml:1.17') | |
import org.yaml.snakeyaml.Yaml | |
import static groovy.io.FileType.FILES | |
def dir = new File("_posts"); | |
def files = []; | |
dir.traverse(type: FILES, maxDepth: 0) { files.add(it) }; | |
def lines = files.collect { | |
return getDurationFor(it) | |
} | |
println(sumDuration(lines)) | |
def getDurationFor(File file) { | |
def lines = file.readLines() | |
def linesWithDuration = lines.findAll({it.contains('podcast_duration: ')}) | |
def linesWithOnlyDurations = linesWithDuration.collect({ | |
m -> | |
def (_, duration) = m.split( ': ' ) | |
return duration.replace("\"", "") | |
}) | |
def linesWithSumableNumbers = linesWithOnlyDurations. collect({ | |
l -> | |
tokens = l.split(":") | |
if(tokens.length==0) { | |
return 0; | |
} | |
else if(tokens.length ==2) { | |
hours = 0 | |
minutes = Integer.parseInt(tokens[0]); | |
seconds = Integer.parseInt(tokens[1]); | |
duration = 3600 * hours + 60 * minutes + seconds; | |
return duration | |
} | |
else if(tokens.length == 3) { | |
hours = Integer.parseInt(tokens[0]) | |
minutes = Integer.parseInt(tokens[1]); | |
seconds = Integer.parseInt(tokens[2]); | |
duration = 3600 * hours + 60 * minutes + seconds; | |
return duration | |
} | |
}) | |
return linesWithSumableNumbers | |
} | |
def sumDuration(lines) { | |
return lines.inject(0, { result, i -> | |
currValue = 0 | |
if(i!=null) { | |
currValue =i | |
} | |
return result + currValue | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment