This commit is contained in:
AndrewTrieu
2023-04-28 09:41:31 +03:00
parent 7d651e0800
commit 2c7fb72aaf
2 changed files with 119 additions and 86 deletions

4
.gitignore vendored
View File

@@ -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

View File

@@ -1,71 +1,119 @@
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]
// Set the request method to GET
connection.setRequestMethod("GET") connection.setRequestMethod("GET")
// Set the request headers
connection.setRequestProperty("Accept", "text/csv") connection.setRequestProperty("Accept", "text/csv")
connection.setRequestProperty("x-api-key", API_KEY) connection.setRequestProperty("x-api-key", API_KEY)
// Send the GET request to the API endpoint
connection.connect() connection.connect()
// Read the response from the API endpoint Try(
val inputStream = connection.getInputStream scala.io.Source.fromInputStream(connection.getInputStream).mkString
val inputStreamReader = new InputStreamReader(inputStream) ) match {
val reader = new BufferedReader(inputStreamReader) case util.Success(response) =>
val response = new StringBuilder Files.write(Paths.get(v), response.getBytes("UTF-8"))
var line: String = reader.readLine case util.Failure(e) =>
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( System.err.println(
"An error occurred while fetching the details: " + e.getMessage 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}"
)
}
}
} }
def readData(filePath: String): Unit = { 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" =>