Created
July 5, 2011 19:26
-
-
Save xuwei-k/1065650 to your computer and use it in GitHub Desktop.
ある User の gist の File をまとめて download してローカルに zip で保存する
This file contains hidden or 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
object Main{ | |
import scala.util.parsing.json.JSON | |
def gistsUrl(user:String) = "http://gist.github.com/api/v1/json/gists/" + user | |
def gistsFile(repo:String,fileName:String) = "https://raw.github.com/gist/" + repo + "/"+ fileName | |
type jsonType = Map[String,List[Map[String,Any]]] | |
def byteIteratorFromURL(url:String):Iterator[Byte] = { | |
val in = new java.net.URL(url).openStream | |
Iterator.continually(in.read).takeWhile(_ != -1).map(_.toByte) | |
} | |
def main(args:Array[String]){ | |
val name = "xuwei-k" | |
val jsonData = io.Source.fromURL(gistsUrl(name)).mkString | |
val json = JSON.parseFull( jsonData ).get.asInstanceOf[jsonType] | |
val RepositoryList = json("gists").map{ o => | |
val repoId = o("repo").asInstanceOf[String] | |
val fileNames = o("files").asInstanceOf[List[String]] | |
val files = fileNames.map{ f => | |
val data = byteIteratorFromURL(gistsFile(repoId,f)).toArray | |
ByteFile(repoId + "/" + f, data ) | |
} | |
Repo(repoId,files) | |
} | |
saveToZipFile(RepositoryList,name + ".zip") | |
} | |
import java.util.zip._ | |
case class Repo(name:String,files:List[ByteFile]) | |
case class ByteFile(name:String,data:Array[Byte]) | |
def saveToZipFile(repoList:List[Repo],saveFileName:String){ | |
val buf = new java.io.FileOutputStream(saveFileName) | |
val o = new ZipOutputStream(buf) | |
def put(name:String,data:Array[Byte]){ | |
o.putNextEntry(new ZipEntry(name)) | |
o.write(data) | |
} | |
repoList.foreach{r => | |
put(r.name + "/" ,Array.empty) | |
r.files.foreach(f => put(f.name,f.data)) | |
} | |
o.close | |
buf.close | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment