Skip to content

Instantly share code, notes, and snippets.

@rickeyvisinski-kanban
Created April 16, 2013 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rickeyvisinski-kanban/5399383 to your computer and use it in GitHub Desktop.
Save rickeyvisinski-kanban/5399383 to your computer and use it in GitHub Desktop.
The second for loop will never be evaluated unless links in explicitly resolved, eg val nl = links()...
package com.test
import dispatch._
import scala.collection.JavaConverters._
import java.io.{ FileOutputStream, File }
import org.apache.commons.io.IOUtils
import scala.concurrent.ExecutionContext.Implicits.global
object App {
val URL = "http://flickr.com/explore"
def mkUrl(u:String) = url(u)
def wrOut(ab:Array[Byte], nm:String):Unit = {
IOUtils.write(ab, new FileOutputStream(new File("/tmp/" + nm)))
}
def fetchByte(u:String) = Http(mkUrl(u) OK as.Bytes)
def main(args:Array[String]) {
println("starting...")
val reqf = Http(mkUrl(URL) OK as.jsoup.Document)
val links = for {
req <- reqf
}
yield req.select("img[src$=.jpg]").asScala.toList
for {
lel <- links
ts <- lel
} {
val nm = ts.text
println(nm)
val link = ts.attr("src")
val nr = fetchByte(link)
for (r <- nr) wrOut(r, nm)
}
println("nothing was evaluated...")
Thread.sleep(5000)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment