Created
June 22, 2015 21:58
-
-
Save pygillier/b8742ea2ca59959f2273 to your computer and use it in GitHub Desktop.
XML2HTML converter and sorter for Vimeo statistics
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
import groovy.xml.MarkupBuilder | |
@Grapes([ | |
@GrabConfig(systemClassLoader=true), | |
@Grab(group='org.slf4j', module='slf4j-api', version='1.7.12'), | |
@Grab(group='ch.qos.logback', module='logback-classic', version='1.0.6') | |
]) | |
import org.slf4j.* | |
import groovy.util.logging.Slf4j | |
@Slf4j | |
class VimeoParser { | |
def xmlFile | |
def outFile | |
def VimeoParser(File xmlFile, def outFile) { | |
log.info "Parsing ${xmlFile}" | |
this.xmlFile = xmlFile | |
this.outFile = outFile | |
} | |
def parse() { | |
log.debug "Parser starting..." | |
def stats = new XmlParser().parse(xmlFile) | |
log.info "Records found : ${stats.statistics.size()}" | |
log.info "Sorting by loads then plays" | |
def sorted = stats.statistics.sort { first, second -> | |
first.total_loads.text().toInteger() <=> second.total_loads.text().toInteger() ?: | |
first.total_plays.text().toInteger() <=> second.total_plays.text().toInteger() | |
} | |
sorted.reverse(true) | |
log.info "Generating HTML file" | |
def writer = new StringWriter() | |
def builder = new MarkupBuilder(writer) | |
builder.html { | |
head { | |
meta(charset:"UTF-8") | |
title "Vimeo Statistics (sorted by loads and plays)" | |
link(rel: "stylesheet", href: "http://yui.yahooapis.com/pure/0.6.0/pure-min.css") | |
} | |
body { | |
table(class:"pure-table pure-table-horizontal") { | |
// table content | |
tbody { | |
sorted.each { clip -> | |
tr { | |
td { | |
a(href: "http://vimeo.com"+clip.url.text()) { | |
mkp.yield clip.title.text() | |
} | |
} | |
td "${clip.total_loads.text()} (${clip.total_local_loads.text()} / ${clip.total_embed_loads.text()})" | |
td "${clip.total_plays.text()} (${clip.total_local_plays.text()} / ${clip.total_embed_plays.text()})" | |
} | |
} | |
} | |
// Header | |
thead { | |
tr { | |
th "Clip" | |
th "Total loads (local/embed)" | |
th "Total plays (local/embed)" | |
} | |
} | |
// Footer | |
tfoot { | |
tr { | |
td(colspan:3) { | |
mkp.yield sorted.size()+" records" | |
} | |
} | |
} | |
} | |
} | |
} | |
log.info "Writing to output file : ${this.outFile}" | |
/* | |
* Delete outFile if exists and write to it. | |
*/ | |
def out = new File(this.outFile) | |
if(out.exists()) { | |
log.warn "output file already exists, deleting..." | |
out.delete() | |
} | |
out.setText(writer.toString()) | |
log.debug "Parser ended." | |
} | |
} | |
// CLI | |
def cli = new CliBuilder( | |
usage : 'groovy vimeo.groovy -f "xmlFile" -o "outFile"' | |
) | |
cli.h(longOpt: "help", "Show usage informations and exit.") | |
cli.f(argName: "xmlFile", longOpt: "file", args:1, required: true, "XML file to load, REQUIRED") | |
cli.o(argName: "outFile", longOpt: "output", args:1, required: true, "Output HTML file, REQUIRED") | |
def opt = cli.parse(args) | |
if(!opt) { | |
println "Erreur while parsing args" | |
System.exit(-1) | |
} | |
else if(opt.h) { | |
println "Usage : " | |
System.exit(-1) | |
} else { | |
if(opt.f && opt.o){ | |
def xmlFile = new File(opt.f) | |
if(!xmlFile.exists()) { | |
println "File ${xmlFile} does not exist !" | |
System.exit(1) | |
} | |
def parser = new VimeoParser(xmlFile, opt.o) | |
parser.parse() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment