Skip to content

Commit

Permalink
-- 104.ua parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
djnzx committed Oct 9, 2023
1 parent f603c31 commit b837f91
Showing 7 changed files with 315 additions and 70 deletions.
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -143,7 +143,8 @@ lazy val ce3 = (project in file("ce3"))
Libraries.refinedCore,
Libraries.shapeless,
"com.kubukoz" %% "debug-utils" % "1.1.3",
Libraries.catsMtl
Libraries.catsMtl,
Libraries.jsoup,
)
)
.enablePlugins(ScalaxbPlugin)
11 changes: 11 additions & 0 deletions ce3/src/main/scala/gas104/Credentials.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package gas104

object Credentials {

val login104 = ""
val password104 = ""
val sessionId104 = ""
val account104 = ""
val meter104 = ""

}
203 changes: 172 additions & 31 deletions ce3/src/main/scala/gas104/DomainSpec.scala
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
package gas104

import cats.effect.Concurrent
import cats.effect.IO
import cats.effect.Sync
import cats.effect.kernel.Async
import cats.effect.kernel.Resource
import cats.implicits._
import gas104.Htttp._
import gas104.domain._
import gas104.domain.api._
import io.circe.parser
import io.circe.syntax.EncoderOps

import java.time.Instant
import java.time.LocalDateTime
import org.http4s.Response
import org.http4s.blaze.client.BlazeClientBuilder
import org.http4s.client.Client
import org.scalatest.Succeeded
import org.http4s.Header
import org.http4s.Method
import org.http4s.Request
import org.http4s.client.middleware.FollowRedirect
import org.http4s.implicits.http4sLiteralsSyntax
import org.scalatest.funspec.AnyFunSpec
import org.scalatest.matchers.should.Matchers
import org.typelevel.ci.CIStringSyntax

class DomainSpec extends AnyFunSpec with Matchers {

import Credentials._

describe("Row object") {

val rowObj: Row = Row(
@@ -67,7 +69,7 @@ class DomainSpec extends AnyFunSpec with Matchers {
0,
1689886800,
)
)
).some
)

val rawData =
@@ -133,41 +135,180 @@ class DomainSpec extends AnyFunSpec with Matchers {

}

