Skip to content

Commit

Permalink
Swapped to retrieving the algorithm for each sheet instead of attempt…
Browse files Browse the repository at this point in the history
…ing to get it out of the MathML
  • Loading branch information
seanrcollings committed Mar 20, 2024
1 parent 354d165 commit 93a2b95
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 26 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ gem 'simplecov'
gem "htmlentities", "~> 4.3"

gem "debug", "~> 1.9"

gem "httparty", "~> 0.21.0"
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ def self.parse(node)
return encodechars(HTMLEntities.new.decode(node.text))
end

binding.break if node.text == "numerical_solve"

case node.name.sub(/^[^:]*:/, "")
when "math"
join_parsed_children(node.elements)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def parse_question(node)
question.answers += plugin_node.search('answers/answer').map { |n| answer_parser.parse(n) }
question.casesensitive = parse_boolean(plugin_node, 'shortanswer/usecase')

question.algorithms, question.algorithms_format = get_code(node, 'shortanswerwiris')
question.algorithms, question.algorithms_format = get_code(node, 'shortanswerwiris', question.id)

question.answers.each do |answer|
if answer.answer_text.start_with?('<math')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'byebug'
require 'digest/md5'
require 'httparty'
require_relative "./mathml2asciimath"

module Moodle2AA::Moodle2
Expand All @@ -19,27 +20,15 @@ def get_wiris_node(node, type)
Nokogiri::XML(question_xml.text)
end

def get_code(node, type)
def get_code(node, type, id)
sheet = get_wiris_node(node, type)
return [[], :none] if sheet.nil?

# Wrapped in a CDATA tag so we need to parse it out
cas_session_node = Nokogiri::XML(sheet.xpath("question/wirisCasSession").text, &:noblanks)
code_algorithms = get_algorithms(cas_session_node)
sheet_algorithms = get_algorithms_from_sheet(cas_session_node)

algorithms_format = if code_algorithms.length > 0 && sheet_algorithms.length > 0
:both
elsif code_algorithms.length > 0
:code
elsif sheet_algorithms.length > 0
:sheet
else
:none
end

algorithms = code_algorithms + sheet_algorithms
[algorithms, algorithms_format]
cas_session = sheet.xpath("question/wirisCasSession").text
sheet_algorithms = get_algorithm_from_session(id, cas_session)
algorithms_format = :sheet

[[sheet_algorithms], algorithms_format]
end

def get_answers(node, type)
Expand All @@ -57,14 +46,35 @@ def get_algorithms(node)
map { |text| normalize_script_string(text) }
end

def get_algorithms_from_sheet(node)
node.
xpath("//task//group/command").
map(&:to_xml).
map { |input| convert_math_ml(input) }.
filter { |input| input != "" }

def get_algorithm_from_session(id, cas_session)
cas_session_hash = Digest::MD5.hexdigest(cas_session)
filepath = "out/cached_algorithms/#{id}_#{cas_session_hash}"

if File.exist?(filepath)
File.read(filepath)
else
puts "Fetching algorithm for #{id}"
sleep rand(0..5) # Sleep for some random amount to avoid looking like a bot
algorithms = convert_sheet_to_algorithm(id, cas_session)

File.write(filepath, algorithms)
algorithms
end
end

def convert_sheet_to_algorithm(id, cas_session)
res = HTTParty.post(
'https://calcme.com/session2algorithm?httpstatus=true',
body: URI.encode_www_form({data: cas_session})
)

if !res.success?
raise "Failed to fetch algorithms for #{id}"
end

res.body
end

def convert_math_ml(string)
normalize_script_string(
Expand Down

0 comments on commit 93a2b95

Please sign in to comment.