diff options
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/facebook/requests/Menu.kt')
-rw-r--r-- | app/src/main/kotlin/com/pitchedapps/frost/facebook/requests/Menu.kt | 137 |
1 files changed, 84 insertions, 53 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/facebook/requests/Menu.kt b/app/src/main/kotlin/com/pitchedapps/frost/facebook/requests/Menu.kt index e83e4e43..dcb0ce10 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/facebook/requests/Menu.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/facebook/requests/Menu.kt @@ -1,3 +1,19 @@ +/* + * Copyright 2018 Allan Wang + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ package com.pitchedapps.frost.facebook.requests import com.fasterxml.jackson.annotation.JsonCreator @@ -19,28 +35,27 @@ import java.io.IOException fun RequestAuth.getMenuData(): FrostRequest<MenuData?> { val body = listOf( - "fb_dtsg" to fb_dtsg, - "__user" to userId + "fb_dtsg" to fb_dtsg, + "__user" to userId ).withEmptyData("m_sess", "__dyn", "__req", "__ajax__") return frostRequest(::parseMenu) { url("${FB_URL_BASE}bookmarks/flyout/body/?id=u_0_2") post(body.toForm()) } - } fun parseMenu(call: Call): MenuData? { val fullString = call.execute().body()?.string() ?: return null var jsonString = fullString.substringAfter("bookmarkGroups", "") - .substringAfter("[", "") + .substringAfter("[", "") if (jsonString.isBlank()) return null jsonString = "{ \"data\" : [${StringEscapeUtils.unescapeEcmaScript(jsonString)}" val mapper = ObjectMapper() - .disable(MapperFeature.AUTO_DETECT_SETTERS) + .disable(MapperFeature.AUTO_DETECT_SETTERS) return try { val data = mapper.readValue(jsonString, MenuData::class.java) @@ -48,11 +63,14 @@ fun parseMenu(call: Call): MenuData? { // parse footer content val footer = fullString.substringAfter("footerMarkup", "") - .substringAfter("{", "") - .substringBefore("}", "") - - val doc = Jsoup.parseBodyFragment(StringEscapeUtils.unescapeEcmaScript( - StringEscapeUtils.unescapeEcmaScript(footer))) + .substringAfter("{", "") + .substringBefore("}", "") + + val doc = Jsoup.parseBodyFragment( + StringEscapeUtils.unescapeEcmaScript( + StringEscapeUtils.unescapeEcmaScript(footer) + ) + ) val footerData = mutableListOf<MenuFooterItem>() val footerSmallData = mutableListOf<MenuFooterItem>() @@ -76,11 +94,14 @@ fun parseMenu(call: Call): MenuData? { } @JsonIgnoreProperties(ignoreUnknown = true) -data class MenuData(val data: List<MenuHeader> = emptyList(), - val footer: MenuFooter = MenuFooter()) { - - @JsonCreator constructor( - @JsonProperty("data") data: List<MenuHeader>? +data class MenuData( + val data: List<MenuHeader> = emptyList(), + val footer: MenuFooter = MenuFooter() +) { + + @JsonCreator + constructor( + @JsonProperty("data") data: List<MenuHeader>? ) : this(data ?: emptyList(), MenuFooter()) fun flatMapValid(): List<MenuItemData> { @@ -95,7 +116,6 @@ data class MenuData(val data: List<MenuHeader> = emptyList(), return items } - } interface MenuItemData { @@ -103,17 +123,20 @@ interface MenuItemData { } @JsonIgnoreProperties(ignoreUnknown = true) -data class MenuHeader(val id: String? = null, - val header: String? = null, - val visible: List<MenuItem> = emptyList(), - val all: List<MenuItem> = emptyList()) : MenuItemData { - - @JsonCreator constructor( - @JsonProperty("id") id: String?, - @JsonProperty("header") header: String?, - @JsonProperty("visible") visible: List<MenuItem>?, - @JsonProperty("all") all: List<MenuItem>?, - @JsonProperty("fake") fake: Boolean? +data class MenuHeader( + val id: String? = null, + val header: String? = null, + val visible: List<MenuItem> = emptyList(), + val all: List<MenuItem> = emptyList() +) : MenuItemData { + + @JsonCreator + constructor( + @JsonProperty("id") id: String?, + @JsonProperty("header") header: String?, + @JsonProperty("visible") visible: List<MenuItem>?, + @JsonProperty("all") all: List<MenuItem>?, + @JsonProperty("fake") fake: Boolean? ) : this(id, header, visible ?: emptyList(), all ?: emptyList()) override val isValid: Boolean @@ -121,41 +144,49 @@ data class MenuHeader(val id: String? = null, } @JsonIgnoreProperties(ignoreUnknown = true) -data class MenuItem(val id: String? = null, - val name: String? = null, - val pic: String? = null, - val url: String? = null, - val badge: String? = null, - val countDetails: String? = null) : MenuItemData { - - @JsonCreator constructor( - @JsonProperty("id") id: String?, - @JsonProperty("name") name: String?, - @JsonProperty("pic") pic: String?, - @JsonProperty("url") url: String?, - @JsonProperty("count") badge: String?, - @JsonProperty("count_details") countDetails: String?, - @JsonProperty("fake") fake: Boolean? - ) : this(id, name, pic?.formattedFbUrl, - url?.formattedFbUrl, - if (badge == "0") null else badge, - countDetails) +data class MenuItem( + val id: String? = null, + val name: String? = null, + val pic: String? = null, + val url: String? = null, + val badge: String? = null, + val countDetails: String? = null +) : MenuItemData { + + @JsonCreator + constructor( + @JsonProperty("id") id: String?, + @JsonProperty("name") name: String?, + @JsonProperty("pic") pic: String?, + @JsonProperty("url") url: String?, + @JsonProperty("count") badge: String?, + @JsonProperty("count_details") countDetails: String?, + @JsonProperty("fake") fake: Boolean? + ) : this( + id, name, pic?.formattedFbUrl, + url?.formattedFbUrl, + if (badge == "0") null else badge, + countDetails + ) override val isValid: Boolean get() = !name.isNullOrBlank() && !url.isNullOrBlank() } -data class MenuFooter(val data: List<MenuFooterItem> = emptyList(), - val smallData: List<MenuFooterItem> = emptyList()) { +data class MenuFooter( + val data: List<MenuFooterItem> = emptyList(), + val smallData: List<MenuFooterItem> = emptyList() +) { val hasContent get() = data.isNotEmpty() || smallData.isNotEmpty() - } -data class MenuFooterItem(val name: String? = null, - val url: String? = null, - val isSmall: Boolean = false) : MenuItemData { +data class MenuFooterItem( + val name: String? = null, + val url: String? = null, + val isSmall: Boolean = false +) : MenuItemData { override val isValid: Boolean get() = name != null && url != null -}
\ No newline at end of file +} |