| 1 | #! /usr/bin/env python |
|---|
| 2 | # coding:utf-8 |
|---|
| 3 | |
|---|
| 4 | """ |
|---|
| 5 | imkayac.py |
|---|
| 6 | This script send message to im.kayac.com . |
|---|
| 7 | ver 0.1 |
|---|
| 8 | |
|---|
| 9 | Created by atusi on 2008-02-17. |
|---|
| 10 | Copyright (c) 2008 __atusi nakamura__. All rights reserved. |
|---|
| 11 | |
|---|
| 12 | ## how to use ## |
|---|
| 13 | from imkayac import * |
|---|
| 14 | a = KayaTalk("yourID", "yourPass") |
|---|
| 15 | a.send("your message") |
|---|
| 16 | ################ |
|---|
| 17 | |
|---|
| 18 | if you use SHA1 authentication, see following. |
|---|
| 19 | ################ |
|---|
| 20 | from imkayac import * |
|---|
| 21 | a = KayaTalk("yourID", "yourPass", True) |
|---|
| 22 | a.send("your message") |
|---|
| 23 | ################ |
|---|
| 24 | |
|---|
| 25 | """ |
|---|
| 26 | import urllib |
|---|
| 27 | import sha |
|---|
| 28 | import base64 |
|---|
| 29 | |
|---|
| 30 | class KayaTalk(object): |
|---|
| 31 | """docstring for IMer""" |
|---|
| 32 | def __init__(self, usrname, password=None, shaAuth=False): |
|---|
| 33 | #super(IMer, self).__init__() |
|---|
| 34 | self.usrname = usrname |
|---|
| 35 | self.password = password |
|---|
| 36 | self.message = {} |
|---|
| 37 | self.base_url = "http://im.kayac.com/api/post/" |
|---|
| 38 | self.shaAuth = shaAuth |
|---|
| 39 | |
|---|
| 40 | def send(self, message): |
|---|
| 41 | """docstring for sendData""" |
|---|
| 42 | if (self.password != None): |
|---|
| 43 | if self.shaAuth: |
|---|
| 44 | self.message["sig"] = sha.new(message + self.password).hexdigest() |
|---|
| 45 | else: |
|---|
| 46 | self.message["password"] = self.password |
|---|
| 47 | self.message["message"] = message |
|---|
| 48 | self.params = urllib.urlencode( self.message ) |
|---|
| 49 | d = urllib.urlopen(self.base_url+self.usrname, self.params) |
|---|
| 50 | #x = d.readline() |
|---|
| 51 | #print x |
|---|
| 52 | d.close |
|---|
| 53 | |
|---|
| 54 | if __name__ == '__main__': |
|---|
| 55 | import sys |
|---|
| 56 | if len(sys.argv) < 3: |
|---|
| 57 | print >> sys.stderr, "Usage: username [password] message" |
|---|
| 58 | sys.exit(1) |
|---|
| 59 | |
|---|
| 60 | else: |
|---|
| 61 | if (len(sys.argv)==4): |
|---|
| 62 | if sys.argv[2][:5] == "sha1:": |
|---|
| 63 | a = KayaTalk(sys.argv[1], sys.argv[2][5:], True) |
|---|
| 64 | else: |
|---|
| 65 | a = KayaTalk(sys.argv[1], sys.argv[2]) |
|---|
| 66 | a.send("%s"%(sys.argv[3])) |
|---|
| 67 | else: |
|---|
| 68 | a = KayaTalk(sys.argv[1]) |
|---|
| 69 | a.send("%s"%(sys.argv[2])) |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|