describe("http") {
describe("read meters - token required") {

it("1") {

def body[F[_] : Concurrent](rs: Response[F]): F[String] =
rs.body
.through(fs2.text.utf8.decode[F])
.compile
.foldMonoid

def mkHttpClient[F[_]: Async]: Resource[F, Client[F]] =
BlazeClientBuilder[F].resource
import cats.effect.unsafe.implicits.global

def mkRequest[F[_]: Concurrent](client: Client[F]) = {
import org.http4s.circe.CirceEntityCodec.circeEntityDecoder
client.expect[Data](Htttp.rq[F])
mkHttpClient[IO]
.use(obtainData[IO](sessionId104))
.flatMap(representData[IO])
.unsafeRunSync()
}

def representData[F[_]: Sync](payload: Data): F[Unit] = payload.data
.traverse_ { x: Row =>
val r = URow.from(x)
val line = (r.dateTime, r.counter, r.delta)
Sync[F].delay(pprint.pprintln(line))
}
}

describe("getting csrf token") {

it("1") {
import cats.effect.unsafe.implicits.global
mkHttpClient[IO]
.use(mkRequest[IO])
.flatMap(representData[IO])

val x = obtainCsrfToken[IO]
.unsafeRunSync()
pprint.pprintln(x)
}

it("stub tun trigger compilation") {
Succeeded
}

describe("try to obtain session id") {
it("1") {
import cats.effect.unsafe.implicits.global

//////////////// CSRF TOKEN ////////////////
val (csrf, (ck, cv)) = obtainCsrfToken[IO].unsafeRunSync().get
pprint.pprintln(csrf)
pprint.pprintln(ck)
pprint.pprintln(cv)

val rq: Request[IO] = mkRequestLogin[IO](login104, password104, csrf, (ck, cv))

//////////////// LOGIN ////////////////
val (body0, hs, st) = mkHttpClient[IO]
.flatMap(_.run(rq))
.use { rs =>
body(rs).map((_, rs.headers, rs.status))
}
.unsafeRunSync()

val (ck2, cv2) = extractCookie(hs).get
// .foreach(h => pprint.pprintln(h))

pprint.pprintln(st)
pprint.pprintln(body0)

val url = uri"https://account.104.ua/ua/account/index"

val index = Request[IO](Method.GET, url)
.withHeaders(Header.Raw(ci"$ck2", cv2))

//////////////// /ua/account/index ////////////////
val (b, s, hs2) = mkHttpClient[IO]
.flatMap(_.run(index))
.use { rs =>
body(rs)
.map(b =>
(
b,
rs.status,
rs.headers
)
)
}.unsafeRunSync()

pprint.pprintln(b)
pprint.pprintln(s)
hs2.foreach(x => pprint.pprintln(x))

//////////////// /ua/login ////////////////
val (ck3, cv3) = extractCookie(hs2).get
val urlLogin = uri"https://account.104.ua/ua/login"
val login = Request[IO](Method.GET, urlLogin)
.withHeaders(Header.Raw(ci"$ck3", cv3))

val (b2, s2, hs3) = mkHttpClient[IO]
.flatMap(_.run(login))
.use { rs =>
body(rs)
.map(b =>
(
b,
rs.status,
rs.headers
)
)
}.unsafeRunSync()

pprint.pprintln(b2)
pprint.pprintln(s2)
hs3.foreach(x => pprint.pprintln(x))

}
it("2") {
import cats.effect.unsafe.implicits.global

//////////////// CSRF TOKEN ////////////////
val (csrf, (ck, cv)) = obtainCsrfToken[IO].unsafeRunSync().get
pprint.pprintln(csrf)
pprint.pprintln(ck)
pprint.pprintln(cv)

val rq: Request[IO] = mkRequestLogin[IO](login104, password104, csrf, (ck, cv))

//////////////// LOGIN ////////////////
val (body0, hs, st) = mkHttpClient[IO]
.map(cl => FollowRedirect(5, _ => true)(cl))
.flatMap(_.run(rq))
.use { rs =>
body(rs).map((_, rs.headers, rs.status))
}
.unsafeRunSync()

val (ck2, cv2) = extractCookie(hs).get
// .foreach(h => pprint.pprintln(h))

hs.headers.foreach(x => pprint.pprintln(x))
pprint.pprintln(st)
pprint.pprintln(body0)
//
// val url = uri"https://account.104.ua/ua/account/index"
//
// val index = Request[IO](Method.GET, url)
// .withHeaders(Header.Raw(ci"$ck2", cv2))
//
// //////////////// /ua/account/index ////////////////
// val (b, s, hs2) = mkHttpClient[IO]
// .flatMap(_.run(index))
// .use { rs =>
// body(rs)
// .map(b =>
// (
// b,
// rs.status,
// rs.headers
// )
// )
// }.unsafeRunSync()
//
// pprint.pprintln(b)
// pprint.pprintln(s)
// hs2.foreach(x => pprint.pprintln(x))
//
// //////////////// /ua/login ////////////////
// val (ck3, cv3) = extractCookie(hs2).get
// val urlLogin = uri"https://account.104.ua/ua/login"
// val login = Request[IO](Method.GET, urlLogin)
// .withHeaders(Header.Raw(ci"$ck3", cv3))
//
// val (b2, s2, hs3) = mkHttpClient[IO]
// .flatMap(_.run(login))
// .use { rs =>
// body(rs)
// .map(b =>
// (
// b,
// rs.status,
// rs.headers
// )
// )
// }.unsafeRunSync()
//
// pprint.pprintln(b2)
// pprint.pprintln(s2)
// hs3.foreach(x => pprint.pprintln(x))
//
}
}

}
20 changes: 0 additions & 20 deletions ce3/src/main/scala/gas104/GasApp.scala

This file was deleted.

Loading

0 comments on commit b837f91

Please sign in to comment.