Initial commit.
This commit is contained in:
commit
60c449feed
37 changed files with 1658 additions and 0 deletions
129
src/main/kotlin/net/thauvin/erik/isgd/Isgd.kt
Normal file
129
src/main/kotlin/net/thauvin/erik/isgd/Isgd.kt
Normal file
|
@ -0,0 +1,129 @@
|
|||
/*
|
||||
* Isgd.kt
|
||||
*
|
||||
* Copyright (c) 2020, Erik C. Thauvin (erik@thauvin.net)
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* Neither the name of this project nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software without
|
||||
* specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package net.thauvin.erik.isgd
|
||||
|
||||
import java.net.HttpURLConnection
|
||||
import java.net.URL
|
||||
import java.net.URLEncoder
|
||||
import java.nio.charset.StandardCharsets
|
||||
|
||||
/**
|
||||
* See the [is.gd API](https://is.gd/apishorteningreference.php)
|
||||
*/
|
||||
enum class Format(val type: String) {
|
||||
WEB("web"), SIMPLE("simple"), XML("xml"), JSON("json")
|
||||
}
|
||||
|
||||
fun String.encode(): String {
|
||||
return URLEncoder.encode(this, StandardCharsets.UTF_8.name())
|
||||
}
|
||||
|
||||
class Isgd private constructor() {
|
||||
companion object {
|
||||
private fun callApi(url: String): String {
|
||||
val connection = URL(url).openConnection() as HttpURLConnection
|
||||
connection.setRequestProperty(
|
||||
"User-Agent",
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)" +
|
||||
"Chrome/80.0.3987.149 Safari/537.36"
|
||||
)
|
||||
return connection.inputStream.bufferedReader().readText()
|
||||
}
|
||||
|
||||
private fun host(isVgd: Boolean = false): String {
|
||||
return if (isVgd) "v.gd" else "is.gd"
|
||||
}
|
||||
|
||||
/**
|
||||
* See the [is.gd API](https://is.gd/apilookupreference.php).
|
||||
*/
|
||||
@JvmStatic
|
||||
@JvmOverloads
|
||||
fun lookup(
|
||||
shorturl: String,
|
||||
callback: String = "",
|
||||
format: Format = Format.SIMPLE,
|
||||
isVgd: Boolean = false
|
||||
): String {
|
||||
if (shorturl.isEmpty()) {
|
||||
throw IllegalArgumentException("Please specify a valid short URL to lookup.")
|
||||
}
|
||||
|
||||
val sb = StringBuilder("https://${host(isVgd)}/forward.php?shorturl=${shorturl.encode()}")
|
||||
|
||||
if (callback.isNotEmpty()) {
|
||||
sb.append("&callback=${callback.encode()}")
|
||||
}
|
||||
|
||||
sb.append("&format=${format.type.encode()}")
|
||||
|
||||
return callApi(sb.toString())
|
||||
}
|
||||
|
||||
/**
|
||||
* See the [is.gd API](https://is.gd/apishorteningreference.php).
|
||||
*/
|
||||
@JvmStatic
|
||||
@JvmOverloads
|
||||
fun shorten(
|
||||
url: String,
|
||||
shorturl: String = "",
|
||||
callback: String = "",
|
||||
logstats: Boolean = false,
|
||||
format: Format = Format.SIMPLE,
|
||||
isVgd: Boolean = false
|
||||
): String {
|
||||
if (url.isEmpty()) {
|
||||
throw IllegalArgumentException("Please enter a valid URL to shorten.")
|
||||
}
|
||||
|
||||
val sb = StringBuilder("https://${host(isVgd)}/create.php?url=${url.encode()}")
|
||||
|
||||
if (shorturl.isNotEmpty()) {
|
||||
sb.append("&shorturl=${shorturl.encode()}")
|
||||
}
|
||||
|
||||
if (callback.isNotEmpty()) {
|
||||
sb.append("&callback=${callback.encode()}")
|
||||
}
|
||||
|
||||
if (logstats) {
|
||||
sb.append("&logstats=1")
|
||||
}
|
||||
|
||||
sb.append("&format=${format.type.encode()}")
|
||||
|
||||
return callApi(sb.toString())
|
||||
}
|
||||
}
|
||||
}
|
96
src/test/kotlin/net/thauvin/erik/isgd/IsgdTest.kt
Normal file
96
src/test/kotlin/net/thauvin/erik/isgd/IsgdTest.kt
Normal file
|
@ -0,0 +1,96 @@
|
|||
/*
|
||||
* IsgdTest.kt
|
||||
*
|
||||
* Copyright (c) 2020, Erik C. Thauvin (erik@thauvin.net)
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* Neither the name of this project nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software without
|
||||
* specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package net.thauvin.erik.isgd
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class IsgdTest {
|
||||
private val url = "https://www.example.com"
|
||||
private val shortUrl = "https://is.gd/Pt2sET"
|
||||
private val shortVgdUrl = "https://v.gd/2z2ncj"
|
||||
|
||||
@Test
|
||||
fun testLookupDefault() {
|
||||
assertEquals(url, Isgd.lookup(shortUrl))
|
||||
assertEquals(url, Isgd.lookup(shortVgdUrl, isVgd = true), "v.gd")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLookupJson() {
|
||||
assertEquals("{ \"url\": \"$url\" }", Isgd.lookup(shortUrl, format = Format.JSON))
|
||||
assertEquals(
|
||||
"test({ \"url\": \"$url\" });",
|
||||
Isgd.lookup(shortUrl, callback = "test", format = Format.JSON),
|
||||
"with callback"
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLookupXml() {
|
||||
assertEquals(
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><output><url>$url</url></output>",
|
||||
Isgd.lookup(shortUrl, format = Format.XML)
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testShortenDefault() {
|
||||
assertEquals(shortUrl, Isgd.shorten(url))
|
||||
assertEquals(shortVgdUrl, Isgd.shorten(url, isVgd = true), "v.gd")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testShortenJson() {
|
||||
assertEquals("{ \"shorturl\": \"$shortUrl\" }", Isgd.shorten(url, format = Format.JSON))
|
||||
assertEquals(
|
||||
"test({ \"shorturl\": \"$shortUrl\" });",
|
||||
Isgd.shorten(url, callback = "test", format = Format.JSON),
|
||||
"with callback"
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testShortenXml() {
|
||||
assertEquals(
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><output><shorturl>$shortUrl</shorturl></output>",
|
||||
Isgd.shorten(url, format = Format.XML)
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testShortenWeb() {
|
||||
assertTrue(Isgd.shorten(url, format = Format.WEB).contains(shortUrl))
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue