Created
September 3, 2011 01:42
-
-
Save philcali/1190380 to your computer and use it in GitHub Desktop.
A simple stand-alone scala script to pull DoD entries.
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
if (args.length < 1) { | |
println("Please provide a date, in the form of year-month. ie: 11-08") | |
} | |
import java.net.URL | |
import java.io._ | |
def read(in: InputStream, out: OutputStream) { | |
val buf = new Array[Byte](1024) | |
in read(buf) match { | |
case n if n > -1 => out.write(buf, 0, n); read(in, out) | |
case _ => in.close(); out.close() | |
} | |
} | |
val Contest = """href="%s-(.+)/">""".format(args(0)).r | |
val MP3 = """href="(.+)\.mp3">""".r | |
val base = "http://dwellingofduels.net/dodarchive" | |
val archives = new URL(base) | |
val out = new ByteArrayOutputStream | |
read(archives.openStream, out) | |
val contents = Contest.findFirstIn(out.toString) | |
contents.map { c => | |
val Contest(name) = c | |
val location = new File(args(0) + "-" + name) | |
if (!location.exists) location.mkdir | |
println("Created %s" format location.getName) | |
val newUrl = new URL("%s/%s-%s".format(base, args(0), name)) | |
val outInner = new ByteArrayOutputStream | |
read(newUrl.openStream, outInner) | |
outInner.toString.split("\n").foreach { html => | |
MP3.findFirstIn(html).map { li => | |
val MP3(song) = li | |
println("Retrieving %s..." format song) | |
val songUrl = new URL("%s/%s.mp3".format(newUrl, song)) | |
val file = new FileOutputStream(new File(location, song + ".mp3")) | |
read(songUrl.openStream, file) | |
} | |
} | |
} orElse { | |
Some(println("Are you sure you entered the right date?")) | |
} | |
println("Closing...") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment