Skip to content

Commit

Permalink
feat: URLに関する置換処理を作成 (#31)
Browse files Browse the repository at this point in the history
* feat: replacerをobjectに切り出し

* feat: URLに関する置換処理を作成

* fix: とりあえず一部review対応

* Update src/main/kotlin/com/jaoafa/vcspeaker/voicetext/textreplacers/UrlReplacer.kt

Co-authored-by: yuuaHP <identity@yuua.dev>

* Update src/main/kotlin/com/jaoafa/vcspeaker/voicetext/textreplacers/UrlReplacer.kt

Co-authored-by: yuuaHP <identity@yuua.dev>

* Update src/main/kotlin/com/jaoafa/vcspeaker/voicetext/textreplacers/UrlReplacer.kt

Co-authored-by: yuuaHP <identity@yuua.dev>

* Update src/main/kotlin/com/jaoafa/vcspeaker/voicetext/textreplacers/UrlReplacer.kt

Co-authored-by: yuuaHP <identity@yuua.dev>

* Update src/main/kotlin/com/jaoafa/vcspeaker/voicetext/textreplacers/UrlReplacer.kt

Co-authored-by: yuuaHP <identity@yuua.dev>

* Update src/main/kotlin/com/jaoafa/vcspeaker/voicetext/textreplacers/UrlReplacer.kt

Co-authored-by: yuuaHP <identity@yuua.dev>

* Update src/main/kotlin/com/jaoafa/vcspeaker/voicetext/textreplacers/UrlReplacer.kt

Co-authored-by: yuuaHP <identity@yuua.dev>

* Update src/main/kotlin/com/jaoafa/vcspeaker/voicetext/textreplacers/UrlReplacer.kt

Co-authored-by: yuuaHP <identity@yuua.dev>

* Update src/main/kotlin/com/jaoafa/vcspeaker/voicetext/textreplacers/UrlReplacer.kt

Co-authored-by: yuuaHP <identity@yuua.dev>

* Update src/main/kotlin/com/jaoafa/vcspeaker/tools/Emoji.kt

Co-authored-by: yuuaHP <identity@yuua.dev>

* Apply suggestions from code review

Co-authored-by: yuuaHP <identity@yuua.dev>

* Update src/main/kotlin/com/jaoafa/vcspeaker/tools/Twitter.kt

Co-authored-by: yuuaHP <identity@yuua.dev>

* Update src/main/kotlin/com/jaoafa/vcspeaker/voicetext/textreplacers/UrlReplacer.kt

Co-authored-by: yuuaHP <identity@yuua.dev>

* Update src/main/kotlin/com/jaoafa/vcspeaker/voicetext/textreplacers/UrlReplacer.kt

Co-authored-by: yuuaHP <identity@yuua.dev>

* fix: renamed file

* fix: models/originalをパッケージで分類するように

* feat: isThreadを拡張関数に変更

---------

Co-authored-by: yuuaHP <identity@yuua.dev>
  • Loading branch information
book000 and yuuahp authored Oct 12, 2023
1 parent 5c61376 commit aaeb1be
Show file tree
Hide file tree
Showing 18 changed files with 844 additions and 116 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ dependencies {
implementation("io.ktor:ktor-client-core:2.2.4")
implementation("io.ktor:ktor-client-cio:2.2.4")
implementation("io.ktor:ktor-client-cio-jvm:2.2.4")
implementation("io.ktor:ktor-client-content-negotiation:2.3.5")
implementation("io.sentry:sentry:6.30.0")
implementation("net.htmlparser.jericho:jericho-html:3.4")
}

tasks.test {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.jaoafa.vcspeaker.models.original.discord

import dev.kord.common.entity.Snowflake

data class DiscordInvite(
val code: String,
val guildId: Snowflake,
val guildName: String,
val channelId: Snowflake,
val channelName: String,
val inviterId: Snowflake?,
val inviterName: String?,
val eventId: Snowflake?,
val eventName: String?,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.jaoafa.vcspeaker.models.original.twitter

data class Tweet(
var authorName: String,
var html: String,
var plainText: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package com.jaoafa.vcspeaker.models.response.discord

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class DiscordGetInviteResponse(
val type: Long,
val code: String,
val inviter: Inviter,
@SerialName("expires_at")
val expiresAt: String,
val flags: Long,
val guild: Guild,
@SerialName("guild_id")
val guildId: String,
val channel: Channel,
@SerialName("guild_scheduled_event")
val guildScheduledEvent: GuildScheduledEvent? = null,
@SerialName("approximate_member_count")
val approximateMemberCount: Long,
@SerialName("approximate_presence_count")
val approximatePresenceCount: Long,
)

@Serializable
data class AvatarDecorationData(
val type: Long,
val id: String,
)

@Serializable
data class Inviter(
val id: String,
val username: String,
val avatar: String,
val discriminator: String,
@SerialName("public_flags")
val publicFlags: Long,
val flags: Long,
val banner: String? = null,
@SerialName("accent_color")
val accentColor: Long? = null,
@SerialName("global_name")
val globalName: String,
@SerialName("avatar_decoration_data")
val avatarDecorationData: AvatarDecorationData? = null,
@SerialName("banner_color")
val bannerColor: Long? = null,
)

@Serializable
data class Guild(
val id: String,
val name: String,
val splash: String,
val banner: String,
val description: String? = null,
val icon: String,
val features: List<String>,
@SerialName("verification_level")
val verificationLevel: Long,
@SerialName("vanity_url_code")
val vanityUrlCode: String? = null,
@SerialName("nsfw_level")
val nsfwLevel: Long,
val nsfw: Boolean,
@SerialName("premium_subscription_count")
val premiumSubscriptionCount: Long,
)

@Serializable
data class Channel(
val id: String,
val type: Long,
val name: String,
)

@Serializable
data class GuildScheduledEvent(
val id: String,
@SerialName("guild_id")
val guildId: String,
val name: String,
val description: String,
@SerialName("channel_id")
val channelId: String? = null,
@SerialName("creator_id")
val creatorId: String,
val image: String,
@SerialName("scheduled_start_time")
val scheduledStartTime: String,
@SerialName("scheduled_end_time")
val scheduledEndTime: String,
val status: Long,
@SerialName("entity_type")
val entityType: Long,
@SerialName("entity_id")
val entityId: String? = null,
@SerialName("recurrence_rule")
val recurrenceRule: String? = null,
@SerialName("user_count")
val userCount: Long,
@SerialName("privacy_level")
val privacyLevel: Long,
@SerialName("sku_ids")
val skuIds: List<String>,
@SerialName("auto_start")
val autoStart: Boolean,
@SerialName("entity_metadata")
val entityMetadata: EntityMetadata,
)

@Serializable
data class EntityMetadata(
val location: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.jaoafa.vcspeaker.models.response.twitter

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class TwitterOEmbedResponse(
val url: String,
@SerialName("author_name")
val authorName: String,
@SerialName("author_url")
val authorUrl: String,
val html: String,
val width: Long,
val height: Long? = null,
val type: String,
@SerialName("cache_age")
val cacheAge: String,
@SerialName("provider_name")
val providerName: String,
@SerialName("provider_url")
val providerUrl: String,
val version: String,
)
8 changes: 8 additions & 0 deletions src/main/kotlin/com/jaoafa/vcspeaker/tools/Discord.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.kotlindiscord.kord.extensions.types.respond
import com.kotlindiscord.kord.extensions.utils.respond
import com.kotlindiscord.kord.extensions.utils.selfMember
import dev.kord.common.Color
import dev.kord.common.entity.ChannelType
import dev.kord.common.entity.Snowflake
import dev.kord.core.behavior.GuildBehavior
import dev.kord.core.behavior.MemberBehavior
Expand Down Expand Up @@ -237,4 +238,11 @@ object Discord {
suspend fun ChatCommandContext<out Arguments>.respond(content: String) = message.respond(content)

suspend fun BaseVoiceChannelBehavior.name() = this.asChannel().name

fun Channel.isThread() =
listOf(
ChannelType.PrivateThread,
ChannelType.PublicGuildThread,
ChannelType.PublicNewsThread
).contains(type)
}
4 changes: 4 additions & 0 deletions src/main/kotlin/com/jaoafa/vcspeaker/tools/Emoji.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ object Emoji {
return emojis.filter { startsWith(it.unicode) }.maxByOrNull { it.unicode.length }!!
}

fun String.removeEmojis() = emojis.fold(this) { replaced, emoji ->
replaced.replace(emoji.unicode, "")
}

fun String.replaceEmojiToName(): String {
return emojis.fold(this) { replaced, emoji ->
replaced.replace(emoji.unicode, emoji.name)
Expand Down
50 changes: 50 additions & 0 deletions src/main/kotlin/com/jaoafa/vcspeaker/tools/Twitter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.jaoafa.vcspeaker.tools

import com.jaoafa.vcspeaker.models.original.twitter.Tweet
import com.jaoafa.vcspeaker.models.response.twitter.TwitterOEmbedResponse
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.engine.cio.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.request.*
import io.ktor.http.*
import io.ktor.serialization.kotlinx.json.*
import kotlinx.serialization.json.Json
import net.htmlparser.jericho.Source

object Twitter {
private const val baseURL = "https://publish.twitter.com/oembed"
private val client = HttpClient(CIO) {
install(ContentNegotiation) {
json(Json {
ignoreUnknownKeys = true
coerceInputValues = true
})
}
}

suspend fun getTweet(screenName: String, tweetId: String): Tweet? {
val tweetUrl = "https://twitter.com/$screenName/status/$tweetId"
val response = client.get(baseURL) {
parameter("url", tweetUrl)
}

return when (response.status) {
HttpStatusCode.OK -> {
val json: TwitterOEmbedResponse = response.body()
Tweet(
json.authorName,
json.html,
Source(json.html.replace("<a.*>(.*)</a>", ""))
.getFirstElement("p")
.renderer
.setMaxLineLength(Integer.MAX_VALUE)
.setNewLine(null)
.toString()
)
}

else -> null
}
}
}
Loading

0 comments on commit aaeb1be

Please sign in to comment.