1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-24 23:57:30 +00:00

try-catch for anonymous user parsing

This commit is contained in:
Austin Huang 2021-07-07 17:22:02 -04:00
parent 036edbea10
commit b135276fa2
No known key found for this signature in database
GPG Key ID: 84C23AA04587A91F
2 changed files with 52 additions and 44 deletions

View File

@ -222,7 +222,7 @@ class ProfileFragmentViewModel(
private suspend fun fetchUser( private suspend fun fetchUser(
currentUser: User?, currentUser: User?,
stateUsername: String, stateUsername: String,
): User { ): User? {
if (currentUser != null) { if (currentUser != null) {
// logged in // logged in
val tempUser = userRepository.getUsernameInfo(stateUsername) val tempUser = userRepository.getUsernameInfo(stateUsername)

View File

@ -178,51 +178,59 @@ open class GraphQLRepository(private val service: GraphQLService) {
// TODO convert string response to a response class // TODO convert string response to a response class
open suspend fun fetchUser( open suspend fun fetchUser(
username: String, username: String,
): User { ): User? {
val response = service.getUser(username) val response = service.getUser(username)
val body = JSONObject(response try {
.split("<script type=\"text/javascript\">window._sharedData = ").get(1) val body = JSONObject(
.split("</script>").get(0) response
.trim().replace(Regex("\\};$"), "}")) .split("<script type=\"text/javascript\">window._sharedData = ").get(1)
val userJson = body .split("</script>").get(0)
.getJSONObject("entry_data") .trim().replace(Regex("\\};$"), "}")
.getJSONArray("ProfilePage") )
.getJSONObject(0) val userJson = body
.getJSONObject("graphql") .getJSONObject("entry_data")
.getJSONObject(Constants.EXTRAS_USER) .getJSONArray("ProfilePage")
val isPrivate = userJson.getBoolean("is_private") .getJSONObject(0)
val id = userJson.optLong(Constants.EXTRAS_ID, 0) .getJSONObject("graphql")
val timelineMedia = userJson.getJSONObject("edge_owner_to_timeline_media") .getJSONObject(Constants.EXTRAS_USER)
// if (timelineMedia.has("edges")) { val isPrivate = userJson.getBoolean("is_private")
// final JSONArray edges = timelineMedia.getJSONArray("edges"); val id = userJson.optLong(Constants.EXTRAS_ID, 0)
// } val timelineMedia = userJson.getJSONObject("edge_owner_to_timeline_media")
var url: String? = userJson.optString("external_url") // if (timelineMedia.has("edges")) {
if (url.isNullOrBlank()) url = null // final JSONArray edges = timelineMedia.getJSONArray("edges");
return User( // }
id, var url: String? = userJson.optString("external_url")
username, if (url.isNullOrBlank()) url = null
userJson.getString("full_name"), return User(
isPrivate, id,
userJson.getString("profile_pic_url_hd"), username,
userJson.getBoolean("is_verified"), userJson.getString("full_name"),
friendshipStatus = FriendshipStatus(
userJson.optBoolean("followed_by_viewer"),
userJson.optBoolean("follows_viewer"),
userJson.optBoolean("blocked_by_viewer"),
false,
isPrivate, isPrivate,
userJson.optBoolean("has_requested_viewer"), userJson.getString("profile_pic_url_hd"),
userJson.optBoolean("requested_by_viewer"), userJson.getBoolean("is_verified"),
false, friendshipStatus = FriendshipStatus(
userJson.optBoolean("restricted_by_viewer"), userJson.optBoolean("followed_by_viewer"),
false userJson.optBoolean("follows_viewer"),
), userJson.optBoolean("blocked_by_viewer"),
mediaCount = timelineMedia.getLong("count"), false,
followerCount = userJson.getJSONObject("edge_followed_by").getLong("count"), isPrivate,
followingCount = userJson.getJSONObject("edge_follow").getLong("count"), userJson.optBoolean("has_requested_viewer"),
biography = userJson.getString("biography"), userJson.optBoolean("requested_by_viewer"),
externalUrl = url, false,
) userJson.optBoolean("restricted_by_viewer"),
false
),
mediaCount = timelineMedia.getLong("count"),
followerCount = userJson.getJSONObject("edge_followed_by").getLong("count"),
followingCount = userJson.getJSONObject("edge_follow").getLong("count"),
biography = userJson.getString("biography"),
externalUrl = url,
)
}
catch (e: Exception) {
Log.e(TAG, "fetchUser failed", e)
return null
}
} }
// TODO convert string response to a response class // TODO convert string response to a response class