-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
930b1f7
commit df32621
Showing
4 changed files
with
51 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package ru.ekuzmichev | ||
package util.ozon | ||
|
||
import zio.Task | ||
|
||
trait OzonShortUrlResolver: | ||
def resolveShortUrl(url: String): Task[String] |
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,23 @@ | ||
package ru.ekuzmichev | ||
package util.ozon | ||
|
||
import util.ozon.OzonShortUrlResolverImpl.ProductUrlExtractor | ||
|
||
import net.ruippeixotog.scalascraper.browser.Browser | ||
import net.ruippeixotog.scalascraper.dsl.DSL.* | ||
import net.ruippeixotog.scalascraper.dsl.DSL.Extract.* | ||
import net.ruippeixotog.scalascraper.model.Element | ||
import net.ruippeixotog.scalascraper.scraper.HtmlExtractor | ||
import zio.{Task, ZIO} | ||
|
||
class OzonShortUrlResolverImpl(browser: Browser) extends OzonShortUrlResolver: | ||
private type BrowserDocument = browser.DocumentType | ||
|
||
override def resolveShortUrl(url: String): Task[String] = | ||
ZIO.attempt { | ||
val responseHtml: BrowserDocument = browser.get(url) | ||
responseHtml >> ProductUrlExtractor | ||
} | ||
|
||
object OzonShortUrlResolverImpl: | ||
val ProductUrlExtractor: HtmlExtractor[Element, String] = attr("content")("meta[data-hid=property::og:url]") |
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,8 @@ | ||
package ru.ekuzmichev | ||
package util.ozon | ||
|
||
import net.ruippeixotog.scalascraper.browser.Browser | ||
import zio.{URLayer, ZLayer} | ||
|
||
object OzonShortUrlResolverLayers: | ||
val impl: URLayer[Browser, OzonShortUrlResolver] = ZLayer.fromFunction(new OzonShortUrlResolverImpl(_)) |
13 changes: 13 additions & 0 deletions
13
src/test/scala/util/ozon/OzonShortUrlResolverImplTestApp.scala
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,13 @@ | ||
package ru.ekuzmichev | ||
package util.ozon | ||
|
||
import net.ruippeixotog.scalascraper.browser.JsoupBrowser | ||
import zio.{Scope, ZIO, ZIOAppArgs, ZIOAppDefault} | ||
|
||
object OzonShortUrlResolverImplTestApp extends ZIOAppDefault: | ||
override def run: ZIO[Any with ZIOAppArgs with Scope, Any, Any] = | ||
val productShortUrl = "https://ozon.ru/t/YKknAE4" | ||
val ozonShortUrlResolver = new OzonShortUrlResolverImpl(new JsoupBrowser()) | ||
ozonShortUrlResolver | ||
.resolveShortUrl(productShortUrl) | ||
.tap(productFullUrl => ZIO.log(s"Resolved $productShortUrl into $productFullUrl")) |