mongodb/generate/hamlet.py

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