root/lang/python/hatenaapigraph/hatenaapigraph.py @ 3106

Revision 3106, 3.2 kB (checked in by yoshiori, 6 years ago)

lang/python/hatenaapigraph : start project

Line 
1#!/opt/local/bin/python2.5
2# -*- coding: utf-8 -*-
3import base64
4import random
5import sha
6from datetime import date,datetime
7import urllib2
8
9def __http_response(self, request, response):
10    code, msg, hdrs = response.code, response.msg, response.info()
11   
12    if not (200 <= code < 300):
13        response = self.parent.error(
14            'http', request, response, code, msg, hdrs)
15
16    return response
17
18urllib2.HTTPErrorProcessor.http_response = __http_response
19
20class WSSEHeader(urllib2.BaseHandler) :
21   
22    def __init__(self,userid,passwd):
23        self.userid = userid
24        self.passwd = passwd
25
26    def get_nonce(self):
27        private = str(random.random())
28        now = datetime.now()
29        timestamp = now.strftime('%Y-%m-%dT%H:%M:%SZ')
30        return '%s %s' % (timestamp, sha.new('%s:%s' % (timestamp, private)).hexdigest())
31
32    def get_wsse(self):
33        nonce = self.get_nonce()
34        base64_encoded_nonce = base64.encodestring(nonce).replace('\n', '')
35        now = datetime.now()
36        post_creation_time = now.strftime('%Y-%m-%dT%H:%M:%SZ')
37        password_digest = base64.encodestring(sha.new(nonce + post_creation_time + self.passwd).digest()).replace('\n', '')
38        return 'UsernameToken Username="%s", PasswordDigest="%s", Created="%s", Nonce="%s"' \
39            % (self.userid, password_digest, post_creation_time, base64_encoded_nonce)
40
41    def http_request(self,req):
42        req.add_header('X-WSSE',self.get_wsse())
43        return req
44
45import urllib
46import yaml
47class HatenaGraph:
48   
49    GRAPH_API_URL = 'http://graph.hatena.ne.jp/api/'
50    GRAPH_API_DATA_URI = GRAPH_API_URL + 'data'
51    GRAPH_API_CONFIG_URI = GRAPH_API_URL + 'config'
52
53    def __init__(self,userid,passwd):
54        self.userid = userid
55        self.passwd = passwd
56
57       
58    def get_data(self,graphname,userid = None):
59        if userid is None:
60            userid = self.userid
61        params = {
62            "graphname" : graphname,
63            "type" : "yaml",
64            "username" : userid
65            }
66        opener = urllib2.build_opener( WSSEHeader(self.userid,self.passwd))
67        params = urllib.urlencode(params)
68        data = opener.open(self.GRAPH_API_DATA_URI+"?%s"%params);
69        return yaml.load(data)
70
71    def post_data(self,graphname,value,updatedate = date.today()):
72        params = {
73            "graphname" : graphname,
74            "value" : value,
75            "date" : updatedate.strftime("%Y-%m-%d")
76            }
77        params = urllib.urlencode(params)
78        opener = urllib2.build_opener( WSSEHeader(self.userid,self.passwd))
79        return opener.open(self.GRAPH_API_DATA_URI,params);
80
81    def get_config(self,graphname,userid = None):
82        params = {
83            "graphname" : graphname,
84            "type" : "yaml",
85            }
86        opener = urllib2.build_opener( WSSEHeader(self.userid,self.passwd))
87        params = urllib.urlencode(params)
88        data = opener.open(self.GRAPH_API_CONFIG_URI+"?%s"%params);
89        return yaml.load(data)
90
91    def post_data(self,graphname,params):
92        params["graphname"] = graphname
93        params = urllib.urlencode(params)
94        opener = urllib2.build_opener( WSSEHeader(self.userid,self.passwd))
95        return opener.open(self.GRAPH_API_CONFIG_URI,params);
Note: See TracBrowser for help on using the browser.