add aod parser
This commit is contained in:
35
app/src/main/java/org/mosad/teapod/MainActivity.kt
Normal file
35
app/src/main/java/org/mosad/teapod/MainActivity.kt
Normal file
@ -0,0 +1,35 @@
|
||||
package org.mosad.teapod
|
||||
|
||||
import android.os.Bundle
|
||||
import com.google.android.material.bottomnavigation.BottomNavigationView
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.navigation.findNavController
|
||||
import androidx.navigation.ui.AppBarConfiguration
|
||||
import androidx.navigation.ui.setupActionBarWithNavController
|
||||
import androidx.navigation.ui.setupWithNavController
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
import org.mosad.teapod.parser.AoDParser
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_main)
|
||||
val navView: BottomNavigationView = findViewById(R.id.nav_view)
|
||||
|
||||
val navController = findNavController(R.id.nav_host_fragment)
|
||||
// Passing each menu ID as a set of Ids because each
|
||||
// menu should be considered as top level destinations.
|
||||
val appBarConfiguration = AppBarConfiguration(setOf(
|
||||
R.id.navigation_home, R.id.navigation_library, R.id.navigation_search))
|
||||
setupActionBarWithNavController(navController, appBarConfiguration)
|
||||
navView.setupWithNavController(navController)
|
||||
|
||||
load()
|
||||
}
|
||||
|
||||
private fun load() {
|
||||
// TODO
|
||||
}
|
||||
}
|
57
app/src/main/java/org/mosad/teapod/dummy/DummyContent.kt
Normal file
57
app/src/main/java/org/mosad/teapod/dummy/DummyContent.kt
Normal file
@ -0,0 +1,57 @@
|
||||
package org.mosad.teapod.dummy
|
||||
|
||||
import java.util.ArrayList
|
||||
import java.util.HashMap
|
||||
|
||||
/**
|
||||
* Helper class for providing sample content for user interfaces created by
|
||||
* Android template wizards.
|
||||
*
|
||||
* TODO: Replace all uses of this class before publishing your app.
|
||||
*/
|
||||
object DummyContent {
|
||||
|
||||
/**
|
||||
* An array of sample (dummy) items.
|
||||
*/
|
||||
val ITEMS: MutableList<DummyItem> = ArrayList()
|
||||
|
||||
/**
|
||||
* A map of sample (dummy) items, by ID.
|
||||
*/
|
||||
val ITEM_MAP: MutableMap<String, DummyItem> = HashMap()
|
||||
|
||||
private val COUNT = 25
|
||||
|
||||
init {
|
||||
// Add some sample items.
|
||||
for (i in 1..COUNT) {
|
||||
addItem(createDummyItem(i))
|
||||
}
|
||||
}
|
||||
|
||||
private fun addItem(item: DummyItem) {
|
||||
ITEMS.add(item)
|
||||
ITEM_MAP.put(item.id, item)
|
||||
}
|
||||
|
||||
private fun createDummyItem(position: Int): DummyItem {
|
||||
return DummyItem(position.toString(), "Item " + position, makeDetails(position))
|
||||
}
|
||||
|
||||
private fun makeDetails(position: Int): String {
|
||||
val builder = StringBuilder()
|
||||
builder.append("Details about Item: ").append(position)
|
||||
for (i in 0..position - 1) {
|
||||
builder.append("\nMore details information here.")
|
||||
}
|
||||
return builder.toString()
|
||||
}
|
||||
|
||||
/**
|
||||
* A dummy item representing a piece of content.
|
||||
*/
|
||||
data class DummyItem(val id: String, val content: String, val details: String) {
|
||||
override fun toString(): String = content
|
||||
}
|
||||
}
|
92
app/src/main/java/org/mosad/teapod/parser/AoDParser.kt
Normal file
92
app/src/main/java/org/mosad/teapod/parser/AoDParser.kt
Normal file
@ -0,0 +1,92 @@
|
||||
package org.mosad.teapod.parser
|
||||
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.jsoup.Connection
|
||||
import org.jsoup.Jsoup
|
||||
import org.mosad.teapod.util.GUIMedia
|
||||
|
||||
class AoDParser {
|
||||
|
||||
private val baseURL = "https://www.anime-on-demand.de"
|
||||
private val loginPath = "/users/sign_in"
|
||||
|
||||
private val login = ""
|
||||
private val pwd = ""
|
||||
|
||||
private fun login(): MutableMap<String, String> = runBlocking {
|
||||
|
||||
|
||||
val userAgent = "Mozilla/5.0 (X11; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0"
|
||||
|
||||
withContext(Dispatchers.Default) {
|
||||
val con = Jsoup.connect(baseURL)
|
||||
|
||||
|
||||
// get the authenticity token
|
||||
val resAuth = con.url(baseURL + loginPath)//Jsoup.connect(baseURL + loginPath)
|
||||
.header("User-Agent", userAgent)
|
||||
.execute()
|
||||
|
||||
val authenticityToken = resAuth.parse().select("meta[name=csrf-token]").attr("content")
|
||||
println("Authenticity token is: $authenticityToken")
|
||||
|
||||
val cookies = resAuth.cookies()
|
||||
println("cookies: $cookies")
|
||||
|
||||
val data = mapOf(
|
||||
Pair("user[login]", login),
|
||||
Pair("user[password]", pwd),
|
||||
Pair("user[remember_me]", "1"),
|
||||
Pair("commit", "Einloggen"),
|
||||
Pair("authenticity_token", authenticityToken)
|
||||
)
|
||||
|
||||
val resLogin = Jsoup.connect(baseURL + loginPath)
|
||||
.method(Connection.Method.POST)
|
||||
.data(data)
|
||||
.postDataCharset("UTF-8")
|
||||
.cookies(cookies)
|
||||
.execute()
|
||||
|
||||
//println(resLogin.body())
|
||||
|
||||
val loginSuccess = resLogin.body().contains("Hallo, du bist jetzt angemeldet.")
|
||||
println("Status: ${resLogin.statusCode()} (${resLogin.statusMessage()}), login successful: $loginSuccess")
|
||||
|
||||
return@withContext resLogin.cookies()
|
||||
}
|
||||
}
|
||||
|
||||
// https://www.anime-on-demand.de/animes
|
||||
fun listAnime(): ArrayList<GUIMedia> = runBlocking {
|
||||
withContext(Dispatchers.Default) {
|
||||
val cookies = login()
|
||||
|
||||
|
||||
val res = Jsoup.connect("$baseURL/animes")
|
||||
.cookies(cookies)
|
||||
.get()
|
||||
|
||||
//println(res)
|
||||
|
||||
val anime = arrayListOf<GUIMedia>()
|
||||
res.select("div.animebox").forEach {
|
||||
val media = GUIMedia(
|
||||
it.select("h3.animebox-title").text(),
|
||||
it.select("p.animebox-image").select("img").attr("src"),
|
||||
it.select("p.animebox-link").select("a").attr("href"),
|
||||
it.select("p.animebox-shorttext").text()
|
||||
)
|
||||
|
||||
anime.add(media)
|
||||
}
|
||||
|
||||
println("got ${anime.size} anime")
|
||||
|
||||
return@withContext anime
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package org.mosad.teapod.preferences
|
||||
|
||||
object Preferences {
|
||||
|
||||
var login = ""
|
||||
internal set
|
||||
var password = ""
|
||||
internal set
|
||||
|
||||
|
||||
fun saveCredentials(login: String, password: String) {
|
||||
this.login = login
|
||||
this.password = password
|
||||
|
||||
// TODO save
|
||||
}
|
||||
|
||||
fun load() {
|
||||
// TODO
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package org.mosad.teapod.ui.account
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.fragment.app.Fragment
|
||||
import kotlinx.android.synthetic.main.fragment_account.*
|
||||
import org.mosad.teapod.R
|
||||
|
||||
class AccountFragment : Fragment() {
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
||||
return inflater.inflate(R.layout.fragment_account, container, false)
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
text_account.text = "This is the Account Fragment"
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package org.mosad.teapod.ui.components
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
import android.widget.LinearLayout
|
||||
import com.bumptech.glide.Glide
|
||||
import kotlinx.android.synthetic.main.linear_media.view.*
|
||||
import org.mosad.teapod.R
|
||||
|
||||
|
||||
class MediaLinearLayout(context: Context?) : LinearLayout(context) {
|
||||
|
||||
init {
|
||||
inflate(context, R.layout.linear_media, this)
|
||||
}
|
||||
|
||||
fun setTitle(title: String): MediaLinearLayout = apply {
|
||||
text_title.text = title
|
||||
}
|
||||
|
||||
fun setPoster(url: String): MediaLinearLayout = apply {
|
||||
Glide.with(context)
|
||||
.load(url)
|
||||
.into(image_poster)
|
||||
}
|
||||
|
||||
|
||||
}
|
32
app/src/main/java/org/mosad/teapod/ui/home/HomeFragment.kt
Normal file
32
app/src/main/java/org/mosad/teapod/ui/home/HomeFragment.kt
Normal file
@ -0,0 +1,32 @@
|
||||
package org.mosad.teapod.ui.home
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.TextView
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.lifecycle.ViewModelProviders
|
||||
import org.mosad.teapod.R
|
||||
|
||||
class HomeFragment : Fragment() {
|
||||
|
||||
private lateinit var homeViewModel: HomeViewModel
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
||||
homeViewModel = ViewModelProviders.of(this).get(HomeViewModel::class.java)
|
||||
val root = inflater.inflate(R.layout.fragment_home, container, false)
|
||||
val textView: TextView = root.findViewById(R.id.text_home)
|
||||
homeViewModel.text.observe(viewLifecycleOwner, Observer {
|
||||
textView.text = it
|
||||
})
|
||||
return root
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
println("created!!!!")
|
||||
}
|
||||
}
|
13
app/src/main/java/org/mosad/teapod/ui/home/HomeViewModel.kt
Normal file
13
app/src/main/java/org/mosad/teapod/ui/home/HomeViewModel.kt
Normal file
@ -0,0 +1,13 @@
|
||||
package org.mosad.teapod.ui.home
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
|
||||
class HomeViewModel : ViewModel() {
|
||||
|
||||
private val _text = MutableLiveData<String>().apply {
|
||||
value = "This is the home Fragment"
|
||||
}
|
||||
val text: LiveData<String> = _text
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
package org.mosad.teapod.ui.library
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.ArrayAdapter
|
||||
import androidx.fragment.app.Fragment
|
||||
import kotlinx.android.synthetic.main.fragment_library.*
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.mosad.teapod.R
|
||||
import org.mosad.teapod.parser.AoDParser
|
||||
import org.mosad.teapod.ui.components.MediaLinearLayout
|
||||
import org.mosad.teapod.util.CustomAdapter
|
||||
import org.mosad.teapod.util.GUIMedia
|
||||
|
||||
class LibraryFragment : Fragment() {
|
||||
|
||||
private var mediaList = arrayListOf<GUIMedia>()
|
||||
private lateinit var adapter : CustomAdapter
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
||||
val root = inflater.inflate(R.layout.fragment_library, container, false)
|
||||
return root
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
|
||||
adapter = CustomAdapter(requireContext(),layoutInflater, mediaList)//ArrayAdapter(requireContext(), R.layout.linear_media, R.id.text_title, mediaList)
|
||||
|
||||
list_library.adapter = adapter
|
||||
|
||||
text_dashboard.text = "Loading Animes ..."
|
||||
|
||||
loadAnimeList()
|
||||
}
|
||||
|
||||
private fun loadAnimeList() = GlobalScope.launch {
|
||||
|
||||
val parser = AoDParser()
|
||||
mediaList = parser.listAnime()
|
||||
|
||||
text_dashboard.text = "got ${mediaList.size} animes"
|
||||
|
||||
withContext(Dispatchers.Main) {
|
||||
adapter.notifyDataSetChanged()
|
||||
println("notifiyed")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package org.mosad.teapod.ui.library
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
|
||||
class LibraryViewModel : ViewModel() {
|
||||
|
||||
private val _text = MutableLiveData<String>().apply {
|
||||
value = "This is the library Fragment"
|
||||
}
|
||||
val text: LiveData<String> = _text
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package org.mosad.teapod.ui.search
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.TextView
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.Observer
|
||||
import androidx.lifecycle.ViewModelProviders
|
||||
import org.mosad.teapod.R
|
||||
|
||||
class SearchFragment : Fragment() {
|
||||
|
||||
private lateinit var notificationsViewModel: SearchViewModel
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View? {
|
||||
notificationsViewModel =
|
||||
ViewModelProviders.of(this).get(SearchViewModel::class.java)
|
||||
val root = inflater.inflate(R.layout.fragment_search, container, false)
|
||||
val textView: TextView = root.findViewById(R.id.text_notifications)
|
||||
notificationsViewModel.text.observe(viewLifecycleOwner, Observer {
|
||||
textView.text = it
|
||||
})
|
||||
return root
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package org.mosad.teapod.ui.search
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
|
||||
class SearchViewModel : ViewModel() {
|
||||
|
||||
private val _text = MutableLiveData<String>().apply {
|
||||
value = "This is the search Fragment"
|
||||
}
|
||||
val text: LiveData<String> = _text
|
||||
}
|
33
app/src/main/java/org/mosad/teapod/util/CustomAdapter.kt
Normal file
33
app/src/main/java/org/mosad/teapod/util/CustomAdapter.kt
Normal file
@ -0,0 +1,33 @@
|
||||
package org.mosad.teapod.util
|
||||
|
||||
import android.content.Context
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.ArrayAdapter
|
||||
import android.widget.ImageView
|
||||
import android.widget.TextView
|
||||
import com.bumptech.glide.Glide
|
||||
import org.mosad.teapod.R
|
||||
|
||||
class CustomAdapter(context: Context, private val layoutInflater: LayoutInflater, private val media: ArrayList<GUIMedia>) : ArrayAdapter<GUIMedia>(context, R.layout.linear_media) {
|
||||
|
||||
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
|
||||
println("view")
|
||||
|
||||
return if (convertView == null) {
|
||||
val guiMedia = layoutInflater.inflate(R.layout.linear_media, null)
|
||||
|
||||
val textTitle = guiMedia.findViewById<TextView>(R.id.text_title)
|
||||
val imagePoster = guiMedia.findViewById<ImageView>(R.id.image_poster)
|
||||
|
||||
textTitle.text = media[position].title
|
||||
Glide.with(context).load(media[position].imageLink).into(imagePoster)
|
||||
|
||||
guiMedia
|
||||
} else {
|
||||
convertView
|
||||
}
|
||||
}
|
||||
|
||||
}
|
3
app/src/main/java/org/mosad/teapod/util/DataTypes.kt
Normal file
3
app/src/main/java/org/mosad/teapod/util/DataTypes.kt
Normal file
@ -0,0 +1,3 @@
|
||||
package org.mosad.teapod.util
|
||||
|
||||
data class GUIMedia(val title: String, val imageLink: String, val shortDesc : String, val link: String)
|
Reference in New Issue
Block a user