1

上班从换一张桌面壁纸开始——开源小工具Bing每日壁纸

 3 years ago
source link: https://www.daqianduan.com/17453.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

发布一个自用的开源小软件, Bing每日壁纸 ,使用c# winform开发。该小软件可以自动获取Bing的精美图片设置为壁纸,并且支持随机切换历史壁纸,查看壁纸故事。

功能特性

  • 自动获取Bing最新图片并设置为壁纸
  • 壁纸故事, 你还可以查看壁纸后面的故事
  • 历史壁纸,支持查看最近两年的壁纸
  • 随机切换,随机获取几年的壁纸,穿梭时光之中
  • 定时切换,开启后每一小时自动切换壁纸

支持中英文,提供桌面widget:

vQvEfu.png

查看壁纸描述:

QjYNbe.png

原理

获取最新壁纸

http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US 可以获取最新的壁纸。

using (var client = new HttpClient())
            {
                using (var jsonStream = await client.GetStreamAsync("http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US"))
                {
                    var ser = new DataContractJsonSerializer(typeof(Result));
                    var res = (Result)ser.ReadObject(jsonStream);

                    return new HistoryImage
                    {
                        Id = Guid.NewGuid().ToString(),
                        Title = res.images[0].Copyright,
                        Description = res.images[0].Copyright,
                        ImageUrl = "http://www.bing.com" + res.images[0].URL,
                        Date = DateTime.Now.ToString("yyyy-MM-dd"),
                        AddDateTime = DateTime.Now.ToString(),
                        Locate = res.images[0].Copyright.GetBetween(",", "(")
                    };
                }
            }

获取历史壁纸

Bing未提供历史壁纸获取接口, https://bing.ioliu.cn/ 提供了历史数据,我们可以爬取:

public static List<HistoryImage> LoadLatestDaysImages()
        {
            var result = new List<HistoryImage>();
            try
            {
                var html = HttpHelper.SendGet("https://bing.ioliu.cn/");
                extractImages(result, html);
            }
            catch
            {

            }
            return result;
        }

        static string SelectTextNode(HtmlDocument htmlDocument,string xpath)
        {
            var node = htmlDocument.DocumentNode.SelectSingleNode(xpath);
            if(node !=null)
            {
                return node.InnerText.Trim();
            }
            return string.Empty;
        }

        private static void extractImages(List<HistoryImage> result, string indexPageHtml)
        {
            var doc = new HtmlDocument();
            doc.LoadHtml(indexPageHtml);
            var items = doc.DocumentNode.SelectNodes("//div[@class='item']");
            //HistoryImageProvider
            items.ToList().ForEach(node =>
            {
                var date = node.SelectSingleNode(".//em[1]").InnerText;
                if (!HistoryImageProvider.IsExist(date))
                {
                    try
                    {
                        var url = "https://bing.ioliu.cn" + node.SelectSingleNode(".//a[@class='mark']").Attributes["href"].Value;
                        result.Add(fetchSpecDayWallpaper(date, url));
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            });
        }

        private static HistoryImage fetchSpecDayWallpaper(string date, string url)
        {
            var detailHtml = HttpHelper.SendGet(url);
            var detailDoc = new HtmlDocument();
            detailDoc.LoadHtml(detailHtml);
            var result =  new HistoryImage
            {
                Id = Guid.NewGuid().ToString(),
                ImageUrl = detailDoc.DocumentNode.SelectSingleNode("//a[@class='ctrl download']").Attributes["href"].Value,
                Title = SelectTextNode(detailDoc, "//p[@class='title']"),
                Description = SelectTextNode(detailDoc, "//p[@class='sub']"),
                Date = date,
                AddDateTime = DateTime.Now.ToLongDateString(),
                updateTime = DateTime.Now.ToLongDateString(),
                Url = url,
                Locate = SelectTextNode(detailDoc, "//p[@class='location']")
            };

            if(result.Locate.Length == 0)
            {
                result.Locate = result.Title.GetBetween(",", "(");
            }

            return result;
        }

桌面widget

创建一个窗体,设置透明色

7jEvQr.png

之前显示的白色文字存在毛边,可以如下方式解决, 将背景颜色和TransparencyKey 设为和文字颜色不一样即可。

// 灰色背景,解决白色背景字体毛边问题
 this.TransparencyKey = Color.Gray;
 this.BackColor = Color.Gray;

主form启动时,显示widget:

if (_settings.ShowWidget)
            {
                // open Desk Widget
                ShowDeskWidget();
            }

        private void ShowDeskWidget()
        {
            if (deskWidget == null)
            {
                deskWidget = new DeskWidget(this);
            }

            deskWidget.Show();
        }

下载地址

https://github.com/jadepeng/bing-wallpaper

备注

该项目fork自 kompiuter/bing-wallpaper ,增加了定时切换、获取历史壁纸等功能。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK