Skip to content

Commit 8024661

Browse files
committed
Add sponsors to readme automatization
1 parent 5e37d6c commit 8024661

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ Common code unit tests: [CommonTests.kt](common/src/commonTest/kotlin/CommonTest
8888

8989
The source code is licensed under the Apache 2.0 license and the copyright of the man pages are copyrighted by their respective authors.
9090

91+
### Public GitHub Sponsors
92+
93+
#### Monthly
94+
95+
<a href="https://github.com/vicenteserra"><img src="https://avatars.githubusercontent.com/u/10818763?s=60&v=4" width="50px" alt="vicenteserra" /></a>
96+
97+
#### Previous
98+
99+
<a href="https://github.com/Loh-San"><img src="https://avatars.githubusercontent.com/u/94805454?s=60&v=4" width="50px" alt="Loh-San" /></a>
100+
91101
### Thanks to
92102

93103
http://letsgokoyo.com - App Icon

websiteBuilder/src/main/kotlin/com/linuxcommandlibrary/desktop/MarkdownBuilder.kt

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package com.linuxcommandlibrary.desktop
22

33
import java.io.File
44
import java.io.PrintStream
5+
import java.net.HttpURLConnection
6+
import java.net.URI
7+
import org.json.JSONObject
58

69
/* Copyright 2022 Simon Schubert
710
*
@@ -135,6 +138,31 @@ class MarkdownBuilder {
135138
stream.appendLine()
136139
stream.appendLine("The source code is licensed under the Apache 2.0 license and the copyright of the man pages are copyrighted by their respective authors.")
137140

141+
// Sponsors
142+
val (currentSponsors, pastSponsors) = getPublicSponsors()
143+
if (currentSponsors.isNotEmpty() || pastSponsors.isNotEmpty()) {
144+
stream.appendLine()
145+
stream.appendLine("### Public GitHub Sponsors")
146+
if (currentSponsors.isNotEmpty()) {
147+
stream.appendLine()
148+
stream.appendLine("#### Monthly")
149+
stream.appendLine()
150+
currentSponsors.forEach { (username, avatar) ->
151+
stream.append("<a href=\"https://github.com/$username\"><img src=\"$avatar\" width=\"50px\" alt=\"$username\" /></a> ")
152+
}
153+
stream.appendLine()
154+
}
155+
if (pastSponsors.isNotEmpty()) {
156+
stream.appendLine()
157+
stream.appendLine("#### Previous")
158+
stream.appendLine()
159+
pastSponsors.forEach { (username, avatar) ->
160+
stream.append("<a href=\"https://github.com/$username\"><img src=\"$avatar\" width=\"50px\" alt=\"$username\" /></a> ")
161+
}
162+
stream.appendLine()
163+
}
164+
}
165+
138166
stream.appendLine()
139167
stream.appendLine("### Thanks to")
140168
stream.appendLine()
@@ -146,4 +174,29 @@ class MarkdownBuilder {
146174

147175
stream.close()
148176
}
177+
178+
private fun getPublicSponsors(): Pair<List<Pair<String, String>>, List<Pair<String, String>>> {
179+
return try {
180+
val connection = URI("https://ghs.vercel.app/v3/sponsors/SimonSchubert").toURL()
181+
.openConnection() as HttpURLConnection
182+
connection.connectTimeout = 5000
183+
connection.readTimeout = 5000
184+
185+
val response = JSONObject(connection.inputStream.bufferedReader().readText())
186+
val sponsors = response.getJSONObject("sponsors")
187+
188+
fun parseSponsors(array: org.json.JSONArray) = (0 until array.length()).map { i ->
189+
val sponsor = array.getJSONObject(i)
190+
Pair(sponsor.getString("username"), sponsor.getString("avatar"))
191+
}
192+
193+
Pair(
194+
parseSponsors(sponsors.getJSONArray("current")),
195+
parseSponsors(sponsors.getJSONArray("past")).take(10)
196+
)
197+
} catch (e: Exception) {
198+
println("Failed to fetch sponsors: ${e.message}")
199+
Pair(emptyList(), emptyList())
200+
}
201+
}
149202
}

0 commit comments

Comments
 (0)