Skip to content

Commit

Permalink
add support for torrent categories
Browse files Browse the repository at this point in the history
  • Loading branch information
tsynik committed Apr 12, 2024
1 parent 9a2114a commit 57e4e9e
Show file tree
Hide file tree
Showing 21 changed files with 95 additions and 28 deletions.
1 change: 1 addition & 0 deletions app/src/main/java/ru/yourok/torrserve/atv/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ object Utils {
vintent.putExtra("hash", torr.hash)
vintent.putExtra("title", torr.title)
vintent.putExtra("poster", torr.poster)
vintent.putExtra("category", torr.category)
vintent.putExtra("data", torr.data)
vintent.putExtra("save", false)
return vintent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,15 @@ class ChannelProvider(private val iName: String, private val dName: String) {
.appendPath(App.context.resources.getResourceEntryName(resourceId))
.build()
}
val type = if (torr.category.equals("tv", true)) TvContractCompat.PreviewPrograms.TYPE_TV_SERIES else TvContractCompat.PreviewPrograms.TYPE_MOVIE
val preview = PreviewProgram.Builder()
.setChannelId(channelId)
.setTitle(torr.title)
.setAvailability(TvContractCompat.PreviewProgramColumns.AVAILABILITY_FREE)
.setGenre(info.joinToString(" · "))
.setIntent(Utils.buildPendingIntent(torr))
.setWeight(size)
.setType(TvContractCompat.PreviewPrograms.TYPE_MOVIE)
.setType(type)
.setSearchable(true)
.setLive(false)
.setPosterArtUri(posterUri)
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/ru/yourok/torrserve/server/api/Api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ object Api {
}

/// Torrents
fun addTorrent(link: String, title: String, poster: String, data: String, save: Boolean): Torrent {
fun addTorrent(link: String, title: String, poster: String, category: String, data: String, save: Boolean): Torrent {
val host = Net.getHostUrl("/torrents")
val req = TorrentReq("add", link = link, title = title, poster = poster, data = data, save_to_db = save).toString()
val req = TorrentReq("add", link = link, title = title, poster = poster, category = category, data = data, save_to_db = save).toString()
val resp = postJson(host, req)
return Gson().fromJson(resp, Torrent::class.java)
}
Expand Down Expand Up @@ -84,9 +84,9 @@ object Api {
postJson(host, req)
}

fun uploadTorrent(file: InputStream, title: String, poster: String, data: String, save: Boolean): Torrent {
fun uploadTorrent(file: InputStream, title: String, poster: String, category: String, data: String, save: Boolean): Torrent {
val host = Net.getHostUrl("/torrent/upload")
val resp = Net.uploadAuth(host, title, poster, data, file, save)
val resp = Net.uploadAuth(host, title, poster, category, data, file, save)
return Gson().fromJson(resp, Torrent::class.java)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class TorrentReq(
val link: String = "",
val title: String = "",
val poster: String = "",
val category: String = "",
val data: String = "",
val save_to_db: Boolean = false,
) : Request(action)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ru.yourok.torrserve.server.models.torrent
data class Torrent(
var title: String,
var poster: String,
var category: String,
var data: String,
var timestamp: Long,
var name: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fun PlayActivity.readArgs() {
"hash" -> torrentHash = this.getString(key) ?: ""
"title" -> torrentTitle = this.getString(key) ?: ""
"poster" -> torrentPoster = this.getString(key) ?: ""
"info" -> torrentData = this.getString(key) ?: ""
"category" -> torrentCategory = this.getString(key) ?: ""
"data" -> torrentData = this.getString(key) ?: ""
"fileindex" -> torrentFileIndex = this.getInt(key, -1)
"save" -> torrentSave = this.getBoolean(key)
Expand Down Expand Up @@ -60,7 +60,7 @@ fun PlayActivity.error(err: ReturnError) {
fun PlayActivity.addAndExit() {
lifecycleScope.launch(Dispatchers.IO) {
try {
addTorrent(torrentHash, torrentLink, torrentTitle, torrentPoster, torrentData, true)
addTorrent(torrentHash, torrentLink, torrentTitle, torrentPoster, torrentCategory, torrentData, true)
} catch (e: Exception) {
error(ErrLoadTorrent)
return@launch
Expand All @@ -70,7 +70,7 @@ fun PlayActivity.addAndExit() {
finish()
}

fun addTorrent(torrentHash: String, torrentLink: String, torrentTitle: String, torrentPoster: String, torrentData: String, torrentSave: Boolean): Torrent? {
fun addTorrent(torrentHash: String, torrentLink: String, torrentTitle: String, torrentPoster: String, torrentCategory: String, torrentData: String, torrentSave: Boolean): Torrent? {
return if (torrentHash.isNotEmpty()) {
try {
Api.getTorrent(torrentHash)
Expand All @@ -83,13 +83,13 @@ fun addTorrent(torrentHash: String, torrentLink: String, torrentTitle: String, t
val fis = App.context.contentResolver.openInputStream(Uri.parse(torrentLink))
fis?.let {
try {
Api.uploadTorrent(fis, torrentTitle, torrentPoster, torrentData, torrentSave)
Api.uploadTorrent(fis, torrentTitle, torrentPoster, torrentCategory, torrentData, torrentSave)
} catch (e: Exception) {
null
}
}
} else try {
Api.addTorrent(torrentLink, torrentTitle, torrentPoster, torrentData, torrentSave)
Api.addTorrent(torrentLink, torrentTitle, torrentPoster, torrentCategory, torrentData, torrentSave)
} catch (e: Exception) {
null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ object Play {

val torrent: Torrent
try {
val torr = addTorrent(torrentHash, torrentLink, torrentTitle, torrentPoster, torrentData, torrentSave)
val torr = addTorrent(torrentHash, torrentLink, torrentTitle, torrentPoster, torrentCategory, torrentData, torrentSave)
?: let {
error(ErrLoadTorrent)
return@launch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class PlayActivity : AppCompatActivity() {
var torrentHash: String = ""
var torrentTitle: String = ""
var torrentPoster: String = ""
var torrentCategory: String = ""
var torrentData: String = ""
var torrentSave: Boolean = false
var torrentFileIndex: Int = -1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class InfoDialog(private val context: Context) {
private val labelsColor = ColorStateList.valueOf(getColorFromAttr(this.context, R.attr.colorPrimary))
private val labelsTextColor = getColorFromAttr(this.context, R.attr.colorSurface)

fun show(torrLink: String, title: String, format: String, video: String, audio: String, subtitles: String, size: String, runtime: String, bitrate: String) {
fun show(torrLink: String, title: String, category: String, format: String, video: String, audio: String, subtitles: String, size: String, runtime: String, bitrate: String) {
val view = (context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater)
.inflate(R.layout.dialog_info, null) as LinearLayout? ?: return

Expand Down Expand Up @@ -138,7 +138,7 @@ class InfoDialog(private val context: Context) {
builder.setPositiveButton(R.string.add) { dlg, _ ->
CoroutineScope(Dispatchers.IO).launch {
try {
val torrent = addTorrent("", torrLink, title, "", "", true)
val torrent = addTorrent("", torrLink, title, "", category, "", true)
torrent?.let { App.toast("${context.getString(R.string.stat_string_added)}: ${it.title}") } ?: App.toast(context.getString(R.string.error_add_torrent))
} catch (e: Exception) {
e.printStackTrace()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ class AddFragment : TSFragment() {
val link = view.findViewById<TextInputEditText>(R.id.etMagnet)?.text?.toString() ?: ""
val title = view.findViewById<TextInputEditText>(R.id.etTitle)?.text?.toString() ?: ""
val poster = view.findViewById<TextInputEditText>(R.id.etPoster)?.text?.toString() ?: ""
val category = view.findViewById<TextInputEditText>(R.id.etCategory)?.text?.toString() ?: ""

if (link.isNotBlank())
lifecycleScope.launch(Dispatchers.IO) {
try {
addTorrent("", link, title, poster, "", true)
addTorrent("", link, title, poster, category, "", true)
} catch (e: Exception) {
e.printStackTrace()
App.toast(e.message ?: getString(R.string.error_retrieve_data))
Expand All @@ -79,17 +80,32 @@ class AddFragment : TSFragment() {
findViewById<Button>(R.id.btnCancel)?.setOnClickListener {
popBackStackFragment()
}
// SEARCH
// SEARCH and CATEGORY
lifecycleScope.launch {
withContext(Dispatchers.IO) {
val ver = Api.echo()
val numbers = Regex("[0-9]+").findAll(ver)
.map(MatchResult::value)
.toList()
val verMajor = numbers.firstOrNull()?.toIntOrNull() ?: 0
val rutorEnabled = loadSettings()?.EnableRutorSearch == true
val categoryEnabled = ( // MatriX.132 add Categories
ver.contains("MatriX", true) &&
verMajor > 131
)
withContext(Dispatchers.Main) {
findViewById<TextInputLayout>(R.id.tvRutor)?.apply {
visibility = if (rutorEnabled)
View.VISIBLE
else
View.GONE
}
findViewById<TextInputLayout>(R.id.tvCategory)?.apply {
visibility = if (categoryEnabled)
View.VISIBLE
else
View.GONE
}
}
}
}
Expand Down Expand Up @@ -167,7 +183,10 @@ class AddFragment : TSFragment() {
torrsAdapter.onClick = {
lifecycleScope.launch(Dispatchers.IO) {
try {
val torrent = addTorrent("", it.Magnet, it.Title, "", "", true)
val category = if (it.Categories.equals("series", true)) "tv"
else if (it.Categories.equals("movie", true)) "movie"
else it.Categories.lowercase()
val torrent = addTorrent("", it.Magnet, it.Title, "", category, "", true)
torrent?.let { App.toast("${getString(R.string.stat_string_added)}: ${it.title}") } ?: App.toast(getString(R.string.error_add_torrent))
} catch (e: Exception) {
e.printStackTrace()
Expand All @@ -178,8 +197,11 @@ class AddFragment : TSFragment() {
}
torrsAdapter.onLongClick = {
lifecycleScope.launch(Dispatchers.IO) {
val category = if (it.Categories.equals("series", true)) "tv"
else if (it.Categories.equals("movie", true)) "movie"
else it.Categories.lowercase()
val torrent: Torrent
val torr = addTorrent("", it.Magnet, it.Title, "", "", false) ?: let {
val torr = addTorrent("", it.Magnet, it.Title, "", category, "", false) ?: let {
return@launch
}
torrent = TorrentHelper.waitFiles(torr.hash) ?: let {
Expand Down Expand Up @@ -213,8 +235,11 @@ class AddFragment : TSFragment() {
if (itemPosition in torrsAdapter.list.indices) {
torrsAdapter.list[itemPosition].let {
lifecycleScope.launch(Dispatchers.IO) {
val category = if (it.Categories.equals("series", true)) "tv"
else if (it.Categories.equals("movie", true)) "movie"
else it.Categories.lowercase()
val torrent: Torrent
val torr = addTorrent("", it.Magnet, it.Title, "", "", false) ?: let {
val torr = addTorrent("", it.Magnet, it.Title, "", category, "", false) ?: let {
return@launch
}
torrent = TorrentHelper.waitFiles(torr.hash) ?: let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class TorrentsAdapter(private val activity: FragmentActivity) : BaseAdapter() {
override fun getView(position: Int, view: View?, parent: ViewGroup?): View {
val vi = view ?: LayoutInflater.from(parent?.context).inflate(R.layout.torrent_item, parent, false)

var title = list[position].title
val category = list[position].category
var title = if (category.isNotBlank()) "${category.uppercase()}${list[position].title}" else list[position].title
val poster = list[position].poster
val hash = list[position].hash
val size = list[position].torrent_size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class TorrentsFragment : TSFragment() {
intent.action = Intent.ACTION_VIEW
intent.putExtra("hash", torr.hash)
intent.putExtra("title", torr.title)
intent.putExtra("category", torr.category)
intent.putExtra("poster", torr.poster)
intent.putExtra("action", "play")
App.context.startActivity(intent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ open class InfoFragment : TSFragment() {
else
findViewById<ImageView>(R.id.ivPoster)?.visibility = View.GONE
}
val category = torr.category
val title = if (category.isNotBlank()) "${category.uppercase()}${torr.title}" else torr.title

val title = torr.title
if (title.isEmpty())
findViewById<TextView>(R.id.tvTitle).visibility = View.GONE
else {
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/ru/yourok/torrserve/utils/Net.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ object Net {
return ""
}

fun uploadAuth(url: String, title: String, poster: String, data: String, file: InputStream, save: Boolean): String {
fun uploadAuth(url: String, title: String, poster: String, category: String, data: String, file: InputStream, save: Boolean): String {
val req = Jsoup.connect(url)
.data("file1", "filename", file)
.ignoreHttpErrors(true)
Expand All @@ -50,6 +50,7 @@ object Net {
req.data("save", "true")
req.data("title", title)
req.data("poster", poster)
req.data("category", category)
req.data("data", data)

val auth = getAuthB64()
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/ru/yourok/torrserve/utils/TorrentHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,12 @@ object TorrentHelper {
}
}
val title = format.tags?.title ?: torrent.title
val category = torrent.category
val size = Format.byteFmt(ffp.format.size.toDouble())
val duration = Format.durFmtS(ffp.format.duration.toDouble())
val bitrate = Format.speedFmt(ffp.format.bit_rate.toDouble() / 8)
withContext(Dispatchers.Main) {
InfoDialog(context).show(torrLink, title.trim(), format.format_long_name, videoDesc.joinToString(""), audioDesc.joinToString(""), subsDesc.joinToString(""), size, duration, bitrate)
InfoDialog(context).show(torrLink, title.trim(), category, format.format_long_name, videoDesc.joinToString(""), audioDesc.joinToString(""), subsDesc.joinToString(""), size, duration, bitrate)
}
} catch (e: Exception) {
e.message?.let { App.toast(it) }
Expand Down
34 changes: 28 additions & 6 deletions app/src/main/res/layout/add_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@
android:id="@+id/adder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:constraint_referenced_ids="textView1,textView2,textView3" />
app:constraint_referenced_ids="tvMagnet,tvTitle,tvPoster,tvCategory" />

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textView1"
android:id="@+id/tvMagnet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
Expand All @@ -74,13 +74,13 @@
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textView2"
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:hint="@string/title"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView1">
app:layout_constraintTop_toBottomOf="@+id/tvMagnet">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/etTitle"
Expand All @@ -91,15 +91,15 @@
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textView3"
android:id="@+id/tvPoster"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:hint="@string/poster_link"
app:helperText="@string/poster_link_help"
app:helperTextEnabled="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2">
app:layout_constraintTop_toBottomOf="@+id/tvTitle">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/etPoster"
Expand All @@ -109,6 +109,28 @@
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/tvCategory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:hint="@string/torrent_category"
app:helperText="@string/torrent_category_help"
app:helperTextEnabled="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvPoster"
android:visibility="gone"
tools:visibility="visible">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/etCategory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
</com.google.android.material.textfield.TextInputLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

<com.google.android.material.floatingactionbutton.FloatingActionButton
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-bg/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@
<string name="poster">Пострер</string>
<string name="poster_link">Постер URL</string>
<string name="poster_link_help">Въведете URL адрес към постер (опционално)</string>
<string name="torrent_category">Категория</string>
<string name="torrent_category_help">Задаване на категория за торент (по избор)</string>
<string name="local_ip">Локално IP</string>
<string name="connected_host">Свързан</string>
<string name="find_hosts">Открий TorrServ адреси</string>
Expand Down
Loading

0 comments on commit 57e4e9e

Please sign in to comment.