wp2hatena 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 86732bb5e4fec9ae7d13f8d33e448cc30f47cd08
4
+ data.tar.gz: 69559f845856391727e91ddc1566d770bcffa734
5
+ SHA512:
6
+ metadata.gz: 4e8abae7b393d30899b01bd4ca0de7c59ba75e519ba5d8507cd6132c776972597a988cf06feeebea2e432a76f14dade108fb7197a85119a5a8d0b8ef51c16899
7
+ data.tar.gz: 3ffbcd83a271433c689a902704bc1324cba936294c5ae5bad409a3402b5c32f446a60bff5e22eef291f1917eaffa512f267df4b82c8700c0786a823415891919
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .idea
11
+ wp2hatena.iml
12
+ /vendor/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in wp2hatena.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 utahta
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # Wp2hatena
2
+
3
+ WordPressエクスポートデータから画像を抽出して、はてなフォトライフにアップロードし、aタグをはてな記法に置換するライブラリ
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```bash
10
+ $ git clone https://github.com/utahta/wp2hatena.git
11
+ $ cd wp2hatena
12
+ $ bundle
13
+ ```
14
+
15
+ or
16
+
17
+ ```bash
18
+ $ gem install wp2hatena
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ ```bash
24
+ $ wp2hatena_oauth
25
+ ```
26
+
27
+ access to `http://localhost:4567`
28
+
29
+ ```bash
30
+ $ wp2hatena
31
+ ```
32
+
33
+ ## Development
34
+
35
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
36
+
37
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
38
+
39
+ ## Contributing
40
+
41
+ Bug reports and pull requests are welcome on GitHub at https://github.com/utahta/wp2hatena. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
42
+
43
+
44
+ ## License
45
+
46
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
47
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'wp2hatena'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/bin/wp2hatena ADDED
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'wp2hatena'
5
+
6
+ print 'CONSUMER_KEY:'
7
+ CONSUMER_KEY = gets.chomp
8
+ print 'CONSUMER_SECRET:'
9
+ CONSUMER_SECRET = gets.chomp
10
+ print 'ACCESS_TOKEN:'
11
+ ACCESS_TOKEN = gets.chomp
12
+ print 'ACCESS_TOKEN_SECRET:'
13
+ ACCESS_TOKEN_SECRET = gets.chomp
14
+
15
+ print 'WORDPRESS_XML_PATH:'
16
+ WORDPRESS_XML_PATH = gets.chomp
17
+
18
+ fotolife = Wp2hatena::Hatena::Fotolife.new(
19
+ CONSUMER_KEY,
20
+ CONSUMER_SECRET,
21
+ ACCESS_TOKEN,
22
+ ACCESS_TOKEN_SECRET
23
+ )
24
+ converter = Wp2hatena::Hatena::Converter.new
25
+
26
+ image = Wp2hatena::Wordpress::Image.new
27
+ image.download(WORDPRESS_XML_PATH) do |data|
28
+ puts "downloaded #{data[:html_tag]}"
29
+ file_path = data[:file_path]
30
+ response = fotolife.upload(
31
+ file_path,
32
+ File.basename(file_path, '.*')
33
+ )
34
+
35
+ puts "uploaded syntax:#{response[:syntax]} width:#{data[:width]} height:#{data[:height]}]"
36
+ converter.set_convert_data(data[:html_tag], data[:width], data[:height], response[:syntax])
37
+ end
38
+
39
+ converter.convert(
40
+ WORDPRESS_XML_PATH,
41
+ WORDPRESS_XML_PATH.gsub(/\.xml$/, '.hatena.xml')
42
+ )
@@ -0,0 +1,90 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'sinatra'
5
+ require 'oauth'
6
+ require 'erb'
7
+ require 'json'
8
+ require 'io/console'
9
+
10
+ print 'CONSUMER_KEY:'
11
+ CONSUMER_KEY = gets.chomp
12
+ print 'CONSUMER_SECRET:'
13
+ CONSUMER_SECRET = gets.chomp
14
+
15
+ set :sessions, true
16
+ enable :sessions
17
+
18
+ before do
19
+ @consumer = OAuth::Consumer.new(
20
+ CONSUMER_KEY,
21
+ CONSUMER_SECRET,
22
+ :site => '',
23
+ :request_token_path => 'https://www.hatena.com/oauth/initiate',
24
+ :access_token_path => 'https://www.hatena.com/oauth/token',
25
+ :authorize_path => 'https://www.hatena.ne.jp/oauth/authorize'
26
+ )
27
+ end
28
+
29
+ get '/' do
30
+ erb :index
31
+ end
32
+
33
+ # リクエストトークン取得から認証用URLにリダイレクトするためのアクション
34
+ get '/oauth' do
35
+ # リクエストトークンの取得
36
+ request_token = @consumer.get_request_token(
37
+ {:oauth_callback => 'http://localhost:4567/oauth_callback'},
38
+ :scope => 'read_public,write_public,read_private,write_private'
39
+ )
40
+
41
+ # セッションへリクエストトークンを保存しておく
42
+ session[:request_token] = request_token.token
43
+ session[:request_token_secret] = request_token.secret
44
+
45
+ # 認証用URLにリダイレクトする
46
+ redirect request_token.authorize_url
47
+ end
48
+
49
+ # 認証からコールバックされ、アクセストークンを取得するためのアクション
50
+ get '/oauth_callback' do
51
+ request_token = OAuth::RequestToken.new(
52
+ @consumer,
53
+ session[:request_token],
54
+ session[:request_token_secret]
55
+ )
56
+
57
+ # リクエストトークンとverifierを用いてアクセストークンを取得
58
+ access_token = request_token.get_access_token(
59
+ {},
60
+ :oauth_verifier => params[:oauth_verifier]
61
+ )
62
+
63
+ session[:request_token] = nil
64
+ session[:request_token_secret] = nil
65
+
66
+ # アクセストークンをセッションに記録しておく
67
+ session[:access_token] = access_token.token
68
+ session[:access_token_secret] = access_token.secret
69
+
70
+ erb :oauth_callback, :locals => {:access_token => access_token}
71
+ end
72
+
73
+ __END__
74
+
75
+ @@ index
76
+ <p><a href="/oauth">Hatena</a></p>
77
+
78
+ @@ oauth_callback
79
+ <p>success getting access_token.</p>
80
+
81
+ <p>your access token is below.</p>
82
+
83
+ <dl>
84
+ <dt>access_token</dt>
85
+ <dd><%= access_token.params[:oauth_token] %></dd>
86
+ <dt>access_token_secret</dt>
87
+ <dd><%= access_token.params[:oauth_token_secret] %></dd>
88
+ </dl>
89
+
90
+ <a href="/">back to top</a>
@@ -0,0 +1,48 @@
1
+ module Wp2hatena
2
+ module Hatena
3
+
4
+ class Converter
5
+ def initialize
6
+ @convert_data = []
7
+ end
8
+
9
+ def set_convert_data(html_tag, width, height, syntax)
10
+ @convert_data << {
11
+ html_tag: html_tag,
12
+ width: width,
13
+ height: height,
14
+ syntax: syntax
15
+ }
16
+ end
17
+
18
+ def convert(src_path, dest_path)
19
+ open(src_path, 'r') do |fp|
20
+ buffer = fp.read
21
+ @convert_data.each do |data|
22
+ tag = make_tag(data)
23
+ buffer.gsub!(data[:html_tag], "[#{data[:syntax]}#{tag}]\r\n")
24
+ puts "converted [#{data[:syntax]}#{tag}]"
25
+ end
26
+
27
+ buffer.gsub!("\r\n", " \n") # 改行してくれないので、スペース2つ入れて対応
28
+ File.write(dest_path, buffer)
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def make_tag(data)
35
+ tag = ''
36
+ if data[:width] and data[:height]
37
+ tag = ":w#{data[:width]},h#{data[:height]}"
38
+ elsif data[:width]
39
+ tag = ":w#{data[:width]}"
40
+ elsif data[:height]
41
+ tag = ":h#{data[:height]}"
42
+ end
43
+ tag
44
+ end
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,43 @@
1
+ require 'rexml/document'
2
+ require 'oauth'
3
+
4
+ module Wp2hatena
5
+ module Hatena
6
+
7
+ class Fotolife
8
+ def initialize(consumer_key, consumer_secret, access_token, access_token_secret)
9
+ @consumer = OAuth::Consumer.new(
10
+ consumer_key,
11
+ consumer_secret,
12
+ :site => 'http://f.hatena.ne.jp',
13
+ )
14
+ @access_token = OAuth::AccessToken.new(
15
+ @consumer,
16
+ access_token,
17
+ access_token_secret
18
+ )
19
+ end
20
+
21
+ def upload(file_path, title, folder_name = 'Hatena Blog')
22
+ header = {'Accept' => 'application/xml', 'Content-Type' => 'application/xml'}
23
+ content = Base64.encode64(open(file_path).read)
24
+ dc_subject = folder_name ? "<dc:subject>#{folder_name}</dc:subject>" : ''
25
+ body =<<-"EOF"
26
+ <entry xmlns=http://purl.org/atom/ns>
27
+ <title>#{title}</title>
28
+ #{dc_subject}
29
+ <content mode='base64' type='image/jpeg'>#{content}</content>
30
+ </entry>
31
+ EOF
32
+ response = @access_token.request(:post, '/atom/post', body, header)
33
+ doc = REXML::Document.new(response.body)
34
+ {
35
+ id: doc.elements['entry/id'].text,
36
+ imageurl: doc.elements['entry/hatena:imageurl'].text.gsub(/\?.*/, ''),
37
+ imageurlsmall: doc.elements['entry/hatena:imageurlsmall'].text.gsub(/\?.*/, ''),
38
+ syntax: doc.elements['entry/hatena:syntax'].text
39
+ }
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module Wp2hatena
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,59 @@
1
+ require 'open-uri'
2
+ require 'tmpdir'
3
+
4
+ module Wp2hatena
5
+ module Wordpress
6
+ class Image
7
+ def initialize
8
+ @tmp_dir = Dir.mktmpdir
9
+ end
10
+
11
+ def download(xml_path)
12
+ open(xml_path, 'r') do |fp|
13
+ fp.each_line do |line|
14
+ line.match(/<a.*?href="([^"]+?)"><img.*?src="(.+?)".*?><\/a>/) do |md|
15
+ image_url = nil
16
+ image_url = md[2] if md[2].match(/.+\.(jpg|jpeg|gif|png|bmp)\Z/)
17
+ image_url = md[1] if md[1].match(/.+\.(jpg|jpeg|gif|png|bmp)\Z/)
18
+ data = {
19
+ file_path: fetch_image(image_url),
20
+ width: match_width(line),
21
+ height: match_height(line),
22
+ html_tag: line,
23
+ }
24
+ yield data
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def fetch_image(url)
33
+ file_path = File.join(@tmp_dir, File.basename(url))
34
+ open(file_path, 'wb') do |fp|
35
+ open(url) do |data|
36
+ fp.write(data.read)
37
+ end
38
+ end
39
+ file_path
40
+ end
41
+
42
+ def match_width(str)
43
+ width = nil
44
+ str.match(/width="([^"]+)"/) do |md|
45
+ width = md[1]
46
+ end
47
+ width
48
+ end
49
+
50
+ def match_height(str)
51
+ height = nil
52
+ str.match(/height="([^"]+)"/) do |md|
53
+ height = md[1]
54
+ end
55
+ height
56
+ end
57
+ end
58
+ end
59
+ end
data/lib/wp2hatena.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'wp2hatena/version'
2
+ require 'wp2hatena/hatena/fotolife'
3
+ require 'wp2hatena/hatena/converter'
4
+ require 'wp2hatena/wordpress/image'
data/wp2hatena.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'wp2hatena/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "wp2hatena"
8
+ spec.version = Wp2hatena::VERSION
9
+ spec.authors = ["utahta"]
10
+ spec.email = ["labs.ninxit@gmail.com"]
11
+
12
+ spec.summary = %q{convert images of wordpress to hatena blog}
13
+ spec.description = %q{convert images of wordpress to hatena blog}
14
+ spec.homepage = "https://github.com/utahta/wp2hatena.git"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+
25
+ spec.add_dependency 'oauth', '~> 0.4.7'
26
+ spec.add_dependency 'sinatra', '~> 1.4.6'
27
+
28
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wp2hatena
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - utahta
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-08-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: oauth
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.4.7
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.4.7
55
+ - !ruby/object:Gem::Dependency
56
+ name: sinatra
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.4.6
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.4.6
69
+ description: convert images of wordpress to hatena blog
70
+ email:
71
+ - labs.ninxit@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - bin/console
82
+ - bin/setup
83
+ - bin/wp2hatena
84
+ - bin/wp2hatena_oauth
85
+ - lib/wp2hatena.rb
86
+ - lib/wp2hatena/hatena/converter.rb
87
+ - lib/wp2hatena/hatena/fotolife.rb
88
+ - lib/wp2hatena/version.rb
89
+ - lib/wp2hatena/wordpress/image.rb
90
+ - wp2hatena.gemspec
91
+ homepage: https://github.com/utahta/wp2hatena.git
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.4.5
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: convert images of wordpress to hatena blog
115
+ test_files: []