0

我有一个从 instagram 下载故事的应用程序,因此您必须登录才能输入帐户并查看联系人的故事,问题是您登录,几个小时后您必须再次登录在这种情况下,Instagram 应用程序本身会因为检测到网络钓鱼而阻止该帐户。基于 Kotlin

在这里我离开了 LoginActivity.kt

import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.view.View
import android.webkit.CookieManager
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.appcompat.app.AppCompatActivity
import androidx.room.Room
import descargar.historias.de.instagram.database.StorySaverDatabase
import descargar.historias.de.instagram.database.UserEntity
import descargar.historias.de.instagram.storage.AppStorage
import descargar.historias.de.instagram.storage.AppStorageImpl
import descargar.historias.de.instagram.utils.InstaUtils
import kotlinx.android.synthetic.main.activity_insta_login.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class LoginActivity : AppCompatActivity() {

    var redirected = false
    var appStorage: AppStorage? = null
    var db: StorySaverDatabase? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_insta_login)

        db = Room.databaseBuilder(
                this,
                StorySaverDatabase::class.java, "story-saver-db"
        ).build()
        appStorage = AppStorageImpl(this)
        val cookieManager = CookieManager.getInstance()
        cookieManager.removeAllCookies(null)

        setupWebView(webview, lyt_welcome)
    }

    private fun setupWebView(webview: WebView, lytWelcome: View) {
        // Get the web view settings instance
        val settings = webview.settings

        // Enable java script in web view
        settings.javaScriptEnabled = true

        // Enable and setup web view cache
        settings.setAppCacheEnabled(true)
        settings.cacheMode = WebSettings.LOAD_DEFAULT
        settings.setAppCachePath(cacheDir?.path)

        // Enable disable images in web view
        settings.blockNetworkImage = false
        // Whether the WebView should load image resources
        settings.loadsImagesAutomatically = true

        val userAgent = settings.userAgentString

        // Set web view client
        webview.webViewClient = object: WebViewClient() {
            override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) {

            }

            override fun onPageFinished(view: WebView, url: String) {
                // Page loading finished

                if (redirected) return

                val cookies = CookieManager.getInstance().getCookie(url)
                val cookieList = cookies?.split(";")

                val filtered = cookieList?.filter {
                    it.contains("ds_user_id") || it.contains("sessionid")
                }

                if (filtered?.size == 2) {
                    redirected = true
                    //Toast.makeText(context,
                    //        "Giriş yapıldı.", Toast.LENGTH_LONG).show()

                    appStorage?.storeString("InstaCookies", cookies)

                    val instaUtils = InstaUtils(this@LoginActivity)
                    instaUtils.setCookies(cookies)
                    instaUtils.setCsrf(null, cookies)

                    cookieList.forEach {
                        if (it.contains("ds_user_id=")) {
                            instaUtils.setUserId(it.replace("ds_user_id=", "").trim())
                        }
                        else if (it.contains("sessionid=")) {
                            instaUtils.setSessionId(it.replace("sessionid=", "").trim())
                        }
                    }

                    GlobalScope.launch {
                        val currentUser = instaUtils.getCurrentUser()

                        val userEntity = UserEntity(
                                currentUser.getUserId(),
                                currentUser.getUserName(),
                                currentUser.getRealName(),
                                currentUser.getImage(),
                                currentUser.getUserId(),
                                instaUtils.getSessionid(),
                                cookies,
                                true
                        )

                        db?.userDao()?.updateAllDefaultToFalse()
                        val id = db?.userDao()?.insert(userEntity)

                        appStorage?.storeObject("CurrentUserId", id)
                        userEntity.username?.let { appStorage?.storeString("CurrentUserName", it) }

                        var list = instaUtils.usersList()
                        if (list != null) {
                            appStorage?.storeObject("CurrentUserList", list)
                        }

                        withContext(Dispatchers.Main) {
                            val mainIntent = Intent(this@LoginActivity,
                                    MainActivity::class.java)
                            mainIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK

                            startActivity(mainIntent)
                        }
                    }
                }
                else {
                    webview.visibility = View.VISIBLE
                    lytWelcome.visibility = View.GONE
                }
            }
        }

        webview.loadUrl("https://www.instagram.com/accounts/login/")
    }
}

4

0 回答 0