-
Notifications
You must be signed in to change notification settings - Fork 490
/
Copy pathmailboxes__map_json_mbox_by_date_time.py
46 lines (35 loc) · 1.27 KB
/
mailboxes__map_json_mbox_by_date_time.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
# -*- coding: utf-8 -*-
import sys
import couchdb
from couchdb.design import ViewDefinition
try:
import jsonlib2 as json
except ImportError:
import json
DB = sys.argv[1]
START_DATE = sys.argv[2] #YYYY-MM-DD
END_DATE = sys.argv[3] #YYYY-MM-DD
server = couchdb.Server('http://localhost:5984')
db = server[DB]
def dateTimeToDocMapper(doc):
# Note that you need to include imports used by your mapper
# inside the function definition
from dateutil.parser import parse
from datetime import datetime as dt
if doc.get('Date'):
# [year, month, day, hour, min, sec]
_date = list(dt.timetuple(parse(doc['Date']))[:-3])
yield (_date, doc)
# Specify an index to back the query. Note that the index won't be
# created until the first time the query is run
view = ViewDefinition('index', 'by_date_time', dateTimeToDocMapper,
language='python')
view.sync(db)
# Now query, by slicing over items sorted by date
start = [int(i) for i in START_DATE.split("-")]
end = [int(i) for i in END_DATE.split("-")]
print 'Finding docs dated from %s-%s-%s to %s-%s-%s' % tuple(start + end)
docs = []
for row in db.view('index/by_date_time', startkey=start, endkey=end):
docs.append(db.get(row.id))
print json.dumps(docs, indent=4)