Skip to content

Commit a4ab9b6

Browse files
committed
fully working Sprite-Resource downloader
0 parents  commit a4ab9b6

File tree

13 files changed

+610
-0
lines changed

13 files changed

+610
-0
lines changed

.idea/artifacts/SpriteResourceDownloader_jar.xml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/compiler.xml

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 371 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SpriteResourceDownloader.iml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4" />
Binary file not shown.

pom.xml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>de.yochyo.sprite-resource-downloader</groupId>
8+
<artifactId>SpriteResourceDownloader</artifactId>
9+
<version>1.0</version>
10+
11+
<properties>
12+
<kotlin.version>1.3.40</kotlin.version>
13+
</properties>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.jsoup</groupId>
18+
<artifactId>jsoup</artifactId>
19+
<version>1.12.1</version>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.jetbrains.kotlin</groupId>
23+
<artifactId>kotlin-stdlib</artifactId>
24+
<version>${kotlin.version}</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.jetbrains.kotlin</groupId>
28+
<artifactId>kotlin-test</artifactId>
29+
<version>${kotlin.version}</version>
30+
<scope>test</scope>
31+
</dependency>
32+
</dependencies>
33+
34+
<build>
35+
<plugins>
36+
<plugin>
37+
<groupId>org.jetbrains.kotlin</groupId>
38+
<artifactId>kotlin-maven-plugin</artifactId>
39+
<version>${kotlin.version}</version>
40+
<executions>
41+
<execution>
42+
<id>compile</id>
43+
<phase>compile</phase>
44+
<goals>
45+
<goal>compile</goal>
46+
</goals>
47+
</execution>
48+
<execution>
49+
<id>test-compile</id>
50+
<phase>test-compile</phase>
51+
<goals>
52+
<goal>test-compile</goal>
53+
</goals>
54+
</execution>
55+
</executions>
56+
</plugin>
57+
</plugins>
58+
</build>
59+
60+
61+
</project>
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package de.yochyo.spriteresourcedownloader
2+
3+
import org.jsoup.Jsoup
4+
import org.jsoup.nodes.Document
5+
import java.io.File
6+
import java.lang.Exception
7+
import java.net.URL
8+
9+
lateinit var host: String
10+
fun main(args: Array<String>) {
11+
if (args.isNotEmpty()) {
12+
val builder = StringBuilder()
13+
for (a in args)
14+
builder.append(a)
15+
val url = builder.toString().filter { it != '"' && it != '\'' }
16+
host = "https://${URL(url).host}"
17+
val doc = Jsoup.connect(url).get()
18+
val sheetsNumber = getSheetsNumber(doc)
19+
val links = getLinksToSprite(doc)
20+
if(sheetsNumber != links.size) println("error in program, not all sprites are downloaded")
21+
var downloaded = 1
22+
val threadAmount = 5
23+
for(i in 0 until threadAmount){
24+
Thread{
25+
for(index in i until sheetsNumber step threadAmount){
26+
try {
27+
val doc = Jsoup.connect(host + links[index]).get()
28+
val bytes = downloadImageByUrl(doc)
29+
val name = getImageNameByUrl(doc)
30+
if(bytes != null && name != null){
31+
saveFile("${index+1} - $name", bytes)
32+
println("Downloaded $downloaded/$sheetsNumber")
33+
}else println("Error downloading $index")
34+
}catch (e: Exception){
35+
e.printStackTrace()
36+
}
37+
downloaded++
38+
}
39+
}.start()
40+
}
41+
42+
}
43+
}
44+
45+
fun getSheetsNumber(doc: Document): Int {
46+
var sheetsNumber = -1
47+
for (sheetRow in doc.getElementsByClass("altrow0")) {
48+
for (e in sheetRow.allElements)
49+
if (e.text() == "Sheets") {
50+
sheetsNumber = sheetRow.allElements.last().text().toInt()
51+
}
52+
}
53+
return sheetsNumber
54+
}
55+
56+
fun getLinksToSprite(doc: Document): ArrayList<String> {
57+
val links = ArrayList<String>(getSheetsNumber(doc))
58+
val spriteContainers = doc.getElementsByClass("updatesheeticons")
59+
for (item in spriteContainers) {
60+
for (i in item.getElementsByAttribute("style"))
61+
links += i.attr("href")
62+
}
63+
return links
64+
}
65+
66+
fun getImageUrl(doc: Document): String? {
67+
try {
68+
for (e in doc.allElements) {
69+
if (e.text() == "Download this Sheet"){
70+
val s = host + e.allElements.last().attr("href") //todo ist das hier jetzt der richtige link?
71+
return s
72+
}
73+
}
74+
}catch (e: Exception){}
75+
return null
76+
}
77+
78+
fun downloadImageByUrl(doc: Document): ByteArray? {
79+
try {
80+
val url = getImageUrl(doc)
81+
if (url != null) {
82+
try {
83+
val con = URL(url).openConnection()
84+
val stream = con.getInputStream()
85+
val bytes = stream.readBytes()
86+
stream.close()
87+
return bytes
88+
}catch (e: Exception){}
89+
}
90+
}catch (e: Exception){}
91+
return null
92+
}
93+
94+
fun getImageNameByUrl(doc: Document): String? {
95+
try {
96+
var name = ""
97+
for (meta in doc.getElementsByTag("meta")) {
98+
if (meta.attr("name") == "description")
99+
name = meta.attr("content")
100+
}
101+
if (name == "") return null
102+
103+
val index = name.indexOf(" - The #1 source for video game sprites on the internet!")
104+
return if (index != -1) name.substring(0, index)
105+
else name
106+
}catch (e: Exception){
107+
return null
108+
}
109+
}
110+
111+
fun saveFile(name: String, bytes: ByteArray){
112+
val file = File(nameToFilename(name))
113+
file.createNewFile()
114+
file.writeBytes(bytes)
115+
}
116+
117+
fun removeParameters(url: String): String{
118+
var url = url
119+
val questionMarkIndex = url.lastIndexOf("?")
120+
if(questionMarkIndex != -1)
121+
url = url.substring(0, questionMarkIndex)
122+
return url
123+
}
124+
fun getAbsoluteUrl(parent: String, child: String): String = parent + child
125+
126+
private fun nameToFilename(name: String): String {
127+
val s = name.filter { it != '/' && it != '\\' && it != '|' && it != ':' && it != '*' && it != '?' && it != '"' && it != '[' && it != ']' }
128+
var last = s.length
129+
if (last > 123) last = 123
130+
return s.substring(0, last) + ".png"
131+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Manifest-Version: 1.0
2+
Main-Class: de.yochyo.spriteresourcedownloader.MainKt
3+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Manifest-Version: 1.0
2+
Main-Class: de.yochyo.spriteresourcedownloader.MainKt
3+

0 commit comments

Comments
 (0)