Skip to content

Commit e195daf

Browse files
committed
ApiUtils.kt: refactor ApiUtils
1 parent fc8dddc commit e195daf

File tree

1 file changed

+60
-108
lines changed

1 file changed

+60
-108
lines changed

app/src/main/java/com/addev/listaspam/util/ApiUtils.kt

Lines changed: 60 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,34 @@ object ApiUtils {
2020

2121
private val client = OkHttpClient()
2222

23-
/**
24-
* Sends a POST request to the UnknownPhone API to retrieve information about the given phone number.
25-
*
26-
* The method constructs a form-encoded request with necessary parameters, sends it using OkHttp,
27-
* and interprets the response to determine if the phone number is likely to be spam.
28-
*
29-
* @param number The phone number to check, in international format.
30-
* @return `true` if the number has an average rating lower than 3 (i.e., bad or dangerous), otherwise `false`.
31-
*/
23+
private fun buildUnknownPhoneRequest(formBody: FormBody): Request {
24+
return Request.Builder()
25+
.url(UNKNOWN_PHONE_API_URL)
26+
.post(formBody)
27+
.apply {
28+
header("Connection", "Keep-Alive")
29+
header("Content-Type", "application/x-www-form-urlencoded")
30+
header("Host", "secure.unknownphone.com")
31+
header("User-Agent", "okhttp/3.14.9")
32+
}
33+
.build()
34+
}
35+
36+
private fun buildTellowsRequest(url: HttpUrl): Request {
37+
return Request.Builder()
38+
.url(url)
39+
.get()
40+
.apply {
41+
header("Connection", "Keep-Alive")
42+
header("Host", TELLOWS_API_URL)
43+
header(
44+
"User-Agent",
45+
"Dalvik/2.1.0 (Linux; U; Android 6.0; I14 Pro Max Build/MRA58K)"
46+
)
47+
}
48+
.build()
49+
}
50+
3251
fun checkListaSpamApi(number: String, lang: String): Boolean {
3352
val formBody = FormBody.Builder()
3453
.add("user_type", "free")
@@ -38,78 +57,41 @@ object ApiUtils {
3857
.add("lang", lang)
3958
.build()
4059

41-
val request = Request.Builder()
42-
.url(UNKNOWN_PHONE_API_URL)
43-
.post(formBody)
44-
.header("Connection", "Keep-Alive")
45-
.header("Content-Type", "application/x-www-form-urlencoded")
46-
.header("Host", "secure.unknownphone.com")
47-
.header("User-Agent", "okhttp/3.14.9")
48-
.build()
60+
val request = buildUnknownPhoneRequest(formBody)
4961

5062
return try {
51-
val response = client.newCall(request).execute()
52-
if (!response.isSuccessful) return false
53-
54-
val bodyString = response.body?.string() ?: return false
55-
56-
val avgRating =
57-
JSONObject(bodyString).optString("avg_ratings").toIntOrNull() ?: return false
58-
59-
// Average ratings:
60-
// 5 - safe
61-
// 4 - good
62-
// 3 - neutral
63-
// 2 - bad
64-
// 1 - dangerous
65-
avgRating < 3
63+
client.newCall(request).execute().use { response ->
64+
if (!response.isSuccessful) return false
65+
val avgRating = JSONObject(response.body?.string() ?: return false)
66+
.optString("avg_ratings").toIntOrNull() ?: return false
67+
avgRating < 3
68+
}
6669
} catch (e: Exception) {
6770
false
6871
}
6972
}
7073

71-
/**
72-
* Sends a POST report to the UnknownPhone API for a given phone number, marking it with a comment and metadata.
73-
*
74-
* This method constructs and sends a form-encoded POST request that includes the phone number, a comment,
75-
* call type, language, and optional metadata. It's used to report marketing or spam calls to the platform.
76-
*
77-
* @param phone The phone number being reported.
78-
* @param comment The user comment about the phone number.
79-
* @param lang The language code (e.g., "ES" for Spanish).
80-
* @param username Optional: The username of the person submitting the report.
81-
* @param phoneOwner Optional: A string indicating ownership or recipient identity.
82-
* @return `true` if the report was submitted successfully; `false` otherwise.
83-
*/
8474
fun reportToUnknownPhone(
8575
phone: String,
8676
comment: String,
8777
isSpam: Boolean,
8878
lang: String,
8979
): Boolean {
90-
val optRating = if (isSpam) "1" else "5"
91-
92-
val formBuilder = FormBody.Builder()
93-
.add("api_key", "d58d5bdaba8a80b2311957e9e4af885c")
80+
val formBody = FormBody.Builder()
81+
.add("api_key", UNKNOWN_PHONE_API_KEY)
9482
.add("phone", phone)
9583
.add("_action", "_submit_comment")
9684
.add("comment", comment)
9785
.add("lang", lang)
98-
.add("_opt_rating", optRating)
99-
100-
val request = Request.Builder()
101-
.url(UNKNOWN_PHONE_API_URL)
102-
.post(formBuilder.build())
103-
.header("Connection", "Keep-Alive")
104-
.header("Content-Type", "application/x-www-form-urlencoded")
105-
.header("Host", "secure.unknownphone.com")
106-
.header("User-Agent", "okhttp/3.14.9")
86+
.add("_opt_rating", if (isSpam) "1" else "5")
10787
.build()
10888

89+
val request = buildUnknownPhoneRequest(formBody)
90+
10991
return try {
110-
val response = client.newCall(request).execute()
111-
Log.d("com.addev.listaspam", response.toString())
112-
response.isSuccessful
92+
client.newCall(request).execute().use { response ->
93+
response.isSuccessful
94+
}
11395
} catch (e: Exception) {
11496
false
11597
}
@@ -128,49 +110,22 @@ object ApiUtils {
128110
.addQueryParameter("showcomments", "50")
129111
.build()
130112

131-
val request = Request.Builder()
132-
.url(url)
133-
.get()
134-
.header("Connection", "Keep-Alive")
135-
.header("Host", "www.tellows.de")
136-
.header("User-Agent", "Dalvik/2.1.0 (Linux; U; Android 6.0; I14 Pro Max Build/MRA58K)")
137-
.build()
113+
val request = buildTellowsRequest(url)
138114

139115
return try {
140-
val response = client.newCall(request).execute()
141-
if (!response.isSuccessful) return false
142-
143-
val bodyString = response.body?.string() ?: return false
144-
145-
val xml = DocumentBuilderFactory.newInstance().newDocumentBuilder()
146-
.parse(bodyString.byteInputStream())
147-
Log.d("com.addev.listaspam", "parsed")
148-
149-
150-
val scoreNode = xml.getElementsByTagName("score").item(0)
151-
val score = scoreNode?.textContent?.toIntOrNull() ?: return false
152-
Log.d("com.addev.listaspam", score.toString())
153-
154-
// Tellows scores: 1 (safe) to 9 (very dangerous)
155-
score >= 7
116+
client.newCall(request).execute().use { response ->
117+
if (!response.isSuccessful) return false
118+
val xml = DocumentBuilderFactory.newInstance().newDocumentBuilder()
119+
.parse(response.body?.byteStream() ?: return false)
120+
val score = xml.getElementsByTagName("score").item(0)
121+
?.textContent?.toIntOrNull() ?: return false
122+
score >= 7
123+
}
156124
} catch (e: Exception) {
157125
false
158126
}
159127
}
160128

161-
/**
162-
* Sends a report to Tellows about a phone number, submitting a comment and associated metadata.
163-
*
164-
* This method builds a POST request to the Tellows API with form data including the phone number,
165-
* comment, complaint type, user type, and score.
166-
*
167-
* @param phone The phone number to report (without country code prefix, if already localized).
168-
* @param comment A description of the issue or behavior associated with the number.
169-
* @param complainTypeId Type of complaint, e.g. 5 = "estafa" (scam).
170-
* @param userScore The danger score (1 = safe, 9 = dangerous).
171-
* @param lang Language and country code, e.g. "es".
172-
* @return `true` if the report was accepted; `false` otherwise.
173-
*/
174129
fun reportToTellows(
175130
phone: String,
176131
comment: String,
@@ -180,8 +135,8 @@ object ApiUtils {
180135
): Boolean {
181136
val userScore = if (isSpam) 9 else 1
182137

183-
val url = "https://www.tellows.de/basic/num/$phone" +
184-
"?xml=1&partner=androidapp&apikey=koE5hjkOwbHnmcADqZuqqq2" +
138+
val url = "https://$TELLOWS_API_URL/basic/num/$phone" +
139+
"?xml=1&partner=androidapp&apikey=$TELLOWS_API_KEY" +
185140
"&createcomment=1&country=$lang&lang=$lang&user_auth=&user_email="
186141

187142
val formBody = FormBody.Builder()
@@ -198,20 +153,17 @@ object ApiUtils {
198153
.header("Accept-Encoding", "gzip")
199154
.header("Connection", "Keep-Alive")
200155
.header("Content-Type", "application/x-www-form-urlencoded")
201-
.header("Host", "www.tellows.de")
156+
.header("Host", TELLOWS_API_URL)
202157
.header("User-Agent", "Dalvik/2.1.0 (Linux; U; Android 6.0; I14 Pro Max Build/MRA58K)")
203158
.build()
204159

205160
return try {
206-
val response = client.newCall(request).execute()
207-
val responseBody = response.body?.string() ?: return false
208-
209-
// Check for success in JSON response
210-
val json = JSONObject(responseBody)
211-
json.optBoolean("success", false)
161+
client.newCall(request).execute().use { response ->
162+
val json = JSONObject(response.body?.string() ?: return false)
163+
json.optBoolean("success", false)
164+
}
212165
} catch (e: Exception) {
213166
false
214167
}
215168
}
216-
217-
}
169+
}

0 commit comments

Comments
 (0)