batch-filecurl

Curl multiple url using batch and save the result in txt file


I want to get data from several websites using curl with a batch file, then save the curl results into a text file.

the syntax that I know is this:

curl "https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=-19.587492&lon=125.952764" >> output.txt
curl "https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=-19.587493&lon=125.952765" >> output.txt
curl "https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=-19.587494&lon=125.952766" >> output.txt
curl "https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=-19.587495&lon=125.952767" >> output.txt
curl "https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=-19.587496&lon=125.952768" >> output.txt

I changed the curl above into looping and it works fine. I have set LF so that the next result can move to the next line, but it didn't work. Here my script :

@echo off
set setlocal EnableDelayedExpansion

set url1="https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=-19.587492&lon=125.952764"
set url2="https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=-19.587493&lon=125.952765"
set url3="https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=-19.587494&lon=125.952766"
set url4="https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=-19.587495&lon=125.952767"
set url5="https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=-19.587496&lon=125.952768"

(set LF=^
%=EMPTY=%
)

echo %url1%
for %%x in (set) do (
 curl %url1%%LF%
 curl %url2%%LF%
 curl %url3%%LF%
 curl %url4%%LF%
 curl %url5%%LF%
)> output.txt

Solution

  • I find it really strange you want to have an ordinary text-file as output, while the urls you're processing are actually JSON objects. To properly save multiple JSON objects as valid JSON you have to put them in a JSON array.

    I'd say you're much better off with a tool like the command-line JSON-parser to work this out.

    You could manually enter the 5 urls you mentioned...

    C:\>xidel -se "array{json-doc('https://nominatim.openstreetmap.org/reverse?format=jsonv2^&lat=-19.587492^&lon=125.952764'),json-doc('https://nominatim.openstreetmap.org/reverse?format=jsonv2^&lat=-19.587493^&lon=125.952765'),json-doc('https://nominatim.openstreetmap.org/reverse?format=jsonv2^&lat=-19.587494^&lon=125.952766'),json-doc('https://nominatim.openstreetmap.org/reverse?format=jsonv2^&lat=-19.587495^&lon=125.952767'),json-doc('https://nominatim.openstreetmap.org/reverse?format=jsonv2^&lat=-19.587496^&lon=125.952768')}"
    C:\>xidel -se ^"^
      array{^
        json-doc('https://nominatim.openstreetmap.org/reverse?format=jsonv2^&lat=-19.587492^&lon=125.952764'),^
        json-doc('https://nominatim.openstreetmap.org/reverse?format=jsonv2^&lat=-19.587493^&lon=125.952765'),^
        json-doc('https://nominatim.openstreetmap.org/reverse?format=jsonv2^&lat=-19.587494^&lon=125.952766'),^
        json-doc('https://nominatim.openstreetmap.org/reverse?format=jsonv2^&lat=-19.587495^&lon=125.952767'),^
        json-doc('https://nominatim.openstreetmap.org/reverse?format=jsonv2^&lat=-19.587496^&lon=125.952768')^
      }^
    "
    

    ...but you can also build them dynamically:

    C:\>xidel -se "(0 to 4) ! {'format':'jsonv2','lat':-19.587492 - (. * 0.000001),'lon':125.952764 + (. * 0.000001)}" --output-json-indent=compact
    {"format": "jsonv2", "lat": -19.587492, "lon": 125.952764}
    {"format": "jsonv2", "lat": -19.587493, "lon": 125.952765}
    {"format": "jsonv2", "lat": -19.587494, "lon": 125.952766}
    {"format": "jsonv2", "lat": -19.587495, "lon": 125.952767}
    {"format": "jsonv2", "lat": -19.587496, "lon": 125.952768}
    
    C:\>xidel -se "(0 to 4) ! request-combine('https://nominatim.openstreetmap.org/reverse',{'format':'jsonv2','lat':-19.587492 - (. * 0.000001),'lon':125.952764 + (. * 0.000001)})/url"
    https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=-19.587492&lon=125.952764
    https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=-19.587493&lon=125.952765
    https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=-19.587494&lon=125.952766
    https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=-19.587495&lon=125.952767
    https://nominatim.openstreetmap.org/reverse?format=jsonv2&lat=-19.587496&lon=125.952768
    
    C:\>xidel -se "array{(0 to 4) ! request-combine('https://nominatim.openstreetmap.org/reverse',{'format':'jsonv2','lat':-19.587492 - (. * 0.000001),'lon':125.952764 + (. * 0.000001)})/json-doc(url)}"
    C:\>xidel -se ^"^
      array{^
        (0 to 4) ! request-combine(^
          'https://nominatim.openstreetmap.org/reverse',^
          {^
            'format':'jsonv2',^
            'lat':-19.587492 - (. * 0.000001),^
            'lon':125.952764 + (. * 0.000001)^
          }^
        )/json-doc(url)^
      }
    "
    [
      {
        "place_id": 28423384,
        "licence": "Data © OpenStreetMap contributors, ODbL 1.0. http://osm.org/copyright",
        "osm_type": "relation",
        "osm_id": 11690317,
        "lat": "-21.7077169",
        "lon": "122.2264619",
        "category": "boundary",
        "type": "administrative",
        "place_rank": 18,
        "importance": 0.28179444113349733,
        "addresstype": "locality",
        "name": "Telfer",
        "display_name": "Telfer, Telfer Village, Shire of East Pilbara, Western Australia, 6762, Australia",
        "address": {
          "locality": "Telfer",
          "residential": "Telfer Village",
          "municipality": "Shire of East Pilbara",
          "state": "Western Australia",
          "ISO3166-2-lvl4": "AU-WA",
          "postcode": "6762",
          "country": "Australia",
          "country_code": "au"
        },
        "boundingbox": ["-23.5097510", "-19.5000000", "120.0641280", "126.0012960"]
      },
      [...]
    ]