阿里云云函数配置百度自动推送
本文最后更新于 1599 天前,其中的信息可能已经有所发展或是发生改变。

实现浏览文章时自动推送链接到百度

如图
baidu_push.png

import requests
import re
import json
from flask import Flask, request

app = Flask(__name__)

@app.route('/baidu-push', methods=['GET'])
def baidu_push():
    data = str(request.referrer)
    #data = 'https://www.ioioi.cn/'
    if data != "":
      if re.match(r'https://.*domain.name.*', data): #防滥用域名推送限制
        url = "http://data.zz.baidu.com/urls?site=https://domain.name&token=your_token"
        headers = {
          'User-Agent': 'curl/7.12.1',
          'Host': 'data.zz.baidu.com',
          'Content-Type': 'text/plain',
          'Content-Length': '83'
        }
        res = requests.post(url, headers=headers, data=data).text
        text = json.loads(res)
        if text['success'] == 1:
          return "{'Res': 'Success'" + "," + "'Remain':" + " " + str(text['remain']) + "}"
        else:
          return "{'Res': 'Fail'}"
      else:
        return "{'Res': 'Not_Same_Site'}"
    else:
      return "{'Res': 'None'}"

if __name__ == '__main__':
    app.run()

函数配置

ROSTemplateFormatVersion: '2015-09-01'
Transform: 'Aliyun::Serverless-2018-04-03'
Resources:
  Blog:
    Type: 'Aliyun::Serverless::Service'
    Properties:
      Role: 'acs:ram::ID:role/AliyunFCLogExecutionRole'
      LogConfig:
        Project: aliyun-fc-ap-southeast-1-ID
        Logstore: function-log
      InternetAccess: true
    baidu-push:
      Type: 'Aliyun::Serverless::Function'
      Properties:
        Handler: index.app
        Runtime: python3
        Timeout: 5
        MemorySize: 128
        CodeUri: Blog\baidu-push
      Events:
        baidu-push:
          Type: HTTP
          Properties:
            AuthType: ANONYMOUS
            Methods:
              - GET

Hexo-Butterfly配置
layout-include-head.pug的适当地方插入以实现自动加载

<script type="text/javascript" defer="" src="https://api.domain.name/baidu-push"></script>

最后发现其实可以直接用百度提供的额🤣🤣🤣 👀

<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https'){
   bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
  }
  else{
  bp.src = 'http://push.zhanzhang.baidu.com/push.js';
  }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>
上一篇
下一篇