🆕 Fetching Post Page and showing it

This commit is contained in:
Mylloon 2021-09-02 14:40:21 +02:00
parent 807999583c
commit 98eae28308
8 changed files with 231 additions and 16 deletions

View file

@ -39,7 +39,7 @@ class MainActivity : AppCompatActivity() {
private val sharedPref = "com.mylloon.MobiDL" // shared pref name
private var timeOfLastToast: Long = System.currentTimeMillis() - 2000
private var listInfos: MutableList<Map<String, String?>>? = null
private var urlInfos: String? = null
private var appMobilismInfos: Map<String, String?>? = null
companion object {
private var instance: MainActivity? = null
@ -359,16 +359,73 @@ class MainActivity : AppCompatActivity() {
}
private fun showAppListPage() {
setContentView(R.layout.activity_app_list) // display main page
setContentView(R.layout.activity_app_list) // display list of app version page
if (!settingsButtonVisible) toggleSettingsButtonVisibility() // check if the settings button isn't already showed and show it if necessary
inAppList = true
urlInfos = "XD"
println(listInfos)
class Adapter(private val values: List<String>) : RecyclerView.Adapter<Adapter.ViewHolder>() {
override fun getItemCount() = values.size
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int,
): ViewHolder { // add viewHolder to the parent page
val itemView = LayoutInflater.from(parent.context)
.inflate(R.layout.list_item_view_app_list, parent, false)
return ViewHolder(itemView)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.button?.text = values[position]
}
inner class ViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView!!) {
var button: Button? = null
init {
button = itemView?.findViewById(R.id.text_list_item_app_list)
button?.setOnClickListener {
inAppList = false
appMobilismInfos = listInfos!![Regex("""\d+""").findAll(button?.text.toString()).map { it.groupValues[0] }.toList()[0].toInt() - 1]
showAppInfoPage()
}
}
}
}
val recyclerViewAppList: RecyclerView = findViewById(R.id.recyclerViewAppList) // get recyclerview
recyclerViewAppList.layoutManager = LinearLayoutManager(this)
val listInfosNames = mutableListOf<String>()
for (i in listInfos!!.indices) { // get all apps name
val tmp = "${i + 1} - ${listInfos!![i]["title"]} (${listInfos!![i]["author"]})\n${listInfos!![i]["date"]}"
listInfosNames += tmp
}
recyclerViewAppList.adapter = Adapter(listInfosNames)
}
private fun showAppInfoPage() {
setContentView(R.layout.activity_app_infos) // display main page
setContentView(R.layout.activity_app_infos) // display app info detailed page
if (!settingsButtonVisible) toggleSettingsButtonVisibility() // check if the settings button isn't already showed and show it if necessary
inAppInfo = true
println(urlInfos)
val link = appMobilismInfos!!["link"]
if (link != null) {
runBlocking { // GlobalScope.launch {
val infoApp = Scraper().parseInfos(link)
findViewById<TextView>(R.id.textViewAppName).text = appMobilismInfos!!["title"]
findViewById<TextView>(R.id.textViewAppAuthor).text = appMobilismInfos!!["author"]
findViewById<TextView>(R.id.textViewAppDate).text = appMobilismInfos!!["date"]
findViewById<TextView>(R.id.textViewAppChangelogs).text = infoApp["changelogs"]
////////////////////////
println("\n")
println("Title: ${appMobilismInfos!!["title"]}\n")
println("Author: ${appMobilismInfos!!["author"]}\n")
println("Date of release: ${appMobilismInfos!!["date"]}\n")
println("Changelogs: \n${infoApp["changelogs"]}\n")
println("Download links: \n${infoApp["downloadLinks"]}")
println("\n\n---\n")
}
} else Toast.makeText(instance, "${getString(R.string.noURL)}...", Toast.LENGTH_SHORT).show()
}
@SuppressLint("SetTextI18n")
@ -431,7 +488,7 @@ class MainActivity : AppCompatActivity() {
inSettings = false
return when {
inAppList -> showAppListPage()
inAppInfo -> showAppListPage()
inAppInfo -> showAppInfoPage()
else -> mainPage()
}
}
@ -441,7 +498,7 @@ class MainActivity : AppCompatActivity() {
}
inAppInfo -> {
inAppInfo = false
return showAppInfoPage()
return showAppListPage()
}
else -> super.onBackPressed()
}

View file

