繁体   English   中英

如何在日期和小时之前按日期和小时从站点下载图像?

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

它是存档。 开始日期和时间是:2016年6月24日和正午13:10。 我想循环下载直到日期24/6/2016更改为25/6/2016

图像的频率为10分钟。

格式为以下链接:

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");
            }
        }
    }
}

也许这样的想法在代码中不是很好,但也许像这样

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");
                }
            }
        }

我将为此使用Microsoft的Reactive Framework。 在尝试此代码之前,只需NuGet“ Rx-Main”:

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("."));

我已经对此进行了测试,并且效果很好。

如果您需要尽早停止订阅,只需调用subscription.Dispose()

请参考此链接 ,其中解释了如何将文件下载到本地。

您还需要获取需要下载的图像URL列表。 为此,您可以使用如下所示的帮助方法:(警告:未经测试)

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;

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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