Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
yueeng committed Oct 30, 2023
1 parent 1a6e198 commit 8c34f48
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 39 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ dependencies {
implementation 'com.google.code.gson:gson:2.8.9'
}

application {
mainClass = 'com.github.yueeng.beijinguniversalcrawler.Main'
}

jar {
duplicatesStrategy DuplicatesStrategy.EXCLUDE
manifest.attributes.put('Main-Class', 'com.github.yueeng.beijinguniversalcrawler.Main')
Expand Down
84 changes: 45 additions & 39 deletions src/main/kotlin/com/github/yueeng/beijinguniversalcrawler/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,46 +93,52 @@ data class Position(
object Main {
@JvmStatic
fun main(args: Array<String>) {
val opts = Options().addOption("?", "help", false, "Help")
.addOption("z", "zone", true, "Time Zone, default: CTT")
.addOption("r", "retry", true, "Retry times when error")
val cmd = DefaultParser().parse(opts, args)
if (cmd.hasOption("?")) {
HelpFormatter().printHelp("unlock", opts)
return
}
val zone = (if (cmd.hasOption("z")) cmd.getOptionValue("z") else null) ?: "CTT"
TimeZone.setDefault(TimeZone.getTimeZone(zone))
val retry = (if (cmd.hasOption("r")) cmd.getOptionValue("r").toIntOrNull() else null) ?: 3
val gson = GsonBuilder().setPrettyPrinting().setDateFormat("yyyy-MM-dd HH:mm:ssZ")
.enableComplexMapKeySerialization().create()
val okhttp = OkHttpClient.Builder().build()
val url = "https://gw.app.universalbeijingresort.com/attraction/list"
for (i in 1..retry) {
val response = okhttp.newCall(Request.Builder().url(url).build()).execute()
if (response.code != 200) continue
val json = response.body?.string() ?: continue
val result = runCatching { gson.fromJson(json, Result::class.java) }.getOrNull() ?: continue
if (result.ret != 0) continue
val now = LocalDateTime.now()
val fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd")
val path = Path(".", "data", "${now.year}", "%02d".format(now.monthValue), "${fmt.format(now)}.json").toFile()
val saved = runCatching { gson.fromJson(path.readText(), Save::class.java) }.getOrNull() ?: Save()
saved[Date()] = result.data.list
if (path.parentFile.isFile) path.parentFile.delete()
if (!path.parentFile.exists()) path.parentFile.mkdirs()
runCatching { path.writeText(gson.toJson(saved)) }.onFailure {
println(it.message)
exitProcess(2)
try {
val opts = Options().addOption("?", "help", false, "Help")
.addOption("z", "zone", true, "Time Zone, default: CTT")
.addOption("r", "retry", true, "Retry times when error")
val cmd = DefaultParser().parse(opts, args)
if (cmd.hasOption("?")) {
HelpFormatter().printHelp("unlock", opts)
return
}
val zone = (if (cmd.hasOption("z")) cmd.getOptionValue("z") else null) ?: "CTT"
TimeZone.setDefault(TimeZone.getTimeZone(zone))
val retry = (if (cmd.hasOption("r")) cmd.getOptionValue("r").toIntOrNull() else null) ?: 3
val gson = GsonBuilder().setPrettyPrinting().setDateFormat("yyyy-MM-dd HH:mm:ssZ")
.enableComplexMapKeySerialization().create()
val okhttp = OkHttpClient.Builder().build()
val url = "https://gw.app.universalbeijingresort.com/attraction/list"
for (i in 1..retry) {
val response = okhttp.newCall(Request.Builder().url(url).build()).execute()
if (response.code != 200) continue
val json = response.body?.string() ?: continue
val result = runCatching { gson.fromJson(json, Result::class.java) }.getOrNull() ?: continue
if (result.ret != 0) continue
val now = LocalDateTime.now()
val fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd")
val path = Path(".", "data", "${now.year}", "%02d".format(now.monthValue), "${fmt.format(now)}.json").toFile()
val saved = runCatching { gson.fromJson(path.readText(), Save::class.java) }.getOrNull() ?: Save()
saved[Date()] = result.data.list
if (path.parentFile.isFile) path.parentFile.delete()
if (!path.parentFile.exists()) path.parentFile.mkdirs()
runCatching { path.writeText(gson.toJson(saved)) }.onFailure {
println(it.message)
exitProcess(2)
}
val files = Path(".", "data").toFile().walk()
.filter { it.isFile }
.filter { it.extension == "json" }
.map { it.name }
.sortedDescending()
.toList()
runCatching { File(".", "files.json").writeText(gson.toJson(files)) }
exitProcess(0)
}
val files = Path(".", "data").toFile().walk()
.filter { it.isFile }
.filter { it.extension == "json" }
.map { it.name }
.sortedDescending()
.toList()
runCatching { File(".", "files.json").writeText(gson.toJson(files)) }
exitProcess(0)
println("Retry failed")
}
catch (e: Exception) {
println(e.message)
}
exitProcess(1)
}
Expand Down

0 comments on commit 8c34f48

Please sign in to comment.