@ -7,11 +7,11 @@ import com.github.kittinunf.result.Result.Failure as FuelFailure
import com.github.kittinunf.result.Result.Success as FuelSuccess
class Scraper(
private var pseudo: String,
private var password: String,
private var app: String,
private var captchaID: String? = null,
private var captchaResponse: String? = null
private var pseudo: String? = null, // can be null if parsing a post
private var password: String? = null, // can be null if parsing a post
private var app: String? = null, // can be null if parsing a post
private var captchaID: String? = null, // can be null if no captcha
private var captchaResponse: String? = null // can be null if no captcha
) {
private var url: String = "https://forum.mobilism.org"
@ -158,4 +158,55 @@ class Scraper(
return finalElements
}
fun parseInfos(urlPost: String): MutableMap<String, String?> {
println("Going to post page...")
FuelManager.instance.basePath = url
val session = urlPost
.httpGet()
.responseString { _, response, result ->
when (result) {
is FuelFailure -> {
val ex = result.getException()
println("[${response.statusCode}] Exception: $ex")
}
is FuelSuccess -> {
println("Post page retrieved") // does nothing but is required otherwise everything bugs :(
}
}
}
var htmlPage = session.join().data.decodeToString()
val elements = mutableMapOf<String, String?>()
// if ("Download Instructions" !in htmlPage) {
// elements["changelogs"] = null
// elements["downloadLinks"] = null
// }
elements["changelogs"] = try {
val tmp = Regex("""What's New:</span> ?<br />(.*)<br /><br /><span style="c|font-weight: bold">T""").findAll(htmlPage)
.map { it.groupValues[1] }.toList()[0]
if (tmp.length < 2) { // if result none, trying other method
Regex("""What's New:</span> ?<br />(.*)<br /><br /><span style="font-weight: bold">T""").findAll(htmlPage)
.map { it.groupValues[1] }.toList()[0]
}
tmp.replace(Regex("""<br />\n?"""), "\n") // convert newline html to \n
} catch (e: Exception) {
"No changelog found."
}
elements["downloadLinks"] = try {
htmlPage = htmlPage.replace(Regex("Download Instructions:</span>(.*)?<br /><s"), "Download Instructions:</span><br /><s")
var tmp = Regex("""Download Instructions:</span> ?<br />(.*|[\s\S]*)<br /><br />Trouble downloading|</a></div>""").findAll(htmlPage)
.map { it.groupValues[1] }.toList()[0]
if (tmp.length < 2) { // if result none, trying other method
Regex("""Download Instructions:</span> ?<br />(.*|[\s\S]*)</a></div>""").findAll(htmlPage)
.map { it.groupValues[1] }.toList()[0]
}
tmp = tmp.replace(Regex("""\n|<a class="postlink" href="|\(Closed Filehost\) ?|<span style="font-weight: bold">|</span>|">(\S*)</a>"""), "") // remove html garbage
tmp = tmp.replace(Regex("""<br />\n?"""), "\n") // convert newline html to \n
tmp = tmp.replace(Regex("""Mirrors(?!:)|Mirror(?!s)(?!:)"""), "Mirror:") // add ":"
tmp.split("""">""")[0]
} catch (e: Exception) {
null
}
return elements
}
}

View file

@ -1,9 +1,84 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textViewAppName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/exo_2_light"
android:layout_marginTop="36dp"
android:textAlignment="center"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textViewAppAuthor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textStyle="italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.898"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewAppName" />
<TextView
android:id="@+id/textViewAppDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:textStyle="italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.052"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewAppName" />
<TextView
android:id="@+id/textViewAppChangelogsTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/exo_2_medium"
android:layout_marginTop="36dp"
android:text="@string/changelogs"
android:textAlignment="center"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewAppName" />
<TextView
android:id="@+id/textViewAppChangelogs"
android:layout_width="334dp"
android:layout_height="230dp"
android:layout_marginTop="12dp"
android:fontFamily="monospace"
android:textAlignment="viewStart"
android:textSize="16sp"
android:textStyle="italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewAppChangelogsTitle" />
<TextView
android:id="@+id/textViewAppDownloadsTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/exo_2_medium"
android:layout_marginTop="20dp"
android:text="@string/downloads"
android:textAlignment="center"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewAppChangelogs" />
</androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -1,9 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewAppList"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>

View file

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="100dp"
android:paddingTop="8dp"

View file

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout
android:id="@+id/linear_layout_app_list"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="208dp"
android:paddingTop="8dp"
android:paddingBottom="15dp"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:gravity="center_vertical"
android:orientation="horizontal" >
<Button
android:id="@+id/text_list_item_app_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="8dp" />
</LinearLayout>

View file

@ -28,4 +28,7 @@
<string name="updateCheck">Vérification des mises à jour</string>
<string name="noResults">Aucun résultat n\'a été trouvé</string>
<string name="gotResults">résultats ont été trouvés</string>
<string name="noURL">Aucune URL n\'a été trouvé pour ce post</string>
<string name="changelogs">Changements</string>
<string name="downloads">Téléchargements</string>
</resources>

View file

@ -35,4 +35,9 @@
<string name="badSID">The SID does not work, new attempt</string>
<string name="noResults">No results were found</string>
<string name="gotResults">results were found</string>
<string name="noURL">No URL were found for this post</string>
<!-- App Infos -->
<string name="changelogs">Changelogs</string>
<string name="downloads">Downloads</string>
</resources>