From ae0650d2bea1a68fc186ca7df731c1b946798711 Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Sun, 22 Aug 2021 09:47:10 +0900 Subject: [PATCH] file download --- spec/agent_spec.cr | 7 +++++++ src/mechanize.cr | 23 +++++++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/spec/agent_spec.cr b/spec/agent_spec.cr index e6b9dbe..a729169 100644 --- a/spec/agent_spec.cr +++ b/spec/agent_spec.cr @@ -56,4 +56,11 @@ describe "Mechanize Agent test" do agent2.history.size.should eq 1 agent2.history.pop.uri.to_s.should eq "http://example.com/form" end + + it "can download page" do + agent = Mechanize.new + agent.download("http://example.com", "mechanizecr_example.html") + File.exists?("mechanizecr_example.html").should eq true + File.delete("mechanizecr_example.html") + end end diff --git a/src/mechanize.cr b/src/mechanize.cr index 5cd6dbc..83413bb 100644 --- a/src/mechanize.cr +++ b/src/mechanize.cr @@ -17,7 +17,9 @@ class Mechanize @agent.user_agent = AGENT["Mechanize"] end - def get(uri : String | URI, headers = HTTP::Headers.new, params : Hash(String, String | Array(String)) = Hash(String, String).new) + def get(uri : String | URI, + headers = HTTP::Headers.new, + params : Hash(String, String | Array(String)) = Hash(String, String).new) method = :get page = @agent.fetch uri, method, headers, params add_to_history(page) @@ -25,7 +27,9 @@ class Mechanize page end - def post(uri : String | URI, headers = HTTP::Headers.new, query : Hash(String, String | Array(String)) = Hash(String, String).new) + def post(uri : String | URI, + headers = HTTP::Headers.new, + query : Hash(String, String | Array(String)) = Hash(String, String).new) node = Node.new node["method"] = "POST" node["enctype"] = "application/x-www-form-urlencoded" @@ -119,4 +123,19 @@ class Mechanize href = link.href get href end + + # download page body from given uri. + # TODO: except this request from history. + def download(uri, + filename, + headers = HTTP::Headers.new, + params : Hash(String, String | Array(String)) = Hash(String, String).new) + page = get(uri, headers, params) + + case page + when MechanizeCr::File + File.write(filename, page.body) + end + page + end end