-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtodb.py
executable file
·82 lines (66 loc) · 2.6 KB
/
todb.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
#!/usr/bin/env python
import random
from datetime import datetime
import sys
from slugify import slugify
import utils
from models import *
from text_generator import get_chain, generate_text
def get_unused_users(site, count):
return User.select().join(Question, JOIN.LEFT_OUTER).switch(User).join(Answer, JOIN.LEFT_OUTER) \
.where((User.site == site) & (Question.id.is_null()) & (Answer.id.is_null()))
def add_username(site, count=500):
print("usernames")
chain = get_chain(site.url, "Usernames")
for _ in range(count):
username = generate_text(chain, "Usernames")
User.create(username=username, site=site)
def add_title(site, count=100):
print("titles")
# TODO: Make sure that every slug is unique
chain = get_chain(site.url, "Titles")
for _ in range(count):
title = generate_text(chain, "Titles")
slug = slugify(title, max_length=70, word_boundary=True)
Title.create(text=title, slug=slug, site=site)
def add_answer(site, count=300):
print("answers")
users = get_unused_users(site, count)
chain = get_chain(site.url, "Answers")
for i in range(count):
text = generate_text(chain, "Answers")
user = users[i]
time = datetime.now()
Answer.create(text=text, user_id=user, site_id=site, datetime=time)
def add_question(site, count=100):
print("Questions")
users = get_unused_users(site, count)
titles = Title.select().join(Question, JOIN.LEFT_OUTER) \
.where((Title.site == site) & (Question.id.is_null())) \
.limit(count)
chain = get_chain(site.url, "Questions")
for i in range(count):
text = generate_text(chain, "Questions")
title = titles[i]
user = users[i]
time = datetime.now()
question = Question.create(text=text, title_id=title, user_id=user, site_id=site, datetime=time)
num_answers = random.randint(1, 4)
answers = Answer.select().where((Answer.site == site) & (Answer.question.is_null())).limit(num_answers)
for answer in answers:
answer.question = question
answer.save()
if __name__ == "__main__":
if len(sys.argv) > 1:
sites = sys.argv[1:]
query = Site.select().where((Site.last_download.is_null(False)) & (Site.url.in_(sites)))
else:
query = Site.select().join(Question, JOIN.LEFT_OUTER).where(
(Question.id.is_null()) & (Site.last_download.is_null(False))).limit(10)
print([i.name for i in query])
for s in query:
print(s.name)
add_username(s)
add_title(s)
add_answer(s)
add_question(s)