Calls Scraper and handles connection errors

This commit is contained in:
Mylloon 2021-08-30 14:04:55 +02:00
parent d6f936e4e8
commit 05ab95f5af
4 changed files with 60 additions and 19 deletions

View file

@ -53,10 +53,9 @@ class MainActivity : AppCompatActivity() {
// read apps from the app preference
val sharedPref = context.getSharedPreferences(sharedPref, MODE_PRIVATE)
var data: Set<String>? = null
data = sharedPref.getStringSet("apps", null)
val data: Set<String>? = sharedPref.getStringSet("apps", null)
return data?.toMutableList() ?: mutableListOf<String>()
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<String>) :
@ -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()
}
}
}
}

View file

@ -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)
}
}

View file

@ -17,4 +17,9 @@
<string name="dropBro">lâche frère</string>
<string name="internetPermission">Pas la permission d\'utiliser internet</string>
<string name="connected_as">connecté en tant que</string>
<string name="connectionFailed">Connexion échoué, changez vos identifiants</string>
<string name="error">Erreur</string>
<string name="credentialsDeleted">Vos identifiants ont été supprimées</string>
<string name="loginAttemptsExceeded">Vous avez dépassé le nombre maximal autorisé de tentatives de connexion, veuillez patienter</string>
<string name="notConnected">Vous n\'êtes pas connecté, redirection vers la page de connexion</string>
</resources>

View file

@ -1,5 +1,6 @@
<resources>
<string name="app_name" translatable="false">MobiDL</string>
<string name="error">Error</string>
<!-- Login page -->
<string name="titleLogin">Login to your Mobilism account</string>
@ -13,6 +14,7 @@
<string name="validate">Validate</string>
<string name="remove">Remove</string>
<string name="cancel">Cancel</string>
<string name="notConnected">You are not logged in, redirection to the login page</string>
<string name="dropBro">drop bro</string>
<!-- Settings page -->
@ -24,4 +26,7 @@
<!-- Scraper -->
<string name="internetPermission">No permission to use internet</string>
<string name="connectionFailed">Login failed, change your credentials</string>
<string name="credentialsDeleted">Your credentials have been deleted</string>
<string name="loginAttemptsExceeded">You have exceeded the maximum number of connection attempts, please wait</string>
</resources>