Skip to content

Commit

Permalink
fully working Sprite-Resource downloader
Browse files Browse the repository at this point in the history
  • Loading branch information
Yochyo committed Jun 23, 2019
0 parents commit a4ab9b6
Show file tree
Hide file tree
Showing 13 changed files with 610 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .idea/artifacts/SpriteResourceDownloader_jar.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

371 changes: 371 additions & 0 deletions .idea/workspace.xml

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions SpriteResourceDownloader.iml
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 not shown.
61 changes: 61 additions & 0 deletions pom.xml
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 src/main/java/de/yochyo/spriteresourcedownloader/Main.kt
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"
}
3 changes: 3 additions & 0 deletions src/main/resources/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: de.yochyo.spriteresourcedownloader.MainKt

3 changes: 3 additions & 0 deletions target/classes/META-INF/MANIFEST.MF
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 not shown.
Binary file not shown.

0 comments on commit a4ab9b6

Please sign in to comment.