-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreerice.rb
117 lines (99 loc) · 3.37 KB
/
freerice.rb
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
require 'watir-webdriver'
b = Watir::Browser.new
username = ENV[USERNAME]
password = ENV[PASSWORD]
SLEEP_TIME = 1
points = 0
memoizer = {}
temp = {}
# read saved dictionary into memoizer
file = File.open('dictionary.json', "r")
file.readlines.each do |line|
words = line.split('---')
memoizer[words[0].chomp] = words[1].chomp
end
file.close
# open dictionary for appending
file = File.open("dictionary.txt", "a")
b.goto "http://freerice.com/user/login"
b.text_field(id: 'edit-name_watermark').when_present.fire_event('focus')
b.text_field(id: 'edit-name').when_present.set(username)
b.text_field(id: 'edit-pass').when_present.set(password)
b.button(id: 'edit-submit').when_present.click
b.goto "http://freerice.com"
sleep SLEEP_TIME*6
while true do
if b.div(id: 'game-status').div(class: 'block-top').exists?
if !b.div(id: 'incorrect').exists?
begin
# Last answer was correct, store temp in memoizer
# (but only if it's not already in there)
puts "memoizing " + temp["matchWord"] + " with " + temp["answer"] + "\n\n"
memoizer[temp["matchWord"].chomp.downcase] = temp["answer"].chomp.downcase
memoizer[temp["answer"].chomp.downcase] = temp["matchWord"].chomp.downcase
file.puts temp["matchWord"].chomp.downcase + "---" + temp["answer"].chomp.downcase
file.puts temp["answer"].chomp.downcase + "---" + temp["matchWord"].chomp.downcase
if b.p(id: 'game-status-right').text.split(' ')[4] != 'donation'
begin
points = b.p(id: 'game-status-right').text.split(' ')[4]
rescue
puts "couldn't find game-status-right, continuing"
end
end
rescue
puts "couldn't find game-status-right, continuing"
end
else
temp = {}
begin
incorrect = b.div(id: 'incorrect')
incorrect = incorrect.text.split(' = ')
match = incorrect[0].split(' ')[1]
ans = incorrect[1]
puts "memoizing " + match.chomp + " with " + ans.chomp
memoizer[match.chomp.downcase] = ans.chomp.downcase
memoizer[ans.chomp.downcase] = match.chomp.downcase
file.puts match.chomp.downcase + "---" + ans.chomp.downcase
file.puts ans.chomp.downcase + "---" + match.chomp.downcase
rescue
puts "failed to memoize incorrect answer"
end
end
end
if b.div(class: 'social-skip-button').exists?
begin
b.div(class: 'social-skip-button').fire_event :click
sleep SLEEP_TIME
rescue
puts "couldn't find social-skip-button, continuing"
end
else
begin
answers = b.links(class: 'answer-item')
matchPhrase = b.link(class: 'question-link')
matchWord = matchPhrase.text.split(' ').first
# puts 'matchWord: ' + matchWord
# if we have the word memoized, click the answer we know is right
if memoizer[matchWord]
puts "we found " + matchWord + " matches " + memoizer[matchWord]
answers.each do |answer|
if answer.text.chomp.downcase.eql? memoizer[matchWord].chomp.downcase
puts "Going to click #{answer.text} \n\n"
answer.click
sleep SLEEP_TIME
break
end
end
answers[0].click
else
temp = {}
temp["answer"] = answers[0].text.chomp
temp["matchWord"] = matchWord.chomp
answers[0].click
sleep SLEEP_TIME
end
rescue
puts "an error occured while selecting an answer, continuing"
end
end
end