Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Volkmar Vogel committed May 13, 2018
2 parents 55afa12 + de648ec commit c65ef0a
Show file tree
Hide file tree
Showing 15 changed files with 1,498 additions and 6,468 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ dependencies {
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.3.0'
compile 'com.github.stupacki:MultiFunctions:1.1.2'
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
}

buildscript {
ext.kotlin_version = '1.1.3-2'
ext.kotlin_version = '1.2.41'
repositories {
mavenCentral()
jcenter()
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/dyrosoft/kvbparser/api/ApiProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ internal object ApiProvider {

private class HeaderInterceptor : Interceptor {

@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response =
chain.proceed(chain.request()
.newBuilder()
Expand All @@ -23,7 +22,7 @@ internal object ApiProvider {
}
}

private const val BASE_URL = "http://www.kvb-koeln.de/"
private const val BASE_URL = "https://www.kvb.koeln/"

val kvbApi: KvbApi
get() = buildRetrofit().create(KvbApi::class.java)
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/dyrosoft/kvbparser/api/KvbApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ internal interface KvbApi {
@GET("qr/{stationId}/")
fun queryDepartures(@Path("stationId") stationId: String): Single<Response<ResponseBody>>

@GET("german/hst/showline/{stationId}/{lineId}/")
@GET("haltestellen/showline/{stationId}/{lineId}/")
fun queryLineDetails(@Path("stationId") stationId: String,
@Path("lineId") lineId: String): Single<Response<ResponseBody>>

@GET("german/hst/overview/{stationId}/")
@GET("haltestellen/overview/{stationId}/")
fun queryStationDetail(@Path("stationId") stationId: String): Single<Response<ResponseBody>>

@GET("german/hst/overview/")
@GET("haltestellen/overview/")
fun queryStations(): Single<Response<ResponseBody>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import com.dyrosoft.kvbparser.parser.DepartureInformationParserFunc
import com.dyrosoft.kvbparser.parser.DeparturesParserFunc
import rx.Single

internal class DeparturesMergerTransformer : Single.Transformer<String, Departures> {
class DeparturesMergerTransformer : Single.Transformer<String, Departures> {

override fun call(htmlSingle: Single<String>): Single<Departures> {
return Single.zip(htmlSingle.flatMap(DeparturesParserFunc()),
htmlSingle.flatMap(DepartureInformationParserFunc())
) { departures, departureInformation -> Departures(departures, departureInformation) }
htmlSingle.flatMap(DepartureInformationParserFunc())) { departures, departureInformation -> Departures(departures, departureInformation) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.dyrosoft.kvbparser.parser
import rx.Single
import rx.functions.Func1

internal abstract class AbstractRxHtmlParserFunc<T> : Func1<String, Single<T>> {
abstract class AbstractRxHtmlParserFunc<T> : Func1<String, Single<T>> {

override fun call(html: String): Single<T> = Single.just(parse(html))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package com.dyrosoft.kvbparser.parser
import com.dyrosoft.kvbparser.utils.StringUtils
import org.jsoup.Jsoup

internal class DepartureInformationParserFunc : AbstractRxHtmlParserFunc<List<String>>() {
class DepartureInformationParserFunc : AbstractRxHtmlParserFunc<List<String>>() {

override fun parse(html: String): List<String> {
val list = mutableListOf<String>()
val document = Jsoup.parse(html)
val elements = document.select("table.qr_table")
val elements = document.select("table")
elements[0].getElementsByTag("tr")
.map { it.getElementsByTag("td") }
.forEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ import com.dyrosoft.kvbparser.utils.StringUtils
import io.multifunctions.letNotNull
import org.jsoup.Jsoup

internal class DeparturesParserFunc : AbstractRxHtmlParserFunc<List<Departure>>() {
class DeparturesParserFunc : AbstractRxHtmlParserFunc<List<Departure>>() {

override fun parse(html: String): List<Departure> {
val list = mutableListOf<Departure>()
val document = Jsoup.parse(html)
val elements = document.select("table.qr_table")
elements[1].getElementsByTag("tr")
.map { it.getElementsByTag("td") }
.forEach {
val elements = document.select("table.display")
elements?.first()
?.getElementsByTag("tbody")
?.first()
?.getElementsByTag("tr")
?.map { it.getElementsByTag("td") }
?.forEach {
Triple(StringUtils.advancedTrim(it[0].text()),
StringUtils.advancedTrim(it[1].text()),
StringUtils.advancedTrim(it[2].text())).letNotNull { line, direction, waitTime ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.dyrosoft.kvbparser.models.Line
import org.jsoup.Jsoup
import java.util.regex.Pattern

internal class StationLinesParserFuc : AbstractRxHtmlParserFunc<List<Line>>() {
class StationLinesParserFuc : AbstractRxHtmlParserFunc<List<Line>>() {

override fun parse(html: String): List<Line> {
val list = mutableListOf<Line>()
Expand All @@ -17,13 +17,13 @@ internal class StationLinesParserFuc : AbstractRxHtmlParserFunc<List<Line>>() {
continue
}

val pattern = Pattern.compile("/german/hst/showline/(\\d+)/(\\d+)/")
val pattern = Pattern.compile("/haltestellen/showline/(\\d+)/(\\d+)/")
val matcher = pattern.matcher(href)
if (!matcher.matches()) {
continue
}

val temp = href.replace("/german/hst/showline/", "")
val temp = href.replace("/haltestellen/showline/", "")
val id = temp.split("/".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[1]

list.add(Line(id))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.dyrosoft.kvbparser.models.Station
import org.jsoup.Jsoup
import java.util.regex.Pattern

internal class StationsParserFuc : AbstractRxHtmlParserFunc<List<Station>>() {
class StationsParserFuc : AbstractRxHtmlParserFunc<List<Station>>() {

override fun parse(html: String): List<Station> {
val list = mutableListOf<Station>()
Expand All @@ -16,13 +16,13 @@ internal class StationsParserFuc : AbstractRxHtmlParserFunc<List<Station>>() {
continue
}

val pattern = Pattern.compile("/german/hst/overview/(\\d+)/")
val pattern = Pattern.compile("/haltestellen/overview/(\\d+)/")
val matcher = pattern.matcher(href)
if (!matcher.matches()) {
continue
}

val id = href.replace("german/hst/overview/", "").replace("/", "")
val id = href.replace("haltestellen/overview/", "").replace("/", "")

list.add(Station(id, element.text()))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@ public class DeparturesTransformerTest {
public void testTransformation() throws Exception {
Single.just(TestUtils.getTestHtmlFile(TestPages.DEPARTURES))
.compose(new DeparturesMergerTransformer())
.subscribe(new Action1<Departures>() {
@Override
public void call(final Departures departures) {
assertThat(departures.getDepartureInformation().size(), is(1));
assertThat(departures.getDepartures().size(), is(90));
}
.subscribe(departures -> {
assertThat(departures.getDepartureInformation().size(), is(1));
assertThat(departures.getDepartures().size(), is(47));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class DeparturesParserFuncTest {
public void testDeparturesParsingCount() throws Exception {
Single.just(TestUtils.getTestHtmlFile(TestPages.DEPARTURES))
.flatMap(new DeparturesParserFunc())
.subscribe(departures -> assertThat(departures.size(), is(90)));
.subscribe(departures -> assertThat(departures.size(), is(47)));
}

@Test
Expand All @@ -27,18 +27,18 @@ public void testDeparturesParsingObjects() throws Exception {
.flatMap(new DeparturesParserFunc())
.subscribe(departures -> {
final Departure departure = departures.get(0);
assertThat(departure.getDirection(), is("Siehe Zugziel"));
assertThat(departure.getDirection(), is("Bensberg"));
assertThat(departure.getWaitTime(), is("Sofort"));

final Line line = departure.getLine();
assertThat(line.getId(), is("E"));
assertThat(line.getId(), is("1"));

final Departure departure2 = departures.get(89);
assertThat(departure2.getDirection(), is("Meschenich"));
assertThat(departure2.getWaitTime(), is("64 Min"));
final Departure departure2 = departures.get(46);
assertThat(departure2.getDirection(), is("Bensberg"));
assertThat(departure2.getWaitTime(), is("59 Min"));

final Line line2 = departure2.getLine();
assertThat(line2.getId(), is("132"));
assertThat(line2.getId(), is("1"));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void testStationLinesParsingCount() throws Exception {
Single.just(TestUtils.getTestHtmlFile(TestPages.STATIONS))
.flatMap(new StationsParserFuc())
.subscribe(stations -> assertThat(stations.size(),
is(979)));
is(75)));
}

@Test
Expand All @@ -27,12 +27,12 @@ public void testStationsParsingObjects() throws Exception {
.flatMap(new StationsParserFuc())
.subscribe(stations -> {
final Station station1 = stations.get(0);
assertThat(station1.getId(), is("178"));
assertThat(station1.getName(), is("Aachener Str./Gürtel"));
assertThat(station1.getId(), is("702"));
assertThat(station1.getName(), is("Weiden West"));

final Station station2 = stations.get(94);
assertThat(station2.getId(), is("542"));
assertThat(station2.getName(), is("Bevingsweg"));
final Station station2 = stations.get(33);
assertThat(station2.getId(), is("667"));
assertThat(station2.getName(), is("Neuenweg"));
});
}
}
Loading

0 comments on commit c65ef0a

Please sign in to comment.