获取Bing每日壁纸用作首屏大图

Bing 搜索每天都会更换一张精美的图片作为壁纸,除了特殊时候不太好看外(比如春节那几天),没多大问题。移动端还有上每日故事,与图片现配。现在我的博客首屏图片就是Bing每日壁纸,有没有感觉B格满满^_^。本文将介绍如何把Bing每日壁纸和故事扒到自己博客。

获取API接口

首先我们进入Bing首页,会发现自动转到中国版。不过这没关系,中国版更符合国情,速度也比国际版快一些。

通过抓包,可以发现http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1这里可以获取到无水印的图片。

那每日故事呢?通过模拟UA,访问移动版。同样发现了API接口http://cn.bing.com/cnhp/coverstory/。不过这里的图片是有水印的。

接口已经得到了,接下来就是写代码了。因为我既想使用无水印的图片,也想获取每日故事,所以我把两个接口结合起来使用。

代码

<?php/**  * 获取Bing每日壁纸和故事  * @author giuem  */function bingImgFetch(){    $ch = curl_init();    curl_setopt($ch, CURLOPT_URL, 'http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1');    curl_setopt($ch, CURLOPT_HTTPHEADER, array(        'User-Agent: Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36'    ));    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    $re = curl_exec($ch);    curl_close($ch);    $re = json_decode($re,1);//电脑版返回内容    $re2 = json_decode(file_get_contents('http://cn.bing.com/cnhp/coverstory/'),1);//移动版返回内容    return array(    	/* 更改图片尺寸,减小体积 */        'url' => str_replace('1920x1080','1366x768',$re['images'][0]['url']),        /* 结束日期 */        'date' => date('j',strtotime($re['images'][0]['enddate'])),        /* 故事标题 */        'title' => $re2['title'],        /* 内容 */        'd' => $re2['para1']    );}?>

使用示例

如果是wordpress,可以把上面的代码丢进function.php,然后该怎么用就怎么用。

$bingimg= bingImgFetch();echo $bingimg['url'];//输出图片地址echo $bingimg['title'];echo $bingimg['d'];

我的使用方法

因为我用的是静态博客,不能在后端完成这些操作,所以只能通过js获取。同时把代码放在国内访问较快的SAE中,使用Memcache缓存内容。而且我在获取后还把数据储存在localStorage中,这样可以减少请求次数。

为什么要缓存?因为不缓存的话,别人每访问一次你都要获取一次,IP可能会被被Bing ban了。

当然,你也可以缓存到数据库中,这些你都可以结合实际情况修改代码。

PHP in SAE

<?phpheader('Access-Control-Allow-Origin: *');$mmc=memcache_init();if($mmc==false){    /* 如果memcache启动失败,直接获取 */    $bingimg = json_encode(bingImgFetch());}else {    $bingimg = memcache_get($mmc,'bing_url');    $date = memcache_get($mmc,'bing_url_date');    /* 判断日期进行更新 */    if(date('j') != $date || !$bingimg || !$date){        $bingimg = json_encode(bingImgFetch());        /* 写入 */        memcache_set($mmc,"bing_url",$bingimg);        memcache_set($mmc,"bing_url_date",date('j'));    }}echo $bingimg;function bingImgFetch(){    $ch = curl_init();    curl_setopt($ch, CURLOPT_URL, 'http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1');    curl_setopt($ch, CURLOPT_HTTPHEADER, array(        'User-Agent: Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36'    ));    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    $re = curl_exec($ch);    curl_close($ch);    $re = json_decode($re,1);    $re2 = json_decode(file_get_contents('http://cn.bing.com/cnhp/coverstory/'),1);    return array(        'url' => str_replace('1920x1080','1366x768',$re['images'][0]['url']),        'date' => date('j',strtotime($re['images'][0]['enddate'])),        'title' => $re2['title'],        'd' => $re2['para1']    );}

HTML

<section id="hero">  <div class="hero-warp">    <p id="hero-title"></p>    <p id="hero-d"></p>  </div></section>

CSS

html,body {    height: 100%;}#hero {    position: relative;    height: 100%;    background-color: rgba(0,0,0,0.6);    background-position: center center;    background-repeat: no-repeat;    background-size: cover;    background-attachment: fixed;    width: 100%;    text-align: center;    color: #fff;    display: table;}.hero-warp {    display: table-cell;    vertical-align: middle;    text-align: center;}#hero-title {    font-size: 28px;    margin-bottom: 8px;}#hero-d {    width: 80%;    margin: 0 auto;}

JS

如果你使用jQuery,可以把这里的ajax换成$.ajax()实现。

(function() {    //bing pic load    var $data = {        title: localStorage.getItem('bing_title') ? localStorage.getItem('bing_title') : '',        d: localStorage.getItem('bing_d') ? localStorage.getItem('bing_d') : '',        url: localStorage.getItem('bing_url') ? localStorage.getItem('bing_url') : '',        date: localStorage.getItem('bing_date') ? localStorage.getItem('bing_date') : ''    };    if (new Date().getDate() != $data.date || $data.title == '' || $data.d == '' || $data.url == '') {        ajax('GET', '//giuem.sinaapp.com/bing/', function($data) {            localStorage.setItem('bing_title', $data.title);            localStorage.setItem('bing_d', $data.d);            localStorage.setItem('bing_url', $data.url);            localStorage.setItem('bing_date', new Date().getDate());            setHero($data);        });    } else {        setHero($data);    }})();function setHero($data) {    var $hero = document.getElementById('hero');    if (!$hero) {return ;}    if ($data) {        $hero.style.cssText += 'background-image:url(' + $data.url + ')';        document.getElementById('hero-title').innerHTML = $data.title;        document.getElementById('hero-d').innerHTML = $data.d;    }}function ajax(type, url, opts, callback) {    var xhr = new XMLHttpRequest();    if (typeof opts === 'function') {        callback = opts;        opts = null;    }    xhr.open(type, url);    var fd = new FormData();    if (type === 'POST' && opts) {        for (var key in opts) {            fd.append(key, JSON.stringify(opts[key]));        }    }    xhr.onload = function() {        callback(JSON.parse(xhr.response));    };    xhr.send(opts ? fd : null);}

获取Bing每日壁纸用作首屏大图的更多相关文章

  1. 如何获取 bing 每日壁纸(超高清版)

    目录 需求描述 实现方式 简单粗暴 如何下载 如何更高清 排坑指南 初级 优点 给有好奇心的孩子 进阶 接口 自动保存 网站集成 爬虫 需求描述 必应作为一个在壁纸圈做搜索引擎最优秀的站点,其每日壁纸 ...

  2. 【开源小软件 】Bing每日壁纸 让桌面壁纸保持更新

    发布一个开源小软件,Bing每日壁纸. 该小软件可以自动获取Bing的精美图片设置为壁纸,并且支持随机切换历史壁纸,查看壁纸故事. 欢迎大家下载使用,点star!有问题请留言或者提issue. 开源地 ...

  3. 【开源小软件 】Bing每日壁纸 V1.2.1

    Bing每日壁纸发布V1.2版本,下载地址Release V1.2.1 该小软件可以自动获取Bing的精美图片设置为壁纸,并且支持随机切换历史壁纸,查看壁纸故事. 本次新增国际化支持,以及桌面widg ...

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

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

  5. DzzOffice添加动态壁纸例子-Bing每日壁纸

    Bing每日壁纸介绍:bing网站每天会更新一张不同的精选图片. 此压缩包内的程序,可以自动同步更新cn.bing.com网站每天更新的图片,作为dzzoffice的壁纸使用.实现自动每天更换不同的云 ...

  6. 获取Bing每日图片API接口

    bing图片每日更新,对于这一点感觉挺不错的,如果能够把bing每日图片作为博客背景是不是很不错呢?首先我们进入Bing首页,会发现自动转到中国版.不过这没关系,中国版更符合国情,速度也比国际版快一些 ...

  7. Bing每日壁纸API

    懒人直接出图 https://www.shadow-forum.com/api/bing/bing.php API API地址: https://bing.biturl.top 调用方式: HTTP ...

  8. 获取bing每日图片

    http://global.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1&mkt=en-US 其中idx表示倒数第几张图片 ...

  9. Shell脚本 curl获取必应每日壁纸(Mac OS)

    Mac上Safari不能下载壁纸,遇到好看的很想用作壁纸.写了一小段脚本用来拉取网页图片. curl: -sS 参数用来取消下载状态显示 grep 首先把含有图片网址的行提取了出来,针对这一行再做se ...

随机推荐

  1. Linux 调优方案, 修改最大连接数(ulimit命令)

    https://blog.csdn.net/isoleo/article/details/51732687 https://www.cnblogs.com/zengkefu/p/5649407.htm ...

  2. [Cqoi2014]危桥 (两遍网络流)

    题目链接 #include <bits/stdc++.h> using namespace std; typedef long long ll; inline int read() { , ...

  3. 无法访问http,会强制跳到https

    1. Close Safari. 2. Delete the ~/Library/Cookies/HSTS.plist file. 3. Reopen Safari.

  4. libaudit_plugin.so安装

    #上传audit到mysql的plugin目录vim /etc/my.cnfplugin-load=AUDIT=libaudit_plugin.soaudit_json_file=1audit_jso ...

  5. Angular2.0的学习(五)

    第五节课: 1.组件的输入输出属性 2.使用中间人模式传递数据 3.组件生命周期以及Angular的变化发现机制

  6. Django-- CRM1客户建表与装饰器

    一.CRM项目(1) 引入三个表:用户表,客户表,校区表,班级表,梳理逻辑关系并迁移数据库,生成表. 使用admin插入数据,admin是Django提供的web形式的后台数据管理页面,它是和用户认证 ...

  7. 基于Jquery的文本提示控件 poshytip

    Html中,如设置了title的属性,则当鼠标在该对象上面短暂的停留时,会显示预设的文本提示,但,这些效果只会短暂的显示,一会就会消失,又要重新把鼠标移出再移回来才被显示,样式也无法重写,实在是恼人之 ...

  8. ADODB.Stream在进行文件上传时报错

    最近在做web项目,有个控件是上传材料文件和文件夹,本地运行正常,放到服务器上,一直报错:AutoRuntime服务器无法创建..... 解决方法: 1.配置ie浏览器的安全级别 2.修改ie浏览器对 ...

  9. hibernate课程 初探单表映射1-5 hibernate第一个demo

    hibernate 开发步骤:(hibernate4.2+mysql6.0) 1 hibernate配置文件(hibernate.cfg.xml) 2 持久化类 3 对象-关系映射文件 4 hiber ...

  10. python之其他模块的用法

    1.时间模块   在Python中通常有三种表示时间的方式,分别是时间戳.元组.格式化的时间字符串. 时间模块的常用方法 time.sleep() #指定延迟时间 time.time() #当前时间的 ...