-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Mikhael Sokolov <mikhael.sokolov@mynd.co>
- Loading branch information
1 parent
8e3433e
commit f5943de
Showing
5 changed files
with
193 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
...rs/src/main/kotlin/ru/sokomishalov/skraper/provider/odnoklassniki/OdnoklassnikiSkraper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright (c) 2019-present Mikhael Sokolov | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ru.sokomishalov.skraper.provider.odnoklassniki | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flow | ||
import org.jsoup.nodes.Document | ||
import ru.sokomishalov.skraper.Skraper | ||
import ru.sokomishalov.skraper.Skrapers | ||
import ru.sokomishalov.skraper.client.HttpRequest | ||
import ru.sokomishalov.skraper.client.SkraperClient | ||
import ru.sokomishalov.skraper.client.fetchDocument | ||
import ru.sokomishalov.skraper.client.fetchOpenGraphMedia | ||
import ru.sokomishalov.skraper.internal.iterable.emitBatch | ||
import ru.sokomishalov.skraper.internal.jsoup.getFirstElementByAttributeValue | ||
import ru.sokomishalov.skraper.internal.jsoup.getFirstElementByClass | ||
import ru.sokomishalov.skraper.internal.jsoup.getMetaPropertyMap | ||
import ru.sokomishalov.skraper.internal.net.host | ||
import ru.sokomishalov.skraper.model.* | ||
|
||
open class OdnoklassnikiSkraper @JvmOverloads constructor( | ||
override val client: SkraperClient = Skrapers.client | ||
) : Skraper { | ||
|
||
override fun getPosts(path: String): Flow<Post> = flow { | ||
val document = getPage(path = path) | ||
|
||
val rawPosts = document | ||
?.getElementsByAttribute("data-feed-id") | ||
?.toList() | ||
.orEmpty() | ||
|
||
emitBatch(rawPosts) { | ||
Post( | ||
id = attr("data-feed-id").orEmpty(), | ||
text = getFirstElementByClass("media-text_cnt")?.wholeText(), | ||
statistics = PostStatistics( | ||
likes = getFirstElementByAttributeValue("data-widget-item-type", "like")?.getFirstElementByClass("widget_count")?.html()?.trim()?.toIntOrNull(), | ||
comments = getFirstElementByAttributeValue("data-widget-item-type", "comment")?.getFirstElementByClass("widget_count")?.html()?.trim()?.toIntOrNull(), | ||
reposts = getFirstElementByAttributeValue("data-widget-item-type", "reshare")?.getFirstElementByClass("widget_count")?.html()?.trim()?.toIntOrNull(), | ||
), | ||
media = getElementsByClass("vid-card").mapNotNull { it.getFirstElementByClass("vid-card_cnt_w")?.id()?.substringAfterLast("_")?.takeWhile { it.isDigit() }?.let { "${BASE_URL}/video/${it}" }?.toVideo() } | ||
+ getElementsByAttribute("data-mp4src").mapNotNull { it.attr("data-mp4src").let { "https:${it}" }.toVideo() } | ||
+ getElementsByClass("collage_img").mapNotNull { it.attr("src").let { "https:${it}" }.toImage() } | ||
) | ||
} | ||
} | ||
|
||
override suspend fun getPageInfo(path: String): PageInfo? { | ||
val page = getPage(path = path) | ||
val properties = page?.getMetaPropertyMap() | ||
|
||
return properties?.let { | ||
PageInfo( | ||
nick = it["og:url"]?.substringAfterLast("/"), | ||
name = it["og:title"].orEmpty().substringBefore(" |"), | ||
description = it["og:description"].orEmpty(), | ||
avatar = it["og:image"]?.toImage(), | ||
) | ||
} | ||
} | ||
|
||
override fun supports(url: String): Boolean { | ||
return "ok.ru" in url.host | ||
} | ||
|
||
override suspend fun resolve(media: Media): Media { | ||
return client.fetchOpenGraphMedia(media) | ||
} | ||
|
||
private suspend fun getPage(path: String): Document? { | ||
return client.fetchDocument( | ||
request = HttpRequest(url = BASE_URL.buildFullURL(path = path)), | ||
) | ||
} | ||
|
||
companion object { | ||
const val BASE_URL: String = "https://ok.ru" | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...n/kotlin/ru/sokomishalov/skraper/provider/odnoklassniki/OdnoklassnikiSkraperExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright (c) 2019-present Mikhael Sokolov | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ru.sokomishalov.skraper.provider.odnoklassniki | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
import ru.sokomishalov.skraper.model.PageInfo | ||
import ru.sokomishalov.skraper.model.Post | ||
|
||
fun OdnoklassnikiSkraper.getCommunityPosts(community: String): Flow<Post> { | ||
return getPosts(path = "/${community}") | ||
} | ||
|
||
suspend fun OdnoklassnikiSkraper.getCommunityInfo(community: String): PageInfo? { | ||
return getPageInfo(path = "/${community}") | ||
} |
48 changes: 48 additions & 0 deletions
48
...rc/test/kotlin/ru/sokomishalov/skraper/provider/odnoklassniki/OdnoklassnikiSkraperTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright (c) 2019-present Mikhael Sokolov | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ru.sokomishalov.skraper.provider.odnoklassniki | ||
|
||
import org.junit.jupiter.api.Test | ||
import ru.sokomishalov.skraper.model.Image | ||
import ru.sokomishalov.skraper.model.Video | ||
import ru.sokomishalov.skraper.provider.SkraperTck | ||
|
||
class OdnoklassnikiSkraperTest : SkraperTck() { | ||
override val skraper: OdnoklassnikiSkraper = OdnoklassnikiSkraper(client = client) | ||
override val path: String = "/milota" | ||
private val community: String = "milota" | ||
|
||
@Test | ||
fun `Check community posts`() { | ||
assertPosts { skraper.getCommunityPosts(community = community) } | ||
} | ||
|
||
@Test | ||
fun `Check community page info`() { | ||
assertPageInfo { skraper.getCommunityInfo(community = community) } | ||
} | ||
|
||
@Test | ||
fun `Check media resolving`() { | ||
assertMediaResolved(Video("https://ok.ru/video/3944589167241")) | ||
assertMediaResolved(Image("https://ok.ru/group/52234248454281/album/52234845225097/937540255625")) | ||
} | ||
|
||
@Test | ||
fun `Check media downloading`() { | ||
assertMediaDownloaded(Video("https://ok.ru/video/3944589167241")) | ||
} | ||
} |