From 44910389e3c63c8fb84e58fa90428d2088d56ab0 Mon Sep 17 00:00:00 2001 From: Arty Bishop Date: Sat, 6 Mar 2021 18:10:15 +0000 Subject: [PATCH] Added zipped TLE import from Mike McCants' web site --- .../rtbishop/look4sat/repository/PrefsRepo.kt | 4 +- .../look4sat/repository/SatelliteRepo.kt | 44 +++++++++++++------ 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/rtbishop/look4sat/repository/PrefsRepo.kt b/app/src/main/java/com/rtbishop/look4sat/repository/PrefsRepo.kt index 12e419cd..363a72d4 100644 --- a/app/src/main/java/com/rtbishop/look4sat/repository/PrefsRepo.kt +++ b/app/src/main/java/com/rtbishop/look4sat/repository/PrefsRepo.kt @@ -121,7 +121,9 @@ class PrefsRepo @Inject constructor(val preferences: SharedPreferences, val mosh private fun loadDefaultSources(): List { return listOf( TleSource("https://celestrak.com/NORAD/elements/active.txt"), - TleSource("https://amsat.org/tle/current/nasabare.txt") + TleSource("https://amsat.org/tle/current/nasabare.txt"), + TleSource("https://www.prismnet.com/~mmccants/tles/classfd.zip"), + TleSource("https://www.prismnet.com/~mmccants/tles/inttles.zip") ) } } diff --git a/app/src/main/java/com/rtbishop/look4sat/repository/SatelliteRepo.kt b/app/src/main/java/com/rtbishop/look4sat/repository/SatelliteRepo.kt index 06f3549c..9c4ac006 100644 --- a/app/src/main/java/com/rtbishop/look4sat/repository/SatelliteRepo.kt +++ b/app/src/main/java/com/rtbishop/look4sat/repository/SatelliteRepo.kt @@ -34,6 +34,7 @@ import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.withContext import java.io.InputStream +import java.util.zip.ZipInputStream import javax.inject.Inject class SatelliteRepo @Inject constructor( @@ -59,8 +60,7 @@ class SatelliteRepo @Inject constructor( withContext(ioDispatcher) { runCatching { resolver.openInputStream(uri)?.use { stream -> - val importedEntries = TLE.importSat(stream).map { tle -> SatEntry(tle) } - updateAndRestoreSelection(importedEntries) + updateAndRestoreSelection(importEntriesFromStreams(listOf(stream))) } } } @@ -68,17 +68,9 @@ class SatelliteRepo @Inject constructor( suspend fun updateEntriesFromWeb(sources: List) { withContext(ioDispatcher) { - val streams = mutableListOf() - sources.forEach { source -> - val stream = satelliteService.fetchFile(source.url).body()?.byteStream() - stream?.let { inputStream -> streams.add(inputStream) } - } - val importedEntries = mutableListOf() - streams.forEach { stream -> - val entries = TLE.importSat(stream).map { tle -> SatEntry(tle) } - importedEntries.addAll(entries) - } - updateAndRestoreSelection(importedEntries) + val streams = getStreamsForSources(sources) + val entries = importEntriesFromStreams(streams) + updateAndRestoreSelection(entries) } } @@ -90,6 +82,32 @@ class SatelliteRepo @Inject constructor( satelliteDao.insertTransmitters(satelliteService.fetchTransmitters()) } + private suspend fun getStreamsForSources(sources: List): List { + val streams = mutableListOf() + sources.forEach { tleSource -> + satelliteService.fetchFile(tleSource.url).body()?.byteStream()?.let { inputStream -> + if (tleSource.url.contains(".zip")) { + // Handle zip stream + val zipInputStream = ZipInputStream(inputStream) + val zipEntry = zipInputStream.nextEntry + if (zipEntry != null && !zipEntry.isDirectory) streams.add(zipInputStream) + } else { + streams.add(inputStream) + } + } + } + return streams + } + + private fun importEntriesFromStreams(streams: List): List { + val importedEntries = mutableListOf() + streams.forEach { stream -> + val entries = TLE.importSat(stream).map { tle -> SatEntry(tle) } + importedEntries.addAll(entries) + } + return importedEntries + } + private suspend fun updateAndRestoreSelection(entries: List) { val selectedCatNums = satelliteDao.getSelectedCatNums() satelliteDao.insertEntries(entries)