Skip to content

Commit

Permalink
fetch sources via the network
Browse files Browse the repository at this point in the history
  • Loading branch information
lizongying committed Dec 13, 2024
1 parent e1cabd6 commit b4e7792
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 42 deletions.
8 changes: 8 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
## 更新日誌

### v1.3.8.8-kitkat

* 通過網絡獲取默認視頻源列表

### v1.3.8.8

* 通過網絡獲取默認視頻源列表

### v1.3.8.7-kitkat

* 修復切換頻道不正確的問題
Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 我的電視·〇

電視網絡視頻播放軟件,可以自定義視頻源
電視視頻播放軟件,可以自定義視頻源

[my-tv-0](https://github.com/lizongying/my-tv-0)

Expand Down Expand Up @@ -84,8 +84,6 @@ adb install my-tv-0.apk
* 詳細EPG
* 淺色菜單
* 無效的頻道?
* 判断文件是否被修改
* 多源管理
* 如果上次播放頻道不在收藏?
* 當list為空,顯示group
* 默認頻道菜單顯示
Expand Down
22 changes: 12 additions & 10 deletions app/src/main/java/com/lizongying/mytv0/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class MainActivity : AppCompatActivity() {
.hide(errorFragment)
.hide(loadingFragment)
.hide(timeFragment)
.commitNow()
.commitNowAllowingStateLoss()
}

gestureDetector = GestureDetector(this, GestureListener(this))
Expand Down Expand Up @@ -215,9 +215,9 @@ class MainActivity : AppCompatActivity() {

Utils.isp.observe(this) {
Log.i(TAG, "isp $it")
// val id = R.raw.mobile
val id = when (it) {
ISP.CHINA_MOBILE -> R.raw.mobile
// ISP.CHINA_MOBILE -> R.raw.mobile
// ISP.IPV6->R.raw.ipv6
else -> 0
}

Expand Down Expand Up @@ -473,7 +473,7 @@ class MainActivity : AppCompatActivity() {

supportFragmentManager.beginTransaction()
.show(fragment)
.commitNow()
.commitNowAllowingStateLoss()
}

private fun hideFragment(fragment: Fragment) {
Expand All @@ -483,7 +483,7 @@ class MainActivity : AppCompatActivity() {

supportFragmentManager.beginTransaction()
.hide(fragment)
.commitNow()
.commitAllowingStateLoss()
}

fun menuActive() {
Expand All @@ -494,7 +494,9 @@ class MainActivity : AppCompatActivity() {
private val hideMenu = Runnable {
if (!isFinishing && !supportFragmentManager.isStateSaved) {
if (!menuFragment.isHidden) {
supportFragmentManager.beginTransaction().hide(menuFragment).commit()
supportFragmentManager.beginTransaction()
.hide(menuFragment)
.commitAllowingStateLoss()
}
}
}
Expand All @@ -510,7 +512,7 @@ class MainActivity : AppCompatActivity() {
try {
supportFragmentManager.beginTransaction()
.hide(settingFragment)
.commitNow()
.commitAllowingStateLoss()
showTime()
} catch (e: Exception) {
e.printStackTrace()
Expand Down Expand Up @@ -594,20 +596,20 @@ class MainActivity : AppCompatActivity() {

supportFragmentManager.beginTransaction()
.show(settingFragment)
.commit()
.commitAllowingStateLoss()
settingActive()
}

fun hideMenuFragment() {
supportFragmentManager.beginTransaction()
.hide(menuFragment)
.commit()
.commitAllowingStateLoss()
}

private fun hideSettingFragment() {
supportFragmentManager.beginTransaction()
.hide(settingFragment)
.commit()
.commitAllowingStateLoss()
showTime()
}

Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/com/lizongying/mytv0/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class MainViewModel : ViewModel() {
cacheChannels = getCache()

if (cacheChannels.isEmpty()) {
cacheChannels = context.resources.openRawResource(R.raw.channels).bufferedReader()
cacheChannels = context.resources.openRawResource(DEFAULT_CHANNELS_FILE).bufferedReader()
.use { it.readText() }
}

Expand Down Expand Up @@ -237,7 +237,7 @@ class MainViewModel : ViewModel() {
}

fun reset(context: Context) {
val str = context.resources.openRawResource(R.raw.channels).bufferedReader()
val str = context.resources.openRawResource(DEFAULT_CHANNELS_FILE).bufferedReader()
.use { it.readText() }

try {
Expand Down Expand Up @@ -485,5 +485,6 @@ class MainViewModel : ViewModel() {
companion object {
private const val TAG = "MainViewModel"
const val CACHE_FILE_NAME = "channels.txt"
val DEFAULT_CHANNELS_FILE = R.raw.channels
}
}
5 changes: 3 additions & 2 deletions app/src/main/java/com/lizongying/mytv0/MenuFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class MenuFragment : Fragment(), GroupAdapter.ItemListener, ListAdapter.ItemList
private fun hideSelf() {
requireActivity().supportFragmentManager.beginTransaction()
.hide(this)
.commit()
.commitAllowingStateLoss()
}

override fun onItemFocusChange(listTVModel: TVListModel, hasFocus: Boolean) {
Expand Down Expand Up @@ -243,7 +243,8 @@ class MenuFragment : Fragment(), GroupAdapter.ItemListener, ListAdapter.ItemList
// listAdapter.focusable(true)
// }

if (viewModel.groupModel.tvGroupValue.size < 2 || viewModel.groupModel.getAllList()?.size() == 0
if (viewModel.groupModel.tvGroupValue.size < 2 || viewModel.groupModel.getAllList()
?.size() == 0
) {
R.string.channel_not_exist.showToast()
return
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/lizongying/mytv0/SettingFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ class SettingFragment : Fragment() {
private fun hideSelf() {
requireActivity().supportFragmentManager.beginTransaction()
.hide(this)
.commit()
.commitAllowingStateLoss()
(activity as MainActivity).showTime()
}

Expand Down
79 changes: 74 additions & 5 deletions app/src/main/java/com/lizongying/mytv0/SimpleServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.lizongying.mytv0

import MainViewModel
import MainViewModel.Companion.CACHE_FILE_NAME
import MainViewModel.Companion.DEFAULT_CHANNELS_FILE
import android.content.Context
import android.net.Uri
import android.os.Handler
Expand All @@ -15,7 +16,12 @@ import com.lizongying.mytv0.data.ReqSourceAdd
import com.lizongying.mytv0.data.ReqSources
import com.lizongying.mytv0.data.RespSettings
import com.lizongying.mytv0.data.Source
import com.lizongying.mytv0.requests.HttpClient
import fi.iki.elonen.NanoHTTPD
import io.github.lizongying.Gua
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import java.io.File
import java.io.IOException
import java.nio.charset.StandardCharsets
Expand All @@ -36,8 +42,9 @@ class SimpleServer(private val context: Context, private val viewModel: MainView
override fun serve(session: IHTTPSession): Response {
return when (session.uri) {
"/api/settings" -> handleSettings()
"/api/channels" -> handleChannelsFromFile(session)
"/api/uri" -> handleChannelsFromUri(session)
"/api/sources" -> handleSources()
"/api/import-text" -> handleImportFromText(session)
"/api/import-uri" -> handleImportFromUri(session)
"/api/proxy" -> handleProxy(session)
"/api/epg" -> handleEPG(session)
"/api/channel" -> handleDefaultChannel(session)
Expand All @@ -56,7 +63,7 @@ class SimpleServer(private val context: Context, private val viewModel: MainView
""
}
if (str.isEmpty()) {
str = context.resources.openRawResource(R.raw.channels).bufferedReader()
str = context.resources.openRawResource(DEFAULT_CHANNELS_FILE).bufferedReader()
.use { it.readText() }
}

Expand Down Expand Up @@ -94,7 +101,69 @@ class SimpleServer(private val context: Context, private val viewModel: MainView
return newFixedLengthResponse(Response.Status.OK, "application/json", response)
}

private fun handleChannelsFromFile(session: IHTTPSession): Response {
private suspend fun fetchSources(url: String): String {
val urls =
if (url.startsWith("https://raw.githubusercontent.com") || url.startsWith("https://github.com")) {
listOf(
"https://ghp.ci/",
"https://gh.llkk.cc/",
"https://github.moeyy.xyz/",
"https://mirror.ghproxy.com/",
"https://ghproxy.cn/",
"https://ghproxy.net/",
"https://ghproxy.click/",
"https://ghproxy.com/",
"https://github.moeyy.cn/",
"https://gh-proxy.llyke.com/",
"https://www.ghproxy.cc/",
"https://cf.ghproxy.cc/"
).map {
Pair("$it$url", url)
}
} else {
listOf(Pair(url, url))
}

var sources = ""
var success = false
for ((a, b) in urls) {
Log.i(TAG, "request $a")
try {
withContext(Dispatchers.IO) {
val request = okhttp3.Request.Builder().url(a).build()
val response = HttpClient.okHttpClient.newCall(request).execute()

if (response.isSuccessful) {
sources = response.bodyAlias()?.string() ?: ""
success = true
} else {
Log.e(TAG, "Request status ${response.codeAlias()}")
}
}
} catch (e: Exception) {
e.printStackTrace()
Log.e(TAG, "fetchSources", e)
}

if (success) break
}

return sources
}

private fun handleSources(): Response {
val response = runBlocking(Dispatchers.IO) {
fetchSources("https://raw.githubusercontent.com/lizongying/my-tv-0/main/app/src/main/res/raw/sources.txt")
}

return newFixedLengthResponse(
Response.Status.OK,
"application/json",
Gua().decode(response)
)
}

private fun handleImportFromText(session: IHTTPSession): Response {
R.string.start_config_channel.showToast()
val response = ""
try {
Expand All @@ -114,7 +183,7 @@ class SimpleServer(private val context: Context, private val viewModel: MainView
return newFixedLengthResponse(Response.Status.OK, "text/plain", response)
}

private fun handleChannelsFromUri(session: IHTTPSession): Response {
private fun handleImportFromUri(session: IHTTPSession): Response {
R.string.start_config_channel.showToast()
val response = ""
try {
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/com/lizongying/mytv0/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.Response
import okhttp3.ResponseBody
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
Expand All @@ -26,6 +24,7 @@ enum class ISP {
CHINA_MOBILE,
CHINA_UNICOM,
CHINA_TELECOM,
IPV6,
}

data class IpInfo(
Expand Down
Loading

0 comments on commit b4e7792

Please sign in to comment.