Skip to content

Commit

Permalink
55924-elys
Browse files Browse the repository at this point in the history
  • Loading branch information
ELY M committed Dec 17, 2024
1 parent 7800bd5 commit e7c1f79
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 36 deletions.
18 changes: 18 additions & 0 deletions app/src/main/java/joshuatee/wx/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,26 @@ fun String.getHtml() = UtilityNetworkIO.getStringFromUrl(this)

fun String.getHtmlWithNewLine() = UtilityNetworkIO.getStringFromUrlWithNewLine(this)

fun String.getHtmlWithNewLineWithRetry(expectedMinSize: Long): String {
var html = this.getHtmlWithNewLine()
if (html.length < expectedMinSize) {
Thread.sleep(expectedMinSize)
html = this.getHtmlWithNewLine()
}
return html
}

fun String.getNwsHtml() = UtilityDownloadNws.getStringFromUrlBaseNoAcceptHeader1(this)

fun String.getNwsHtmlWithRetry(expectedMinSize: Long): String {
var html = this.getNwsHtml()
if (html.length < expectedMinSize) {
Thread.sleep(expectedMinSize)
html = this.getNwsHtml()
}
return html
}

fun String.parseColumnAll(pattern: Pattern) = UtilityString.parseColumnAll(this, pattern)

fun String.parseLastMatch(pattern: Pattern) = UtilityString.parseLastMatch(this, pattern)
Expand Down
6 changes: 1 addition & 5 deletions app/src/main/java/joshuatee/wx/misc/UtilityHourly.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,7 @@ object UtilityHourly {
}

