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/wind.csv
|
||||
src/main/scala/apiKey.txt
|
||||
src/main/scala/sources.csv
|
||||
project
|
||||
target
|
||||
|
||||
|
||||
@@ -1,59 +1,46 @@
|
||||
import java.io._
|
||||
import java.net._
|
||||
import java.net.{HttpURLConnection, URL}
|
||||
import java.nio.file.{Files, Paths}
|
||||
import scala.util.Try
|
||||
import scala.io.StdIn.readLine
|
||||
import scala.io.Source
|
||||
|
||||
val API_KEY = Source.fromFile("apiKey.txt").getLines().mkString
|
||||
|
||||
def collectData(
|
||||
variableId: Int,
|
||||
startTime: String,
|
||||
endTime: String,
|
||||
filePath: String
|
||||
dataSources: Map[Int, String]
|
||||
): 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 {
|
||||
// Set the URL for the API endpoint
|
||||
for ((k, v) <- dataSources) {
|
||||
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
|
||||
val connection =
|
||||
url.openConnection().asInstanceOf[HttpURLConnection]
|
||||
Try(url.openConnection().asInstanceOf[HttpURLConnection]) match {
|
||||
case util.Success(connection) =>
|
||||
connection.setRequestMethod("GET")
|
||||
connection.setRequestProperty("Accept", "text/csv")
|
||||
connection.setRequestProperty("x-api-key", API_KEY)
|
||||
connection.connect()
|
||||
|
||||
// Set the request method to GET
|
||||
connection.setRequestMethod("GET")
|
||||
|
||||
// Set the request headers
|
||||
connection.setRequestProperty("Accept", "text/csv")
|
||||
connection.setRequestProperty("x-api-key", API_KEY)
|
||||
|
||||
// Send the GET request to the API endpoint
|
||||
connection.connect()
|
||||
|
||||
// Read the response from the API endpoint
|
||||
val inputStream = connection.getInputStream
|
||||
val inputStreamReader = new InputStreamReader(inputStream)
|
||||
val reader = new BufferedReader(inputStreamReader)
|
||||
val response = new StringBuilder
|
||||
var line: String = reader.readLine
|
||||
while (line != null) {
|
||||
response.append(line + "\n")
|
||||
line = reader.readLine
|
||||
Try(
|
||||
scala.io.Source.fromInputStream(connection.getInputStream).mkString
|
||||
) match {
|
||||
case util.Success(response) =>
|
||||
Files.write(Paths.get(v), response.getBytes("UTF-8"))
|
||||
case util.Failure(e) =>
|
||||
System.err.println(
|
||||
s"An error occurred while reading the response: ${e.getMessage}"
|
||||
)
|
||||
}
|
||||
case util.Failure(e) =>
|
||||
System.err.println(
|
||||
s"An error occurred while connecting to the API endpoint: ${e.getMessage}"
|
||||
)
|
||||
}
|
||||
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)
|
||||
for (line <- bufferedSource.getLines) {
|
||||
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
|
||||
}
|
||||
|
||||
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 = {
|
||||
val now = (java.time.LocalDateTime.now)
|
||||
.truncatedTo(java.time.temporal.ChronoUnit.SECONDS)
|
||||
@@ -77,51 +125,32 @@ def main(args: Array[String]): Unit = {
|
||||
|
||||
choice match {
|
||||
case "1" =>
|
||||
println("Energy sources:")
|
||||
println("1) Wind\n2) Solar\n3) Hydro\n4) Nuclear\n5) Fossil\n6) All")
|
||||
print("Enter your choice: ")
|
||||
val choice2 = readLine()
|
||||
choice2 match {
|
||||
case "1" =>
|
||||
println("Wind")
|
||||
case "2" =>
|
||||
println("Solar")
|
||||
case "3" =>
|
||||
println("Hydro")
|
||||
case "4" =>
|
||||
println("Nuclear")
|
||||
case "5" =>
|
||||
println("Fossil")
|
||||
case "6" =>
|
||||
println("All")
|
||||
case _ =>
|
||||
println("Invalid choice")
|
||||
}
|
||||
// println("Energy sources:")
|
||||
// println("1) Wind\n2) Hydro\n3) Nuclear\n4) All")
|
||||
// print("Enter your choice: ")
|
||||
// val choice2 = readLine()
|
||||
// choice2 match {
|
||||
// case "1" =>
|
||||
// println("Wind")
|
||||
// case "2" =>
|
||||
// println("Hydro")
|
||||
// case "3" =>
|
||||
// println("Nuclear")
|
||||
// case "4" =>
|
||||
// println("All")
|
||||
// case _ =>
|
||||
// println("Invalid choice")
|
||||
// }
|
||||
checkSources("sources.csv")
|
||||
case "2" =>
|
||||
println("Collecting data...")
|
||||
collectData(
|
||||
188,
|
||||
now.minusMonths(3).toString().concat("Z"),
|
||||
now.toString.concat("Z"),
|
||||
"nuclear.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"
|
||||
Map(
|
||||
188 -> "nuclear.csv",
|
||||
191 -> "hydro.csv",
|
||||
181 -> "wind.csv",
|
||||
192 -> "data.csv"
|
||||
)
|
||||
)
|
||||
println("Collection completed")
|
||||
case "3" =>
|
||||
|
||||
Reference in New Issue
Block a user