-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fully working Sprite-Resource downloader
- Loading branch information
0 parents
commit a4ab9b6
Showing
13 changed files
with
610 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="JAVA_MODULE" version="4" /> |
Binary file added
BIN
+1.8 MB
out/artifacts/SpriteResourceDownloader_jar/SpriteResourceDownloader.jar
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>de.yochyo.sprite-resource-downloader</groupId> | ||
<artifactId>SpriteResourceDownloader</artifactId> | ||
<version>1.0</version> | ||
|
||
<properties> | ||
<kotlin.version>1.3.40</kotlin.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.jsoup</groupId> | ||
<artifactId>jsoup</artifactId> | ||
<version>1.12.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jetbrains.kotlin</groupId> | ||
<artifactId>kotlin-stdlib</artifactId> | ||
<version>${kotlin.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jetbrains.kotlin</groupId> | ||
<artifactId>kotlin-test</artifactId> | ||
<version>${kotlin.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.jetbrains.kotlin</groupId> | ||
<artifactId>kotlin-maven-plugin</artifactId> | ||
<version>${kotlin.version}</version> | ||
<executions> | ||
<execution> | ||
<id>compile</id> | ||
<phase>compile</phase> | ||
<goals> | ||
<goal>compile</goal> | ||
</goals> | ||
</execution> | ||
<execution> | ||
<id>test-compile</id> | ||
<phase>test-compile</phase> | ||
<goals> | ||
<goal>test-compile</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
|
||
</project> |
131 changes: 131 additions & 0 deletions
131
src/main/java/de/yochyo/spriteresourcedownloader/Main.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package de.yochyo.spriteresourcedownloader | ||
|
||
import org.jsoup.Jsoup | ||
import org.jsoup.nodes.Document | ||
import java.io.File | ||
import java.lang.Exception | ||
import java.net.URL | ||
|
||
lateinit var host: String | ||
fun main(args: Array<String>) { | ||
if (args.isNotEmpty()) { | ||
val builder = StringBuilder() | ||
for (a in args) | ||
builder.append(a) | ||
val url = builder.toString().filter { it != '"' && it != '\'' } | ||
host = "https://${URL(url).host}" | ||
val doc = Jsoup.connect(url).get() | ||
val sheetsNumber = getSheetsNumber(doc) | ||
val links = getLinksToSprite(doc) | ||
if(sheetsNumber != links.size) println("error in program, not all sprites are downloaded") | ||
var downloaded = 1 | ||
val threadAmount = 5 | ||
for(i in 0 until threadAmount){ | ||
Thread{ | ||
for(index in i until sheetsNumber step threadAmount){ | ||
try { | ||
val doc = Jsoup.connect(host + links[index]).get() | ||
val bytes = downloadImageByUrl(doc) | ||
val name = getImageNameByUrl(doc) | ||
if(bytes != null && name != null){ | ||
saveFile("${index+1} - $name", bytes) | ||
println("Downloaded $downloaded/$sheetsNumber") | ||
}else println("Error downloading $index") | ||
}catch (e: Exception){ | ||
e.printStackTrace() | ||
} | ||
downloaded++ | ||
} | ||
}.start() | ||
} | ||
|
||
} | ||
} | ||
|
||
fun getSheetsNumber(doc: Document): Int { | ||
var sheetsNumber = -1 | ||
for (sheetRow in doc.getElementsByClass("altrow0")) { | ||
for (e in sheetRow.allElements) | ||
if (e.text() == "Sheets") { | ||
sheetsNumber = sheetRow.allElements.last().text().toInt() | ||
} | ||
} | ||
return sheetsNumber | ||
} | ||
|
||
fun getLinksToSprite(doc: Document): ArrayList<String> { | ||
val links = ArrayList<String>(getSheetsNumber(doc)) | ||
val spriteContainers = doc.getElementsByClass("updatesheeticons") | ||
for (item in spriteContainers) { | ||
for (i in item.getElementsByAttribute("style")) | ||
links += i.attr("href") | ||
} | ||
return links | ||
} | ||
|
||
fun getImageUrl(doc: Document): String? { | ||
try { | ||
for (e in doc.allElements) { | ||
if (e.text() == "Download this Sheet"){ | ||
val s = host + e.allElements.last().attr("href") //todo ist das hier jetzt der richtige link? | ||
return s | ||
} | ||
} | ||
}catch (e: Exception){} | ||
return null | ||
} | ||
|
||
fun downloadImageByUrl(doc: Document): ByteArray? { | ||
try { | ||
val url = getImageUrl(doc) | ||
if (url != null) { | ||
try { | ||
val con = URL(url).openConnection() | ||
val stream = con.getInputStream() | ||
val bytes = stream.readBytes() | ||
stream.close() | ||
return bytes | ||
}catch (e: Exception){} | ||
} | ||
}catch (e: Exception){} | ||
return null | ||
} | ||
|
||
fun getImageNameByUrl(doc: Document): String? { | ||
try { | ||
var name = "" | ||
for (meta in doc.getElementsByTag("meta")) { | ||
if (meta.attr("name") == "description") | ||
name = meta.attr("content") | ||
} | ||
if (name == "") return null | ||
|
||
val index = name.indexOf(" - The #1 source for video game sprites on the internet!") | ||
return if (index != -1) name.substring(0, index) | ||
else name | ||
}catch (e: Exception){ | ||
return null | ||
} | ||
} | ||
|
||
fun saveFile(name: String, bytes: ByteArray){ | ||
val file = File(nameToFilename(name)) | ||
file.createNewFile() | ||
file.writeBytes(bytes) | ||
} | ||
|
||
fun removeParameters(url: String): String{ | ||
var url = url | ||
val questionMarkIndex = url.lastIndexOf("?") | ||
if(questionMarkIndex != -1) | ||
url = url.substring(0, questionMarkIndex) | ||
return url | ||
} | ||
fun getAbsoluteUrl(parent: String, child: String): String = parent + child | ||
|
||
private fun nameToFilename(name: String): String { | ||
val s = name.filter { it != '/' && it != '\\' && it != '|' && it != ':' && it != '*' && it != '?' && it != '"' && it != '[' && it != ']' } | ||
var last = s.length | ||
if (last > 123) last = 123 | ||
return s.substring(0, last) + ".png" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Manifest-Version: 1.0 | ||
Main-Class: de.yochyo.spriteresourcedownloader.MainKt | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Manifest-Version: 1.0 | ||
Main-Class: de.yochyo.spriteresourcedownloader.MainKt | ||
|
Binary file not shown.
Binary file added
BIN
+3.07 KB
target/classes/de/yochyo/spriteresourcedownloader/MainKt$main$1.class
Binary file not shown.
Binary file not shown.