@@ -11,6 +11,7 @@ import android.os.Looper
1111import android.util.Log
1212import com.google.gson.Gson
1313import com.google.gson.reflect.TypeToken
14+ import com.lizongying.mytv0.Utils.getUrls
1415import com.lizongying.mytv0.data.ReqSettings
1516import com.lizongying.mytv0.data.ReqSourceAdd
1617import com.lizongying.mytv0.data.ReqSources
@@ -23,19 +24,19 @@ import kotlinx.coroutines.Dispatchers
2324import kotlinx.coroutines.runBlocking
2425import kotlinx.coroutines.withContext
2526import java.io.File
26- import java.io.IOException
2727import java.nio.charset.StandardCharsets
2828
2929
3030class SimpleServer (private val context : Context , private val viewModel : MainViewModel ) :
3131 NanoHTTPD (PORT ) {
3232 private val handler = Handler (Looper .getMainLooper())
33+ private val gson = Gson ()
3334
3435 init {
3536 try {
3637 start()
3738 } catch (e: Exception ) {
38- e.printStackTrace( )
39+ Log .e( TAG , " init " , e )
3940 }
4041 }
4142
@@ -72,7 +73,7 @@ class SimpleServer(private val context: Context, private val viewModel: MainView
7273 if (! SP .sources.isNullOrEmpty()) {
7374 try {
7475 val type = object : TypeToken <List <Source >>() {}.type
75- val sources: List <Source > = Gson () .fromJson(SP .sources!! , type)
76+ val sources: List <Source > = gson .fromJson(SP .sources!! , type)
7677 history = sources.toMutableList()
7778 } catch (e: Exception ) {
7879 e.printStackTrace()
@@ -88,9 +89,9 @@ class SimpleServer(private val context: Context, private val viewModel: MainView
8889 epg = SP .epg ? : " " ,
8990 history = history
9091 )
91- response = Gson () .toJson(respSettings) ? : " "
92+ response = gson .toJson(respSettings) ? : " "
9293 } catch (e: Exception ) {
93- e.printStackTrace( )
94+ Log .e( TAG , " handleSettings " , e )
9495 return newFixedLengthResponse(
9596 Response .Status .INTERNAL_ERROR ,
9697 MIME_PLAINTEXT ,
@@ -102,31 +103,11 @@ class SimpleServer(private val context: Context, private val viewModel: MainView
102103 }
103104
104105 private suspend fun fetchSources (url : String ): String {
105- val urls =
106- if (url.startsWith(" https://raw.githubusercontent.com" ) || url.startsWith(" https://github.com" )) {
107- listOf (
108- " https://ghp.ci/" ,
109- " https://gh.llkk.cc/" ,
110- " https://github.moeyy.xyz/" ,
111- " https://mirror.ghproxy.com/" ,
112- " https://ghproxy.cn/" ,
113- " https://ghproxy.net/" ,
114- " https://ghproxy.click/" ,
115- " https://ghproxy.com/" ,
116- " https://github.moeyy.cn/" ,
117- " https://gh-proxy.llyke.com/" ,
118- " https://www.ghproxy.cc/" ,
119- " https://cf.ghproxy.cc/"
120- ).map {
121- Pair (" $it$url " , url)
122- }
123- } else {
124- listOf (Pair (url, url))
125- }
106+ val urls = getUrls(url)
126107
127108 var sources = " "
128109 var success = false
129- for ((a, b) in urls) {
110+ for (a in urls) {
130111 Log .i(TAG , " request $a " )
131112 try {
132113 withContext(Dispatchers .IO ) {
@@ -141,7 +122,6 @@ class SimpleServer(private val context: Context, private val viewModel: MainView
141122 }
142123 }
143124 } catch (e: Exception ) {
144- e.printStackTrace()
145125 Log .e(TAG , " fetchSources" , e)
146126 }
147127
@@ -173,7 +153,7 @@ class SimpleServer(private val context: Context, private val viewModel: MainView
173153 }
174154 }
175155 } catch (e: Exception ) {
176- e.printStackTrace( )
156+ Log .e( TAG , " handleImportFromText " , e )
177157 return newFixedLengthResponse(
178158 Response .Status .INTERNAL_ERROR ,
179159 MIME_PLAINTEXT ,
@@ -188,13 +168,14 @@ class SimpleServer(private val context: Context, private val viewModel: MainView
188168 val response = " "
189169 try {
190170 readBody(session)?.let {
191- val req = Gson () .fromJson(it, ReqSourceAdd ::class .java)
171+ val req = gson .fromJson(it, ReqSourceAdd ::class .java)
192172 val uri = Uri .parse(req.uri)
193173 handler.post {
194174 viewModel.importFromUri(uri, req.id)
195175 }
196176 }
197- } catch (e: IOException ) {
177+ } catch (e: Exception ) {
178+ Log .e(TAG , " handleImportFromUri" , e)
198179 return newFixedLengthResponse(
199180 Response .Status .INTERNAL_ERROR ,
200181 MIME_PLAINTEXT ,
@@ -208,7 +189,7 @@ class SimpleServer(private val context: Context, private val viewModel: MainView
208189 try {
209190 readBody(session)?.let {
210191 handler.post {
211- val req = Gson () .fromJson(it, ReqSettings ::class .java)
192+ val req = gson .fromJson(it, ReqSettings ::class .java)
212193 if (req.proxy != null ) {
213194 SP .proxy = req.proxy
214195 R .string.default_proxy_set_success.showToast()
@@ -218,7 +199,7 @@ class SimpleServer(private val context: Context, private val viewModel: MainView
218199 }
219200 }
220201 } catch (e: Exception ) {
221- e.printStackTrace( )
202+ Log .e( TAG , " handleProxy " , e )
222203 return newFixedLengthResponse(
223204 Response .Status .INTERNAL_ERROR ,
224205 MIME_PLAINTEXT ,
@@ -233,7 +214,7 @@ class SimpleServer(private val context: Context, private val viewModel: MainView
233214 try {
234215 readBody(session)?.let {
235216 handler.post {
236- val req = Gson () .fromJson(it, ReqSettings ::class .java)
217+ val req = gson .fromJson(it, ReqSettings ::class .java)
237218 if (req.epg != null ) {
238219 SP .epg = req.epg
239220 viewModel.updateEPG()
@@ -244,7 +225,7 @@ class SimpleServer(private val context: Context, private val viewModel: MainView
244225 }
245226 }
246227 } catch (e: Exception ) {
247- e.printStackTrace( )
228+ Log .e( TAG , " handleEPG " , e )
248229 return newFixedLengthResponse(
249230 Response .Status .INTERNAL_ERROR ,
250231 MIME_PLAINTEXT ,
@@ -261,7 +242,7 @@ class SimpleServer(private val context: Context, private val viewModel: MainView
261242 try {
262243 readBody(session)?.let {
263244 handler.post {
264- val req = Gson () .fromJson(it, ReqSettings ::class .java)
245+ val req = gson .fromJson(it, ReqSettings ::class .java)
265246 if (req.channel != null && req.channel > - 1 ) {
266247 SP .channel = req.channel
267248 R .string.default_channel_set_success.showToast()
@@ -271,7 +252,7 @@ class SimpleServer(private val context: Context, private val viewModel: MainView
271252 }
272253 }
273254 } catch (e: Exception ) {
274- e.printStackTrace( )
255+ Log .e( TAG , " handleDefaultChannel " , e )
275256 return newFixedLengthResponse(
276257 Response .Status .INTERNAL_ERROR ,
277258 MIME_PLAINTEXT ,
@@ -286,7 +267,7 @@ class SimpleServer(private val context: Context, private val viewModel: MainView
286267 try {
287268 readBody(session)?.let {
288269 handler.post {
289- val req = Gson () .fromJson(it, ReqSources ::class .java)
270+ val req = gson .fromJson(it, ReqSources ::class .java)
290271 Log .i(TAG , " req $req " )
291272 if (req.sourceId.isNotEmpty()) {
292273 val res = viewModel.sources.removeSource(req.sourceId)
@@ -301,7 +282,7 @@ class SimpleServer(private val context: Context, private val viewModel: MainView
301282 }
302283 }
303284 } catch (e: Exception ) {
304- e.printStackTrace( )
285+ Log .e( TAG , " handleRemoveSource " , e )
305286 return newFixedLengthResponse(
306287 Response .Status .INTERNAL_ERROR ,
307288 MIME_PLAINTEXT ,
0 commit comments