上班從換一張桌面桌布開始——開源小工具Bing每日桌布
阿新 • • 發佈:2021-01-12
釋出一個自用的開源小軟體,[Bing每日桌布](https://github.com/jadepeng/bing-wallpaper),使用c# winform開發。該小軟體可以自動獲取Bing的精美圖片設定為桌布,並且支援隨機切換歷史桌布,檢視桌布故事。
## 功能特性
- 自動獲取Bing最新圖片並設定為桌布
- 桌布故事, 你還可以檢視桌布後面的故事
- 歷史桌布,支援檢視最近兩年的桌布
- 隨機切換,隨機獲取幾年的桌布,穿梭時光之中
- 定時切換,開啟後每一小時自動切換桌布
支援中英文,提供桌面widget:
![](https://img2020.cnblogs.com/blog/38465/202101/38465-20210112201505394-60823830.png)
檢視桌布描述:
![每日桌布](https://img2020.cnblogs.com/blog/38465/202101/38465-20210112201323894-850070155.png)
## 原理
### 獲取最新桌布
從`http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US`可以獲取最新的桌布。
```c#
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/`提供了歷史資料,我們可以爬取:
```c#
public static List LoadLatestDaysImages()
{
var result = new List();
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 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
建立一個窗體,設定透明色
![](https://img2020.cnblogs.com/blog/38465/202101/38465-20210112202042215-1181563230.png)
之前顯示的白色文字存在毛邊,可以如下方式解決, 將背景顏色和TransparencyKey 設為和文字顏色不一樣即可。
```c#
// 灰色背景,解決白色背景字型毛邊問題
this.TransparencyKey = Color.Gray;
this.BackColor = Color.Gray;
```
主form啟動時,顯示widget:
```c#
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](https://github.com/jadepeng/bing-wallpaper/releases)
## 備註
該專案fork自[`kompiuter/bing-wallpaper`](https://github.com/kompiuter/bing-wallpaper),增加了定時切換、獲取歷史桌布等功能。
## 相關推薦
使用IDEA的同學,這裡有彩虹屁外掛,寫程式碼時可以瘋狂誇你 https://github.com/jadepeng/idea-rain