Skip to content

Commit

Permalink
55938-elys
Browse files Browse the repository at this point in the history
  • Loading branch information
ELY M committed Jan 30, 2025
1 parent ec40b6e commit 7c456fe
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 65 deletions.
8 changes: 3 additions & 5 deletions app/src/main/java/joshuatee/wx/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,11 @@ fun String.getHtmlWithNewLineWithRetry(expectedMinSize: Long): String {
return html
}

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

fun String.getNwsHtmlWithRetry(expectedMinSize: Long): String {
var html = this.getNwsHtml()
fun String.getHtmlWithRetry(expectedMinSize: Long): String {
var html = this.getHtml()
if (html.length < expectedMinSize) {
Thread.sleep(expectedMinSize)
html = this.getNwsHtml()
html = this.getHtml()
}
return html
}
Expand Down
19 changes: 10 additions & 9 deletions app/src/main/java/joshuatee/wx/objects/Sites.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,18 @@ class Sites(

init {
checkValidityMaps()
synchronized(sites) {
nameDict.forEach { (key, value) ->
sites.add(Site(key, value, latDict[key]!!, lonDict[key]!!, lonReversed))
byCode[key] = sites.last()
}

nameDict.forEach { (key, value) ->
sites.add(Site(key, value, latDict[key]!!, lonDict[key]!!, lonReversed))
byCode[key] = sites.last()
}

sites.sortBy { it.fullName }
sites.sortBy { it.fullName }

sites.forEach { site ->
codeList.add(site.codeName)
nameList.add("${site.codeName}: ${site.fullName}")
sites.forEach { site ->
codeList.add(site.codeName)
nameList.add("${site.codeName}: ${site.fullName}")
}
}
}

Expand Down
10 changes: 6 additions & 4 deletions app/src/main/java/joshuatee/wx/radar/Metar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,12 @@ internal object Metar {
private fun getNearbyObsSites(radarSite: String): String {
val radarLocation = RadarSites.getLatLon(radarSite)
val obsListSb = StringBuilder(1000)
sites.sites.forEach {
if (LatLon.distance(radarLocation, it.latLon) < 200.0) {
obsListSb.append(it.codeName)
obsListSb.append(",")
synchronized(sites.sites) {
sites.sites.forEach {
if (LatLon.distance(radarLocation, it.latLon) < 200.0) {
obsListSb.append(it.codeName)
obsListSb.append(",")
}
}
}
return obsListSb.toString().replace(",$".toRegex(), "")
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/joshuatee/wx/settings/Location.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import joshuatee.wx.objects.LatLon
import joshuatee.wx.util.Utility
import java.util.Locale
import joshuatee.wx.common.GlobalDictionaries
import joshuatee.wx.getNwsHtml
import joshuatee.wx.getHtml
import joshuatee.wx.parse
import joshuatee.wx.radar.RadarSites
import joshuatee.wx.util.To
Expand Down Expand Up @@ -133,7 +133,7 @@ object Location {

private fun getWfoRadarSiteFromPoint(location: LatLon): List<String> {
val pointData =
("https://api.weather.gov/points/" + location.latString + "," + location.lonString).getNwsHtml()
("https://api.weather.gov/points/" + location.latString + "," + location.lonString).getHtml()
// "cwa": "IWX",
// "radarStation": "KGRR"
val wfo = pointData.parse("\"cwa\": \"(.*?)\"")
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/joshuatee/wx/util/DownloadText.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import joshuatee.wx.settings.Location
import joshuatee.wx.common.RegExp
import joshuatee.wx.getHtml
import joshuatee.wx.getHtmlWithNewLine
import joshuatee.wx.getNwsHtml
import joshuatee.wx.getHtml
import joshuatee.wx.parse
import joshuatee.wx.parseAcrossLines
import joshuatee.wx.removeHtml
Expand Down Expand Up @@ -286,9 +286,9 @@ object DownloadText {
val location = prod.substring(3, 5).replace("%", "")
val url =
GlobalVariables.NWS_API_URL + "/products/types/$product/locations/$location"
val html = url.getNwsHtml()
val html = url.getHtml()
val urlProd = html.parse("\"id\": \"(.*?)\"")
val prodHtml = (GlobalVariables.NWS_API_URL + "/products/$urlProd").getNwsHtml()
val prodHtml = (GlobalVariables.NWS_API_URL + "/products/$urlProd").getHtml()
text = UtilityString.parseAcrossLines(prodHtml, "\"productText\": \"(.*?)\\}")
text = text.replace("\\n\\n", "\n")
text = text.replace("\\n", "\n")
Expand All @@ -314,9 +314,9 @@ object DownloadText {
val t2 = prod.substring(3).replace("%", "")
if (USE_NWS_API) {
val url = GlobalVariables.NWS_API_URL + "/products/types/$t1/locations/$t2"
val html = url.getNwsHtml()
val html = url.getHtml()
val urlProd = html.parse("\"id\": \"(.*?)\"")
val prodHtml = (GlobalVariables.NWS_API_URL + "/products/$urlProd").getNwsHtml()
val prodHtml = (GlobalVariables.NWS_API_URL + "/products/$urlProd").getHtml()
text = UtilityString.parseAcrossLines(prodHtml, "\"productText\": \"(.*?)\\}")
if (!prod.startsWith("RTP")) {
text = text.replace("\\n\\n", "<BR>")
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/joshuatee/wx/util/Hazards.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ package joshuatee.wx.util
import joshuatee.wx.parseColumn
import joshuatee.wx.settings.UIPreferences
import joshuatee.wx.common.GlobalVariables
import joshuatee.wx.getNwsHtml
import joshuatee.wx.getHtml
import joshuatee.wx.objects.LatLon
import joshuatee.wx.settings.Location

Expand Down Expand Up @@ -58,6 +58,6 @@ class Hazards {

companion object {
fun getHtml(latLon: LatLon): String =
("https://api.weather.gov/alerts?point=" + latLon.latForNws + "," + latLon.lonForNws + "&active=1").getNwsHtml()
("https://api.weather.gov/alerts?point=" + latLon.latForNws + "," + latLon.lonForNws + "&active=1").getHtml()
}
}
10 changes: 5 additions & 5 deletions app/src/main/java/joshuatee/wx/util/UtilityDownloadNWS.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import java.util.Locale
import joshuatee.wx.objects.LatLon
import joshuatee.wx.settings.UIPreferences
import joshuatee.wx.common.GlobalVariables
import joshuatee.wx.getNwsHtml
import joshuatee.wx.getNwsHtmlWithRetry
import joshuatee.wx.getHtml
import joshuatee.wx.getHtmlWithRetry
import joshuatee.wx.parse

// https://www.weather.gov/documentation/services-web-api
Expand All @@ -48,7 +48,7 @@ object UtilityDownloadNws {
fun getHourlyData(latLon: LatLon): String {
val pointsData = getLocationPointData(latLon)
val hourlyUrl = pointsData.parse("\"forecastHourly\": \"(.*?)\"")
return hourlyUrl.getNwsHtmlWithRetry(1000)
return hourlyUrl.getHtmlWithRetry(1000)
}

fun getHourlyOldData(latLon: LatLon): String {
Expand All @@ -62,11 +62,11 @@ object UtilityDownloadNws {
fun get7DayData(latLon: LatLon): String = if (UIPreferences.useNwsApi) {
val pointsData = getLocationPointData(latLon)
val forecastUrl = pointsData.parse("\"forecast\": \"(.*?)\"")
forecastUrl.getNwsHtmlWithRetry(3000)
forecastUrl.getHtmlWithRetry(3000)
} else {
UtilityUS.getLocationHtml(latLon)
}

private fun getLocationPointData(latLon: LatLon): String =
(GlobalVariables.NWS_API_URL + "/points/" + latLon.latForNws + "," + latLon.lonForNws).getNwsHtml()
(GlobalVariables.NWS_API_URL + "/points/" + latLon.latForNws + "," + latLon.lonForNws).getHtml()
}
1 change: 0 additions & 1 deletion app/src/main/java/joshuatee/wx/util/UtilityFavorites.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ object UtilityFavorites {
val name = when (type) {
FavoriteType.RID -> RadarSites.getName(fav)
FavoriteType.WFO -> WfoSites.getFullName(fav)
// FavoriteType.SND -> UtilityLocation.getSoundingSiteName(fav)
FavoriteType.SND -> SoundingSites.sites.byCode[fav]?.fullName
FavoriteType.NWS_TEXT -> UtilityWpcText.getLabel(fav)
FavoriteType.SPCMESO -> UtilitySpcMeso.getLabelFromParam(fav)
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/joshuatee/wx/util/UtilityIO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ object UtilityIO {
}
}

@Suppress("unused")
fun downloadAsByteArray(url: String): ByteArray = URL(url).getBytes()
// @Suppress("unused")
// fun downloadAsByteArray(url: String): ByteArray = URL(url).getBytes()

fun getHtml(url: String): String = UtilityNetworkIO.getStringFromUrlWithNewLine(url)

Expand Down
13 changes: 0 additions & 13 deletions app/src/main/java/joshuatee/wx/util/UtilityMath.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,6 @@ object UtilityMath {
): List<Double> =
listOf(x0 + fraction * (x1 - x0), y0 + fraction * (y1 - y0))

// 42.98888 to 42.99
// fun latLonFix(x: String): String {
// val dblX = To.double(x)
// var newX = "0.0"
// try {
// newX = String.format(Locale.US, "%.2f", dblX).replace("00$".toRegex(), "")
// .replace("0$".toRegex(), "").replace("\\.$".toRegex(), "")
// } catch (e: Exception) {
// UtilityLog.handleException(e)
// }
// return newX
// }

// convert polar cords to rect
fun toRect(r: Float, t: Float): FloatArray =
floatArrayOf((r * cos(t / (180.0f / PI))).toFloat(), (r * sin(t / (180.0f / PI))).toFloat())
Expand Down
27 changes: 10 additions & 17 deletions app/src/main/java/joshuatee/wx/util/UtilityNetworkIO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,6 @@ object UtilityNetworkIO {
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 fun getStringFromUrlNew(url: String, withNewLine: Boolean): String {
UtilityLog.download("getStringFromUrlNew $withNewLine: $url")
val request = Request.Builder().url(url).header("User-Agent", USER_AGENT).build()
return requestToString(request, withNewLine)
}

// String.getHtml()
// output has newlines removed
fun getStringFromUrl(url: String): String = getStringFromUrlNew(url, false)

// String.getHtmlWithNewLine()
fun getStringFromUrlWithNewLine(url: String): String = getStringFromUrlNew(url, true)

// String.getImage()
fun getBitmapFromUrl(url: String): Bitmap = try {
UtilityLog.download("getBitmapFromUrl: $url")
Expand Down Expand Up @@ -95,11 +82,17 @@ object UtilityNetworkIO {
return requestToString(request)
}

// target for String.getNwsHtml()
fun getStringFromUrlBaseNoAcceptHeader1(url: String): String {
UtilityLog.download("getStringFromUrlBaseNoAcceptHeader1 getNwsHtml: $url")
// String.getHtml()
// output has newlines removed
fun getStringFromUrl(url: String): String = getStringFromUrlNew(url, false)

// String.getHtmlWithNewLine()
fun getStringFromUrlWithNewLine(url: String): String = getStringFromUrlNew(url, true)

private fun getStringFromUrlNew(url: String, withNewLine: Boolean): String {
UtilityLog.download("getStringFromUrlNew $withNewLine: $url")
val request = Request.Builder().url(url).header("User-Agent", USER_AGENT).build()
return requestToString(request)
return requestToString(request, withNewLine)
}

// PolygonWarning.kt
Expand Down

0 comments on commit 7c456fe

Please sign in to comment.