<commit_before>-- vim: set noexpandtab:
local sql = require'lsqlite3'
local iconv = require'iconv'

local utf2iso = iconv.new('iso-8859-15', 'utf-8')
local iso2utf = iconv.new('utf-8', 'iso-8859-15')

local days = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }

local parseDate = function(datestr)
	local year, month, day, hour, min, sec = datestr:match("([^-]+)%-([^-]+)%-([^T]+)T([^:]+):([^:]+):(%d%d)")
	return os.time{
		year = year,
		month = month,
		day = day,
		hour = hour,
		min = min,
		sec = sec,
	}
end

local feelsLike = function(celsius, wind)
	local V = wind * 3.6
	return math.floor(13.12 + 0.6215 * celsius - 11.37 * V^0.16 + 0.3965 * celsius * V^0.16 + .5)
end

local formatPeriod = function(period)
	local out = {}

	table.insert(out, string.format("%s, %s°C (feels like %s°C)", period.symbol.name, period.temperature.value, period.temperature.feels, period.temperature.feels))

	local rain = period.precipitation
	if(rain.value ~= "0") then
		if(rain.minvalue and rain.maxvalue) then
			table.insert(out, string.format("%s-%s mm", rain.minvalue, rain.maxvalue))
		else
			table.insert(out, string.format("%s mm", rain.value))
		end
	end

	table.insert(out,
		string.format(
			"%s, %s %s mps",
			period.windSpeed.name,
			period.windDirection.name,
			period.windSpeed.mps
		)
	)

	return table.concat(out, ", ")
end

local formatShortPeriod = function(period)
	local wday = os.date('*t', period.from)['wday']
	return string.format(
		"\002%s\002: %s, %s°C (feels like %s°C)",
		days[wday],
		period.symbol.name,
		period.temperature.value,
		period.temperature.feels
	)
end

local handleData = function(type, line)
	local out = {}
	local data = line:match(string.format("<%s (.-) />", type))
	if not data then return end

	string.gsub(data, '(%w+)="([^"]+)"', function(a, b)
		out[a] = b
	end)

	return out
end

local handleObservationOutput = function(self, source, destination, data)
	local location = data:match("<location>(.-)</location>")
	local name = location:match("<name>([^<]+)</name>"):lower():gsub("^%l", string.upper)

	local tabular = data:match("<observations>(.*)</observations>")
	for stno, sttype, name, distance, lat, lon, source, data in tabular:gmatch([[<weatherstation stno="([^"]+)" sttype="([^"]+)" name="([^"]+)" distance="([^"]+)" lat="([^"]+)" lon="([^"]+)" source="([^"]+)">(.-)</weatherstation]]) do
		local windDirection = handleData('windDirection', data)
		local windSpeed = handleData("windSpeed", data)
		if windSpeed then windSpeed = windSpeed.name else windSpeed = '' end
		local temperature = handleData('temperature', data)
        local time = temperature.time:match('T([0-9][0-9]:[0-9][0-9])')
		if windDirection then windDirection = windDirection.name else windDirection = '' end
        local color
        if tonumber(temperature.value) > 0 then
            color = ivar2.util.red
        else
            color = ivar2.util.blue
        end
		-- Use the first result
		return '%s°C, %s %s (%s, %s)', color(temperature.value), windDirection, windSpeed, name, time
	end
end

local handleOutput = function(source, destination, seven, data, city, try)
	local location = data:match("<location>(.-)</location>")
	if(not location and not try) then
		ivar2.util.simplehttp(
			("http://yr.no/place/%s/%s/%s~%s/varsel.xml"):format(
				ivar2.util.urlEncode(city.countryName),
				ivar2.util.urlEncode(city.adminName1),
				ivar2.util.urlEncode(city.toponymName),
				city.geonameId
			),
			function(data)
				handleOutput(source, destination, seven, data, city, true)
			end
		)
	end

	local name = location:match("<name>([^<]+)</name>"):lower():gsub("^%l", string.upper)
	local country = location:match("<country>([^<]+)</country>")<commit_msg>yr: use first observation with temp<commit_after>-- vim: set noexpandtab:
local sql = require'lsqlite3'
local iconv = require'iconv'

local utf2iso = iconv.new('iso-8859-15', 'utf-8')
local iso2utf = iconv.new('utf-8', 'iso-8859-15')

local days = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }

