Created
April 27, 2017 12:34
-
-
Save SeongUgJung/ea21769f01693c15ae7959ddc4109bf2 to your computer and use it in GitHub Desktop.
java to kotlin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pluu.support.tstore | |
import android.content.Context | |
import com.pluu.support.impl.AbstractWeekApi | |
import com.pluu.support.impl.NAV_ITEM | |
import com.pluu.support.impl.NetworkSupportApi | |
import com.pluu.webtoon.item.Status | |
import com.pluu.webtoon.item.WebToonInfo | |
import org.jsoup.Jsoup | |
import java.util.* | |
import java.util.regex.Matcher | |
import java.util.regex.Pattern | |
class TStoreWeekApi2(val context: Context, | |
var currentPosition: Int = 0) : AbstractWeekApi(context, titles) { | |
override val method: String | |
get() = NetworkSupportApi.POST | |
override val url: String | |
get() = WEEKLY_URL + (currentPosition + 1) | |
override val naviItem: NAV_ITEM | |
get() = NAV_ITEM.T_STORE | |
override fun parseMain(position: Int): List<WebToonInfo> { | |
currentPosition = position | |
val list = ArrayList<WebToonInfo>() | |
val response = requestApi() | |
val doc = Jsoup.parse(response) | |
var href: String | |
val bottom = doc.select("#weekToon0" + (position + 1) + " ul li a") | |
bottom.forEach { | |
href = it.attr("href") | |
asb( | |
{ | |
find(URL_PATTERN.matcher(href)) // find : matcher, else null | |
}, | |
{ | |
find(ID_PATTERN.matcher(it?.group())) | |
}, | |
{ matcher2 -> | |
list.add(WebToonInfo(matcher2?.group()).apply { | |
title = it.select(".detail dl dt").text() | |
image = it.select(".thum img").last().attr("src") | |
writer = it.select(".txt").text() | |
updateDate = it.select(".grade strong").text() | |
if (!it.select(".new-up").isEmpty()) { | |
// 최근 업데이트 | |
status = Status.UPDATE | |
} | |
}) | |
}) | |
} | |
return list | |
} | |
fun <T, R, V> asb(filter1: () -> T, filter2: (matcher: T) -> R, data: (matcher2: R) -> V): V? { | |
val result1 = filter1.invoke() | |
return result1?.let { | |
val result2 = filter2.invoke(it) | |
result2?.let { | |
data.invoke(it) | |
} | |
} | |
} | |
fun find(matcher: Matcher): Matcher? { | |
if (matcher.find()) { | |
return matcher | |
} else { | |
return null | |
} | |
} | |
companion object { | |
private val titles = arrayOf("월", "화", "수", "목", "금", "토", "T툰") | |
private val WEEKLY_URL = "http://m.tstore.co.kr/mobilepoc/webtoon/weekdayList.omp?weekday=" | |
private val URL_PATTERN = Pattern.compile("(?<=goInnerUrlDetail\\(\\\\\\').+(?=\\\\'\\)'\\);)") | |
private val ID_PATTERN = Pattern.compile("(?<=prodId=).+(?=&)") | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.pluu.support.tstore; | |
import android.content.Context; | |
import com.pluu.support.impl.AbstractWeekApi; | |
import com.pluu.support.impl.NAV_ITEM; | |
import com.pluu.webtoon.item.Status; | |
import com.pluu.webtoon.item.WebToonInfo; | |
import org.jsoup.Jsoup; | |
import org.jsoup.nodes.Document; | |
import org.jsoup.nodes.Element; | |
import org.jsoup.select.Elements; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
/** | |
* TStore 웹툰 Week Api | |
* Created by PLUUSYSTEM-NEW on 2015-10-31. | |
*/ | |
public class TStorerWeekApi extends AbstractWeekApi { | |
private static final String[] TITLE = new String[]{"월", "화", "수", "목", "금", "토", "T툰"}; | |
private final String WEEKLY_URL = "http://m.tstore.co.kr/mobilepoc/webtoon/weekdayList.omp?weekday="; | |
private final Pattern URL_PATTERN = Pattern.compile("(?<=goInnerUrlDetail\\(\\\\\\').+(?=\\\\'\\)'\\);)"); | |
private final Pattern ID_PATTERN = Pattern.compile("(?<=prodId=).+(?=&)"); | |
private int currentPos; | |
public TStorerWeekApi(Context context) { | |
super(context, TITLE); | |
} | |
@Override | |
public NAV_ITEM getNaviItem() { | |
return NAV_ITEM.T_STORE; | |
} | |
@Override | |
public List<WebToonInfo> parseMain(int position) { | |
this.currentPos = position; | |
ArrayList<WebToonInfo> list = new ArrayList<>(); | |
try { | |
String response = requestApi(); | |
Document doc = Jsoup.parse(response); | |
WebToonInfo item; | |
String href; | |
Matcher matcher, matcher2; | |
Elements bottom = doc.select("#weekToon0" + (position + 1) + " ul li a"); | |
for (Element a : bottom) { | |
href = a.attr("href"); | |
matcher = URL_PATTERN.matcher(href); | |
if (!matcher.find()) { | |
continue; | |
} | |
matcher2 = ID_PATTERN.matcher(matcher.group()); | |
if (!matcher2.find()) { | |
continue; | |
} | |
item = new WebToonInfo(matcher2.group()); | |
item.setTitle(a.select(".detail dl dt").text()); | |
item.setImage(a.select(".thum img").last().attr("src")); | |
item.setWriter(a.select(".txt").text()); | |
// item.setRate(a.select(".cRed1").text()); | |
item.setUpdateDate(a.select(".grade strong").text()); | |
if (!a.select(".new-up").isEmpty()) { | |
// 최근 업데이트 | |
item.setStatus(Status.UPDATE); | |
} | |
list.add(item); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return list; | |
} | |
@Override | |
public String getMethod() { | |
return POST; | |
} | |
@Override | |
public String getUrl() { | |
return WEEKLY_URL + (currentPos + 1) ; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment