Skip to content

Instantly share code, notes, and snippets.

@lgzh1215
Created October 16, 2018 03:17
Show Gist options
  • Select an option

  • Save lgzh1215/2b26b6c87049c97662dddb34f94d2876 to your computer and use it in GitHub Desktop.

Select an option

Save lgzh1215/2b26b6c87049c97662dddb34f94d2876 to your computer and use it in GitHub Desktop.
no description
package tsundb
import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.http.GET
import retrofit2.http.Path
import retrofit2.http.Query
import java.io.File
import java.util.*
import java.util.concurrent.TimeUnit
import okhttp3.OkHttpClient
const val baseUrl = "https://tsundb.kc3.moe"
interface TsunDB {
@GET("api/routing/{map}")
fun getRouting(
@Path("map") map: String,
@Query("offset") offset: Int,
@Query("limit") limit: Int, // max 50
@Query("next_route") next_route: Int = 0,
@Query("edge_id") edge_id: Int
): Call<List<RoutingData>>
}
data class RoutingData(
val cleared: Boolean,
val date: String,
val edgeid: List<Int>,
val eventid: Int,
val eventkind: Int,
val fleet1: List<Fleet>,
val fleet2: List<Fleet>,
val fleetspeed: Int,
val fleettype: Int,
val hqlvl: Int,
val id: String,
val los: List<Double>,
val map: String,
val nextroute: Int,
val nodetype: Int,
val sortiedfleet: Int
) {
data class Fleet(
val equip: List<Int>,
val exslot: Int,
val flee: Boolean,
val id: Int,
val level: Int,
val name: String,
val shiplock: Int,
val speed: Int,
val type: Int
)
}
fun getDataFromDB(): List<RoutingData> {
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
val okHttpClient = OkHttpClient.Builder()
.readTimeout(100, TimeUnit.SECONDS)
.connectTimeout(100, TimeUnit.SECONDS)
.build()
val retrofit = Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create(moshi))
.baseUrl(baseUrl)
.client(okHttpClient)
.build()
val db = retrofit.create(TsunDB::class.java)
val datas = arrayListOf<RoutingData>()
val maxLimit = 50
var offset = 0
while (true) {
val call = db.getRouting("5-5", offset, limit = maxLimit, edge_id = 17) // P->Q 17 // P->S 28
val response = call.execute()
if (response.isSuccessful) {
val list = response.body()!!
datas.addAll(list)
println("接收数据${list.size}条!现在一共${datas.size}条!${Date()}")
offset += maxLimit
if (list.size < maxLimit) {
break // 已经没有更多数据
}
} else {
println("连接失败")
println(response.errorBody())
break
}
}
println("完毕!总共接收数据${datas.size}条!${Date()}")
return datas
}
fun getDataFromFile(file: File): List<RoutingData> {
println("读取中")
val json = file.readText()
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
val adapter = moshi.adapter<List<RoutingData>>(Types.newParameterizedType(List::class.java, RoutingData::class.java))
val list = adapter.fromJson(json)!!
println("读取完毕,一共${list.size}条记录!")
return list
}
fun saveDataToFile(data: List<RoutingData>, file: File) {
println("保存中,一共${data.size}条记录!")
val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
val adapter = moshi.adapter<List<RoutingData>>(Types.newParameterizedType(List::class.java, RoutingData::class.java))
val json = adapter.toJson(data)
file.writeText(json)
println("保存完毕")
}
fun main(): Unit = TODO()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment