Update
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -7,3 +7,7 @@ src/main/scala/hydro.csv
|
|||||||
src/main/scala/nuclear.csv
|
src/main/scala/nuclear.csv
|
||||||
src/main/scala/wind.csv
|
src/main/scala/wind.csv
|
||||||
src/main/scala/apiKey.txt
|
src/main/scala/apiKey.txt
|
||||||
|
src/main/scala/sources.csv
|
||||||
|
project
|
||||||
|
target
|
||||||
|
|
||||||
|
|||||||
@@ -1,59 +1,46 @@
|
|||||||
import java.io._
|
import java.net.{HttpURLConnection, URL}
|
||||||
import java.net._
|
import java.nio.file.{Files, Paths}
|
||||||
|
import scala.util.Try
|
||||||
import scala.io.StdIn.readLine
|
import scala.io.StdIn.readLine
|
||||||
import scala.io.Source
|
import scala.io.Source
|
||||||
|
|
||||||
val API_KEY = Source.fromFile("apiKey.txt").getLines().mkString
|
val API_KEY = Source.fromFile("apiKey.txt").getLines().mkString
|
||||||
|
|
||||||
def collectData(
|
def collectData(
|
||||||
variableId: Int,
|
dataSources: Map[Int, String]
|
||||||
startTime: String,
|
|
||||||
endTime: String,
|
|
||||||
filePath: String
|
|
||||||
): Unit = {
|
): Unit = {
|
||||||
|
val now = (java.time.LocalDateTime.now)
|
||||||
|
.truncatedTo(java.time.temporal.ChronoUnit.SECONDS)
|
||||||
|
val startTime = now.minusMonths(3).toString().concat("Z")
|
||||||
|
val endTime = now.toString.concat("Z")
|
||||||
|
|
||||||
try {
|
for ((k, v) <- dataSources) {
|
||||||
// Set the URL for the API endpoint
|
|
||||||
val url = new URL(
|
val url = new URL(
|
||||||
s"https://api.fingrid.fi/v1/variable/$variableId/events/csv?start_time=$startTime&end_time=$endTime"
|
s"https://api.fingrid.fi/v1/variable/$k/events/csv?start_time=$startTime&end_time=$endTime"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Open a connection to the API endpoint
|
Try(url.openConnection().asInstanceOf[HttpURLConnection]) match {
|
||||||
val connection =
|
case util.Success(connection) =>
|
||||||
url.openConnection().asInstanceOf[HttpURLConnection]
|
connection.setRequestMethod("GET")
|
||||||
|
connection.setRequestProperty("Accept", "text/csv")
|
||||||
|
connection.setRequestProperty("x-api-key", API_KEY)
|
||||||
|
connection.connect()
|
||||||
|
|
||||||
// Set the request method to GET
|
Try(
|
||||||
connection.setRequestMethod("GET")
|
scala.io.Source.fromInputStream(connection.getInputStream).mkString
|
||||||
|
) match {
|
||||||
// Set the request headers
|
case util.Success(response) =>
|
||||||
connection.setRequestProperty("Accept", "text/csv")
|
Files.write(Paths.get(v), response.getBytes("UTF-8"))
|
||||||
connection.setRequestProperty("x-api-key", API_KEY)
|
case util.Failure(e) =>
|
||||||
|
System.err.println(
|
||||||
// Send the GET request to the API endpoint
|
s"An error occurred while reading the response: ${e.getMessage}"
|
||||||
connection.connect()
|
)
|
||||||
|
}
|
||||||
// Read the response from the API endpoint
|
case util.Failure(e) =>
|
||||||
val inputStream = connection.getInputStream
|
System.err.println(
|
||||||
val inputStreamReader = new InputStreamReader(inputStream)
|
s"An error occurred while connecting to the API endpoint: ${e.getMessage}"
|
||||||
val reader = new BufferedReader(inputStreamReader)
|
)
|
||||||
val response = new StringBuilder
|
|
||||||
var line: String = reader.readLine
|
|
||||||
while (line != null) {
|
|
||||||
response.append(line + "\n")
|
|
||||||
line = reader.readLine
|
|
||||||
}
|
}
|
||||||
reader.close()
|
|
||||||
// Save response to a CSV file
|
|
||||||
val file = new File(filePath)
|
|
||||||
val bw = new BufferedWriter(new FileWriter(file))
|
|
||||||
bw.write(response.toString)
|
|
||||||
bw.close()
|
|
||||||
|
|
||||||
} catch {
|
|
||||||
case e: IOException =>
|
|
||||||
System.err.println(
|
|
||||||
"An error occurred while fetching the details: " + e.getMessage
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,11 +48,72 @@ def readData(filePath: String): Unit = {
|
|||||||
val bufferedSource = io.Source.fromFile(filePath)
|
val bufferedSource = io.Source.fromFile(filePath)
|
||||||
for (line <- bufferedSource.getLines) {
|
for (line <- bufferedSource.getLines) {
|
||||||
val cols = line.split(",").map(_.trim)
|
val cols = line.split(",").map(_.trim)
|
||||||
println(s"${cols(0)}|${cols(1)}|${cols(2)}")
|
println(s"${cols(0)}\t${cols(1)}\t${cols(2)}")
|
||||||
}
|
}
|
||||||
bufferedSource.close
|
bufferedSource.close
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def checkSources(filePath: String): Unit = {
|
||||||
|
val bufferedSource = io.Source.fromFile(filePath)
|
||||||
|
for (line <- bufferedSource.getLines) {
|
||||||
|
val cols = line.split(",").map(_.trim)
|
||||||
|
println(s"${cols(0)}\t${cols(1)}\t${cols(2)}")
|
||||||
|
}
|
||||||
|
bufferedSource.close
|
||||||
|
|
||||||
|
print("Enter your choice:\n1) Modify\n2) Exit\n")
|
||||||
|
val choice = readLine()
|
||||||
|
choice match {
|
||||||
|
case "1" =>
|
||||||
|
print(
|
||||||
|
"Choose the source to modify:\n1) Wind\n2) Hydro\n3) Nuclear\n4) Exit\n"
|
||||||
|
)
|
||||||
|
val choice2 = readLine()
|
||||||
|
choice2 match {
|
||||||
|
case "1" =>
|
||||||
|
print("Enter the new value: ")
|
||||||
|
val newValue = readLine()
|
||||||
|
val lines = Source.fromFile(filePath).getLines.toList
|
||||||
|
val temp = lines(1).split(",")
|
||||||
|
val newLines = lines.updated(1, s"$newValue,${temp(1)},${temp(2)}")
|
||||||
|
val pw = new java.io.PrintWriter(filePath)
|
||||||
|
newLines.foreach(pw.println)
|
||||||
|
pw.close()
|
||||||
|
println("Value updated")
|
||||||
|
case "2" =>
|
||||||
|
print("Enter the new value: ")
|
||||||
|
val newValue = readLine()
|
||||||
|
val lines = Source.fromFile(filePath).getLines.toList
|
||||||
|
val temp = lines(1).split(",")
|
||||||
|
val newLines = lines.updated(1, s"${temp(0)},$newValue,${temp(2)}")
|
||||||
|
val pw = new java.io.PrintWriter(filePath)
|
||||||
|
newLines.foreach(pw.println)
|
||||||
|
pw.close()
|
||||||
|
println("Value updated")
|
||||||
|
case "3" =>
|
||||||
|
print("Enter the new value: ")
|
||||||
|
val newValue = readLine()
|
||||||
|
val lines = Source.fromFile(filePath).getLines.toList
|
||||||
|
val temp = lines(1).split(",")
|
||||||
|
val newLines = lines.updated(1, s"${temp(0)},${temp(1)},$newValue")
|
||||||
|
val pw = new java.io.PrintWriter(filePath)
|
||||||
|
newLines.foreach(pw.println)
|
||||||
|
pw.close()
|
||||||
|
println("Value updated")
|
||||||
|
case "4" =>
|
||||||
|
println("Exiting...")
|
||||||
|
System.exit(0)
|
||||||
|
case _ =>
|
||||||
|
println("Invalid choice")
|
||||||
|
}
|
||||||
|
case "2" =>
|
||||||
|
println("Exiting...")
|
||||||
|
System.exit(0)
|
||||||
|
case _ =>
|
||||||
|
println("Invalid choice")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
def main(args: Array[String]): Unit = {
|
def main(args: Array[String]): Unit = {
|
||||||
val now = (java.time.LocalDateTime.now)
|
val now = (java.time.LocalDateTime.now)
|
||||||
.truncatedTo(java.time.temporal.ChronoUnit.SECONDS)
|
.truncatedTo(java.time.temporal.ChronoUnit.SECONDS)
|
||||||
@@ -77,51 +125,32 @@ def main(args: Array[String]): Unit = {
|
|||||||
|
|
||||||
choice match {
|
choice match {
|
||||||
case "1" =>
|
case "1" =>
|
||||||
println("Energy sources:")
|
// println("Energy sources:")
|
||||||
println("1) Wind\n2) Solar\n3) Hydro\n4) Nuclear\n5) Fossil\n6) All")
|
// println("1) Wind\n2) Hydro\n3) Nuclear\n4) All")
|
||||||
print("Enter your choice: ")
|
// print("Enter your choice: ")
|
||||||
val choice2 = readLine()
|
// val choice2 = readLine()
|
||||||
choice2 match {
|
// choice2 match {
|
||||||
case "1" =>
|
// case "1" =>
|
||||||
println("Wind")
|
// println("Wind")
|
||||||
case "2" =>
|
// case "2" =>
|
||||||
println("Solar")
|
// println("Hydro")
|
||||||
case "3" =>
|
// case "3" =>
|
||||||
println("Hydro")
|
// println("Nuclear")
|
||||||
case "4" =>
|
// case "4" =>
|
||||||
println("Nuclear")
|
// println("All")
|
||||||
case "5" =>
|
// case _ =>
|
||||||
println("Fossil")
|
// println("Invalid choice")
|
||||||
case "6" =>
|
// }
|
||||||
println("All")
|
checkSources("sources.csv")
|
||||||
case _ =>
|
|
||||||
println("Invalid choice")
|
|
||||||
}
|
|
||||||
case "2" =>
|
case "2" =>
|
||||||
println("Collecting data...")
|
println("Collecting data...")
|
||||||
collectData(
|
collectData(
|
||||||
188,
|
Map(
|
||||||
now.minusMonths(3).toString().concat("Z"),
|
188 -> "nuclear.csv",
|
||||||
now.toString.concat("Z"),
|
191 -> "hydro.csv",
|
||||||
"nuclear.csv"
|
181 -> "wind.csv",
|
||||||
)
|
192 -> "data.csv"
|
||||||
collectData(
|
)
|
||||||
191,
|
|
||||||
now.minusMonths(3).toString().concat("Z"),
|
|
||||||
now.toString.concat("Z"),
|
|
||||||
"hydro.csv"
|
|
||||||
)
|
|
||||||
collectData(
|
|
||||||
181,
|
|
||||||
now.minusMonths(3).toString().concat("Z"),
|
|
||||||
now.toString.concat("Z"),
|
|
||||||
"wind.csv"
|
|
||||||
)
|
|
||||||
collectData(
|
|
||||||
192,
|
|
||||||
now.minusMonths(3).toString().concat("Z"),
|
|
||||||
now.toString.concat("Z"),
|
|
||||||
"data.csv"
|
|
||||||
)
|
)
|
||||||
println("Collection completed")
|
println("Collection completed")
|
||||||
case "3" =>
|
case "3" =>
|
||||||
|
|||||||
Reference in New Issue
Block a user