aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/facebook/UsernameFetcher.kt
blob: da244ba3cc18d0f382e1dcbbffb3bdd7d6a2aa82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.pitchedapps.frost.facebook

import com.pitchedapps.frost.dbflow.CookieModel
import com.pitchedapps.frost.dbflow.saveFbCookie
import com.pitchedapps.frost.events.FbAccountEvent
import com.pitchedapps.frost.utils.L
import org.greenrobot.eventbus.EventBus
import org.jsoup.Jsoup
import kotlin.concurrent.thread

/**
 * Created by Allan Wang on 2017-06-02.
 */
object UsernameFetcher {

    fun fetch(data: CookieModel, sender: Int) {
        thread {
            try {
                val title = Jsoup.connect(FbTab.PROFILE.url)
                        .cookie(FACEBOOK_COM, data.cookie)
                        .get().title()
                L.d("User name found: $title")
                data.name = title
            } catch (e: Exception) {
                L.e("User name fetching failed: ${e.message}")
                data.name = ""
            } finally {
                if (data.name != null) {
                    saveFbCookie(data)
                    EventBus.getDefault().post(FbAccountEvent(data, sender, FbAccountEvent.FLAG_USER_NAME))
                }
            }
        }
    }

}