Skip to content

Instantly share code, notes, and snippets.

@marc0der
Created April 5, 2014 16:59
Show Gist options
  • Save marc0der/9994607 to your computer and use it in GitHub Desktop.
Save marc0der/9994607 to your computer and use it in GitHub Desktop.
A wallpaper downloader using Geb
@Grab("org.codehaus.geb:geb-core:0.7.2")
@Grab("org.seleniumhq.selenium:selenium-chrome-driver:2.32.0")
import geb.*
import static java.net.URLDecoder.decode
def images = new HashSet()
def userHome = System.getProperty('user.home')
def pictureFolder = "${userHome}/Pictures"
def history = new File("${userHome}/.chromecast")
if(history.exists()) history.eachLine { images.add it }
println "Loaded up ${images.size()} images from ${history.toString()}"
System.setProperty('webdriver.chrome.driver', "${userHome}/bin/chromedriver")
def extractUrlFrom = { attr ->
attr[0] ? decode(attr[0].tokenize("' + '")[4]) : ""
}
def correctResolution = {
it?.replace('s1280', 's1920').replace('w1280', 'w1920').replace('h720', 'h1080')
}
def download = { address ->
def fileName = address.tokenize('/')[-1]
def file = new FileOutputStream("${pictureFolder}/${fileName}")
def out = new BufferedOutputStream(file)
out << new URL(address).openStream()
out.close()
}
Browser.drive {
go "https://clients3.google.com/cast/chromecast/home"
while(true) {
def attribute = ($(".S9aygc-AHe6Kc")*."@ng-style")
def imageURL = correctResolution(extractUrlFrom(attribute))
if(imageURL && !images.contains(imageURL)) {
history.append("${imageURL}\n")
images.add(imageURL)
print "Downloading image (#${images.size()}) : $imageURL"
download(imageURL)
println " ...done..."
}
sleep 10000
}
}
@marc0der
Copy link
Author

marc0der commented Apr 5, 2014

Make sure you have chromedriver at the prescribed location in order for this to work. Also check that all other folders are available on your platform before running this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment