Skip to content

Commit 9df16cb

Browse files
crawSinaImageService
1 parent 9c8019b commit 9df16cb

19 files changed

+208
-104
lines changed

src/main/kotlin/com/light/saber/api/GankApiBuilder.kt renamed to src/main/kotlin/com/light/saber/api/GankImageApiBuilder.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.light.saber.api
22

3-
object GankApiBuilder {
3+
object GankImageApiBuilder {
44
fun build(page: Int): String {
55
// 此处 api 中的中文需要转义
66
return "http://gank.io/api/data/%E7%A6%8F%E5%88%A9/100/${page}"

src/main/kotlin/com/light/saber/api/ImageSearchApiBuilder.kt

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.light.saber.api
2+
3+
object SinaImageApiBuilder {
4+
fun build(page: Int): String {
5+
return "http://platform.sina.com.cn/slide/album_photo_col?app_key=1985696825&photo_col_id=13&tags=cat&tagmode=any&format=json&page=${page}"
6+
}
7+
}

src/main/kotlin/com/light/saber/controller/CrawController.kt

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.light.saber.controller
2+
3+
import com.light.saber.service.impl.CrawGankImageService
4+
import com.light.saber.service.impl.CrawSinaImageService
5+
import org.springframework.beans.factory.annotation.Autowired
6+
import org.springframework.stereotype.Controller
7+
import org.springframework.web.bind.annotation.RequestMapping
8+
import org.springframework.web.bind.annotation.RequestMethod
9+
import org.springframework.web.bind.annotation.ResponseBody
10+
11+
/**
12+
* Created by jack on 2017/7/22.
13+
*/
14+
15+
@Controller
16+
class CrawImageController {
17+
18+
@Autowired
19+
lateinit var crawGankImageService: CrawGankImageService
20+
@Autowired
21+
lateinit var crawSinaImageService: CrawSinaImageService
22+
23+
@RequestMapping(value = ["crawGankImageService"], method = [(RequestMethod.GET)])
24+
@ResponseBody
25+
fun crawGankImageService(): String {
26+
crawGankImageService.doCrawJob()
27+
return "crawGankImageService JOB Started"
28+
}
29+
30+
@RequestMapping(value = ["crawSinaImageService"], method = [(RequestMethod.GET)])
31+
@ResponseBody
32+
fun crawSinaImageService(): String {
33+
crawSinaImageService.doCrawJob()
34+
return "crawSinaImageService JOB Started"
35+
}
36+
37+
}

src/main/kotlin/com/light/saber/controller/KnowledgeController.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package com.light.saber.controller
22

33
import com.light.saber.dao.KnowledgeMapper
44
import com.light.saber.model.Knowledge
5+
import com.light.saber.result.Result
56
import com.light.saber.service.KnowledgeService
6-
import com.light.saber.service.TakeKeyWordsService
7+
import com.light.saber.service.impl.TakeKeyWordsService
78
import org.springframework.beans.factory.annotation.Autowired
89
import org.springframework.data.domain.PageRequest
910
import org.springframework.data.domain.Sort

src/main/kotlin/com/light/saber/controller/KnowledgeCrawController.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.light.saber.controller
22

33
import com.light.saber.crawler.*
4-
import com.light.saber.service.CrawKnowledgeService
4+
import com.light.saber.service.impl.CrawKnowledgeService
55
import org.springframework.beans.factory.annotation.Autowired
66
import org.springframework.web.bind.annotation.GetMapping
77
import org.springframework.web.bind.annotation.RestController
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
package com.light.saber.controller
1+
package com.light.saber.result
22

33
data class Result<T>(var data: T, var msg: String, var success: Boolean)
Lines changed: 4 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,6 @@
11
package com.light.saber.service
22

3-
import com.light.saber.api.GankApiBuilder
4-
import com.light.saber.dao.ImageRepository
5-
import com.light.saber.model.Image
6-
import com.light.saber.util.JsonResultProcessor
7-
import com.light.saber.webclient.CrawlerWebClient
8-
import kotlinx.coroutines.experimental.CommonPool
9-
import kotlinx.coroutines.experimental.launch
10-
import kotlinx.coroutines.experimental.runBlocking
11-
import org.slf4j.LoggerFactory
12-
import org.springframework.beans.factory.annotation.Autowired
13-
import org.springframework.stereotype.Service
14-
import java.net.URL
15-
import java.text.SimpleDateFormat
16-
import java.util.*
17-
18-
@Service
19-
class CrawImageService {
20-
val logger = LoggerFactory.getLogger(CrawImageService::class.java)
21-
22-
@Autowired
23-
lateinit var CrawlerWebClient: CrawlerWebClient
24-
25-
@Autowired
26-
lateinit var imageRepository: ImageRepository
27-
28-
fun doGankImageCrawJob() = runBlocking {
29-
for (page in 1..7) {
30-
launch(CommonPool) {
31-
saveGankImage(page)
32-
}
33-
}
34-
}
35-
36-
private fun saveGankImage(page: Int) {
37-
val api = GankApiBuilder.build(page)
38-
JsonResultProcessor.getGankImageUrls(api).forEach {
39-
val url = it
40-
if (imageRepository.countByUrl(url) == 0) {
41-
val image = Image()
42-
image.category = "干货集中营福利 ${SimpleDateFormat("yyyy-MM-dd").format(Date())}"
43-
image.url = url
44-
image.sourceType = 1
45-
image.imageBlob = getByteArray(url)
46-
logger.info("image = ${image}")
47-
imageRepository.save(image)
48-
}
49-
}
50-
}
51-
52-
private fun getByteArray(url: String): ByteArray {
53-
val urlObj = URL(url)
54-
return urlObj.readBytes()
55-
}
56-
}
3+
interface CrawImageService {
4+
fun doCrawJob()
5+
fun saveImage(page: Int)
6+
}

src/main/kotlin/com/light/saber/service/KnowledgeService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import org.springframework.data.domain.Page
55
import org.springframework.data.domain.Pageable
66

77
interface KnowledgeService {
8-
abstract fun page(title: String?, page: Pageable): Page<Knowledge>
8+
fun page(title: String?, page: Pageable): Page<Knowledge>
99
fun doSaveKnowledge(articleUrl: String, articleTitle: String, articleBody: String?)
1010

1111
}

0 commit comments

Comments
 (0)