简体   繁体   中英

How can i download the images from the site by date and hours until the date is changed?

It's archive. The starting date and time is: 24/6/2016 and 13:10 noon. I want to download in loop until the date 24/6/2016 change to 25/6/2016

The images are in a 10 minute frequency.

The format isi n the link below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;

namespace Search_Text_In_Files
{
    class DownloadRadarImages
    {
        private void DownloadImages()
        {
            string imageslinks = "http://www.meteoswiss.admin.ch/product/output/radar-processing/VRAG05.CCSK_20160624_1310.png";

            using (WebClient client = new WebClient())
            {
                client.DownloadFileAsync(new Uri(imageslinks), @"c:\temp\image35.png");
            }
        }
    }
}

Maybe something like that in the idea the code is not good but something like this maybe

private void DownloadImages()
        {
            int countTime = 0;

            while (true)
            {
                string imageslinks = "http://www.meteoswiss.admin.ch/product/output/radar-processing/VRAG05.CCSK_20160624_" + countTime + ".png";
                using (WebClient client = new WebClient())
                {
                    client.DownloadFileAsync(new Uri(imageslinks), @"c:\temp\" + countTime + ".png");
                }
            }
        }

I would use Microsoft's Reactive Framework for this. Just NuGet "Rx-Main" before you try this code:

string imageslinks = "http://www.meteoswiss.admin.ch/product/output/radar-processing/VRAG05.CCSK_20160624_1310.png";
Func<long, string> createFileName = n => String.Format(@"C:\temp\image-{0}-{1}.png", DateTime.Now.ToString("yyyyMMddHHmmss"), n);

var query =
    Observable
        .Timer(TimeSpan.FromDays(0.0), TimeSpan.FromMinutes(10.0))
        .TakeUntil(DateTimeOffset.Now.Date.AddDays(1.0))
        .SelectMany(n =>
            Observable
                .Using(
                    () => new WebClient(),
                    wc => Observable.FromAsync(() => wc.DownloadFileTaskAsync(new Uri(imageslinks), createFileName(n)))));

var subscription =
    query
        .Subscribe(u => Console.Write("."));

I've tested this and it works just fine.

If you need to stop the subscription early just call subscription.Dispose() .

Please refer to this link which explain how to download a file to local.

You would also need to get the list of image url you need to download. For that you can use a helper method like below: (Warning: Not tested)

public List<string> GetImages(string baseAddress, DateTime startDate, DateTime endDate) {

        var imageList = new List<string>();
        while (startDate < endDate) {
            var dateString = startDate.ToString("yyyymmdd");
            var timeString = startDate.ToString("HH:mm:ss", System.Globalization.DateTimeFormatInfo.InvariantInfo);

            imageList.Add($"{baseAddress}_{dateString}_{timeString}.png"); //C# 6.0 feature.
            startDate = startDate.AddMinutes(10);

        }

        return imageList;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM