Skip to content

Commit

Permalink
get suggestion from dekhvbhai
Browse files Browse the repository at this point in the history
  • Loading branch information
ratanparai committed Apr 24, 2019
1 parent 79d586a commit d1a2195
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ratanparai.moviedog.db.dao

import android.database.Cursor
import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Insert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,44 @@ import android.content.ContentProvider
import android.content.ContentValues
import android.content.UriMatcher
import android.database.Cursor
import android.database.MatrixCursor
import android.net.Uri
import android.provider.BaseColumns
import android.util.Log
import com.ratanparai.moviedog.db.AppDatabase
import com.ratanparai.moviedog.db.dao.MovieDao
import com.ratanparai.moviedog.db.entity.Movie
import com.ratanparai.moviedog.scrapper.DekhvhaiScrapper

class VideoProvider : ContentProvider() {

private val TAG = "VideoContentProvider"


private lateinit var movieDao: MovieDao

private lateinit var dekhvhaiScrapper: DekhvhaiScrapper

private lateinit var uriMatcher: UriMatcher

private val AUTHORITY = "com.ratanparai.moviedog"
private val SEARCH_SUGGEST = 1

private val queryProjection = arrayOf(
BaseColumns._ID,
SearchManager.SUGGEST_COLUMN_TEXT_1,
SearchManager.SUGGEST_COLUMN_TEXT_2,
SearchManager.SUGGEST_COLUMN_RESULT_CARD_IMAGE,
SearchManager.SUGGEST_COLUMN_PRODUCTION_YEAR,
SearchManager.SUGGEST_COLUMN_DURATION,
SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID
)

override fun onCreate(): Boolean {
movieDao = AppDatabase.getInstance(context).movieDao()
uriMatcher = buildUriMatcher()
dekhvhaiScrapper = DekhvhaiScrapper()
return true
}

Expand All @@ -48,8 +67,30 @@ class VideoProvider : ContentProvider() {
}
}

private fun search(lastPathSegment: String?): Cursor? {
TODO("Not Implemented")
private fun search(query: String?): Cursor? {
if(query != null) {
val movies = dekhvhaiScrapper.search(query)
val matrixCursor = MatrixCursor(queryProjection)

for (movie in movies) {
matrixCursor.addRow(convertMovieIntoRow(movie))
}
return matrixCursor
}
return null
}

private fun convertMovieIntoRow(movie: Movie): Array<Any> {
return arrayOf(
movie.id,
movie.title,
movie.description,
movie.cardImage,
movie.productionYear,
movie.duration,
"GLOBALSEARCH",
movie.id
)
}

private fun buildUriMatcher(): UriMatcher {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.ratanparai.moviedog.scrapper

import com.ratanparai.moviedog.Movie
import com.ratanparai.moviedog.db.entity.Movie
import org.jsoup.Jsoup
import org.jsoup.nodes.Element
import java.io.IOException
import java.util.ArrayList
import java.util.*
import java.util.concurrent.TimeUnit

class DekhvhaiScrapper {
Expand Down Expand Up @@ -53,10 +52,25 @@ class DekhvhaiScrapper {

val yearInInt = Integer.parseInt(year)

var duration = (TimeUnit.HOURS.toMillis(getHourFromTime(movieTime)) + TimeUnit.MINUTES.toMillis(getMinuteFromTime(movieTime))).toInt()


val href1 = select.attr("href")
println(href1)

var movie = Movie(
id = "1",
cardImage = cardImage,
description = description,
duration = duration,
productionYear = yearInInt,
title = title,
videoUrl = href1,
imdbId = ""
)

results.add(movie)

} catch (e: IOException) {
e.printStackTrace()
}
Expand All @@ -76,15 +90,15 @@ class DekhvhaiScrapper {
return titleWithYear.substring(titleWithYear.indexOf("(") + 1, titleWithYear.indexOf(")"))
}

fun getHourFromTime(time: String): Int {
fun getHourFromTime(time: String): Long {

val stringHour = time.substring(0, time.indexOf("H")).trim { it <= ' ' }
return Integer.parseInt(stringHour)
return stringHour.toLong()
}

fun getMinuteFromTime(time: String): Int {
fun getMinuteFromTime(time: String): Long {
val stringMinute = time.substring(time.indexOf("H") + 1, time.indexOf("M")).trim { it <= ' ' }
return Integer.parseInt(stringMinute)
return stringMinute.toLong()
}

}

0 comments on commit d1a2195

Please sign in to comment.