Skip to content

Commit

Permalink
Merge pull request #482 from VGJohn/import-by-url
Browse files Browse the repository at this point in the history
Added a new OPML import option to import subscriptions from an url
  • Loading branch information
geekygecko authored Oct 25, 2022
2 parents fba1b2a + 6509955 commit 10ba56c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 3 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
* New Features:
* Added Tasker integration with "Play Filter" and "Control Playback" actions.
([#415](https://github.com/Automattic/pocket-casts-android/pull/431)).
* Fixed background color for screens using the compose theme
([#432](https://github.com/Automattic/pocket-casts-android/pull/432)).
* Import OPML from a URL
([#482](https://github.com/Automattic/pocket-casts-android/pull/482)).
* Bug Fixes:
* Fixed Help & Feedback buttons being hidden when using text zoom.
([#446](https://github.com/Automattic/pocket-casts-android/pull/446)).
* Fixed when system bar didn't disappear on full screen video player
([#461](https://github.com/Automattic/pocket-casts-android/pull/461)).
* Fixed background color for screens using the compose theme
([#432](https://github.com/Automattic/pocket-casts-android/pull/432)).

7.25
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.preference.EditTextPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import au.com.shiftyjelly.pocketcasts.preferences.Settings
Expand Down Expand Up @@ -45,6 +46,14 @@ class ExportSettingsFragment : PreferenceFragmentCompat() {
true
}

findPreference<EditTextPreference>("importPodcastsByUrl")?.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue ->
val url = newValue.toString()
if (url.isNotBlank()) {
OpmlImportTask.run(url, requireActivity())
}
false
}

findPreference<Preference>("exportSendEmail")?.onPreferenceClickListener = Preference.OnPreferenceClickListener {
exporter = OpmlExporter(this@ExportSettingsFragment, serverManager, podcastManager, settings, activity).apply {
sendEmail()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
android:key="importPodcasts"
android:summary="@string/settings_import_file_summary"
android:title="@string/settings_import_file" />
<EditTextPreference
android:key="importPodcastsByUrl"
android:summary="@string/settings_import_url_summary"
android:title="@string/settings_import_url" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/settings_export_subscriptions" >
<Preference
Expand Down
2 changes: 2 additions & 0 deletions modules/services/localization/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,8 @@
<string name="settings_import_choose_file">Choose an OPML file.</string>
<string name="settings_import_file">Select file</string>
<string name="settings_import_file_summary">If you have podcast subscriptions in another app or service, Pocket Casts can import them from an OPML file.</string>
<string name="settings_import_url">By URL</string>
<string name="settings_import_url_summary">Import your podcasts from an OPML file using a URL.</string>
<string name="settings_import_opml_progress">%1$d of %2$d podcasts</string>
<string name="settings_import_opml_stop">Stop</string>
<string name="settings_import_opml_title">Import OPML</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.widget.Toast
import androidx.hilt.work.HiltWorker
import androidx.work.Constraints
import androidx.work.CoroutineWorker
import androidx.work.Data
import androidx.work.ForegroundInfo
import androidx.work.NetworkType
import androidx.work.OneTimeWorkRequestBuilder
Expand Down Expand Up @@ -35,6 +36,7 @@ import org.xml.sax.SAXException
import org.xml.sax.helpers.DefaultHandler
import java.io.InputStream
import java.io.StringReader
import java.net.URL
import java.util.Scanner
import java.util.regex.Pattern
import javax.xml.parsers.SAXParserFactory
Expand All @@ -52,13 +54,23 @@ class OpmlImportTask @AssistedInject constructor(

companion object {
const val INPUT_URI = "INPUT_URI"
const val INPUT_URL = "INPUT_URL"

fun run(uri: Uri, context: Context) {
val data = workDataOf(INPUT_URI to uri.toString())
run(data, context)
}

fun run(url: String, context: Context) {
val data = workDataOf(INPUT_URL to url)
run(data, context)
}

private fun run(data: Data, context: Context) {
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()

val data = workDataOf(INPUT_URI to uri.toString())
val task = OneTimeWorkRequestBuilder<OpmlImportTask>()
.setInputData(data)
.setConstraints(constraints)
Expand Down Expand Up @@ -132,6 +144,12 @@ class OpmlImportTask @AssistedInject constructor(

override suspend fun doWork(): Result {
try {
val url = inputData.getString(INPUT_URL)
if (!url.isNullOrBlank()) {
processUrl(URL(url))
return Result.success()
}

val uri = Uri.parse(inputData.getString(INPUT_URI)) ?: return Result.failure()
processFile(uri)
return Result.success()
Expand All @@ -141,6 +159,22 @@ class OpmlImportTask @AssistedInject constructor(
}
}

private suspend fun processUrl(url: URL) {
var urls = emptyList<String>()

try {
url.openStream()?.use { inputStream ->
urls = readOpmlUrlsSax(inputStream)
}
} catch (e: SAXException) {
url.openStream()?.use { inputStream ->
urls = readOpmlUrlsRegex(inputStream)
}
}

processUrls(urls)
}

private suspend fun processFile(uri: Uri) {
var urls = emptyList<String>()

Expand All @@ -155,6 +189,10 @@ class OpmlImportTask @AssistedInject constructor(
}
}

processUrls(urls)
}

private suspend fun processUrls(urls: List<String>) {
val podcastCount = urls.size
val initialDatabaseCount = podcastManager.countPodcasts()

Expand Down

0 comments on commit 10ba56c

Please sign in to comment.