Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
ProjektMavenowyPatryka
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patryk Czarnik
ProjektMavenowyPatryka
Commits
6663e798
Commit
6663e798
authored
Jul 06, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Przykład pobierania pogody
parent
9d82701c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
80 additions
and
0 deletions
+80
-0
06_pobierz_pogode_dla_miasta.py
src/main/java/pogoda/06_pobierz_pogode_dla_miasta.py
+27
-0
PobierzPogodeDlaMiasta.java
src/main/java/pogoda/PobierzPogodeDlaMiasta.java
+53
-0
No files found.
src/main/java/pogoda/06_pobierz_pogode_dla_miasta.py
0 → 100644
View file @
6663e798
import
requests
miasto
=
input
(
'Podaj nazwę miasta: '
)
# Dokończ: wyślij zapytanie o miasto, odczytaj współrzędne
# zadaj zapytanie o prognozę
# wypisz bieżacą pogodę oraz listę temperatur
json
=
requests
.
get
(
f
'https://geocoding-api.open-meteo.com/v1/search?name={miasto}'
)
.
json
()
miejsce
=
json
[
'results'
][
0
]
# Jeśli nie znajdzie żadnego takiego miejsca, to tu program się wykrzaczy
lat
,
lon
,
country
,
name
=
miejsce
[
'latitude'
],
miejsce
[
'longitude'
],
miejsce
[
'country'
],
miejsce
[
'name'
]
print
(
f
'Znalazłem miejscowość {name} w kraju {country}'
)
json
=
requests
.
get
(
f
'https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}¤t_weather=true&hourly=temperature_2m'
)
.
json
()
current
=
json
[
'current_weather'
]
print
(
'Temperatura:'
,
current
[
'temperature'
])
print
(
'Wiatr:'
,
current
[
'windspeed'
])
# print('Daty:', json['hourly']['time'])
# print('Temperatury:', json['hourly']['temperature_2m'])
# Przeglądanie dwóch list na raz: funkcja zip
print
(
'
\n
Prognozowane temperatury:'
)
for
dt
,
temp
in
zip
(
json
[
'hourly'
][
'time'
],
json
[
'hourly'
][
'temperature_2m'
]):
print
(
dt
,
temp
)
src/main/java/pogoda/PobierzPogodeDlaMiasta.java
0 → 100644
View file @
6663e798
package
pogoda
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.net.MalformedURLException
;
import
java.net.URL
;
import
javax.swing.JOptionPane
;
import
jakarta.json.Json
;
import
jakarta.json.JsonArray
;
import
jakarta.json.JsonObject
;
import
jakarta.json.JsonReader
;
import
jakarta.json.JsonValue
;
public
class
PobierzPogodeDlaMiasta
{
public
static
void
main
(
String
[]
args
)
{
try
{
double
lat
=
0
;
double
lon
=
0
;
String
miasto
=
JOptionPane
.
showInputDialog
(
"Podaj nazwę miasta: "
);
URL
url
=
new
URL
(
"https://geocoding-api.open-meteo.com/v1/search?name="
+
miasto
);
try
(
InputStream
input
=
url
.
openStream
();
JsonReader
reader
=
Json
.
createReader
(
input
))
{
JsonObject
json
=
reader
.
readObject
();
JsonArray
results
=
json
.
getJsonArray
(
"results"
);
System
.
out
.
println
(
"Znalezione lokalizacje:"
);
for
(
JsonValue
result
:
results
)
{
System
.
out
.
println
(
result
);
}
JsonObject
loc
=
results
.
getJsonObject
(
0
);
lat
=
loc
.
getJsonNumber
(
"latitude"
).
doubleValue
();
lon
=
loc
.
getJsonNumber
(
"longitude"
).
doubleValue
();
}
url
=
new
URL
(
"https://api.open-meteo.com/v1/forecast?latitude="
+
lat
+
"&longitude="
+
lon
+
"¤t_weather=true&hourly=temperature_2m"
);
try
(
InputStream
input
=
url
.
openStream
();
JsonReader
reader
=
Json
.
createReader
(
input
))
{
JsonObject
json
=
reader
.
readObject
();
JsonObject
weather
=
json
.
getJsonObject
(
"current_weather"
);
System
.
out
.
println
(
weather
);
System
.
out
.
println
(
"T = "
+
weather
.
getJsonNumber
(
"temperature"
).
doubleValue
());
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment