Yahoo! search interface: Difference between revisions

implement in nim lang
m (→‎{{header|Phix}}: added libheader)
(implement in nim lang)
Line 1,566:
Button["Prev", page -= 10, Enabled -> Dynamic[page >= 10]],
Button["Next", page += 10]}]]</lang>
 
=={{header|Nim}}==
<lang nim>import httpclient, strutils, htmlparser, xmltree, strtabs
const
PageSize = 7
YahooURLPattern = "https://search.yahoo.com/search?fr=opensearch&b=$$#&pz=$#&p=".format(PageSize)
type
SearchResult = ref object
url, title, content: string
SearchInterface = ref object
client: HttpClient
urlPattern: string
page: int
results: array[PageSize+2, SearchResult]
proc newSearchInterface(question: string): SearchInterface =
new result
result.client = newHttpClient()
# result.client = newHttpClient(proxy = newProxy(
# "http://localhost:40001")) # only http_proxy supported
result.urlPattern = YahooURLPattern&question
proc search(si: SearchInterface) =
let html = parseHtml(si.client.getContent(si.urlPattern.format(
si.page*PageSize+1)))
var
i: int
attrs: XmlAttributes
for d in html.findAll("div"):
attrs = d.attrs
if attrs != nil and attrs.getOrDefault("class").startsWith("dd algo algo-sr relsrch"):
let d_inner = d.child("div")
for a in d_inner.findAll("a"):
attrs = a.attrs
if attrs != nil and attrs.getOrDefault("class") == " ac-algo fz-l ac-21th lh-24":
si.results[i] = SearchResult(url: attrs["href"], title: a.innerText,
content: d.findAll("p")[0].innerText)
i+=1
break
while i < len(si.results) and si.results[i] != nil:
si.results[i] = nil
i+=1
proc nextPage(si: SearchInterface) =
si.page+=1
si.search()
 
proc echoResult(si: SearchInterface) =
for res in si.results:
if res == nil:
break
echo(res[])
 
var searchInf = newSearchInterface("weather")
searchInf.search()
searchInf.echoResult()
echo("searching for next page...")
searchInf.nextPage()
searchInf.echoResult()
</lang>
{{out}}
<pre>(url: "https://weather.com/", title: "National and Local Weather Radar, Daily Forecast, Hurricane ...", content: "The Weather Channel and weather.com provide a national and local weather forecast for cities, as well as weather radar, report and hurricane coverage ")
(url: "https://weather.com/weather/tenday/l/California+MO?canonicalCityId=d58964aa4d5c9ba2a8e76fe9175052ef5d1ed9ee98eb5514e2b58d67722f7e0e", title: "California, MO 10-Day Weather Forecast - The Weather Channel ...", content: "Be prepared with the most accurate 10-day forecast for California, MO with highs, lows, chance of precipitation from The Weather Channel and Weather.com ")
(url: "https://www.accuweather.com/", title: "Local, National, & Global Daily Weather Forecast | AccuWeather", content: "AccuWeather has local and international weather forecasts from the most accurate weather forecasting technology featuring up to the minute weather reports ")
(url: "https://www.weather.gov/", title: "National Weather Service", content: "Surface Weather Upper Air Marine and Buoy Reports Snow Cover Satellite Space Weather International Observations. FORECAST Local Forecast International Forecasts Severe Weather Current Outlook Maps Drought Fire Weather Fronts/Precipitation Maps Current Graphical Forecast Maps Rivers Marine Offshore and High Seas Hurricanes Aviation Weather ")
(url: "https://www.wunderground.com/", title: "Local Weather Forecast, News and Conditions | Weather Underground", content: "Weather Underground provides local & long-range weather forecasts, weather reports, maps & tropical weather conditions for locations worldwide ")
(url: "https://graphical.weather.gov/sectors/missouri.php", title: "NOAA Graphical Forecast for Missouri - National Weather Service", content: "National Weather Service 1325 East West Highway Silver Spring, MD 20910 Page Author: NWS Internet Services Team: Disclaimer Information Quality Credits ... ")
(url: "https://www.weatherbug.com/weather-forecast/now/macon-mo-63552", title: "Macon, Missouri | Current Weather Forecasts, Live Radar Maps ...", content: "For more than 20 years Earth Networks has operated the world’s largest and most comprehensive weather observation, lightning detection, and climate networks. We are now leveraging our big data smarts to deliver on the promise of IoT. ")
(url: "https://www.accuweather.com/en/us/lakeview-heights-mo/65338/weather-forecast/2107359", title: "Lakeview Heights, MO Today, Tonight & Tomorrow\'s Weather ...", content: "Get the forecast for today, tonight & tomorrow\'s weather for Lakeview Heights, MO. Hi/Low, RealFeel®, precip, radar, & everything you need to be ready for the day, commute, and weekend! ")
(url: "https://www.wunderground.com/forecast/us/az/tucson", title: "Tucson, AZ 10-Day Weather Forecast | Weather Underground", content: "Tucson Weather Forecasts. Weather Underground provides local & long-range weather forecasts, weatherreports, maps & tropical weather conditions for the Tucson area. ")
searching for next page...
(url: "https://forecast.weather.gov/", title: "National Weather Service", content: "NOAA National Weather Service National Weather Service. Widespread Heat This Week; Monsoon Rain Lingers. Widespread heat concerns are expected through at least midweek as high pressure covers a large portion of the U.S., especially the Central half. ")
(url: "https://weather.com/weather/tenday/l/Port+Huron+MI?canonicalCityId=772884de37d69a70824031f9ab1202b956e665bdf14a6ffd3257184d64d33351", title: "Port Huron, MI 10-Day Weather Forecast - The Weather Channel ...", content: "Be prepared with the most accurate 10-day forecast for Port Huron, MI with highs, lows, chance of precipitation from The Weather Channel and Weather.com ")
(url: "https://www.weather.gov/sgx/", title: "San Diego, CA - National Weather Service", content: "Jul 17, 2021 · NOAA National Weather Service San Diego, CA. Seasonable temperatures will be felt tonight with low cloudiness along the coast and into the valleys with a mostly clear sky inland. ")
(url: "https://www.weatherbug.com/weather-forecast/now/houston-tx-77007", title: "Houston, Texas | Current Weather Forecasts, Live Radar Maps ...", content: "For more than 20 years Earth Networks has operated the world’s largest and most comprehensive weather observation, lightning detection, and climate networks. We are now leveraging our big data smarts to deliver on the promise of IoT. ")
(url: "https://www.msn.com/en-us/weather", title: "MSN", content: "Sunny. There will be mostly sunny skies. The high will be 103°. Feels Like. 65°. Air Quality. Moderate air ( 51 - 100) Primary pollutant PM2.5 11 μg/m³. 52. ")
(url: "https://www.noaa.gov/weather", title: "Weather | National Oceanic and Atmospheric Administration", content: "Jun 04, 2021 · Weather, water and climate events, cause an average of approximately 650 deaths and $15 billion in damage per year and are responsible for some 90 percent of all presidentially-declared disasters. About one-third of the U.S. economy – some $3 trillion – is sensitive to weather and climate. ")
(url: "https://www.nbcphiladelphia.com/weather/", title: "NBC10 Philadelphia – Philadelphia News, Local News, Weather ...", content: "Weather stories. severe weather 3 mins ago ‘Destructive Damage\' to Be Added to National Weather Service Cell Phone Alerts weather 5 hours ago Today\'s NBC10 First Alert Forecast ... ")</pre>
 
=={{header|Oz}}==