LoginSignup
5
5

More than 5 years have passed since last update.

python で redmine の更新を hipchat へ流す

Last updated at Posted at 2014-02-01

IRC では redmine の更新を ikachan に投げて通知したりしてるんだけど、 hipchat でも流したいなぁと思い久々に python で script を書いた。
HipChat の API をたたく library は公式でまとまってる。
python のは PyPI にあるので、 pip install python-hipchat で install できる。

RSS で parse できるものなら何でも投げられると思う。

# -*- coding: utf-8 -*-
from datetime import datetime
from fcache.cache import FileCache

import feedparser
from hypchat import HypChat
import hashlib
import os
import pytz
import time

os.environ['TZ'] = 'UTC'

room_id  = 123456
feed_url = "http://redmine/issue/atom/url"
feed = feedparser.parse(feed_url)
cache_key = hashlib.sha224(feed_url).hexdigest()
cache = FileCache('/tmp/redmine')
hc = HypChat("YOUR API TOKEN")
tz_utc = pytz.timezone('UTC')

if not cache.get(cache_key):
    cache[cache_key] = time.mktime(datetime.now(tz_utc).timetuple())

last_updated = cache[cache_key]
for entry in feed['entries']:

    updated = time.mktime(datetime.strptime(entry.updated, '%Y-%m-%dT%H:%M:%SZ').timetuple())

    if int( last_updated ) >= int( updated ):
        print "continue: %s > %s" % (cache[cache_key], updated)
        continue

    if cache[cache_key] <= updated:
        cache[cache_key] = updated

    if entry.authors[0].name:
        name = entry.authors[0].name

    message = '<a href="%s">%s</a> (%s)' % (entry.link, entry.title, name)
    hc.get_room(room_id).notification(message, color='gray')

cache.close()

Perl における Cache::FileCache 相当のものを探すのにちょっと手間取ってしまったのと、久々に python 書いたら忘れてんなとおもったw

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5