private fun getString(locationNumber: Int): List<String> {
var html = UtilityDownloadNws.getHourlyData(Location.getLatLon(locationNumber))
if (html.length < 1000) {
Thread.sleep(1000)
html = UtilityDownloadNws.getHourlyData(Location.getLatLon(locationNumber))
}
val html = UtilityDownloadNws.getHourlyData(Location.getLatLon(locationNumber))
val header = To.stringPadLeft("Time", 7) + " " +
To.stringPadLeft("Temp", 5) +
To.stringPadLeft("WindSpd", 9) +
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/joshuatee/wx/util/ObjectMetar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import joshuatee.wx.objects.Site
import joshuatee.wx.radar.Metar
import joshuatee.wx.settings.UIPreferences
import joshuatee.wx.common.GlobalVariables
import joshuatee.wx.getHtmlWithNewLine
import joshuatee.wx.getHtmlWithNewLineWithRetry
import joshuatee.wx.objects.ObjectDateTime
import joshuatee.wx.parse
import kotlin.math.roundToInt
Expand Down Expand Up @@ -60,7 +60,7 @@ internal class ObjectMetar(context: Context, location: LatLon, index: Int = 0) {
init {
val urlMetar =
"${GlobalVariables.TGFTP_WEBSITE_PREFIX}/data/observations/metar/decoded/" + obsClosest.codeName + ".TXT"
val metarData = urlMetar.getHtmlWithNewLine()
val metarData = urlMetar.getHtmlWithNewLineWithRetry(200)
temperature = metarData.parse("Temperature: (.*?) F")
dewPoint = metarData.parse("Dew Point: (.*?) F")
windDirection = metarData.parse("Wind: from the (.*?) \\(.*? degrees\\) at .*? MPH ")
Expand Down
63 changes: 34 additions & 29 deletions app/src/main/java/joshuatee/wx/util/UtilityDownloadNWS.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,30 @@ import okhttp3.Request
import joshuatee.wx.settings.UIPreferences
import joshuatee.wx.common.GlobalVariables
import joshuatee.wx.getNwsHtml
import joshuatee.wx.getNwsHtmlWithRetry
import joshuatee.wx.parse

// https://www.weather.gov/documentation/services-web-api
// default format via accept header is GeoJSON: application/geo+json

object UtilityDownloadNws {

private const val USER_AGENT = "Android ${GlobalVariables.PACKAGE_NAME} ${GlobalVariables.EMAIL}"
private const val ACCEPT_HEADER = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
private const val USER_AGENT =
"Android ${GlobalVariables.PACKAGE_NAME} ${GlobalVariables.EMAIL}"
private const val ACCEPT_HEADER =
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"

fun getHazardData(url: String): String = url.getNwsHtml()

// used for USWarningsWithRadarActivity / AlertSummary / CapAlert.initializeFromCap (XML) - CONUS wide alerts activity
fun getCap(sector: String): String = if (sector == "us") {
getStringFromUrlXml(GlobalVariables.NWS_API_URL + "/alerts/active?region_type=land")
} else {
getStringFromUrlXml(GlobalVariables.NWS_API_URL + "/alerts/active/area/" + sector.uppercase(Locale.US))
getStringFromUrlXml(
GlobalVariables.NWS_API_URL + "/alerts/active/area/" + sector.uppercase(
Locale.US
)
)
}

// used for CapAlert (XML)
Expand All @@ -56,10 +63,10 @@ object UtilityDownloadNws {
val out = StringBuilder(5000)
try {
val request = Request.Builder()
.url(url)
.header("User-Agent", USER_AGENT)
.addHeader("Accept", "application/atom+xml")
.build()
.url(url)
.header("User-Agent", USER_AGENT)
.addHeader("Accept", "application/atom+xml")
.build()
val response = MyApplication.httpClient.newCall(request).execute()
val inputStream = BufferedInputStream(response.body.byteStream())
val bufferedReader = BufferedReader(InputStreamReader(inputStream))
Expand All @@ -81,9 +88,9 @@ object UtilityDownloadNws {
val out = StringBuilder(5000)
try {
val request = Request.Builder()
.url(url)
.header("User-Agent", USER_AGENT)
.build()
.url(url)
.header("User-Agent", USER_AGENT)
.build()
val response = MyApplication.httpClient.newCall(request).execute()
val inputStream = BufferedInputStream(response.body.byteStream())
val bufferedReader = BufferedReader(InputStreamReader(inputStream))
Expand All @@ -106,10 +113,10 @@ object UtilityDownloadNws {
val out = StringBuilder(5000)
try {
val request = Request.Builder()
.url(url)
.header("User-Agent", USER_AGENT)
.addHeader("Accept", ACCEPT_HEADER)
.build()
.url(url)
.header("User-Agent", USER_AGENT)
.addHeader("Accept", ACCEPT_HEADER)
.build()
val response = MyApplication.httpClient.newCall(request).execute()
val inputStream = BufferedInputStream(response.body.byteStream())
val bufferedReader = BufferedReader(InputStreamReader(inputStream))
Expand All @@ -132,10 +139,13 @@ object UtilityDownloadNws {
val out = StringBuilder(5000)
try {
val request = Request.Builder()
.url(url)
.header("User-Agent", USER_AGENT)
.addHeader("Accept", "application/vnd.noaa.dwml+xml;version=1") // TODO FIXME, not valid defaulting to application/geo+json
.build()
.url(url)
.header("User-Agent", USER_AGENT)
.addHeader(
"Accept",
"application/vnd.noaa.dwml+xml;version=1"
) // TODO FIXME, not valid defaulting to application/geo+json
.build()
val response = MyApplication.httpClient.newCall(request).execute()
val inputStream = BufferedInputStream(response.body.byteStream())
val bufferedReader = BufferedReader(InputStreamReader(inputStream))
Expand All @@ -155,30 +165,25 @@ object UtilityDownloadNws {
fun getHourlyData(latLon: LatLon): String {
val pointsData = getLocationPointData(latLon)
val hourlyUrl = pointsData.parse("\"forecastHourly\": \"(.*?)\"")
return hourlyUrl.getNwsHtml()
return hourlyUrl.getNwsHtmlWithRetry(1000)
}

fun getHourlyOldData(latLon: LatLon): String {
return UtilityIO.getHtml(
"https://forecast.weather.gov/MapClick.php?lat=" +
latLon.latForNws + "&lon=" +
latLon.lonForNws + "&FcstType=digitalDWML"
"https://forecast.weather.gov/MapClick.php?lat=" +
latLon.latForNws + "&lon=" +
latLon.lonForNws + "&FcstType=digitalDWML"
)
}

fun get7DayData(latLon: LatLon): String = if (UIPreferences.useNwsApi) {
val pointsData = getLocationPointData(latLon)
val forecastUrl = pointsData.parse("\"forecast\": \"(.*?)\"")
var html = forecastUrl.getNwsHtml()
if (html.length < 3000) {
Thread.sleep(1000)
html = forecastUrl.getNwsHtml()
}
html
forecastUrl.getNwsHtmlWithRetry(3000)
} else {
UtilityUS.getLocationHtml(latLon)
}

private fun getLocationPointData(latLon: LatLon): String =
(GlobalVariables.NWS_API_URL + "/points/" + latLon.latString + "," + latLon.lonString).getNwsHtml()
(GlobalVariables.NWS_API_URL + "/points/" + latLon.latString + "," + latLon.lonString).getNwsHtml()
}
4 changes: 4 additions & 0 deletions doc/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
* [ADD] per **PNS24-66 Updated** NWS will implement a fix in advance of Dec 22 for the NWS API, thus
7 day forecast is now configurable again and defaults to using the new NWS API
* [REF] refactor parts of color palette generators to match other ports
* [REF] add extensions getHtmlWithNewLineWithRetry and getNwsHtmlWithRetry and use for NWS API 7-day
and Hourly
* [ADD] ObjectMetar - for getting current conditions for main screen do manual retry if needed (like
7-day/hourly)

## 55923 2024_12_03

Expand Down

0 comments on commit e7c1f79

Please sign in to comment.