Archived
PARSING HTML WORKS AND IT GETS SAVED TO THE DATABASE WOOO
This commit is contained in:
@@ -12,7 +12,7 @@ import androidx.recyclerview.widget.PagerSnapHelper
|
||||
import com.denizk0461.studip.R
|
||||
import com.denizk0461.studip.adapter.StudIPEventPageAdapter
|
||||
import com.denizk0461.studip.data.DummyData
|
||||
import com.denizk0461.studip.data.StudIPScraper
|
||||
import com.denizk0461.studip.data.StudIPParser
|
||||
import com.denizk0461.studip.databinding.FragmentEventBinding
|
||||
import com.denizk0461.studip.viewmodel.EventViewModel
|
||||
|
||||
@@ -40,16 +40,17 @@ class EventFragment : Fragment() {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
viewModel.allEvents.observe(viewLifecycleOwner) { events ->
|
||||
recyclerViewAdapter = StudIPEventPageAdapter(DummyData.events.toList())
|
||||
recyclerViewAdapter = StudIPEventPageAdapter(events)//DummyData.events.toList())
|
||||
binding.recyclerView.adapter = recyclerViewAdapter
|
||||
recyclerViewLayoutManager =
|
||||
LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
|
||||
binding.recyclerView.layoutManager = recyclerViewLayoutManager
|
||||
binding.recyclerView.onFlingListener = null
|
||||
PagerSnapHelper().attachToRecyclerView(binding.recyclerView)
|
||||
binding.recyclerView.scheduleLayoutAnimation()
|
||||
}
|
||||
|
||||
viewModel.doAsync { StudIPScraper().parse(requireContext()) }
|
||||
// viewModel.doAsync { StudIPParser().parse(requireContext()) }
|
||||
|
||||
binding.fab.setOnClickListener { view ->
|
||||
launchWebview()
|
||||
|
||||
+28
-12
@@ -1,31 +1,33 @@
|
||||
package com.denizk0461.studip.fragment
|
||||
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import androidx.fragment.app.Fragment
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.webkit.ValueCallback
|
||||
import android.webkit.WebChromeClient
|
||||
import android.webkit.WebResourceRequest
|
||||
import android.webkit.WebView
|
||||
import android.webkit.WebViewClient
|
||||
import androidx.navigation.fragment.findNavController
|
||||
import com.denizk0461.studip.R
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.viewModels
|
||||
import com.denizk0461.studip.data.StudIPParser
|
||||
import com.denizk0461.studip.databinding.FragmentSecondBinding
|
||||
import com.denizk0461.studip.viewmodel.ParserViewModel
|
||||
import java.net.URLDecoder
|
||||
|
||||
/**
|
||||
* A simple [Fragment] subclass as the second destination in the navigation.
|
||||
*/
|
||||
class SecondFragment : Fragment() {
|
||||
class ParserFragment : Fragment() {
|
||||
|
||||
private var _binding: FragmentSecondBinding? = null
|
||||
private var html = "" // temporary storage for the website HTML
|
||||
|
||||
// This property is only valid between onCreateView and
|
||||
// onDestroyView.
|
||||
private val binding get() = _binding!!
|
||||
|
||||
private val viewModel: ParserViewModel by viewModels()
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater, container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
@@ -43,24 +45,38 @@ class SecondFragment : Fragment() {
|
||||
// findNavController().navigate(R.id.action_SecondFragment_to_FirstFragment)
|
||||
// }
|
||||
|
||||
binding.webview.settings.javaScriptEnabled = true
|
||||
binding.webview.settings.apply {
|
||||
javaScriptEnabled = true
|
||||
}
|
||||
binding.webview.webViewClient = object : WebViewClient() {
|
||||
override fun shouldOverrideUrlLoading(
|
||||
view: WebView?,
|
||||
request: WebResourceRequest?
|
||||
view: WebView,
|
||||
request: WebResourceRequest
|
||||
): Boolean {
|
||||
return false
|
||||
}
|
||||
override fun onPageFinished(view: WebView, url: String) {
|
||||
binding.webview.loadUrl(
|
||||
"javascript:window.HtmlViewer.showHTML" +
|
||||
"('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
binding.webview.loadUrl("https://elearning.uni-bremen.de/index.php?again=yes")
|
||||
|
||||
binding.fab.setOnClickListener { view ->
|
||||
binding.webview.evaluateJavascript(
|
||||
"(function(){return window.document.body.outerHTML})();"
|
||||
"(function(){return encodeURI(document.getElementsByTagName('html')[0].innerHTML)})();"
|
||||
) { p0 ->
|
||||
Log.d("HELLO", p0 ?: "nothing returned")
|
||||
viewModel.nukeEvents()
|
||||
StudIPParser().parse(URLDecoder.decode(p0, "UTF-8")) { events ->
|
||||
viewModel.insertEvents(events)
|
||||
}
|
||||
}
|
||||
// html.chunked(3000).forEach {
|
||||
// Log.d("HELLO", it)
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user