2016 九月 存档:

  • EarthLiveSharp中cloudinary的CDN图片缓存自动清理

    最近总是对地球宇宙着迷。

    恰巧发现有个叫“EarthLiveSharp”,可用将日本向日葵8号卫星的地球实时图片设为屏保。向日葵8号卫星的地球实时图片官网为:http://himawari8.nict.go.jp/,EarthLiveSharp的项目地址是:https://github.com/bitdust/EarthLiveSharp。

    为了减轻向日葵8号的服务器负担,同时也是提高地球实时图片的获取成功率,需要使用cloudinary来做CDN。注册配置都在软件里有说明。

    目前EarthLiveSharp暂时没有清理cloudinary的CDN图片缓存的功能,于是我用python写了一个,并尝试用gist管理,地址为:https://gist.github.com/creke/c5a8a18fa41b8f5c1a0719a7e0cf4de6

    同时,为了大家方便,顺便编译成了Windows可执行文件,下载: https://pan.baidu.com/s/1c27fXEo 提取码:k33n

    为了自己检索方便,顺便在这里附上python脚本源码,可以作为python如何使用RESTful接口的例子,相关工具类函数也便于参考。

    EarthLiveCleanCloudinary.py

    # -*- coding: utf-8 -*-
    # Author: Creke
    # HomePage: http://blog.creke.net

    import sys
    import urllib, urllib2
    import base64
    import json

    URLLIB_DEBUG_LEVEL = 1
    URLLIB_TIMEOUT = 5

    def Dict2Uri(dic):
    return urllib.urlencode(dic)

    def GenUrllibReq(url, method, api_key, api_secret, post_data=None):
    urlreq = None
    if post_data is None:
    urlreq = urllib2.Request(url)
    else:
    urlreq = urllib2.Request(url, post_data)
    urlreq.get_method = lambda: method
    auth_str = base64.b64encode('%s:%s' % (api_key, api_secret))
    urlreq.add_header("Authorization", "Basic %s" % auth_str)
    urlreq.add_header('Cache-Control', 'no-cache')
    return urlreq

    def GetApiDelUrl(cloud_name, img_type):
    url = "https://api.cloudinary.com/v1_1/%s/resources/image/%s" % (cloud_name, img_type)
    params = {"prefix": "http://himawari8-dl"}
    url = url + "?" + Dict2Uri(params)
    return url

    def main(argv):
    arg_idx = 1
    api_key = argv[arg_idx]
    arg_idx += 1
    api_secret = argv[arg_idx]
    arg_idx += 1
    cloud_name = argv[arg_idx]

    while True:
    del_url = GetApiDelUrl(cloud_name, 'fetch')
    urlreq = GenUrllibReq(del_url, 'DELETE', api_key, api_secret)

    print "==========================="
    print "Requesting %s" % (del_url)
    opener = urllib2.build_opener(urllib2.HTTPSHandler(debuglevel=URLLIB_DEBUG_LEVEL))
    urllib_open = opener.open(urlreq, timeout=URLLIB_TIMEOUT)
    response = urllib_open.read()
    print "==========================="
    print "Response:"
    print "%s" % (response)
    print "==========================="
    urllib_open.close()
    print "Done Requesting"

    res_json = json.loads(response)
    deleted_cnt = len(res_json['deleted'])
    print "Deleted %u himawari8 pics" % (deleted_cnt)
    print "==========================="
    if 'next_cursor' in res_json and deleted_cnt>0:
    print "Due to Cloudinary limits, we're starting a new round"
    else:
    break

    return 0

    def PrintHelp(argv):
    print "\t USAGE: %s [api_key] [api_secret] [cloud_name]" % (argv[0])

    if __name__ == '__main__':
    if len(sys.argv) < 4:
    PrintHelp(sys.argv)
    exit(1)
    print "RUNNING main"
    main(sys.argv)
    print "DONE main"

    EarthLiveCleanCloudinary2exe.py

    # -*- coding: utf-8 -*-
    # Author: Creke
    # HomePage: http://blog.creke.net

    from distutils.core import setup
    import py2exe

    setup(console=['EarthLiveCleanCloudinary.py'])

     

    2016.09.12 / 暂无评论 / 975 次点击 / 分类: 所谓技术

  • WordPress取消英文标点符号自动替换中文标点符号的优雅方法

    众所周知,WordPress中文版有个特性,会将每一处英文引号("")都稀里哗啦转化为中文引号(“”)。不仅仅是英文引号,在WordPress翻译文件定义了不少自动转换符号,具体可以参考这里

    对文字博客来说,这是个好功能,这样会避免用错英文标点。

    然而,WordPress是独立的php程序,中文社区中懂得使用者,大部分都是技术博客。而技术博客会经常贴出代码,里面包含各种英文标点是再正常不过的事情。这时候,英文标点符号自动转换就是一个恼人的画蛇添足功能了。

    网上很多文章都是让该WordPress核心文件。只不过,WordPress是一个高度模块化的系统,肯定有更优雅的解决方案。

    一番搜寻之后,发现最简单的办法,就是安装一个叫Quotmarks Replacer的插件,地址在这里

    插件很简单,就是将WordPress自动转换标点符号的函数用系统接口去掉。核心代码如下:

    <?php
    /*
    Plugin Name: Quotmarks Replacer
    Plugin URI: http://sparanoid.com/work/quotmarks-replacer/
    Description: Quotmarks Replacer disables wptexturize function that keeps all quotation marks and suspension points in half-width form.
    Version: 2.6.17
    Author: Tunghsiao Liu
    Author URI: http://sparanoid.com/
    Author Email: t@sparanoid.com
    License: GPLv2 or later

    Copyright 2016 Tunghsiao Liu, aka. Sparanoid (t@sparanoid.com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 2, as
    published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    */

    $qmr_work_tags = array(
    'the_title',             // http://codex.wordpress.org/Function_Reference/the_title
    'the_content',           // http://codex.wordpress.org/Function_Reference/the_content
    'the_excerpt',           // http://codex.wordpress.org/Function_Reference/the_excerpt
    // 'list_cats',          Deprecated. http://codex.wordpress.org/Function_Reference/list_cats
    'single_post_title',     // http://codex.wordpress.org/Function_Reference/single_post_title
    'comment_author',        // http://codex.wordpress.org/Function_Reference/comment_author
    'comment_text',          // http://codex.wordpress.org/Function_Reference/comment_text
    // 'link_name',          Deprecated.
    // 'link_notes',         Deprecated.
    'link_description',      // Deprecated, but still widely used.
    'bloginfo',              // http://codex.wordpress.org/Function_Reference/bloginfo
    'wp_title',              // http://codex.wordpress.org/Function_Reference/wp_title
    'term_description',      // http://codex.wordpress.org/Function_Reference/term_description
    'category_description',  // http://codex.wordpress.org/Function_Reference/category_description
    'widget_title',          // Used by all widgets in themes
    'widget_text'            // Used by all widgets in themes
    );

    foreach ( $qmr_work_tags as $qmr_work_tag ) {
    remove_filter ($qmr_work_tag, 'wptexturize');
    }

     

    2016.09.01 / 2 条评论 / 1,065 次点击 / 分类: 所谓技术