Created
March 23, 2015 23:57
-
-
Save spiiin/53312ac444f73497f645 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
package scala_vk_get | |
import scala.sys.process._ | |
import scala.concurrent._ | |
import scala.concurrent.duration._ | |
import scala.xml.XML | |
import scala.io.Source | |
import java.net.URL | |
import java.io.File | |
import java.util.concurrent.Executors | |
object scala_app extends App { | |
var token: String = "" | |
implicit def string2xml(v: String) = XML.loadString(v) | |
override def main(args: Array[String]): Unit = { | |
implicit val threadPool = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(4)) | |
val cmdName = "../../vkAuthorizeToOutput/vkAuthorize.exe" | |
token = (cmdName.!!).replace("\r\n", "") | |
val photosAns = getPhotos(OWNER_ID, ALBUM_ID) | |
val photo_1280 = extractBigPhotos(photosAns) | |
val fut = photo_1280.zipWithIndex.map { | |
case (url, i) => Future { println(i + " " + url); saveToFile(url, f"C:/users/USER_NAME/desktop/test/$i%03d.jpg") } | |
} | |
Await.result(Future.sequence(fut), 30 seconds) | |
println("END") | |
} | |
def getPhotos(ownerId: String, album_id: String) = executeCommand("photos.get", token, ("owner_id" -> ownerId), ("album_id" -> album_id)) | |
def extractBigPhotos(xml: String) = (xml \\ "photo").map(v => (v \ "src_xxbig").text) | |
def saveToFile(url: String, filename: String) = new URL(url) #> new File(filename) !! | |
def executeCommand(cmd: String, accessToken: String, params: (String, String)*) = { | |
val fmt = s"https://api.vkontakte.ru/method/$cmd.xml?access_token=$accessToken" + params.map { case (k, v) => k + "=" + v }.mkString("&", "&", "") | |
Source.fromURL(fmt).mkString | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment