Repository navigation
Expand file tree
/
Copy pathseeds.rb
More file actions
170 lines (146 loc) · 3.75 KB
/
Copy pathseeds.rb
File metadata and controls
170 lines (146 loc) · 3.75 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
require 'open-uri'
require 'nokogiri'
#Creation Methods
def create_new_user(username, email, password)
u = User.new(
:username => username,
:email => email,
:password => password,
:password_confirmation => password
)
u.set_session_token
u.save!
u
end
def create_question(title, body, answers, comments)
q = Question.new(:title => title, :body => body)
q.user_id = [5, 15, 25, 35, 45, 55, 65, 75, 85, 95].sample
q.save!
ActiveRecord::Base.transaction do
build_answers(q, answers).each(&:save!)
q.answers.first.accept
build_comments(q, comments).each(&:save!)
build_random_upvotes(rand(80), q.id, "Question", q.user_id)
end
end
def build_answers(q, answers)
user_ids = [q.user_id]
answers.map do |answer|
num = find_non_taken_id(user_ids)
user_ids << num
a = Answer.new(:body => answer)
a.user_id = num
a.question_id = q.id
a
end
end
def build_comments(q, comments)
user_ids = [q.user_id]
comments.map do |comment|
num = find_non_taken_id(user_ids)
user_ids << num
c = Comment.new(:body => comment)
c.user_id = num
c.commentable_id = q.id
c.commentable_type = "Question"
c
end
end
def build_random_upvotes(num, id, type, taken_id)
used_ids = [taken_id]
num.times do
u_id = find_non_taken_id(used_ids)
used_ids << u_id
Vote.build_vote("up", id, type, u_id)
end
end
def find_non_taken_id(taken_ids)
num = rand(1..100)
while taken_ids.include?(num)
num = rand(1..100)
end
num
end
#Scraping Methods
def get_html_doc(url)
Nokogiri::HTML(open(url)) do |config|
config.noerror
end
end
def get_question_content(ldoc)
pphs = []
ldoc.xpath('//div[contains(@class, "question")]//td[contains(@class, "postcell")]//p').each do |pph|
break if pph.content == "Sign up using Google"
pphs << pph.content
end
pphs.join("\n\n")
end
def get_question_answers(ldoc)
path = '//div[contains(@class, "answer")]//div[contains(@class, "post-text")]'
ldoc.xpath(path).map do |answer|
answer.text
end
end
def get_question_comments(ldoc)
path = '//div[contains(@class, "question")]//span[contains(@class, "comment-copy")]'
ldoc.xpath(path).map do |comm|
comm.content
end
end
#Create a Guest User
guestuser = create_new_user(
"Guest",
"guest@example.com",
"password"
)
guestuser.add_points(10000)
#Create Users
users = []
User.transaction do
100.times do
users << create_new_user(
Faker::Name.name,
Faker::Internet.email,
SecureRandom.urlsafe_base64(16)
)
end
end
User.transaction do
users.each do |user|
num = rand(5000)
user.add_points(num)
end
end
#Get Questions Pages
docs = []
docs << get_html_doc("http://stackoverflow.com/questions?page=5&sort=votes")
docs << get_html_doc("http://stackoverflow.com/questions?page=4&sort=votes")
docs << get_html_doc("http://stackoverflow.com/questions?page=3&sort=votes")
docs << get_html_doc("http://stackoverflow.com/questions?page=2&sort=votes")
docs << get_html_doc("http://stackoverflow.com/questions?sort=votes")
question_titles = []
question_links = []
questions_answers = []
questions_comments = []
docs.each do |doc|
doc.xpath('questions', '//h3/a').each do |question|
question_titles << question.content
question_links << "http://www.stackoverflow.com#{question["href"]}"
end
end
question_content = question_links.map do |link|
begin
ldoc = get_html_doc(link)
questions_answers << get_question_answers(ldoc)
questions_comments << get_question_comments(ldoc)
get_question_content(ldoc)
rescue
end
end
(0...question_titles.length).each do |i|
title = question_titles[i]
body = question_content[i]
answers = questions_answers[i]
comments = questions_comments[i]
create_question(title, body, answers, comments)
end