호출
ConcurrentDispatchQueueScheduler(qos: .background)
이걸 써줘서 fetchCinemaCalendar
가 메인쓰레드가 아닌 곳에서 돌도록
tempNearCinemas
.flatMap {
guard let nearestCinema = $0.first else {
return Observable<CinemaCalendar>.empty()
}
return CinemaService.shared.fetchCinemaCalendar(cinema: nearestCinema)
}
.subscribe(on: ConcurrentDispatchQueueScheduler(qos: .background))
.subscribe(onNext: tempCinemaCalendar.onNext(_:))
.disposed(by: disposeBag)
SwiftSoup 웹 크롤링
func fetchCinemaCalendar(cinema: IndieCinema = IndieCinema.list[0]) -> Observable<CinemaCalendar> {
return Observable<CinemaCalendar>.create { emitter in
let urlString = "<https://www.dtryx.com/cinema/main.do?cgid=FE8EF4D2-F22D-4802-A39A-D58F23A29C1E&BrandCd=\\(cinema.code[0])&CinemaCd=\\(cinema.code[1])>"
var cinemaCalendar = CinemaCalendar()
do {
let html = try String(contentsOf: URL(string: urlString)!, encoding: .utf8)
let doc: Document = try SwiftSoup.parse(html)
let elements = try doc.select("div.main-schedule").select("div.swiper-slide").select("a")
for element in elements.array() {
if try element.attr("class") == "btnDay disabled" {
cinemaCalendar.holidays.append(try element.attr("data-dt"))
} else {
cinemaCalendar.businessDays.append(try element.attr("data-dt"))
}
cinemaCalendar.alldays.append(try element.attr("data-dt"))
}
} catch {
print(error.localizedDescription)
}
emitter.onNext(cinemaCalendar)
return Disposables.create()
}
}