Skip to content

Instantly share code, notes, and snippets.

@hamukichi
Created December 27, 2013 12:03
Show Gist options
  • Save hamukichi/8146166 to your computer and use it in GitHub Desktop.
Save hamukichi/8146166 to your computer and use it in GitHub Desktop.
im.kayac.com を利用するためのPythonモジュール。http://blog.livedoor.jp/hamu_nbr/archives/32644041.html に掲載。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Send notification using im.kayac.com
"""
__author__ = "Hamukichi (Nombiri) <Twitter: @hamukichi_nbr>"
__version__ = "1.00"
__date__ = "September 30, 2013"
__licence__ = "MIT License"
# Import modules
import hashlib # Python 2.5 or higher
import requests # Please install
# The base URI
IMURI = "http://im.kayac.com/api/post/"
class ImKayac:
def __init__(self, username, mode = 0, key = None):
"""
Initialization.
@param username: Your username.
@type username: str
@param mode: Specify how to authorize.
0: No authorization.
1: Use password.
2: Use secret key cryptosystem.
@type mode: int
@param key: Specify your password or secret key.
@type key: str
"""
self.posturi = IMURI + username
self.mode = mode
self.key = key
def send(self, message, handler = None):
"""
Send notification.
@param message: The message which should be sent (utf-8).
@type message: unicode
@param handler: Specify URL scheme.
(for iPhone, optional.)
@type handler: str
"""
# Prepare parameter(s)
self.postparams = {"message": message}
if handler is not None:
self.postparams["handler"] = handler
if self.mode == 1:
self.postparams["password"] = self.key
if self.mode == 2:
s = hashlib.sha1(message.encode("utf-8") + self.key)
self.postparams["sig"] = s.hexdigest()
# Access im.kayac.com
r = requests.post(self.posturi, data = self.postparams)
# Parse
self.result = r.json()
if self.result[u"result"] <> u"posted":
raise ImKayacError(self.result["error"])
class ImKayacError(Exception):
def __init__(self, errstr):
self.errstr = errstr
def __str__(self):
return u"im.kayac.com says: %s" % self.errstr
def main():
#Your username
USERNAME = "username"
#Your password for *API auth*
#Note: It is not necessarily the password of your *account*
PASSWORD = "password"
MESSAGE = u"テスト"
HANDLER = "mailto:"
imk = ImKayac(USERNAME, 1, PASSWORD)
imk.send(MESSAGE, HANDLER)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment