Skip to content

Instantly share code, notes, and snippets.

@uehaj
Created October 11, 2010 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uehaj/620451 to your computer and use it in GitHub Desktop.
Save uehaj/620451 to your computer and use it in GitHub Desktop.
// g100pon #78 HTTPファイルアップロード
// (はてなfotolifeに画像をアップロード、要はてなアカウント)
@Grab('net.sourceforge.htmlunit:htmlunit:2.8')
import java.net.URL;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
if (args.size() == 0) {
println "Usage: groovy fotoupload.groovy file"
System.exit(0)
}
imageFile = args[0]
proxyHost = null // set the proxy host name if you are in Firewall
proxyPort = 18080
if (proxyHost != null) {
c = new WebClient(BrowserVersion.FIREFOX_3, proxyHost , proxyPort);
}
else {
c = new WebClient(BrowserVersion.FIREFOX_3);
}
ins = new BufferedReader(new InputStreamReader(System.in))
userid = null
password = null
if (userid == null && System.console() != null) {
userid = System.console().readLine("Hatena id: ")
password = new String(System.console().readPassword("Password: "))
}
else if (userid == null) {
print "Hatena id: "
userid = ins.readLine()
print "Password: "
password = ins.readLine()
}
url = "https://www.hatena.ne.jp/login?location=http://f.hatena.ne.jp/$userid/up?mode=classic"
loginPage = c.getPage(new URL(url));
loginPage.getByXPath("//input[@name='name']")[0].valueAttribute = userid
loginPage.getByXPath("//input[@name='password']")[0].valueAttribute = password
uploadPage = loginPage.getByXPath("//input[@type='submit']")[0].click();
uploadForm = uploadPage.getElementById('upload')
fileInput = uploadForm.getInputByName('image1')
fileInput.valueAttribute = imageFile
nextPage = uploadPage.getElementById("submit-button").click();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment