root/lang/vim/ruby/mixi.vim @ 15697

Revision 15697, 3.2 kB (checked in by ujihisa, 5 years ago)

using VIM::Buffer.current.append

Line 
1command! Mixi :call <SID>MixiStart()
2
3function! s:MixiStart()
4  set nonu
5  ruby mixi_run
6endfunction
7
8ruby << EOF
9class Mixi
10  def initialize(email, password, mixi_premium = false, image_dir = '~/.vim/mixi_images')
11    require 'kconv'
12    require 'rubygems'
13    require 'mechanize'
14
15    @image_dir = File.expand_path image_dir
16    @email, @password, @mixi_premium =
17      email, password, mixi_premium
18    @agent = WWW::Mechanize.new
19    @agent.user_agent_alias = 'Mac Safari'
20  end
21
22  def post(title, body, images)
23    page = @agent.get 'http://mixi.jp/home.pl'
24    form = page.forms[0]
25    form.email = @email
26    form.password = @password
27    @agent.submit form
28
29    page = @agent.get "http://mixi.jp/home.pl"
30    page = @agent.get(page.links.map(&:href).detect {|l| /add_diary/ =~ l })
31    form = page.forms[(@mixi_premium ? 2 : 1)]
32    form.diary_title = title
33    form.diary_body = self.class.magic_body(body)
34    get_image images
35    images[0, 3].each_with_index do |img, i|
36      if /darwin/ =~ RUBY_PLATFORM && /\.png$/i =~ img
37        imgjpg = '/tmp/mixi-vim-' << File.basename(img).sub(/\.png$/i, '.jpg')
38        system "sips -s format jpeg --out #{imgjpg} #{img} > /dev/null 2>&1"
39        img = imgjpg
40      end
41      form.file_uploads[i].file_name = img
42    end
43    page = @agent.submit form
44    page = @agent.submit page.forms[1]
45  end
46
47  def get_latest
48    page = @agent.get 'http://mixi.jp/list_diary.pl'
49    [
50      page.links[33].text.toutf8,
51      "http://mixi.jp/" << page.links[33].uri.to_s.toutf8
52    ]
53  end
54
55  def self.magic_body(body)
56    body.gsub(/^(  )+/) {|i| ' '.toeuc * (i.length/2) }
57  end
58
59  def get_image(images)
60    images.each_with_index do |img, i|
61      if img =~ %r{^http://}
62        path =
63          File.join @image_dir, i.to_s + File.extname(img)
64        unless File.exist? @image_dir
65          Dir.mkdir @image_dir
66        else
67          Dir.chdir(@image_dir) do
68            Dir.entries(@image_dir).
69              each {|f| File.unlink f if File.file? f }
70          end
71        end
72        system "wget -O #{path} #{img} > /dev/null 2>&1"
73        if File.exist? path and !File.zero? path
74          images[i] = path
75        else
76          images.delete_at i
77        end
78      end
79    end
80  end
81end
82
83def mixi_run
84  vim = VIM::Buffer.current
85  return if VIM.evaluate('confirm("really?")') == 0
86
87  endline = VIM.evaluate %[line("$")]
88  title   = VIM.evaluate %[getline(1)]
89  body = VIM.evaluate(%[join(getline(2, #{endline}), "\n")]).split "\n"
90
91  images = body.reverse.inject([]) {|r, l| r.unshift l[4..-1] if l =~ /^img:/; r }
92  body = body[0..-(images.length + 1)].join "\n"
93
94  # ~/.mixi
95  # line1: input your email
96  # line2: input your password
97  # line3: input "premium" if you are premium
98  m =
99    if File.exist?(File.expand_path('~/.mixi'))
100      mixi_config = File.read(File.expand_path('~/.mixi'))
101      email, password, premium = mixi_config.split(/\r?\n/)
102      Mixi.new email, password, (premium == 'premium')
103    else
104      m = Mixi.new 'YOUR_EMAIL', 'YOUR_PASSWORD'
105    end
106  # if you are mixi premium member:
107  # m = Mixi.new 'YOUR_EMAIL', 'YOUR_PASSWORD', true
108  m.post title.toeuc, body.toeuc, images
109  m.get_latest.each do |line|
110    vim.append(vim.count, line)
111  end
112end
113EOF
Note: See TracBrowser for help on using the browser.