-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathme2day.py
More file actions
115 lines (99 loc) · 3.67 KB
/
me2day.py
File metadata and controls
115 lines (99 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/python
import urllib, sys, os
from datetime import datetime
sys.path.append( os.path.abspath('lib') )
import lib.simplejson as json
#ME2DAY_DATETIME_FORMAT="%Y-%m-%dT%X+0900"
ME2DAY_DATETIME_FORMAT="%Y-%m-%dT%X"
def time_iso8601(time):
return time.strftime("%Y-%m-%dT%H:%M:%S")
class OpenStruct:
def __init__(self, params=None):
if params:
self.parse(params)
def parse(self, params, recursive=True):
d = OpenStruct.parse_dictionary(params, recursive)
for key, value in d.iteritems():
setattr(self, key, value)
@staticmethod
def parse(params, recursive=True):
result = OpenStruct.parse_dictionary(params, recursive)
def pack_open_struct(param):
o = OpenStruct()
for key, value in param.iteritems():
setattr(o, key, value)
return o
# list of objects
if isinstance(result, list):
return [pack_open_struct(d) for d in result]
# single object
else:
return pack_open_struct(result)
@staticmethod
def parse_dictionary(params, recursive=True):
# list of objects
if isinstance(params, list):
return [OpenStruct.parse_dictionary(d) for d in params]
# single object
elif isinstance(params, dict):
def nestable(value):
return isinstance(value, dict) and recursive
d = {}
for key, value in params.iteritems():
# nest if value is dictionary
if nestable(value):
value = OpenStruct.parse(value, recursive)
# check if value is list with dictionary inside
elif isinstance(value, list):
value = [
OpenStruct.parse(v, recursive) \
if nestable(v) else v \
for v in value[:] \
]
# type conversion
if key == "pubDate":
value = datetime.strptime(value[:-5], ME2DAY_DATETIME_FORMAT)
d[key] = value
return d
else:
return params
class Json:
@staticmethod
def parse(data):
data = data.replace('null', 'None') \
.replace('false', 'False') \
.replace('true', 'True')
e_data = eval(data)
if "pubDate" in e_data:
e_data["pubDate"] = datetime.strptime(e_data["pubDate"], ME2DAY_DATETIME_FORMAT)
return e_data
class Me2day:
@staticmethod
def posts(username, **params):
url = 'http://me2day.net/api/get_posts/%s' % username
isdatetime = lambda x: isinstance(x, datetime)
if 'to' in params and isdatetime(params['to']):
params['to'] = time_iso8601(params['to'])
if 'since' in params:
value = params.pop('since')
params['from'] = time_iso8601(value) if isdatetime(value) else value
# me2day api changed (2009.03.13)
if 'tag' in params:
params['scope'] = "tag[%s]" % params.pop('tag')
posts = Me2day.fetch_resource(url, params)
return posts
@staticmethod
def fetch_resource(url, params={}):
url += '.json'
if params:
query = urllib.urlencode(params)
url += "?" + urllib.unquote(query)
# fetch from me2day
data = urllib.urlopen(url).read()
#data = Json.parse(data)
data = json.loads(data)
return OpenStruct.parse(data)
@staticmethod
def convert(s):
""" '\\ud55c\\uae00' => u'\ud55c\uae00' """
return eval("u'%s'" % s)