でもそんなことをするのは面倒なので、pingを一括送信できるスクリプトを書きました。
#!/usr/bin/env python # -*- coding: utf-8 -*- u"""ブログの更新pingを一括で送信するスクリプト 別途インストールの必要があるライブラリ https://pypi.python.org/pypi/feedparser """ import os import time import datetime import feedparser import xmlrpclib # bloggerの投稿(Atom)のURL FEED_URL = "http://ブログのURL.blogspot.com/feeds/posts/default" # 前回パース時の最新記事の投稿時刻を記録しておくファイル(本スクリプトと同一フォルダ内) LOG_FILE = "log.txt" # 上ファイルの時刻保存フォーマット TIME_FORMAT = "%c" # データがパース出来なかった際に送信されるサイト名とURL SITE_TITLE = "ブログ名" SITE_URL = "http://ブログのURL.blogspot.com/" # PINGの送信先一覧 PING_SERVER_LIST = [ "http://api.my.yahoo.co.jp/RPC2", "http://blog.goo.ne.jp/XMLRPC", "PING送信先を追記していく", ] def parse_articles(log_file_path): u"""最新記事の投稿時刻とブログ名、URLを取得する """ flag_updated = False title, url = SITE_TITLE, SITE_URL try: last_update_time = datetime.datetime.strptime(open(log_file_path, 'r').read(), TIME_FORMAT) except Exception as e: print e last_update_time = datetime.datetime(2013,1,1) print "last update : ", last_update_time try: feed = feedparser.parse(FEED_URL) feed_time_str = feed['entries'][0]['updated'] feed_update_time = datetime.datetime.strptime(feed_time_str[:feed_time_str.rfind('.')],"%Y-%m-%dT%H:%M:%S") if feed_update_time > last_update_time: print "new articles available" flag_updated = True open(log_file_path, 'w').write(feed_update_time.strftime(TIME_FORMAT)) title = feed['feed']['title'] url = feed['feed']['link'] print "parsed : ", feed_time_str, title, url except Exception as e: print "parse error : ", e return flag_updated, title, url def send_ping(title, url): u"""PINGを各サーバに送信する """ if isinstance(title, unicode): title = title.encode('utf-8') xmlrpclib.Transport.user_agent = __file__ for ping_server in PING_SERVER_LIST: try: server = xmlrpclib.Server(ping_server) response = server.weblogUpdates.ping(title, url) print "ping sended to ", ping_server print "server response :", response['message'] except Exception as e: print "send error : ", e time.sleep(1.0) def main(): log_file_path = os.path.join(os.path.abspath(os.path.dirname(__file__)),LOG_FILE) updated, title, url = parse_articles(log_file_path) if updated: send_ping(title, url) if __name__ == "__main__": main()
なお feedparser のインストールは、feedparser.zip をダウンロードして、以下のコマンドを実行するだけ。
$ unzip feedparser.zip $ cd feedparser/ $ sudo python setup.py install
僕はこれを crontab で1時間に1回動かしています。最低でも更新から1時間以内にpingが送信される仕組み。
$ crontab -e # Bloggerの更新pingを毎時0分に送信する 0 * * * * /pythonまでのパス/python /スクリプトまでのパス/send_ping.py >> /cronのログファイルまでのパス/cron_log.txt 2>&1
参考にしたページ
ブログの更新を通知するPythonスクリプト - Python Tips
0 件のコメント:
コメントを投稿