local parseDate = function(datestr)
	local year, month, day, hour, min, sec = datestr:match("([^-]+)%-([^-]+)%-([^T]+)T([^:]+):([^:]+):(%d%d)")
	return os.time{
		year = year,
		month = month,
		day = day,
		hour = hour,
		min = min,
		sec = sec,
	}
end

local feelsLike = function(celsius, wind)
	local V = wind * 3.6
	return math.floor(13.12 + 0.6215 * celsius - 11.37 * V^0.16 + 0.3965 * celsius * V^0.16 + .5)
end

local formatPeriod = function(period)
	local out = {}

	table.insert(out, string.format("%s, %s°C (feels like %s°C)", period.symbol.name, period.temperature.value, period.temperature.feels, period.temperature.feels))

	local rain = period.precipitation
	if(rain.value ~= "0") then
		if(rain.minvalue and rain.maxvalue) then
			table.insert(out, string.format("%s-%s mm", rain.minvalue, rain.maxvalue))
		else
			table.insert(out, string.format("%s mm", rain.value))
		end
	end

	table.insert(out,
		string.format(
			"%s, %s %s mps",
			period.windSpeed.name,
			period.windDirection.name,
			period.windSpeed.mps
		)
	)

	return table.concat(out, ", ")
end

local formatShortPeriod = function(period)
	local wday = os.date('*t', period.from)['wday']
	return string.format(
		"\002%s\002: %s, %s°C (feels like %s°C)",
		days[wday],
		period.symbol.name,
		period.temperature.value,
		period.temperature.feels
	)
end

local handleData = function(type, line)
	local out = {}
	local data = line:match(string.format("<%s (.-) />", type))
	if not data then return end

	string.gsub(data, '(%w+)="([^"]+)"', function(a, b)
		out[a] = b
	end)

	return out
end

local handleObservationOutput = function(self, source, destination, data)
	local location = data:match("<location>(.-)</location>")
	local name = location:match("<name>([^<]+)</name>"):lower():gsub("^%l", string.upper)

	local tabular = data:match("<observations>(.*)</observations>")
	for stno, sttype, name, distance, lat, lon, source, data in tabular:gmatch([[<weatherstation stno="([^"]+)" sttype="([^"]+)" name="([^"]+)" distance="([^"]+)" lat="([^"]+)" lon="([^"]+)" source="([^"]+)">(.-)</weatherstation]]) do
		local windDirection = handleData('windDirection', data)
		local windSpeed = handleData("windSpeed", data)
		if windSpeed then windSpeed = windSpeed.name else windSpeed = '' end
		local temperature = handleData('temperature', data)
		-- Continue to next observation if no temperature
		if temperature then
			local time = temperature.time:match('T([0-9][0-9]:[0-9][0-9])')
			if windDirection then windDirection = windDirection.name else windDirection = '' end
			local color
			if tonumber(temperature.value) > 0 then
				color = ivar2.util.red
			else
				color = ivar2.util.blue
			end
			-- Use the first result
			return '%s°C, %s %s (%s, %s)', color(temperature.value), windDirection, windSpeed, name, time
		end
	end
end

local handleOutput = function(source, destination, seven, data, city, try)
	local location = data:match("<location>(.-)</location>")
	if(not location and not try) then
		ivar2.util.simplehttp(
			("http://yr.no/place/%s/%s/%s~%s/varsel.xml"):format(
				ivar2.util.urlEncode(city.countryName),
				ivar2.util.urlEncode(city.adminName1),
				ivar2.util.urlEncode(city.toponymName),
				city.geonameId
			),
			function(data)
				handleOutput(source, destination, seven, data, city, true)
			end
		)
	end

	local name = location:match("<name>([^<]+)</name>"):lower():gsub("^%l", string.upper)
	local country = location:match("<country>([^<]+)</country>")
{'fraction_non_alphanumeric': 0.19553595295375567, 'fraction_numerical': 0.017909649826249666, 'mean_word_length': 4.322190611664296, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1301377', 'n_tokens_mistral': 2920, 'n_tokens_neox': 2684, 'n_words': 617}