This commit is contained in:
lunaticbum 2024-10-17 18:07:49 +09:00
parent 8e64f444a5
commit 7bfd2a0e62
6 changed files with 118 additions and 32 deletions

View File

@ -11,11 +11,13 @@ import org.springframework.web.reactive.function.client.WebClient
@SpringBootApplication @SpringBootApplication
class LunApplication { class LunApplication {
init { init {
val client = WebClient.create() try {
client.get() val client = WebClient.create()
.uri("https://api.telegram.org/bot7934509464:AAE_xUbICxMdywLGnxo7BkeIqA1nVza4P9w/sendMessage?chat_id=71476436&text=SERVER_START") client.get()
.retrieve() .uri("https://api.telegram.org/bot7934509464:AAE_xUbICxMdywLGnxo7BkeIqA1nVza4P9w/sendMessage?chat_id=71476436&text=SERVER_START")
.bodyToMono(String::class.java).block() ?: "FAIL" .retrieve()
.bodyToMono(String::class.java).block() ?: "FAIL"
} catch (e : Exception) {}
} }

View File

@ -1,8 +1,12 @@
package kr.lunaticbum.back.lun.controllers package kr.lunaticbum.back.lun.controllers
import com.google.gson.Gson
import jakarta.servlet.http.HttpServletRequest import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse import jakarta.servlet.http.HttpServletResponse
import kr.lunaticbum.back.lun.configs.GlobalEnvironment
import kr.lunaticbum.back.lun.model.CurrentWeather
import kr.lunaticbum.back.lun.model.FileSaveResult import kr.lunaticbum.back.lun.model.FileSaveResult
import kr.lunaticbum.back.lun.model.LocationLogService
import kr.lunaticbum.back.lun.model.ResponceResult import kr.lunaticbum.back.lun.model.ResponceResult
import kr.lunaticbum.back.lun.utils.LogService import kr.lunaticbum.back.lun.utils.LogService
import kr.lunaticbum.back.lun.utils.getFileExtension import kr.lunaticbum.back.lun.utils.getFileExtension
@ -14,6 +18,7 @@ import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.* import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile import org.springframework.web.multipart.MultipartFile
import org.springframework.web.reactive.function.client.WebClient
import org.springframework.web.servlet.ModelAndView import org.springframework.web.servlet.ModelAndView
import java.io.* import java.io.*
import java.util.* import java.util.*
@ -23,6 +28,12 @@ import java.util.*
@RequestMapping("/blog") @RequestMapping("/blog")
class BlogController() { class BlogController() {
@Autowired
lateinit var globalEvv : GlobalEnvironment
@Autowired
private lateinit var locationLogService: LocationLogService
@Autowired @Autowired
lateinit var logService: LogService lateinit var logService: LogService
@ -52,6 +63,32 @@ class BlogController() {
return vm return vm
} }
@GetMapping("recent")
fun recent() : ModelAndView{
val vm = ModelAndView("content/blog/viewer")
locationLogService.find50().forEach {
logService.log(Gson().toJson(it))
}
locationLogService.getLocationLog()?.let {
try {
val client0 = WebClient.create()
val result = client0.get()
.uri("http://api.weatherapi.com/v1/current.json?key=${globalEvv.weatherApiKey}&q=${it.mLatitude},${it.mLongitude}&aqi=no")
.retrieve()
.bodyToMono(String::class.java)
.block() ?: "FAIL"
Gson().fromJson(result, CurrentWeather::class.java)?.let { sss ->
logService.log("지역:${sss.location?.name}\n날씨:${sss.current?.condition?.text}\n온도:${sss.current?.temp_c}\n습도:${sss.current?.humidity}\n" +
"체감온도:${sss.current?.feelslike_c}\nhttps://www.accuweather.com/ko/search-locations?query=${it.mLatitude},${it.mLongitude}")
}
}
catch (e : Exception) {
}
}
return vm
}
@Value("\${image.upload.path}") @Value("\${image.upload.path}")
private val uploadPath: String? = null private val uploadPath: String? = null

View File

@ -2,6 +2,9 @@ package kr.lunaticbum.back.lun.controllers
import com.google.gson.Gson import com.google.gson.Gson
import jakarta.servlet.http.HttpServletRequest import jakarta.servlet.http.HttpServletRequest
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kr.lunaticbum.back.lun.configs.GlobalEnvironment import kr.lunaticbum.back.lun.configs.GlobalEnvironment
import kr.lunaticbum.back.lun.model.* import kr.lunaticbum.back.lun.model.*
import kr.lunaticbum.back.lun.utils.LogService import kr.lunaticbum.back.lun.utils.LogService
@ -10,6 +13,7 @@ import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity import org.springframework.http.ResponseEntity
import org.springframework.security.core.userdetails.UserDetails import org.springframework.security.core.userdetails.UserDetails
import org.springframework.web.bind.annotation.* import org.springframework.web.bind.annotation.*
import org.springframework.web.reactive.function.client.WebClient
import java.util.* import java.util.*
@ -32,9 +36,11 @@ class BumsPrivate {
logService.log(jsonString) logService.log(jsonString)
var lResultCode = 0 var lResultCode = 0
var lResultMsg = "Suscces" var lResultMsg = "Suscces"
var location : LocationLog? = null
val decodedBytes: ByteArray = Base64.getMimeDecoder().decode(jsonString) val decodedBytes: ByteArray = Base64.getMimeDecoder().decode(jsonString)
String(decodedBytes).let { String(decodedBytes).let {
Gson().fromJson<LocationLog>(it, LocationLog::class.java)?.let { model -> Gson().fromJson<LocationLog>(it, LocationLog::class.java)?.let { model ->
location = model
logService.log(model.toString()) logService.log(model.toString())
locationService.save(model) locationService.save(model)
} }
@ -43,6 +49,15 @@ class BumsPrivate {
this.resultCode = lResultCode this.resultCode = lResultCode
this.resultMsg = lResultMsg this.resultMsg = lResultMsg
}) })
CoroutineScope(Dispatchers.IO).launch {
location?.let {
val client = WebClient.create()
client.get()
.uri("https://api.telegram.org/${globalEvv.telegramBotKey}/sendMessage?chat_id=${globalEvv.telegramMyId}&text=${it.mAddressLines.first()} 저장")
.retrieve()
.bodyToMono(String::class.java).block() ?: "FAIL"
}
}
return responce return responce
} }

View File

@ -128,18 +128,25 @@ class Telegram {
((globalEvv.telegramBotKey?.length ?: 0) > 3) && ((globalEvv.telegramBotKey?.length ?: 0) > 3) &&
((globalEvv.telegramMyId?.length ?: 0) > 3) ((globalEvv.telegramMyId?.length ?: 0) > 3)
) { ) {
val client0 = WebClient.create() locationLogService.getLocationLog()?.let {
val result = client0.get() try {
.uri("http://api.weatherapi.com/v1/current.json?key=${globalEvv.weatherApiKey}&q=seoul&aqi=no") val client0 = WebClient.create()
.retrieve() val result = client0.get()
.bodyToMono(String::class.java) .uri("http://api.weatherapi.com/v1/current.json?key=${globalEvv.weatherApiKey}&q=${it.mLatitude},${it.mLongitude}&aqi=no")
.block() ?: "FAIL" .retrieve()
Gson().fromJson(result, CurrentWeather::class.java)?.let { sss -> .bodyToMono(String::class.java)
val client = WebClient.create() .block() ?: "FAIL"
client.get() Gson().fromJson(result, CurrentWeather::class.java)?.let { sss ->
.uri("https://api.telegram.org/${globalEvv.telegramBotKey}/sendMessage?chat_id=${globalEvv.telegramMyId}&text=온도${sss.current?.temp_c}") val client = WebClient.create()
.retrieve() client.get()
.bodyToMono(String::class.java).block() ?: "FAIL" .uri("https://api.telegram.org/${globalEvv.telegramBotKey}/sendMessage?chat_id=${globalEvv.telegramMyId}&text=${sss.getSummaryInfo()}")
.retrieve()
.bodyToMono(String::class.java).block() ?: "FAIL"
}
}
catch (e : Exception) {
}
} }
} }
}catch (e : Exception) { }catch (e : Exception) {
@ -217,7 +224,7 @@ class Telegram {
val client = WebClient.create() val client = WebClient.create()
locationLogService.getLocationLog()?.let { locationLogService.getLocationLog()?.let {
client.get() client.get()
.uri("https://api.telegram.org/${globalEvv.telegramBotKey}/sendMessage?chat_id=${target}&text=${SimpleDateFormat("yyyy/MM/dd-HH:mm:ss").format(Date(it.time))}\n${it.mAddressLines.first()}") .uri("https://api.telegram.org/${globalEvv.telegramBotKey}/sendMessage?chat_id=${target}&text=${it.timeString}\n${it.mAddressLines.first()}\nhttps://www.google.com/maps/search/?api=1&query=${it.mLatitude},${it.mLongitude}")
.retrieve() .retrieve()
.bodyToMono(String::class.java).block() ?: "FAIL" .bodyToMono(String::class.java).block() ?: "FAIL"
} }

View File

@ -1,6 +1,10 @@
package kr.lunaticbum.back.lun.model package kr.lunaticbum.back.lun.model
import com.google.gson.Gson import com.google.gson.Gson
import com.mongodb.client.model.Sorts.descending
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kr.lunaticbum.back.lun.configs.GlobalEnvironment import kr.lunaticbum.back.lun.configs.GlobalEnvironment
import kr.lunaticbum.back.lun.utils.LogService import kr.lunaticbum.back.lun.utils.LogService
import lombok.AllArgsConstructor import lombok.AllArgsConstructor
@ -16,8 +20,10 @@ import org.springframework.data.mongodb.repository.ReactiveMongoRepository
import org.springframework.stereotype.Repository import org.springframework.stereotype.Repository
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
import org.springframework.web.reactive.function.client.WebClient import org.springframework.web.reactive.function.client.WebClient
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono import reactor.core.publisher.Mono
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.time.Duration
import java.util.* import java.util.*
import kotlin.collections.ArrayList import kotlin.collections.ArrayList
import kotlin.collections.List import kotlin.collections.List
@ -47,6 +53,7 @@ class LocationLog {
var mHasLatitude = false var mHasLatitude = false
var mHasLongitude = false var mHasLongitude = false
var mPhone: String? = null var mPhone: String? = null
var timeString : String? = null
var mUrl: String? = null var mUrl: String? = null
var time : Long = 0L var time : Long = 0L
@ -75,6 +82,7 @@ class LocationLog {
@Repository @Repository
interface LocationLogRepository : ReactiveMongoRepository<LocationLog, String> { interface LocationLogRepository : ReactiveMongoRepository<LocationLog, String> {
fun findAllBy() : Mono<LocationLog>
fun findFirstByOrderByTimeDesc() : Mono<LocationLog> fun findFirstByOrderByTimeDesc() : Mono<LocationLog>
fun save(log: LocationLog): Mono<LocationLog> fun save(log: LocationLog): Mono<LocationLog>
} }
@ -90,6 +98,9 @@ class LocationLogService : LocationService {
@Autowired @Autowired
private lateinit var logRepository: LocationLogRepository private lateinit var logRepository: LocationLogRepository
fun find50() : List<LocationLog> {
return logRepository.findAll().takeLast(50).buffer(50).blockLast(Duration.ofSeconds(30)) ?: listOf()
}
fun getLocationLog() : LocationLog? { fun getLocationLog() : LocationLog? {
return logRepository.findFirstByOrderByTimeDesc().block() return logRepository.findFirstByOrderByTimeDesc().block()
} }
@ -258,11 +269,16 @@ object QVZTb2dpcmw : SoInterface {
this.thumbnail = img this.thumbnail = img
this.pubDate = Date().time this.pubDate = Date().time
this.category = RssDataType.GURU.name this.category = RssDataType.GURU.name
})
}) {
// CoroutineScope(Dispatchers.IO).launch {
// service.sendMsg("${title}\n${img}\n${href}")
// }
}
} }
service.sendMsg(lists.map { // lists.map {
"${it.title}\n${it.description}\n${it.thumbnail}\n${it.originPage}\n" // service.sendMsg("${it.title}\n${it.description}\n${it.thumbnail}\n${it.originPage}")
}.joinToString(" \n ")) // }
} }
} }
@ -291,12 +307,16 @@ object SkFWTW9zdA : SoInterface {
try { try {
pubDate = dmy.parse(date).time pubDate = dmy.parse(date).time
}catch (e : Exception) {e.printStackTrace()} }catch (e : Exception) {e.printStackTrace()}
}) }){
// CoroutineScope(Dispatchers.IO).launch {
// service.sendMsg("${title}\n${thumb}\n${link}")
// }
}
} }
} }
service.sendMsg(lists.map { // service.sendMsg(lists.map {
"${it.title}\n${it.description}\n${it.thumbnail}\n${it.originPage}\n" // "${it.title}\n${it.description}\n${it.thumbnail}\n${it.originPage}\n"
}.joinToString(" \n ")) // }.joinToString(" \n "))
} }
} }
@Repository @Repository
@ -321,27 +341,29 @@ class RssDataService {
} }
fun save(log: RssData) { fun save(log: RssData, callback : (Boolean)->Unit) {
println("saved msg before ${Gson().toJson(log)}") println("saved msg before ${Gson().toJson(log)}")
log.originPage?.let { log.originPage?.let {
if(rssDataRepository.findFirstByOriginPageEquals(it).block() == null) { if(rssDataRepository.findFirstByOriginPageEquals(it).block() == null) {
rssDataRepository.save(log) rssDataRepository.save(log)
.subscribe({ println("saved msg after ${it}") }, { e -> e.printStackTrace() }, { .subscribe({ println("saved msg after ${it}") }, { e -> e.printStackTrace() }, {
println("saved msg comp") println("saved msg comp")
callback(true)
}) })
} else { } else {
println("있어???") println("있어???")
} }
} }
} }
@Autowired @Autowired
lateinit var globalEvv : GlobalEnvironment lateinit var globalEvv : GlobalEnvironment
fun sendMsg(data : String) { suspend fun sendMsg(data : String) {
val client = WebClient.create() val client = WebClient.create()
client.get() client.get()
.uri("https://api.telegram.org/${globalEvv.telegramBotKey}/sendMessage?chat_id=${globalEvv.telegramMyId}&text=${data}") .uri("https://api.telegram.org/${globalEvv.telegramBotKey}/sendMessage?chat_id=${globalEvv.telegramMyId}&text=${data}")
.retrieve() .retrieve()
.bodyToMono(String::class.java).block() ?: "FAIL" .bodyToMono(String::class.java).block() ?: "FAIL"
} }
} }

View File

@ -52,4 +52,7 @@ class Location {
class CurrentWeather { class CurrentWeather {
var location: Location? = null var location: Location? = null
var current: Current? = null var current: Current? = null
fun getSummaryInfo() = "지역:${this.location?.name}\n날씨:${this.current?.condition?.text}\n온도:${this.current?.temp_c}\n습도:${this.current?.humidity}\n" +
"체감온도:${this.current?.feelslike_c}\nhttps://www.accuweather.com/ko/search-locations?query=${this.location?.lat},${this.location?.lon}"
} }