Beispiel #1
0
        public async Task AddPictureAsync(BingPicture picture)
        {
            var dto = new
            {
                Info = picture.Copyright,
                Url  = _options.Value.CdnBase.Combine(picture.FileName).AbsoluteUri
            };

            var client = _httpClientFactory.CreateClient();

            var resp = await client.PostAsJsonAsync(_options.Value.ApiBase.Combine("/Pictures"), dto);

            resp.EnsureSuccessStatusCode();
        }
        public async Task StorageBingPictureAsync(BingPicture pictureInfo)
        {
            var path = Path.Combine(
                _options.Value.StoragePath,
                pictureInfo.FileName
                );

            var httpClient = _httpClientFactory.CreateClient();

            using var stream = await httpClient.GetStreamAsync($"https://cn.bing.com/{pictureInfo.Url}");

            using var fileStream = new FileStream(path, FileMode.OpenOrCreate);

            await stream.CopyToAsync(fileStream);
        }
Beispiel #3
0
        public async void GetBackgroundUri()
        {
            if (isBackgroundTypeXml == true)
            {
                try {
                    var httpClient = new HttpClient();
                    Uri uri        = new Uri("https://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1");
                    HttpResponseMessage httpResponse = await httpClient.GetAsync(uri);

                    httpResponse.EnsureSuccessStatusCode();
                    string responseBody = await httpResponse.Content.ReadAsStringAsync();

                    var xml = new XmlDocument();
                    xml.LoadXml(responseBody);
                    XmlNode node  = xml.GetElementsByTagName("url")[0];
                    var     image = new BitmapImage(new Uri("https://cn.bing.com" + node.InnerText));
                    background.ImageSource = image;
                } catch (Exception e) {
                    Debug.WriteLine($"{e.ToString()}");
                }
                return;
            }
            try {
                var httpClient = new HttpClient();
                Uri uri        = new Uri("https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1");
                HttpResponseMessage httpResponse = await httpClient.GetAsync(uri);

                httpResponse.EnsureSuccessStatusCode();
                string responseBody = await httpResponse.Content.ReadAsStringAsync();

                BingPicture bingPicture = JsonConvert.DeserializeObject <BingPicture>(responseBody);
                var         image       = new BitmapImage(new Uri("https://cn.bing.com" + bingPicture.images[0].url));
                background.ImageSource = image;
            } catch (Exception e) {
                Debug.WriteLine($"{e.ToString()}");
            }
        }