mongodb/generate/hamlet.py

raw link view readme
1 #+ copyright (c) 2015-2016 David Betz
2
3 import datetime
4 import sys, os
5 from random import randint
6 from time import time
7 import threading
8
9 from pymongo import MongoClient
10
11 from random import randrange
12
13 with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'hamletdistinct.t'), 'r') as f:
14 hamlet_all = f.read()
15
16 genData = hamlet_all.split(' ')
17
18 def hamlet(count):
19 return genData[randint(1, len(genData) - 1)] + ('' if count == 1 else ' ' + hamlet(count - 1))
20
21 book = MongoClient('mongodb://{}:{}@127.0.0.1:27017'.format('hamletuser', 'taco3'))['librarygen']['book']
22
23 def to_iso(ts):
24 return datetime.datetime.fromtimestamp(ts).replace(microsecond=0).isoformat() + 'Z'
25
26 def run():
27 then = time()
28 count = 0
29 while True:
30 try:
31 item = {
32 "title": hamlet(4),
33 "authors": [hamlet(_) for _ in range(1, randrange(2,4))],
34 "editor": hamlet(1) if randrange(4) == 0 else None,
35 "abstract": hamlet(randrange(100, 400)),
36 "metadata": {
37 "pages": randrange(1,400),
38 "isbn": '9780' + str(randrange(100000000, 999999999)),
39 "genre": hamlet(1),
40 },
41 "created": to_iso(datetime.datetime.utcnow().timestamp()),
42 "modified": to_iso(datetime.datetime.utcnow().timestamp()),
43 }
44 book.insert_one(item)
45 count = count + 1
46 except KeyboardInterrupt:
47 now = time()
48 print('Stopped ({})'.format(count / (now - then)))
49 sys.exit(0)
50
51
52 if __name__ == '__main__':
53 run()
54