HEMiDEMi文章產生器 -- Ver. 0.2

阅读更多
此版本尚未支援 影片代碼 功能

原始碼如下:
require 'rubygems'
require 'mechanize'

class HEMiDEMi
def initialize(username="", password="")
agent = WWW::Mechanize.new{}
f = agent.get("http://www.hemidemi.com/member/signin_form").forms[1]
f["member[username]"], f["member[password]"] = username, password
f.submit
@agent = agent
@base_url = "http://www.hemidemi.com"
@new_path = "/user_bookmark/new"
end

def run(file)
File.open(file) do |f|
f.readlines.each do |frl|
next if frl[0].chr == "#"
title, url, quotes, description, t_s, g_s = frl.split(/\t/)
self.add({
"title" => title,
"url" => url,
"quotes" => quotes,
"description" => description,
"tag_string" => t_s,
"group_string" => g_s
})
end
end
end

def add(qs)
f = @agent.get(@base_url+@new_path).forms[1]
qs.each do |k, v|
puts "Add: #{k} as #{v}"
f["user_bookmark[#{k}]"] = v
end
f.fields.each do |x| puts x.name + " => " + x.value end
f.submit
end

end
puts "請輸入HEMiDEMi使用者帳號跟密碼(用空格分開):"
ud = gets.chomp.split(/ /)
hemidemi = HEMiDEMi.new(ud[0], ud[1])
puts "請輸入檔案位置,不輸入則預設值為當前目錄下的bms.txt:"
path = gets.chomp
path == "" ? hemidemi.run("bms.txt") : hemidemi.run(path)

另外,bms.txt檔案內容是:
# 欄位說明:
# title\turl\tquotes\tdescription\ttag_string\tgroup_string
# 標題

你可能感兴趣的:(F#,rubygems)