This repository was archived by the owner on May 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZoneBourse.scala
More file actions
executable file
·83 lines (74 loc) · 2.78 KB
/
ZoneBourse.scala
File metadata and controls
executable file
·83 lines (74 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env scalas
/***
scalaVersion := "2.12.6"
libraryDependencies ++= Seq(
"net.sourceforge.htmlcleaner" % "htmlcleaner" % "2.22",
"org.apache.commons" % "commons-lang3" % "3.5"
)
*/
import org.htmlcleaner.HtmlCleaner
import java.net.URL
import org.apache.commons.lang3.StringEscapeUtils
import org.htmlcleaner.TagNode
val stocks = List("BIC-4617/", "DISTRIBUIDORA-INTER-DE-AL-8322842", "DBV-TECHNOLOGIES-10189744", "EDF-4998", "GENFIT-16311755",
"INNATE-PHARMA-35620", "NICOX-25281955", "PEUGEOT-4682", "ORANGE-SA-4649", "SOITEC-4695", "TECHNICOLOR-6411898")
def priceTargetRec(zoneBourseId: String): List[Double] = {
val url = s"https://www.zonebourse.com/${zoneBourseId}/consensus/"
val cleaner = new HtmlCleaner
val props = cleaner.getProperties
val rootNode = cleaner.clean(new URL(url))
val elements = rootNode.getElementsByName("table", true)
def rec(l: List[TagNode], keepNext: Boolean, acc: List[Double]): List[Double] = {
if (l.isEmpty) acc
else if (acc.length == 3) return acc
else if (keepNext == true) {
val content = l.head.getText.toString
if (content.contains("%")) {
val value = content.filterNot { "%" contains _ }.replace(",", ".")
val n = value.toDouble
rec(l.tail, false, n :: acc)
} else rec(l.tail, false, acc)
} else {
val classType = l.head.getAttributeByName("class")
if (classType != null && classType.equalsIgnoreCase("RC_tdL")) rec(l.tail, true, acc)
else rec(l.tail, false, acc)
}
}
for (elem ← elements) {
val res = elem.getAllElements(true).toList
val res2 = rec(res, false, Nil)
if (res2.nonEmpty) return res2
}
//Should be unreachable
Nil
}
def priceTarget(zoneBourseId: String): List[Double] = {
val url = s"https://www.zonebourse.com/${zoneBourseId}/consensus/"
val cleaner = new HtmlCleaner
val props = cleaner.getProperties
val rootNode = cleaner.clean(new URL(url))
val elements = rootNode.getElementsByName("table", true)
var r: List[Double] = Nil
var keepNext = false
for (elem ← elements) {
val res = elem.getAllElements(true)
for (i <- res) {
if (keepNext == true) {
keepNext = false
val content = i.getText.toString
if (content.contains("%")) {
val value = content.filterNot { "%" contains _ }.replace(",", ".")
val n = value.toDouble
r = n :: r
if (r.size == 3) return r.reverse
}
}
val classType = i.getAttributeByName("class")
if (classType != null && classType.equalsIgnoreCase("RC_tdL")) {
keepNext = true
}
}
}
r
}
stocks.foreach { s => println(s); println(priceTargetRec(s)) }