Location>code7788 >text

Bing Daily Wallpaper API Package

Popularity:860 ℃/2024-09-06 23:17:40

synopsis

This class encapsulates the Bing home page of the daily wallpaper view function, provides a way to view, save the wallpaper, the maximum support to view the wallpaper of the last 8 days

Usage

async Task Main()
async
async
var bing = (8); //initialize.
var bing = (8); //initialize, parameter 8 means 8 images will be loaded
var task = await (); //load the wallpaper.
(); //load wallpaper
("date\t" + ); //load wallpapers
("Title\t" + ); //load related information
("Copyright\t" + ); //Load relevant information.
("Link\t" + ); //Load relevant information.
if (await (@"D:\")) //save the current wallpaper
{
("Saved successfully");
};
var task1 = await (); //load the next wallpaper
(); //print the wallpaper
var task2 = await (); //load the previous wallpaper
(); //print the wallpaper
}
catch (Exception ex)
{
(); }
}
}

Effect Preview


API encapsulation

Click to view code
using System;
using ;
using ;
using ;
using ;
using ;
using ;

   /// <summary>
   /// (of cargo etc) loadBingwallpapers,singleton example,utilizationCreateInstancemethod creates an instance of the class
   /// </summary>
   public sealed class BingWallpaperAPI
   {
       private static BingWallpaperAPI _instance = null;
       private static readonly object _padlock = new Object();
       /// <summary>
       /// Creating instances of classes
       /// </summary>
       /// <param name="imgnums">wallpapers的sheet of paper数,can be1-8intermediate numbers</param>
       /// <returns></returns>
       public static BingWallpaperAPI CreateInstance(byte imgnums = 3)
       {
           if (_instance is null)
           {
               lock (_padlock)
               {
                   if (_instance is null) _instance = new BingWallpaperAPI(imgnums);
               }
           }
           return _instance;
       }
       private BingWallpaperAPI(byte imgnums)
       {
           if (imgnums > 8 || imgnums <= 0)
           {
               _imgnums = 3;
               ($"support only1-8Pictures,You entered the{imgnums}is reset to3");
           }
           else { _imgnums = imgnums; }
           GetWallpaperLink();
       }
       /// <summary>built-in index</summary>
       private int _index = -1;
       /// <summary>
       /// (of cargo etc) loadBingwallpapers的quantities,default3
       /// </summary>
       private static byte _imgnums;
       private BingApiModel _bingApiModel;
       /// <summary>
       /// (of cargo etc) loadwallpapers的JSONformat file,文件中包含了wallpapers的下载地址等关键信息
       /// </summary>
       /// <exception cref="Exception"></exception>
       private void GetWallpaperLink()
       {
           HttpClient client = new HttpClient();
           for (int i = 0; i < 3; i++) //This step will try to3substandard
           {
               try
               {
                   var res = ($"/?format=js&idx=0&n={_imgnums}&mkt=zh-CN").Result;
                   //parametersn:1-8 Number of requests returned,目前最多一substandard获取8sheet of paper
                   //parametersformat:js/xml
                   //parametersidx:Number of days to request images 0 at the present;1 As of yesterday,reason by analogy(Currently get up to7Pictures from days ago)
                   //parametersmkt:as suffix city name, means prefecture or county (area administered by a prefecture level city or county level city)
                   var json = ().Result;
                   BingApiModel bingApiModel = new BingApiModel();
                   _bingApiModel = <BingApiModel>(json);
                   ImageCount = _bingApiModel.();
                   if (ImageCount == _imgnums)
                   {
                       break;  //(of cargo etc) load成功时直接终止循环
                   }
               }
               catch (Exception ex)
               {
                   if (i == 2) { throw ex; } else ();
               }
           }
       }
       /// <summary>
       /// according toJSON数据下载wallpapers
       /// </summary>
       /// <param name="index">_bingApiModelindexing</param>
       /// <returns></returns>
       /// <exception cref="Exception"></exception>
       private async Task<BingWallpaper> GetWallpaper(int index)
       {
           try
           {
               using (HttpClient client = new HttpClient())
               {
                   var element = _bingApiModel.images[index];
                   var res = await ("" + );
                   //this oneURLThe first of the1920x1080adapt (a story to another medium)UHDbe4k
                   var imgStream = await ();
                   return (new BingWallpaper()
                   {
                       Wallpaper = (imgStream),
                       Title = ,
                       Copyright = ,
                       CopyrightLink = ,
                       EndDate = ,
                   });
               }
           }
           catch (Exception ex)
           {
               throw ex;
           }
       }
       /// <summary>
       /// (of cargo etc) loadJSONData contained in theURLquantities
       /// </summary>
       public int ImageCount { get;private set; }
       /// <summary>
       /// 下一sheet of paperwallpapers
       /// </summary>
       /// <returns></returns>        
       /// <exception cref="Exception"></exception>
       public async Task<BingWallpaper> Next()
       {
           if (ImageCount == 0)
           {
               return null;
           }
           _index++;
           if (_index >= ImageCount)   //重置indexing
           {
               _index = 0;
           }
           return await GetWallpaper(_index);
       }
       /// <summary>
       /// 上一sheet of paperwallpapers
       /// </summary>
       /// <returns></returns>
       /// <exception cref="Exception"></exception>
       public async Task<BingWallpaper> Previous()
       {
           if (ImageCount == 0)
           {
               return null;
           }
           _index--;
           if (_index < 0)
           {
               _index = ImageCount - 1;    //重置indexing
           }
           return await GetWallpaper(_index);
       }

       /// <summary>
       /// 保存当前的wallpapers到本地
       /// </summary>
       /// <param name="path">Path to save、Includes file name(as ifc:\),formatJPEG</param>
       /// <returns>保存be否成功</returns>
       /// <exception cref="Exception"></exception>
       public async Task<bool> SaveCurrentWallpaper(string path)
       {
           try
           {
               var task = await Current();
               (path, );
               return true;
           }
           catch (Exception ex)
           {
               throw ex;
           }
       }
       /// <summary>
       /// 当前wallpapers
       /// </summary>
       /// <returns></returns>
       /// <exception cref="Exception"></exception>
       public async Task<BingWallpaper> Current()
       {
           if (ImageCount == 0)
           {
               return null;
           }
           if (_index == -1)
           {
               _index = 0;
           }
           return await GetWallpaper(_index);
       }
   }
   /// <summary>
   /// 用来保存wallpapers信息的类模型
   /// </summary>
   public class BingWallpaper
   {
       /// <summary>wallpapers</summary>
       public  Wallpaper { get; set; }
       /// <summary>caption</summary>
       public string Title { get; set; }
       /// <summary>copyright</summary>
       public string Copyright { get; set; }
       /// <summary>copyright链接</summary>
       public string CopyrightLink { get; set; }
       /// <summary>
       /// wallpapers所属日期
       /// </summary>
       public string EndDate { get; set; }
   }
   /// <summary>
   /// be used to parseJSONClass Models of Data
   /// </summary>
   public class BingApiModel
   {
       /// <summary>
       /// Returns a collection of images
       /// </summary>
       public Image[] images { get; set; }
   }
   public class Image
   {
       /// <summary>
       /// 每日wallpapers的日期
       /// </summary>
       public string enddate { get; set; }
       /// <summary>
       /// wallpapers的地址
       /// </summary>
       public string url { get; set; }
       /// <summary>wallpapers的简述、Shooting address(photographer)</summary>
       public string copyright { get; set; }
       /// <summary>wallpapers相关搜索链接</summary>
       public string copyrightlink { get; set; }
       /// <summary>wallpapers的caption</summary>
       public string title { get; set; }
   }

Original Statement

Author:Falling Moon
Provenance:blogosphere
Reprints: Reprints are permitted and must be credited. A link to the original article must be given in the article.


Bing Home