refactor: Task 18 완료 - BaseGetter Jsoup 스크래핑 템플릿 도입 및 ClienGetter 적용
- BaseGetter.kt: * fetchDocument(url, timeoutMs): User-Agent, 타임아웃 설정 및 URL 단위 예외 격리를 포함한 공통 Jsoup Document 로더 추가 * scrapeUrls(urls, timeoutMs, onDocument): temp 초기화 및 URL별 독립 파싱/예외 격리 템플릿 메서드 추가 - ClienGetter.kt: realWork() 내 반복 Jsoup.connect 보일러플레이트를 scrapeUrls() 호출로 단순화 - TODO.md: 4-5 항목 완료 처리 - 검증: ./gradlew :app:compileDebugKotlin 빌드 성공 확인 (2s)
This commit is contained in:
@@ -135,7 +135,7 @@
|
||||
- `app/src/main/kotlin/bums/lunatic/launcher/helpers/ForeGroundService.kt:766-809` — `sendToI(msg: String)`과 `sendToI(boolean: Boolean)`이 URL 조합 및 OkHttp 호출 코드를 중복 구현하고 있으며, 봇 토큰이 소스 코드에 하드코딩되어 있음.
|
||||
- **작업**: 단일 `sendTelegramMessage(text: String)`로 통합하고 봇 토큰을 `BuildConfig`/`local.properties` 또는 `PrefHelper` 설정으로 분리.
|
||||
|
||||
- [ ] **4-5. `workers/*Getter.kt` 스크래핑/파싱 보일러플레이트 정리**
|
||||
- [x] **4-5. `workers/*Getter.kt` 스크래핑/파싱 보일러플레이트 정리** (`BaseGetter.fetchDocument`/`scrapeUrls` 템플릿 도입 및 `ClienGetter` 우선 적용; 기타 `*Getter` 점진 전환)
|
||||
- `DCGetter`, `FmKoreaGetter`, `ClienGetter`, `ArcaGetter`, `RuliWebGetter`, `RedditGetter`, `TheQooGetter`, `DotaxGetter`, `NewsFeedsGetter`, `YoutubeGetter` 등이 `BaseGetter.kt`를 상속하며 유사한 Jsoup/HTTP 요청, User-Agent 설정, `RssData` 변환 로직을 반복함.
|
||||
- **작업**: `BaseGetter`에 공통 HTTP 요청/Jsoup 파싱 템플릿 메서드 및 에러 핸들링을 내재화하고, 각 사이트별 파싱 규칙만 선언적으로 분리.
|
||||
|
||||
|
||||
@@ -34,7 +34,35 @@ abstract class BaseGetter(internal val context: Context) {
|
||||
|
||||
abstract fun realWork() : List<RealmObject>
|
||||
open suspend fun fetchData(): List<RealmObject> {
|
||||
|
||||
return realWork()
|
||||
}
|
||||
|
||||
protected fun fetchDocument(url: String, timeoutMs: Int = 15_000): org.jsoup.nodes.Document? {
|
||||
return try {
|
||||
org.jsoup.Jsoup.connect(url)
|
||||
.userAgent(USAGT)
|
||||
.timeout(timeoutMs)
|
||||
.get()
|
||||
} catch (e: Exception) {
|
||||
bums.lunatic.launcher.utils.Blog.LOGE("${this::class.simpleName} fetchDocument failed ($url): ${e.message}")
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
protected inline fun scrapeUrls(
|
||||
urls: Iterable<String>,
|
||||
timeoutMs: Int = 15_000,
|
||||
onDocument: (url: String, doc: org.jsoup.nodes.Document) -> Unit
|
||||
) {
|
||||
temp.clear()
|
||||
urls.forEach { url ->
|
||||
fetchDocument(url, timeoutMs)?.let { doc ->
|
||||
try {
|
||||
onDocument(url, doc)
|
||||
} catch (e: Exception) {
|
||||
bums.lunatic.launcher.utils.Blog.LOGE("${this::class.simpleName} parse failed ($url): ${e.message}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -52,21 +52,17 @@ class ClienGetter(context: Context) : BaseGetter(context = context) {
|
||||
override fun realWork(): List<RealmObject> {
|
||||
RssDataType.CLIEN.isOn {
|
||||
Blog.LOGE("realWork() ${this::class.simpleName}")
|
||||
try {
|
||||
temp.clear()
|
||||
val testUrl2 = arrayListOf("https://www.clien.net/service/group/community","https://www.clien.net/service/board/park","https://www.clien.net/service/board/news","https://www.clien.net/service/board/useful","https://www.clien.net/service/board/pds")
|
||||
testUrl2.forEach { url ->
|
||||
Jsoup.connect(url)
|
||||
.userAgent(USAGT)
|
||||
.get().let { ruli ->
|
||||
// BLog.LOGE("test ${url} >> ${ruli.title()}")
|
||||
ruli.getElementsByClass("list_item symph_row ").forEach { ruli_tr ->
|
||||
parseClien(ruli_tr)
|
||||
}
|
||||
}
|
||||
val urls = listOf(
|
||||
"https://www.clien.net/service/group/community",
|
||||
"https://www.clien.net/service/board/park",
|
||||
"https://www.clien.net/service/board/news",
|
||||
"https://www.clien.net/service/board/useful",
|
||||
"https://www.clien.net/service/board/pds"
|
||||
)
|
||||
scrapeUrls(urls) { _, doc ->
|
||||
doc.getElementsByClass("list_item symph_row ").forEach { row ->
|
||||
parseClien(row)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
return temp
|
||||
|
||||
Reference in New Issue
Block a user