PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/app/src/main/java/ro/codetwisters/series/android/io/network/filelist/FileListHelper.kt

https://bitbucket.org/codetwister/series
Kotlin | 83 lines | 61 code | 17 blank | 5 comment | 2 complexity | 8b7db41f99f0ce47cc36289caad1bd1a MD5 | raw file
  1. package ro.codetwisters.series.android.io.network.filelist
  2. import android.net.Uri
  3. import org.xml.sax.InputSource
  4. import org.xml.sax.SAXException
  5. import org.xml.sax.helpers.XMLReaderFactory
  6. import ro.codetwisters.series.xentities.SimpleCredential
  7. import ro.codetwisters.series.xentities.torrent.TorrentResult
  8. import java.io.*
  9. import java.net.*
  10. import java.util.*
  11. /**
  12. * helper methods for file list stuff
  13. */
  14. internal class FileListHelper(private val flCredential: SimpleCredential) {
  15. fun loginToFl() {
  16. try {
  17. val flUrl = URL("http://filelist.ro/takelogin.php")
  18. val conn = flUrl.openConnection() as HttpURLConnection
  19. conn.readTimeout = 10000
  20. conn.connectTimeout = 15000
  21. conn.requestMethod = "POST"
  22. conn.doInput = true
  23. conn.doOutput = true
  24. val os = conn.outputStream
  25. val writer = BufferedWriter(
  26. OutputStreamWriter(os, "UTF-8"))
  27. writer.write(String.format("username=%s&password=%s", flCredential.username, flCredential.pass))
  28. writer.flush()
  29. writer.close()
  30. os.close()
  31. conn.connect()
  32. val br = BufferedReader(InputStreamReader(conn.inputStream))
  33. // TODO there must be a more stable alternative to do this.
  34. while (br.readLine() != null) {
  35. // NO-OP but for some reason we must do this
  36. }
  37. } catch (e: IOException) {
  38. e.printStackTrace()
  39. }
  40. }
  41. fun searchTorrent(searchTerm: String): ArrayList<TorrentResult> {
  42. try {
  43. val browseUrl = URL(String.format("http://filelist.ro/browse.php?search=%s&cat=0&searchin=0&sort=0", Uri.encode(searchTerm)))
  44. val urlConnection = browseUrl.openConnection() as HttpURLConnection
  45. val xr = XMLReaderFactory.createXMLReader("org.ccil.cowan.tagsoup.Parser")
  46. val handler = FileListParserHandler()
  47. xr.contentHandler = handler
  48. val inStream = InputSource()
  49. inStream.byteStream = urlConnection.inputStream
  50. xr.parse(inStream)
  51. return handler.resultList
  52. } catch (e: IOException) {
  53. e.printStackTrace()
  54. } catch (e: SAXException) {
  55. e.printStackTrace()
  56. }
  57. return ArrayList()
  58. }
  59. companion object {
  60. init {
  61. val cookieManager = CookieManager()
  62. CookieHandler.setDefault(cookieManager)
  63. cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL)
  64. }
  65. }
  66. }