Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
TalbotGooday committed Nov 14, 2022
1 parent 4b2a009 commit ffeb478
Show file tree
Hide file tree
Showing 26 changed files with 266 additions and 115 deletions.
28 changes: 22 additions & 6 deletions app/src/main/java/com/gapps/videonoapi/ui/base/BaseActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.net.Uri
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.gapps.library.api.models.video.VideoPreviewModel
import com.gapps.library.ui.bottom_menu.BottomVideoController
import com.gapps.videonoapi.R
Expand Down Expand Up @@ -32,15 +33,30 @@ abstract class BaseActivity : AppCompatActivity() {
setSize(model.width, model.height)
setTitle(title)
setVideoUrl(initUrl)
setBackgroundColor(R.color.colorBackground)
setTextColor(R.color.colorHostName)
setTitleColor(R.color.colorVideoTitle)
setBackgroundColor(ContextCompat.getColor(this@BaseActivity, R.color.colorBackground))
setTextColor(ContextCompat.getColor(this@BaseActivity, R.color.colorHostName))
setTitleColor(ContextCompat.getColor(this@BaseActivity, R.color.colorVideoTitle))
setLeftButtonText(R.string.vna_close)
setRightButtonText(R.string.vna_open_in)
setRightButtonTextColor(R.color.colorVideoTitle)
setLeftButtonTextColor(R.color.colorVideoTitle)
setRightButtonTextColor(
ContextCompat.getColor(
this@BaseActivity,
R.color.colorVideoTitle
)
)
setLeftButtonTextColor(
ContextCompat.getColor(
this@BaseActivity,
R.color.colorVideoTitle
)
)
setCenterButtonIcon(R.drawable.ic_vna_content_copy)
setCenterButtonIconTint(R.color.colorVideoTitle)
setCenterButtonIconTint(
ContextCompat.getColor(
this@BaseActivity,
R.color.colorVideoTitle
)
)
setProgressView(TextView(this@BaseActivity).apply {
text = "Loading"; setTextColor(-1)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class MainActivity : BaseActivity() {
"https://streamable.com/s0phr",
"https://vimeo.com/259411563",
"https://rutube.ru/video/d70e62b44b8893e98e3e90a6e2c9fcd4/?pl_type=source&pl_id=18265",
"https://www.facebook.com/kinodizi/videos/965220097161488",
"https://www.facebook.com/watch?v=795751214848051",
"https://www.dailymotion.com/video/x5sxbmb",
"https://dave.wistia.com/medias/0k5h1g1chs/",
"https://vzaar.com/videos/401431",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import coil.load
import com.gapps.library.api.VideoService
import com.gapps.library.api.models.api.*
import com.gapps.library.api.models.api.builder.EmbeddingRequest
import com.gapps.library.api.models.video.VideoPreviewModel
import com.gapps.videonoapi.R
import com.gapps.videonoapi.utils.extensions.collapse
Expand Down Expand Up @@ -91,7 +93,7 @@ class VideoAdapter(

if (loadedDataItem == null) {
videoService.loadVideoPreview(
url = item,
request = createRequestBuilder(item),
onSuccess = { video ->
loadedData.put(bindingAdapterPosition, video)
loadedDataItem = video
Expand Down Expand Up @@ -177,21 +179,21 @@ class VideoAdapter(
type ?: return

val icon = when (type) {
"YouTube" -> R.drawable.youtube
"YouTube Music" -> R.drawable.youtube_music
"Vimeo" -> R.drawable.vimeo
"Rutube" -> R.drawable.rutube
"Facebook" -> R.drawable.ic_fb
"Dailymotion" -> R.drawable.dailymotion
"Wistia" -> R.drawable.ic_wistia
"Vzaar" -> R.drawable.ic_vzaar
"Hulu" -> R.drawable.hulu
"Ustream" -> R.drawable.ibm
"Ted Talks" -> R.drawable.ted_talks
"Coub" -> R.drawable.ic_coub
"Ultimedia" -> R.drawable.ultimedia
"Streamable" -> R.drawable.streamable
"Loom" -> R.drawable.loom
YOUTUBE_HOST_NAME -> R.drawable.youtube
YOUTUBE_MUSIC_HOST_NAME -> R.drawable.youtube_music
VIMEO_HOST_NAME -> R.drawable.vimeo
RUTUBE_HOST_NAME -> R.drawable.rutube
FACEBOOK_HOST_NAME -> R.drawable.ic_fb
DAILYMOTION_HOST_NAME -> R.drawable.dailymotion
WISTIA_HOST_NAME -> R.drawable.ic_wistia
VZAAR_HOST_NAME -> R.drawable.ic_vzaar
HULU_HOST_NAME -> R.drawable.hulu
USTREAM_HOST_NAME -> R.drawable.ibm
TED_TALKS_HOST_NAME -> R.drawable.ted_talks
COUB_HOST_NAME -> R.drawable.ic_coub
ULTIMEDIA_HOST_NAME -> R.drawable.ultimedia
STREAMABLE_HOST_NAME -> R.drawable.streamable
LOOM_HOST_NAME -> R.drawable.loom
else -> R.drawable.ic_video
}

Expand All @@ -202,4 +204,14 @@ class VideoAdapter(
interface Listener {
fun onItemClick(item: VideoPreviewModel)
}
}

fun createRequestBuilder(url: String) = EmbeddingRequest.build {
setUrl(url)
headers {
host(FACEBOOK_HOST_NAME) {
add("access_token", "2940857442908969|0733ab34586cc8a92080dc1b1d1e6971")
addAll(mapOf())
}
}
}
8 changes: 3 additions & 5 deletions embedded_video_lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ android {
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

//AndroidX
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.core:core-ktx:1.7.0'
api 'androidx.appcompat:appcompat:1.4.1'
api 'com.google.android.material:material:1.5.0'
api 'androidx.core:core-ktx:1.7.0'

//Gson
api 'com.google.code.gson:gson:2.8.7'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ internal class VideoLoadHelper(
) {
val videoInfoModel = requestModel.videoInfoModel
val originalUrl = requestModel.originalUrl
val headers = requestModel.requestHeaders

if (videoInfoModel == null) {
onError(originalUrl, ERROR_1)
Expand Down Expand Up @@ -73,7 +74,7 @@ internal class VideoLoadHelper(
}

val jsonBody = withContext(Dispatchers.IO) {
makeCallGetBody(client, finalUrl)
makeCallGetBody(client, finalUrl, headers)
}

if (isLogEnabled) {
Expand Down Expand Up @@ -112,9 +113,19 @@ internal class VideoLoadHelper(
}
}

private fun makeCallGetBody(client: OkHttpClient, url: String): JsonElement? =
private fun makeCallGetBody(
client: OkHttpClient,
url: String,
headers: Map<String, String>
): JsonElement? =
runBlocking {
val response = client.newCall(Request.Builder().url(url).build()).execute()
val requestBuilder = Request.Builder().url(url)

headers.entries.forEach { header ->
requestBuilder.addHeader(header.key, header.value)
}

val response = client.newCall(requestBuilder.build()).execute()
val stringBody = response.body?.string() ?: return@runBlocking null
val jsonObject = parseString(stringBody)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class VideoService private constructor(
StreamableVideoInfoModel()
)

val videoPatterns
get() = videoInfoModelsList.map { it.pattern }

inline fun build(block: Builder.() -> Unit) = Builder().apply(block).build()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ open class CoubVideoInfoModel : VideoInfoModel<CoubResponse>() {

//https://regex101.com/r/ZoQVLa/1
override val pattern: String
get() = "(?:http[s]?:\\/\\/)?(?:www)?\\.?coub\\.com\\/(?:embed|view|api)\\/([_a-zA-Z0-9]+)[^,;\\s]*"
get() = COUB_PATTERN

override val idPattern: String
get() = pattern
Expand All @@ -20,7 +20,7 @@ open class CoubVideoInfoModel : VideoInfoModel<CoubResponse>() {
get() = CoubResponse::class.java

override val hostingName: String
get() = "Coub"
get() = COUB_HOST_NAME

override fun getInfoUrl(incomingUrl: String?): String? {
incomingUrl ?: return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ open class DailymotionVideoInfoModel : VideoInfoModel<DailymotionResponse>() {
get() = "https://www.dailymotion.com"

override val pattern: String
get() = "(?:http[s]?://)?(?:www\\.)?(?:(?:dailymotion\\.com(?:/embed)?/video)|dai\\.ly)/([a-zA-Z0-9]+)[^,;\\s]*"

get() = DAILYMOTION_PATTERN
override val idPattern: String
get() = pattern

override val type: Class<DailymotionResponse> = DailymotionResponse::class.java

override val hostingName: String
get() = "Dailymotion"
get() = DAILYMOTION_HOST_NAME

override fun getInfoUrl(incomingUrl: String?): String? {
incomingUrl ?: return null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
package com.gapps.library.api.models.api

import com.gapps.library.api.FORMAT_JSON
import com.gapps.library.api.URL
import com.gapps.library.api.models.api.base.VideoInfoModel
import com.gapps.library.api.models.video.facebook.FacebookResponse

open class FacebookVideoInfoModel : VideoInfoModel<FacebookResponse>() {
override val baseUrl: String
get() = "https://apps.facebook.com"
get() = "https://graph.facebook.com/v15.0/"

//Pattern: https://regex101.com/r/98Nfkr/5
//Pattern: https://regex101.com/r/98Nfkr/6
override val pattern: String
get() = "(?:http[s]?://)?(?:www.|web.|m.)?(?:facebook|fb)?.com/(?:(?:video.php|watch?/)?\\?v=|.+/videos(?:/.+)?/)(\\d+)[^,;\\s]*"
get() = FACEBOOK_PATTERN
override val idPattern: String
get() = pattern
override val type: Class<FacebookResponse>
get() = FacebookResponse::class.java
override val hostingName: String
get() = "Facebook"
get() = FACEBOOK_HOST_NAME

override fun getInfoUrl(incomingUrl: String?): String? {
val id = parseVideoId(incomingUrl)
return "$baseUrl/plugins/video/oembed.$FORMAT_JSON?$URL=https://www.facebook.com/facebook/videos/$id"
return "$baseUrl/oembed_page?$URL=https://www.facebook.com/facebook/videos/$id"
}

override fun getPlayLink(videoId: String): String {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@file:JvmName("HostConstants")
package com.gapps.library.api.models.api

const val COUB_HOST_NAME ="Coub"
const val DAILYMOTION_HOST_NAME = "Dailymotion"
const val FACEBOOK_HOST_NAME ="Facebook"
const val HULU_HOST_NAME = "Hulu"
const val LOOM_HOST_NAME = "Loom"
const val RUTUBE_HOST_NAME = "Rutube"
const val STREAMABLE_HOST_NAME = "Streamable"
const val TED_TALKS_HOST_NAME = "Ted Talks"
const val TIKTOK_HOST_NAME = "TikTok"
const val ULTIMEDIA_HOST_NAME = "Ultimedia"
const val USTREAM_HOST_NAME = "Ustream"
const val VIMEO_HOST_NAME = "Vimeo"
const val VZAAR_HOST_NAME = "Vzaar"
const val WISTIA_HOST_NAME = "Wistia"
const val YOUTUBE_MUSIC_HOST_NAME = "Youtube Music"
const val YOUTUBE_HOST_NAME = "YouTube"
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ open class HuluVideoInfoModel : VideoInfoModel<HuluResponse>() {

//https://regex101.com/r/LORZgZ/2
override val pattern: String
get() = "(?:http[s]?:\\/\\/)?(?:www.)?hulu\\.(?:(?:com\\/\\S*(?:w(?:atch)?|eid)(?:\\/|=)?)|(?:tv\\/))?([a-zA-Z0-9]+)[^,;\\s]*"
get() = HULU_PATTERN
override val idPattern: String
get() = pattern
override val type: Class<HuluResponse>
get() = HuluResponse::class.java
override val hostingName: String
get() = "Hulu"
get() = HULU_HOST_NAME

override fun getInfoUrl(incomingUrl: String?): String? {
val id = parseVideoId(incomingUrl)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ open class LoomVideoInfoModel : VideoInfoModel<LoomResponse>() {

//https://regex101.com/r/0TwCJy/1
override val pattern: String
get() = "(?:http[s]?:\\/\\/)?(?:www)?\\.?(?:use)?loom\\.com\\/(?:share|default|api)\\/([_a-zA-Z0-9]+)\\S*"
get() = LOOM_PATTERN
override val idPattern: String
get() = pattern
override val type: Class<LoomResponse>
get() = LoomResponse::class.java
override val hostingName: String
get() = "Loom"
get() = LOOM_HOST_NAME

override fun getInfoUrl(incomingUrl: String?): String? {
return "$baseUrl/v1/oembed?$FORMAT=$FORMAT_JSON&$URL=$incomingUrl"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.gapps.library.api.models.api

const val COUB_PATTERN = "(?:http[s]?:\\/\\/)?(?:www)?\\.?coub\\.com\\/(?:embed|view|api)\\/([_a-zA-Z0-9]+)[^,;\\s]*"
const val DAILYMOTION_PATTERN = "(?:http[s]?://)?(?:www\\.)?(?:(?:dailymotion\\.com(?:/embed)?/video)|dai\\.ly)/([a-zA-Z0-9]+)[^,;\\s]*"
const val FACEBOOK_PATTERN = "(?:http[s]?:\\/\\/)?(?:www.|web.|m.)?(?:facebook|fb)?.com\\/(?:(?:video.php|watch)?\\?v=|.+\\/videos(?:\\/.+)?\\/)(\\d+)(?:\\S+)?"
const val HULU_PATTERN = "(?:http[s]?:\\/\\/)?(?:www.)?hulu\\.(?:(?:com\\/\\S*(?:w(?:atch)?|eid)(?:\\/|=)?)|(?:tv\\/))?([a-zA-Z0-9]+)[^,;\\s]*"
const val LOOM_PATTERN = "(?:http[s]?:\\/\\/)?(?:www)?\\.?(?:use)?loom\\.com\\/(?:share|default|api)\\/([_a-zA-Z0-9]+)\\S*"
const val RUTUBE_PATTERN = "(?:http[s]?://)(?:w{3})?(?:player\\.)?rutube\\.ru/video/(?:embed/)?([A-Za-z0-9]+)[^,\\s]*"
const val STREAMABLE_PATTERN = "(?:http[s]?:\\/\\/)?(?:www)?\\.?streamable\\.com\\/([_a-zA-Z0-9]+)\\S*"
const val TED_TALKS_PATTERN = "(?:http[s]?:\\/\\/)?(?:www|embed)?\\.?ted\\.com\\/talks\\/([_a-zA-Z0-9]+)[^,;\\s]*"
const val TIKTOK_PATTERN = "(?:http[s]?:\\/\\/)(?:www.)?(?:m.)?youtu(?:be|.be)?(?:\\.com)?(?:(?:\\w*.?:\\/\\/)?\\w*.?\\w*-?.?\\w*\\/(?:embed|e|v|watch|.*\\/)?\\??(?:feature=\\w*\\.?\\w*)?&?(?:v=)?\\/?)([\\w\\d_-]{11})[^,;\\s]*"
const val ULTIMEDIA_PATTERN = "(?:http[s]?:\\/\\/)?(?:www)?\\.?ultimedia\\.com\\/(?:deliver|default|api)\\/.*\\/([_a-zA-Z0-9]+)\\S*"
const val USTREAM_PATTERN = "(?:http[s]?:\\/\\/)?(?:www\\.)?ustream.(?:com|tv)\\/(?:recorded|embed|channel)\\/?(?:([0-9]+)|(\\S+))[^,;\\s]*"
const val VIMEO_PATTERN = "(?:http[s]?://)(?:w{3})?(?:player\\.)?vimeo\\.com/(?:[a-z]*/)*([0-9]{6,11})[^,;\\s]*"
const val VZAAR_PATTERN = "(?:http[s]?://)?(?:.+)?vzaar.com/?(?:videos/)?([0-9]+)[^,;\\s]*"
const val WISTIA_PATTERN = "(?:http[s]?:\\/\\/)?(?:.+)?(?:wistia\\.(?:com|net)|wi\\.st)\\/(?:medias|embed|series)\\/(?:iframe\\/?)?(?:\\S+\\?\\S*wvideoid=)?([a-zA-Z0-9]+)[^,;\\s]*"
const val YOUTUBE_MUSIC_PATTERN = "(?:http[s]?:\\/\\/)(?:www.)?(?:m.)?music.youtu(?:be|.be)?(?:\\.com)?(?:(?:\\w*.?:\\/\\/)?\\w*.?\\w*-?.?\\w*\\/(?:embed|e|v|watch|.*\\/)?\\??(?:feature=\\w*\\.?\\w*)?&?(?:v=)?\\/?)([\\w\\d_-]{11})[^,;\\s]*"
const val YOUTUBE_PATTERN = "(?:http[s]?:\\/\\/)(?:www.)?(?:m.)?youtu(?:be|.be)?(?:\\.com)?(?:(?:\\w*.?:\\/\\/)?\\w*.?\\w*-?.?\\w*\\/(?:embed|e|v|watch|.*\\/)?\\??(?:feature=\\w*\\.?\\w*)?&?(?:v=)?\\/?)([\\w\\d_-]{11})[^,;\\s]*"
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ open class RutubeVideoInfoModel : VideoInfoModel<RutubeResponse>() {
override val baseUrl: String
get() = "http://rutube.ru"
override val pattern: String
get() = "(?:http[s]?://)(?:w{3})?(?:player\\.)?rutube\\.ru/video/(?:embed/)?([A-Za-z0-9]+)[^,\\s]*"
get() = RUTUBE_PATTERN
override val idPattern: String
get() = pattern
override val type: Class<RutubeResponse>
get() = RutubeResponse::class.java
override val hostingName: String
get() = "Rutube"
get() = RUTUBE_HOST_NAME

override fun getInfoUrl(incomingUrl: String?): String? {
val id = parseVideoId(incomingUrl) ?: return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ class StreamableVideoInfoModel : VideoInfoModel<StreamableResponse>() {
override val baseUrl: String
get() = "https://api.streamable.com"
override val pattern: String
get() = "(?:http[s]?:\\/\\/)?(?:www)?\\.?streamable\\.com\\/([_a-zA-Z0-9]+)\\S*"
get() = STREAMABLE_PATTERN
override val idPattern: String
get() = pattern
override val type: Class<StreamableResponse>
get() = StreamableResponse::class.java
override val hostingName: String
get() = "Streamable"
get() = STREAMABLE_HOST_NAME

override fun getInfoUrl(incomingUrl: String?): String? {
return "$baseUrl/oembed.json?$URL=$incomingUrl"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ open class TedTalksVideoInfoModel : VideoInfoModel<TedTalksResponse>() {
override val baseUrl: String
get() = "https://www.ted.com"
override val pattern: String
get() = "(?:http[s]?:\\/\\/)?(?:www|embed)?\\.?ted\\.com\\/talks\\/([_a-zA-Z0-9]+)[^,;\\s]*"
get() = TED_TALKS_PATTERN
override val idPattern: String
get() = pattern
override val type: Class<TedTalksResponse>
get() = TedTalksResponse::class.java
override val hostingName: String
get() = "Ted Talks"
get() = TED_TALKS_HOST_NAME

override fun getInfoUrl(incomingUrl: String?): String? {
return "$baseUrl/services/v1/oembed.$FORMAT_JSON?$URL=$incomingUrl"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ open class TikTokVideoInfoModel : VideoInfoModel<TikTokResponse>() {

//https://regex101.com/r/nJzgG0/1
override val pattern: String
get() = "(?:http[s]?:\\/\\/)(?:www.)?(?:m.)?youtu(?:be|.be)?(?:\\.com)?(?:(?:\\w*.?:\\/\\/)?\\w*.?\\w*-?.?\\w*\\/(?:embed|e|v|watch|.*\\/)?\\??(?:feature=\\w*\\.?\\w*)?&?(?:v=)?\\/?)([\\w\\d_-]{11})[^,;\\s]*"
get() = TIKTOK_PATTERN
override val idPattern: String
get() = pattern
override val type: Class<TikTokResponse>
get() = TikTokResponse::class.java
override val hostingName: String
get() = "YouTube"
get() = TIKTOK_HOST_NAME

override fun getInfoUrl(incomingUrl: String?): String? {
val id = parseVideoId(incomingUrl)
Expand Down
Loading

0 comments on commit ffeb478

Please sign in to comment.