-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopulator.py
62 lines (55 loc) · 2.06 KB
/
populator.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
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from database_setup import Base, Products, Reviews, Categories, User
engine = create_engine('sqlite:///1_electronics_catalog.db')
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
f = open("amazon.txt", 'r')
content = f.read().split("\n")
uid = 1
rid = 1
for line in content:
details = line.split(":::")
ch = ['#', '=', '-', '+']
if any(ext not in details[0] for ext in ch):
reviewers = details[5].split(">>>")
reviews = details[6].split("<<<")
fir = details[0].find("\"")
if fir != "-1":
name = details[0][0:fir]
try:
cat = session.query(Categories).filter_by(
name=details[2].strip()).one()
P = Products(name=name.strip(), cat_id=cat.id,
category=details[2].strip(),
desc=details[3].strip(), user_id=uid,
img=details[4], url=details[7])
session.add(P)
session.commit()
except:
new_cat = Categories(name=details[2].strip())
session.add(new_cat)
session.commit()
cat = session.query(Categories).filter_by(
name=details[2].strip()).one()
P = Products(name=name.strip(), cat_id=cat.id,
category=details[2].strip(),
desc=details[3].strip(), user_id=uid,
img=details[4], url=details[7])
session.add(P)
session.commit()
pass
for ind in range(3):
U = User(name=reviewers[ind], id=uid, email=reviewers[ind].replace(
" ", "") + "@amz.com")
U.hash_password("1234")
session.add(U)
session.commit()
R = Reviews(
user_id=rid, product_id=details[1], review=reviews[ind])
session.add(R)
session.commit()
print uid
uid = uid + 1
print "added reviews!"