-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkmail.py
138 lines (118 loc) · 4.78 KB
/
markmail.py
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python
# -*- coding: utf8 -*-
# LinkedMarkMail, an RDFizer for Mark Mail
#
# Copyright (C) 2011 Sergio Fernández
#
# This file is part of SWAML <http://swaml.berlios.de/>
#
# LinkedMarkMail is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# LinkedMarkMail is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LinkedMarkMail. If not, see <http://www.gnu.org/licenses/>.
"""
A simple python client for MarkMail hacky API
Futher details at: http://pastebin.com/M5NnyEZ8
"""
import urllib
import simplejson as json
from io import StringIO
from datetime import datetime, timedelta
from pytz import timezone
import re
#import warnings
class MarkMail:
def __init__(self, base="http://markmail.org"):
self.base = base
self.p_yesterday = re.compile('^yesterday.*', re.IGNORECASE)
def search(self, query, page=1, mode="json"):
uri = "%s/results.xqy?q=%s&page=%d&mode=%s" % (self.base, query, page, mode)
response = self.__request(uri).read()
response = response.decode('utf8').replace('\t', '')
obj = json.load(StringIO(response))
#warnings.warn("This method is still fully unimplemented")
return obj #FIXME
def get_message(self, key, mode="json"):
uri = "%s/message.xqy?id=%s&mode=%s" % (self.base, key, mode)
response = self.__request(uri).read()
obj = json.load(StringIO(response))
message = obj["message"]
if (message["subject"]==None or message["subject"]==None):
return None
else:
return message
def get_thread(self, key, mode="json"):
uri = "%s/thread.xqy?id=%s&mode=%s" % (self.base, key, mode)
response = self.__request(uri).read()
obj = json.load(StringIO(response))
thread = obj["thread"]
if (thread["subject"]==None or thread["list"]==None):
return None
return thread
def parse_date(self, date):
if (date is None):
return None
date = date.rstrip()
# yesterday
regex = re.compile("^yesterday\s+(\d+):(\d+)\s.m",re.IGNORECASE)
r = regex.search(date)
if (r):
hour = int(r.group(1))
minute = int(r.group(2))
d = datetime.utcnow()
d = d - timedelta(days = 1)
d = d.replace(hour = hour, minute = minute)
d = d.replace(tzinfo=timezone('UTC'))
d = d.astimezone(tz=timezone('America/Los_Angeles'))
d = datetime.strptime(d.strftime('%Y-%m-%d %H:%M:%S'), '%Y-%m-%d %H:%M:%S')
return d
# today
regex = re.compile("^today\s*(\d*):(\d*)\s.m",re.IGNORECASE)
r = regex.search(date)
if (r):
hour = int(r.group(1))
minute = int(r.group(2))
d = datetime.utcnow()
d = d.replace(hour = hour, minute = minute)
d = d.replace(tzinfo=timezone('UTC'))
d = d.astimezone(tz=timezone('America/Los_Angeles'))
d = datetime.strptime(d.strftime('%Y-%m-%d %H:%M:%S'), '%Y-%m-%d %H:%M:%S')
return d
# n days ago
regex = re.compile("^(\d*)\sdays\sago",re.IGNORECASE)
r = regex.search(date)
if (r):
days = r.group(1)
d = datetime.utcnow()
d = d - timedelta(days = int(days))
d = d.replace(tzinfo=timezone('UTC'))
d = d.astimezone(tz=timezone('America/Los_Angeles'))
d = datetime.strptime(d.strftime('%Y-%m-%d %H:%M:%S'), '%Y-%m-%d %H:%M:%S')
return d
# default parse date string
d = datetime.strptime(date, '%b %d, %Y')
d = d.replace(tzinfo=timezone('UTC'))
d = d.astimezone(tz=timezone('America/Los_Angeles'))
d = datetime.strptime(d.strftime('%Y-%m-%d %H:%M:%S'), '%Y-%m-%d %H:%M:%S')
return d
def __request(self, uri, accept="application/json"):
"""
Generic HTTP request
@param uri: uri to request
@return: response
@rtype: file-like object
"""
headers = {
"User-Agent" : "swaml (http://swaml.berlios.de/; sergio@wikier.org)",
"Accept" : accept
}
request = urllib.request.Request(uri, headers=headers)
return urllib.request.urlopen(request)