@@ -2,6 +2,9 @@ package com.linuxcommandlibrary.desktop
22
33import java.io.File
44import 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