Question

I've been trying to write a ruby script using mechanize to batch upload a large number of images to a MediaWiki. The script runs without any errors but I suspect there is something wrong with the way I'm handling multipart forms with mechanize. The result variable at the end of the code gives no indication of success or failure, it just shows the page with all of the values filled in, wpDestFile is DezzImage.png and so on like I specified. end.submit seems to do nothing.

Below is a the complete code for uploading a single file, a few variables need filling in for it to work.

require 'rubygems'
require 'mechanize'
require 'nokogiri'

loginName = ""
loginPassword = ""
wikiUploadPage = "http://en.wikipedia.org/wiki/Special:Upload"
wikiLoginPage = "http://en.wikipedia.org/wiki/Special:UserLogin"
pathToImage = "/home/milo/image.png"

agent = Mechanize.new {|agent| agent.user_agent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.4) Gecko/20100513 Firefox/3.6.4" }
agent.pre_connect_hooks << lambda { |params| params[:request]['Connection'] = 'keep-alive' }
agent.follow_meta_refresh = true
agent.get(wikiLoginPage) do |page|
    login_result = page.form_with(:method => /POST/) do |form|
        form.wpName = loginName
        form.wpPassword = loginPassword
    end.submit
end

uploadPage = agent.get(wikiUploadPage)

result = uploadPage.form_with(:method => /POST/) do |form|
    form.file_uploads.first.file_name = pathToImage
    form.wpDestFile = "DezzImage.png"
    form.wpUploadDescription = "DezzImage.png"
end.submit
Was it helpful?

Solution

We solved this elsewhere, but the problem seemed to be a misconfiguration in the MediaWiki install. Setting:

form.checkbox_with(:name => "wpIgnoreWarning").check

with the form submission seems to have addressed the issue.

OTHER TIPS

Looks like you're not actually setting the POST parameter that submits the page for uploading. Try something like this:

result = uploadPage.form_with(:method => /POST/) do |form|
    form.file_uploads.first.file_name = pathToImage
    form.wpDestFile = "DezzImage.png"
    form.wpUploadDescription = "DezzImage.png"
    form.wpUpload = True
end.submit

I have the same problem.

after viewed the source code, I found the solution:

result = uploadPage.form_with(:method => /POST/) do |form|
    form.file_uploads.first.file_name = pathToImage
    form.wpDestFile = "DezzImage.png"
    form.wpUploadDescription = "DezzImage.png"
    form.checkbox_with(:name => "wpIgnoreWarning").check
end.click_button
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top