Skip to content

Instantly share code, notes, and snippets.

@AvatarQing
Last active August 29, 2015 14:10
Show Gist options
  • Save AvatarQing/d4c13a0ee280f147b3cc to your computer and use it in GitHub Desktop.
Save AvatarQing/d4c13a0ee280f147b3cc to your computer and use it in GitHub Desktop.
http://2048.malash.net/jiaoutiancheng
import org.codehaus.groovy.runtime.StringBufferWriter;
def downloadSingleImage(String imageUrl, String savePath) {
try {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
DataInputStream is = new DataInputStream(connection.getInputStream());
FileOutputStream fos=new FileOutputStream(savePath)
DataOutputStream os = new DataOutputStream(fos);
byte[] buffer = new byte[4096];
int count = 0;
while ((count = is.read(buffer)) > 0) {
os.write(buffer, 0, count);
}
os.close();
is.close();
connection.disconnect();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
def getRemoteText(String remoteUrl) {
try {
URL url = new URL(remoteUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
BufferedReader is = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = is.readLine()) != null){
buffer.append(line+"\n");
}
is.close();
connection.disconnect();
return buffer.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
def downloadImages(styleCssUrl,saveFolderPath){
// def styleCssUrl="http://2048.malash.net/custom/style.css?jiaoutiancheng"
def text=getRemoteText(styleCssUrl)
def pattern= /\/upload\/.*png/
def matcher= text=~pattern
def host="http://2048.malash.net"
matcher.eachWithIndex { element, i ->
def fileNamePreffix="cell_rectangle_"
def number=Math.pow(2, i+1) as int
def fileName= "${fileNamePreffix}${number}.png"
def imageUrl="${host}${element}"
def savePath="${saveFolderPath}${File.separator}${fileName}"
new File(saveFolderPath).mkdirs()
downloadSingleImage(imageUrl,savePath)
println savePath
}
}
def parseStyleCssUrl(String gameUrl){
def gameName= gameUrl.substring(1+gameUrl.lastIndexOf("/"))
def styleCssUrl="http://2048.malash.net/custom/style.css?${gameName}"
}
// command line interface builder
def cli = new CliBuilder()
cli.h( longOpt: 'help', required: false, 'Show usage information' )
cli.u( longOpt: 'url', argName: 'url', required: true, args: 1, '2048 game url (e.g. http://2048.malash.net/jiaoutiancheng)' )
cli.o( longOpt: 'outdir', argName: 'outdir', required: true, args: 1, 'Image saved directory path (e.g. D:\\\\drawable-mdpi)' )
def opt = cli.parse(args)
if (!opt) { return }
if (opt.h) {
cli.usage();
return
}
def styleCssUrl
def saveDir
if(opt.u) {
println 'you use option -u, value:'+opt.u
styleCssUrl=parseStyleCssUrl(opt.u)
}
if(opt.o) {
println 'you use option -o, value:'+opt.o
saveDir=opt.o
}
downloadImages(styleCssUrl,saveDir)
@echo off
set appInfoFile=%~dp0gameurl.txt
set url=
set saveDir=%~dp0drawable-mdpi
for /f "tokens=1 delims==" %%i in (%appInfoFile%) do (
set url=%%i
)
groovy ImageDownloader -u %url% -o %saveDir%
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment