diff --git a/app/src/main/java/com/mylloon/mobidl/MainActivity.kt b/app/src/main/java/com/mylloon/mobidl/MainActivity.kt index dc1a2bd..cffb4d1 100644 --- a/app/src/main/java/com/mylloon/mobidl/MainActivity.kt +++ b/app/src/main/java/com/mylloon/mobidl/MainActivity.kt @@ -53,10 +53,9 @@ class MainActivity : AppCompatActivity() { // read apps from the app preference val sharedPref = context.getSharedPreferences(sharedPref, MODE_PRIVATE) - var data: Set? = null - data = sharedPref.getStringSet("apps", null) + val data: Set? = sharedPref.getStringSet("apps", null) - return data?.toMutableList() ?: mutableListOf() + return data?.toMutableList() ?: mutableListOf() } override fun onCreate(savedInstanceState: Bundle?) { // Main function @@ -122,7 +121,7 @@ class MainActivity : AppCompatActivity() { }, 100) // delay to add button settings } val user = Credentials().get(0).toString() - this.title = "${getString(R.string.app_name)} (${getString(R.string.connected_as)} $user)"; + this.title = "${getString(R.string.app_name)} (${getString(R.string.connected_as)} $user)" val valuesRecyclerView = getValuesRecyclerView() // list of all the element in the main page class Adapter(private val values: List) : @@ -161,8 +160,14 @@ class MainActivity : AppCompatActivity() { true } button?.setOnClickListener { - val password = Credentials().get(1).toString() - callScript(user, password, "Plex") // call script + val password = Credentials().get(1) + if (password != null) { // call script + callScript(user, password.toString(), button?.text.toString()) + } else { + Toast.makeText(applicationContext, R.string.notConnected, Toast.LENGTH_LONG) + .show() + loginPage() + } } } } diff --git a/app/src/main/java/com/mylloon/mobidl/Scraper.kt b/app/src/main/java/com/mylloon/mobidl/Scraper.kt index 9ec050f..81a3bee 100644 --- a/app/src/main/java/com/mylloon/mobidl/Scraper.kt +++ b/app/src/main/java/com/mylloon/mobidl/Scraper.kt @@ -1,8 +1,10 @@ package com.mylloon.mobidl +import android.widget.Toast import com.github.kittinunf.fuel.core.FuelManager import com.github.kittinunf.fuel.httpGet import com.github.kittinunf.fuel.httpPost +import com.mylloon.mobidl.MainActivity.Companion.applicationContext import com.github.kittinunf.result.Result.Failure as FuelFailure import com.github.kittinunf.result.Result.Success as FuelSuccess @@ -15,9 +17,13 @@ class Scraper( ) { private var url: String = "https://forum.mobilism.org" - private var sid: String? = null + private var sid: String? = Credentials().getSID() private var retry: Int = 0 + private var errorNotConnected = "Log me on automatically each visit" + private var loginAttemptsExceeded = "You exceeded the maximum allowed number of login attempts." + private var searchNotLogged = "Sorry but you are not permitted to use the search system. If you're not logged in please" + private fun errorFormat( code: Int? = null, message: String = "" @@ -25,7 +31,7 @@ class Scraper( return "${if (code != null) "[$code]" else ""}${if ((message.isNotEmpty()) and (code is Int)) " " else ""}$message." } - private fun connect(calledFromSearch: Boolean = false) { + private fun connect() { if (debug) println("Connection attempt...") retry++ FuelManager.instance.basePath = url @@ -39,22 +45,36 @@ class Scraper( println(errorFormat(response.statusCode, "Exception: $ex")) } is FuelSuccess -> { - if (debug) { - println("") // does nothing but is required otherwise everything bugs :( - } + println("") // does nothing but is required otherwise everything bugs :( } } } - sid = "(?<=sid=)(.{32})".toRegex().find(session.join().data.decodeToString())?.value - if (calledFromSearch) return search() + val data = session.join().data.decodeToString() + sid = "(?<=sid=)(.{32})".toRegex().find(data)?.value + if (errorNotConnected in data) return if (loginAttemptsExceeded in data) error(loginAttemptsExceeded) else error(data) + Credentials().storeSID(sid.toString()) + return search() + } + + private fun error(htmlPage: String) { + var message = "" + if (errorNotConnected in htmlPage) { + message = "${applicationContext().getString(R.string.connectionFailed)}\n${applicationContext().getString(R.string.credentialsDeleted)}." + Credentials().delete() + } else if (loginAttemptsExceeded in htmlPage) { + message = applicationContext().getString(R.string.loginAttemptsExceeded) + } + println(errorFormat(message = message)) + Toast.makeText(applicationContext(), message, Toast.LENGTH_LONG) + .show() } fun search() { // Do the research. if (sid == null) { println(errorFormat(message = "No SID found")) - if (retry < 3) return connect(true) else return + if (retry < 3) return connect() else return } else { - if (debug) println("Successful login!") + if (debug) println("SID recovered") retry = 0 } if (debug) println("Going to search page...") @@ -70,14 +90,20 @@ class Scraper( println(errorFormat(response.statusCode, "Exception: $ex")) } is FuelSuccess -> { - if (debug) { - if (debug) println("Successful search!") - } + println("") // does nothing but is required otherwise everything bugs :( } } } val data = session.join().data.decodeToString() - //println(data) + if (searchNotLogged in data) { + println(errorFormat(message = "The SID doesn't work, new attempt...")) + if (retry < 3) return connect() else return + } else return parse(data) + } + + private fun parse(htmlPage: String) { + if (debug) println("Fetching results for $app...") + // println(htmlPage) } } diff --git a/app/src/main/res/values-fr-rFR/strings.xml b/app/src/main/res/values-fr-rFR/strings.xml index 560adeb..2048e99 100644 --- a/app/src/main/res/values-fr-rFR/strings.xml +++ b/app/src/main/res/values-fr-rFR/strings.xml @@ -17,4 +17,9 @@ lâche frère Pas la permission d\'utiliser internet connecté en tant que + Connexion échoué, changez vos identifiants + Erreur + Vos identifiants ont été supprimées + Vous avez dépassé le nombre maximal autorisé de tentatives de connexion, veuillez patienter + Vous n\'êtes pas connecté, redirection vers la page de connexion \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6eaed02..c140098 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,5 +1,6 @@ MobiDL + Error Login to your Mobilism account @@ -13,6 +14,7 @@ Validate Remove Cancel + You are not logged in, redirection to the login page drop bro @@ -24,4 +26,7 @@ No permission to use internet + Login failed, change your credentials + Your credentials have been deleted + You have exceeded the maximum number of connection attempts, please wait \ No newline at end of file