ameblogazo 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .project
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in ameblogazo.gemspec
4
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,36 @@
1
+ = Ameblogazo
2
+
3
+ アメーバブログの画像のダウンロードを簡単にしてくれるツールとライブラリ。
4
+
5
+ == Installing
6
+
7
+ gem install ameblogazo
8
+
9
+ dependencies: nokogiri
10
+
11
+ == Using tool
12
+
13
+ 画像を全てダウンロード
14
+ ameblogazo -i ameba_id -d /tmp -c date
15
+
16
+ 画像を新しいものから10個ダウンロード
17
+ ameblogazo -i ameba_id -d /tmp -c date -n 10
18
+
19
+ オプションの説明
20
+ -i アメーバIDを指定
21
+ -d 保存先ディレクトリを指定
22
+ -c カテゴライズ設定(title or date)。デフォルトは無効。titleは記事タイトル毎に分類。dateは日付で分類。
23
+ -n ダウンロードする画像の枚数を指定
24
+ -p まだダウンロードしてない画像だけダウンロードする
25
+
26
+ == Using library
27
+
28
+ サンプル
29
+ require 'ameblogazo'
30
+ Ameblogazo.download(:ameba_id=>'ameba_id', :dir=>'/tmp', :categorize=>'date', :num=>10)
31
+ Ameblogazo.download_periodic(:ameba_id=>'ameba_id', :dir=>'/tmp', :categorize=>'date')
32
+
33
+ == Copyright
34
+
35
+ Copyright (c) 2012 utahta. See LICENSE.txt for
36
+ further details.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "ameblogazo/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "ameblogazo"
7
+ s.version = Ameblogazo::VERSION
8
+ s.authors = ["utahta"]
9
+ s.email = ["labs.ninxit@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{get ameba blog images}
12
+ s.description = %q{get ameba blog images}
13
+
14
+ s.rubyforge_project = "ameblogazo"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ s.add_dependency('nokogiri')
25
+ end
data/bin/ameblogazo ADDED
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ require "ameblogazo"
5
+ require "optparse"
6
+
7
+ opts = {}
8
+ op = OptionParser.new do |opt|
9
+ opt.on('-i ID', '--ameba-id=ID', 'アメーバIDを指定') {|v| opts[:ameba_id] = v}
10
+ opt.on('-d DIR', '--dir=DIR', '画像の保存先ディレクトリを指定') {|v| opts[:dir] = v}
11
+ opt.on('-c TYPE', '--categorize=TYPE', ['title', 'date'], 'カテゴライズ(title or date)を指定') {|v| opts[:categorize] = v}
12
+ opt.on('-n NUM', '--num=NUM', '画像の枚数を指定。指定がないと全て取得') {|v| opts[:num] = v}
13
+ opt.on('-p', '--periodic', 'ダウンロード済みの画像が見つかるまで取得') {|v| opts[:periodic] = v}
14
+
15
+ opt.parse!
16
+ end
17
+ if opts.empty?
18
+ puts op
19
+ exit
20
+ end
21
+
22
+ begin
23
+ if opts[:periodic]
24
+ Ameblogazo.download_periodic(opts)
25
+ else
26
+ Ameblogazo.download(opts)
27
+ end
28
+ rescue => ex
29
+ puts ex.message
30
+ end
data/lib/ameblogazo.rb ADDED
@@ -0,0 +1,213 @@
1
+ # coding: utf-8
2
+ require "ameblogazo/version"
3
+ require 'open-uri'
4
+ require 'nokogiri'
5
+ require 'fileutils'
6
+
7
+ module Ameblogazo
8
+ class Gazo
9
+ class GazoException < StandardError
10
+ end
11
+
12
+ # オプションチェック
13
+ def _check_options(options)
14
+ if options.nil? or !options.is_a?(Hash)
15
+ raise GazoException, "オプションがnil、もしくはハッシュじゃないです"
16
+ end
17
+ if options[:dir].nil?
18
+ raise GazoException, "ディレクトリ(:dir)を指定してください"
19
+ end
20
+ if options[:num]
21
+ options[:num] = options[:num].to_s
22
+ if (/^\d+$/ =~ options[:num]).nil?
23
+ raise GazoException, "数値じゃないです"
24
+ return
25
+ end
26
+ options[:num] = options[:num].to_i
27
+ end
28
+ options
29
+ end
30
+
31
+ # 一番最初の画像のURLを取得する
32
+ def _find_image_url(ameba_id)
33
+ puts "検索中..."
34
+ image_url = nil
35
+ catch :image_found do
36
+ 100.times do |i|
37
+ page = i+1
38
+ url = "http://ameblo.jp/#{ameba_id}/page-#{page}.html"
39
+ begin
40
+ html = open(url)
41
+ rescue
42
+ raise GazoException, "画像が見つからなかったです"
43
+ end
44
+ doc = Nokogiri::HTML(html)
45
+
46
+ a = doc.xpath("//a")
47
+ a.each do |node|
48
+ if /http:\/\/ameblo.jp\/#{ameba_id}\/image-\d{11}-\d{11}.html/ =~ node[:href]
49
+ image_url = node[:href]
50
+ throw :image_found
51
+ end
52
+ end
53
+ sleep(0.1)
54
+ end
55
+ end
56
+ # 念のためチェック
57
+ if image_url.nil?
58
+ raise GazoException, "画像がみつからなかったです"
59
+ end
60
+ image_url
61
+ end
62
+
63
+ # カテゴライズしたパスを返す
64
+ def _categorize(dir, categorize, img)
65
+ # カテゴリー分け(タイトルとか日付)
66
+ if categorize == "title"
67
+ title = img[:alt]
68
+ dir = "#{dir}/#{title}"
69
+ elsif categorize == "date"
70
+ if /http:\/\/stat.ameba.jp\/user_images\/(\d{8})\/.*/ =~ img[:src]
71
+ date = $1
72
+ dir = "#{dir}/#{date}"
73
+ else
74
+ print "うまく日付がとれなかったのでカテゴライズせずに保存します\n"
75
+ end
76
+ end
77
+ dir
78
+ end
79
+
80
+ # ダウンロード対象のドキュメントを取得
81
+ def _download_doc(url)
82
+ html = open(url)
83
+ doc = Nokogiri::HTML(html)
84
+ end
85
+
86
+ # ダウンロード対象のURLや保存先を取得
87
+ def _download_info(doc, dir, categorize)
88
+ img = doc.xpath('//img[@id="centerImg"]')
89
+ unless img.empty?
90
+ download_url = img[0][:src]
91
+ filename = File.basename(download_url)
92
+
93
+ # カテゴリー分け(タイトルとか日付)
94
+ dir = _categorize(dir, categorize, img[0])
95
+
96
+ # ディレクトリが存在しなければ作成する
97
+ unless File.directory?(dir)
98
+ print "ディレクトリを作成します #{dir}\n"
99
+ FileUtils.mkdir_p(dir)
100
+ end
101
+ download_file = "#{dir}/#{filename}"
102
+ return {:url=>download_url, :file=>download_file}
103
+ end
104
+ return {}
105
+ end
106
+
107
+ # 画像本体をダウンロード
108
+ def _download_img(url, file)
109
+ open(url) do |doc|
110
+ open(file, 'w') do |fp|
111
+ fp.print doc.read
112
+ end
113
+ end
114
+ filename = File.basename(file)
115
+ print "#{filename} をダウンロードしました\n"
116
+ end
117
+
118
+ # 次にダウンロードするべきURLを取得する
119
+ def _download_next_url(doc)
120
+ a = doc.xpath('//a[@id="imgLink"]')
121
+ unless a.empty?
122
+ image_url = a[0][:href] # 次の画像URL
123
+ end
124
+ end
125
+
126
+ # 指定URLの物を保存する
127
+ def _download(url, dir, categorize)
128
+ doc = _download_doc(url)
129
+ info = _download_info(doc, dir, categorize)
130
+
131
+ if File.exists?(info[:file])
132
+ print "もうダウロード済みみたいです\n"
133
+ else
134
+ _download_img(info[:url], info[:file])
135
+ end
136
+ _download_next_url(doc)
137
+ end
138
+
139
+ # ダウンロード済みの画像をみつけたら止める
140
+ def _download_periodic(url, dir, categorize)
141
+ doc = _download_doc(url)
142
+ info = _download_info(doc, dir, categorize)
143
+
144
+ if File.exists?(info[:file])
145
+ print "ダウロード済みの見つけたので終了します\n"
146
+ return nil
147
+ else
148
+ _download_img(info[:url], info[:file])
149
+ end
150
+ _download_next_url(doc)
151
+ end
152
+
153
+ # 画像を取得する
154
+ # :ameba_id => アメーバID
155
+ # :dir => 保存先ディレクトリ
156
+ # :categorize => タイトル毎などに分類する(デフォルト無効)[nil, "title", "date"]
157
+ # :num => 取得する枚数(新しいものから順番に)[数値]
158
+ def get(options)
159
+ options = _check_options(options)
160
+ ameba_id = options[:ameba_id]
161
+ categorize = options[:categorize]
162
+ dir = options[:dir]
163
+ num = options[:num]
164
+
165
+ image_url = _find_image_url(ameba_id)
166
+
167
+ if num
168
+ num.times do |i|
169
+ # 最初にみつけた画像から次の画像へと順番にたどってく
170
+ image_url = _download(image_url, dir, categorize)
171
+ break if image_url.nil?
172
+ sleep(0.3)
173
+ end
174
+ else
175
+ loop do
176
+ # 最初にみつけた画像から次の画像へと順番にたどってく
177
+ image_url = _download(image_url, dir, categorize)
178
+ break if image_url.nil?
179
+ sleep(0.3)
180
+ end
181
+ end
182
+ end
183
+
184
+ # 取得済みの画像がみつかるまで新しいものから順番に画像を取得する
185
+ # 毎日定期的に取得したいひと向け
186
+ def get_periodic(options)
187
+ options = _check_options(options)
188
+ ameba_id = options[:ameba_id]
189
+ categorize = options[:categorize]
190
+ dir = options[:dir]
191
+
192
+ image_url = _find_image_url(ameba_id)
193
+ loop do
194
+ # 最初にみつけた画像から次の画像へと順番にたどってく
195
+ image_url = _download_periodic(image_url, dir, categorize)
196
+ break if image_url.nil?
197
+ sleep(0.3)
198
+ end
199
+ end
200
+ end
201
+
202
+ def download(options)
203
+ gazo = Gazo.new
204
+ gazo.get(options)
205
+ end
206
+
207
+ def download_periodic(options)
208
+ gazo = Gazo.new
209
+ gazo.get_periodic(options)
210
+ end
211
+
212
+ module_function :download, :download_periodic
213
+ end
@@ -0,0 +1,3 @@
1
+ module Ameblogazo
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ameblogazo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - utahta
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-09 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: &85180950 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *85180950
25
+ description: get ameba blog images
26
+ email:
27
+ - labs.ninxit@gmail.com
28
+ executables:
29
+ - ameblogazo
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - .gitignore
34
+ - Gemfile
35
+ - README.rdoc
36
+ - Rakefile
37
+ - ameblogazo.gemspec
38
+ - bin/ameblogazo
39
+ - lib/ameblogazo.rb
40
+ - lib/ameblogazo/version.rb
41
+ homepage: ''
42
+ licenses: []
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project: ameblogazo
61
+ rubygems_version: 1.8.10
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: get ameba blog images
65
+ test_files: []