hateblo-mixed-contents-finder 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
+ SHA256:
3
+ metadata.gz: 84d989b9fc4ccc5bf699f91bbce1792400c1c10ab8084f10321dbce12a283c77
4
+ data.tar.gz: 64d9660708d58753dab805bcdef9ff6aebb2c7de0296cd2312fccb212ebd51b0
5
+ SHA512:
6
+ metadata.gz: '02838224ef4714c281c538fc29232d18edceb6a7b5d5b5347ad3602e5697a66b06bc8ed906edf36ba32c3e7b63d675a0ac40135b88263dd4ee42c9111ec79b68'
7
+ data.tar.gz: 8d0397e88c9825d87144ee93ab38496041b5e51415c4d04b8ac58422b3c0d3d882e2278adb30d9a96a3ea3d54729d67851a5ef69b89bf6945baf16a3f2362d21
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ result.txt
2
+ config.yml
3
+ invalid_entries.txt
4
+ .rspec_status
5
+ /pkg/
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.5.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at jit@sonicgarden.jp. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in dev-gem-sandbox.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,85 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ hateblo-mixed-contents-finder (0.1.0)
5
+ hatenablog (~> 0.5)
6
+ mechanize (~> 2.7)
7
+ thor (~> 0.20)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ addressable (2.5.2)
13
+ public_suffix (>= 2.0.2, < 4.0)
14
+ connection_pool (2.2.2)
15
+ crack (0.4.3)
16
+ safe_yaml (~> 1.0.0)
17
+ diff-lcs (1.3)
18
+ domain_name (0.5.20180417)
19
+ unf (>= 0.0.5, < 1.0.0)
20
+ hashdiff (0.3.7)
21
+ hatenablog (0.5.2)
22
+ nokogiri
23
+ oauth
24
+ http-cookie (1.0.3)
25
+ domain_name (~> 0.5)
26
+ mechanize (2.7.6)
27
+ domain_name (~> 0.5, >= 0.5.1)
28
+ http-cookie (~> 1.0)
29
+ mime-types (>= 1.17.2)
30
+ net-http-digest_auth (~> 1.1, >= 1.1.1)
31
+ net-http-persistent (>= 2.5.2)
32
+ nokogiri (~> 1.6)
33
+ ntlm-http (~> 0.1, >= 0.1.1)
34
+ webrobots (>= 0.0.9, < 0.2)
35
+ mime-types (3.1)
36
+ mime-types-data (~> 3.2015)
37
+ mime-types-data (3.2016.0521)
38
+ mini_portile2 (2.3.0)
39
+ net-http-digest_auth (1.4.1)
40
+ net-http-persistent (3.0.0)
41
+ connection_pool (~> 2.2)
42
+ nokogiri (1.8.4)
43
+ mini_portile2 (~> 2.3.0)
44
+ ntlm-http (0.1.1)
45
+ oauth (0.5.4)
46
+ public_suffix (3.0.2)
47
+ rake (10.5.0)
48
+ rspec (3.7.0)
49
+ rspec-core (~> 3.7.0)
50
+ rspec-expectations (~> 3.7.0)
51
+ rspec-mocks (~> 3.7.0)
52
+ rspec-core (3.7.1)
53
+ rspec-support (~> 3.7.0)
54
+ rspec-expectations (3.7.0)
55
+ diff-lcs (>= 1.2.0, < 2.0)
56
+ rspec-support (~> 3.7.0)
57
+ rspec-mocks (3.7.0)
58
+ diff-lcs (>= 1.2.0, < 2.0)
59
+ rspec-support (~> 3.7.0)
60
+ rspec-support (3.7.1)
61
+ safe_yaml (1.0.4)
62
+ thor (0.20.0)
63
+ unf (0.1.4)
64
+ unf_ext
65
+ unf_ext (0.0.7.5)
66
+ vcr (4.0.0)
67
+ webmock (3.4.2)
68
+ addressable (>= 2.3.6)
69
+ crack (>= 0.3.2)
70
+ hashdiff
71
+ webrobots (0.1.2)
72
+
73
+ PLATFORMS
74
+ ruby
75
+
76
+ DEPENDENCIES
77
+ bundler (~> 1.16)
78
+ hateblo-mixed-contents-finder!
79
+ rake (~> 10.0)
80
+ rspec (~> 3.0)
81
+ vcr (~> 4.0)
82
+ webmock (~> 3.4)
83
+
84
+ BUNDLED WITH
85
+ 1.16.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Junichi Ito
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,154 @@
1
+ # hateblo-mixed-contents-finder
2
+
3
+ Helper scripts for Hatena blog HTTPS migration
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'hateblo-mixed-contents-finder'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install hateblo-mixed-contents-finder
20
+
21
+ ## Usage
22
+
23
+ Describe later in Japanese.
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ 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).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/JunichiIto/hateblo-mixed-contents-finder. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
34
+
35
+ ## License
36
+
37
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
38
+
39
+ ## Code of Conduct
40
+
41
+ Everyone interacting in the Dev::Gem::Sandbox project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/JunichiIto/hateblo-mixed-contents-finder/blob/master/CODE_OF_CONDUCT.md).
42
+
43
+ ----
44
+
45
+ # hateblo-mixed-contents-finder
46
+
47
+ はてなブログをHTTP配信からHTTPS配信に移行する際に利用すると便利なヘルパースクリプト集です。
48
+
49
+ 参考: [自分のブログ(独自ドメインのはてなブログ)をHTTPS配信に切り替えました \- give IT a try](https://blog.jnito.com/entry/2018/07/16/084116)
50
+
51
+ ## 免責事項
52
+
53
+ このスクリプトを使用して、何か問題が起きても作者は一切の責任を負いません。
54
+
55
+ ## 動作環境
56
+
57
+ Ruby 2.5.1 + Mac環境で動作確認しています。
58
+
59
+ Ruby 2.4以下や、Windows環境で正常に動くかどうかは未検証です。
60
+
61
+ ## セットアップ
62
+
63
+ 過去記事の更新を実行する場合は`config.yml`の設定が必要になります。以下のページを参照して、自分のOAuthクレデンシャルを`config.yml`に設定してください。
64
+
65
+ https://github.com/kymmt90/hatenablog/blob/master/README.md
66
+
67
+ ## 使用方法
68
+
69
+ ここでは `http://my-example.hatenablog.com` という架空のブログを対象とします。
70
+
71
+ ### ブログ全体のhttpコンテンツを検証する
72
+
73
+ 以下コマンドを実行すると、`result.txt`に検証結果(見つかったhttpコンテンツの一覧)が保存されます。ファイルはタブ区切りになっているので、Excel等にコピー&ペーストで貼り付けることができます。
74
+
75
+ ```
76
+ hateblo_mixed_contents_finder validate_all http://my-example.hatenablog.com
77
+ ```
78
+
79
+ 本文だけでなく、ページ全体を検証の対象にしたい場合は、`--entire-page`オプションを指定します。
80
+
81
+ ```
82
+ hateblo_mixed_contents_finder validate_all http://my-example.hatenablog.com --entire-page
83
+ ```
84
+
85
+ エントリが多くて時間がかかる場合は`--limit`オプションを指定します。
86
+
87
+ ```
88
+ hateblo_mixed_contents_finder validate_all http://my-example.hatenablog.com --limit=5
89
+ ```
90
+
91
+ #### 検証する要素や属性について
92
+
93
+ このタスクは以下の要素や属性を検証します。
94
+
95
+ - `<img>`要素のsrc属性およびsrcset属性
96
+ - `<source>`要素のsrc属性およびsrcset属性
97
+ - `<script>`要素のsrc属性
98
+ - `<video>`要素のsrc属性
99
+ - `<audio>`要素のsrc属性
100
+ - `<iframe>`要素のsrc属性
101
+ - `<embed>`要素のsrc属性
102
+ - rel属性にstylesheetが指定されている`<link>`要素のhref属性
103
+ - `<form>`要素のaction属性
104
+ - `<object>`要素のdata属性
105
+
106
+ 参考: https://smdn.jp/works/tools/HatenaBlogTools/
107
+
108
+ #### 制限事項
109
+
110
+ このタスクではサーバーから返却されたHTMLを静的解析するだけです。そのため、JavaScriptやCSSの内部で外部のリソースをHTTPでリクエストしている場合はhttpコンテンツを検出できません。
111
+
112
+ ### 特定のエントリのhttpコンテンツを検証する
113
+
114
+ 以下のコマンドを実行すると、特定のエントリのhttpコンテンツを検証します。検証結果は標準出力に出力されます。
115
+
116
+ ```
117
+ hateblo_mixed_contents_finder validate_entry http://my-example.hatenablog.com/2018/07/17/075334
118
+ ```
119
+
120
+ 本文だけでなく、ページ全体を検証の対象にしたい場合は、`--entire-page`オプションを指定します。
121
+
122
+ ```
123
+ hateblo_mixed_contents_finder validate_entry http://my-example.hatenablog.com/2018/07/17/075334 --entire-page
124
+ ```
125
+
126
+ ### 過去記事に対して無変更で「更新」だけを実行する
127
+
128
+ `invalid_entries.txt`というテキストファイルに更新したいエントリのURLを行区切りで記述します。URLが重複するとそのぶん繰り返し更新が走るため、重複しないように記述する方が望ましいです。
129
+
130
+ ```
131
+ http://my-example.hatenablog.com/2018/07/17/075334
132
+ http://my-example.hatenablog.com/2018/07/13/123434
133
+ http://my-example.hatenablog.com/2018/06/27/053436
134
+ ```
135
+
136
+ また、前述の「セットアップ」に書いた`config.yml`も設定してください。
137
+
138
+ NOTE: 予期せぬ問題が起きてエントリの内容が失われた場合に備え、全エントリを事前に[バックアップ](http://staff.hatenablog.com/entry/2014/08/22/180000)しておくことをお勧めします。
139
+
140
+ 準備ができたら、以下のコマンドを実行します。
141
+
142
+ ```
143
+ hateblo_mixed_contents_finder update_all
144
+ ```
145
+
146
+ 実行するかどうかの確認を求められるので、実行する場合は`yes`を入力してください。
147
+
148
+ ```
149
+ [WARNING] Please backup your entries before update!!
150
+ Do you update 3 entries? [yes|no]: yes
151
+ ```
152
+
153
+ ## License
154
+ MIT License.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "dev/gem/sandbox"
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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/config.yml.sample ADDED
@@ -0,0 +1,6 @@
1
+ consumer_key: xxxxx
2
+ consumer_secret: xxxxx
3
+ access_token: xxxxx
4
+ access_token_secret: xxxxx
5
+ user_id: xxxxx
6
+ blog_id: xxxxx.hateblo.jp
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby -wU
2
+
3
+ require 'hateblo_mixed_contents_finder'
4
+
5
+ HatebloMixedContentsFinder::CLI.start(ARGV)
@@ -0,0 +1,33 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "hateblo_mixed_contents_finder/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "hateblo-mixed-contents-finder"
7
+ spec.version = HatebloMixedContentsFinder::VERSION
8
+ spec.authors = ["Junichi Ito"]
9
+ spec.email = ["me@jnito.com"]
10
+
11
+ spec.summary = %q{Helper tools for Hatena blog HTTPS migration}
12
+ spec.homepage = "https://github.com/JunichiIto/hateblo-mixed-contents-finder"
13
+ spec.license = "MIT"
14
+
15
+ # Specify which files should be added to the gem when it is released.
16
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
17
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_runtime_dependency "mechanize", "~> 2.7"
25
+ spec.add_runtime_dependency "hatenablog", "~> 0.5"
26
+ spec.add_runtime_dependency "thor", "~> 0.20"
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.16"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec", "~> 3.0"
31
+ spec.add_development_dependency "vcr", "~> 4.0"
32
+ spec.add_development_dependency "webmock", "~> 3.4"
33
+ end
@@ -0,0 +1,3 @@
1
+ http://xxxxx.hateblo.jp/entry/2017/04/05/082746
2
+ http://xxxxx.hateblo.jp/entry/2017/05/02/082639
3
+ http://xxxxx.hateblo.jp/entry/2018/07/09/053436
@@ -0,0 +1,6 @@
1
+ require "hateblo_mixed_contents_finder/element_validator"
2
+ require "hateblo_mixed_contents_finder/hatena_client"
3
+ require "hateblo_mixed_contents_finder/invalid_content"
4
+ require "hateblo_mixed_contents_finder/mixed_contents_finder"
5
+ require "hateblo_mixed_contents_finder/cli"
6
+ require "hateblo_mixed_contents_finder/version"
@@ -0,0 +1,59 @@
1
+ require 'fileutils'
2
+ require 'thor'
3
+
4
+ module HatebloMixedContentsFinder
5
+ class CLI < Thor
6
+ desc "validate_all SITE_URL", "Find http contents in all entries."
7
+ option :entire_page, type: :boolean, default: false, desc: "Validate entire page or content part only."
8
+ option :limit, type: :numeric, default: nil, desc: "Specify upper limit of entry validation."
9
+ option :path, type: :string, default: './result.txt', hide: true
10
+ def validate_all(site_url)
11
+ entire_page = options[:entire_page]
12
+ limit = options[:limit]
13
+ path = options[:path]
14
+
15
+ invalid_contents = MixedContentsFinder.new(entire_page: entire_page).validate_all(site_url, limit: limit)
16
+ FileUtils.rm(path) if File.exist?(path)
17
+ File.write(path, invalid_contents.join("\n"))
18
+ if invalid_contents.empty?
19
+ puts 'OK💚'
20
+ else
21
+ puts "#{invalid_contents.size} errors found. Please open result.txt."
22
+ end
23
+ end
24
+
25
+ desc "validate_entry ENTRY_URL", "Find http contents in a specified entry."
26
+ option :entire_page, type: :boolean, default: false, desc: "Validate entire page or content part only."
27
+ def validate_entry(entry_url)
28
+ entire_page = options[:entire_page]
29
+
30
+ invalid_contents = MixedContentsFinder.new(entire_page: entire_page).validate_entry(entry_url)
31
+ if invalid_contents.empty?
32
+ puts 'OK💚'
33
+ else
34
+ puts invalid_contents
35
+ end
36
+ end
37
+
38
+ desc "update_all", "Update entries specified in invalid_entries.txt."
39
+ option :path, type: :string, default: './invalid_entries.txt', hide: true
40
+ option :sleep_sec, type: :numeric, default: 1, hide: true
41
+ def update_all
42
+ sleep_sec = options[:sleep_sec]
43
+ path = options[:path]
44
+
45
+ count = File.read(path).lines.size
46
+ puts "[WARNING] Please backup your entries before update!!"
47
+ print "Do you update #{count} entries? [yes|no]: "
48
+ res = STDIN.gets.chomp.downcase
49
+ return unless res == 'yes'
50
+
51
+ client = HatenaClient.new
52
+ File.foreach(path, chomp: true) do |url|
53
+ client.update_entry(url)
54
+ sleep sleep_sec
55
+ end
56
+ puts 'Completed.'
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,37 @@
1
+ module HatebloMixedContentsFinder
2
+ class ElementValidator
3
+ attr_reader :tag, :attr, :root
4
+
5
+ def initialize(tag, attr, root)
6
+ @tag = tag
7
+ @attr = attr
8
+ @root = root
9
+ end
10
+
11
+ def validate(page)
12
+ entry_title = find_entry_title(page)
13
+ entry_id = find_entry_id(page)
14
+ nodes = page.search("#{root} #{tag}")
15
+ nodes.map { |node|
16
+ link_url = node[attr].to_s
17
+ if link_url.include?('http:') && target?(tag, node)
18
+ InvalidContent.new(page.uri, entry_id, entry_title, tag, attr, link_url)
19
+ end
20
+ }.compact
21
+ end
22
+
23
+ private
24
+
25
+ def target?(tag, node)
26
+ tag != 'link' || (tag == 'link' && node['rel'] == 'stylesheet')
27
+ end
28
+
29
+ def find_entry_title(page)
30
+ page.search('.entry-title')[0].text.strip
31
+ end
32
+
33
+ def find_entry_id(page)
34
+ page.search('article.entry')[0]['data-uuid']
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,43 @@
1
+ require 'mechanize'
2
+ require 'hatenablog'
3
+
4
+ module HatebloMixedContentsFinder
5
+ class HatenaClient
6
+ def update_entry(entry_url)
7
+ entry_id = fetch_id(entry_url)
8
+ Hatenablog::Client.create do |blog_client|
9
+ posted_entry = blog_client.get_entry(entry_id)
10
+ puts "[#{Time.now.strftime("%H:%M:%S")}] Updating #{entry_url} #{posted_entry.title}"
11
+
12
+ updated_entry = blog_client.update_entry(
13
+ posted_entry.id,
14
+ posted_entry.title,
15
+ posted_entry.content,
16
+ posted_entry.categories,
17
+ posted_entry.draft,
18
+ posted_entry.updated.strftime('%Y-%m-%dT%T%:z')
19
+ )
20
+
21
+ assert_same(posted_entry, updated_entry)
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def assert_same(posted_entry, updated_entry)
28
+ %w(title content categories updated draft).each do |attr|
29
+ original = posted_entry.send(attr)
30
+ updated = updated_entry.send(attr)
31
+ if original != updated
32
+ raise "#{attr} is changed: #{original} => #{updated} / #{posted_entry.id} / #{posted_entry.title}"
33
+ end
34
+ end
35
+ end
36
+
37
+ def fetch_id(url)
38
+ agent = Mechanize.new
39
+ page = agent.get(url)
40
+ page.search('article.entry')[0]['data-uuid']
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,18 @@
1
+ module HatebloMixedContentsFinder
2
+ class InvalidContent
3
+ attr_reader :page_url, :entry_id, :entry_title, :tag, :attr, :link_url
4
+
5
+ def initialize(page_url, entry_id, entry_title, tag, attr, link_url)
6
+ @page_url = page_url.to_s
7
+ @entry_id = entry_id
8
+ @entry_title = entry_title
9
+ @tag = tag
10
+ @attr = attr
11
+ @link_url = link_url
12
+ end
13
+
14
+ def to_s
15
+ [page_url, entry_id, entry_title, tag, attr, link_url].join("\t")
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,65 @@
1
+ require 'mechanize'
2
+
3
+ module HatebloMixedContentsFinder
4
+ class MixedContentsFinder
5
+ attr_reader :entire_page
6
+
7
+ def initialize(entire_page: false)
8
+ @entire_page = entire_page
9
+ end
10
+
11
+ def validate_all(site_url, limit: 3, sleep_sec: 1)
12
+ puts "Validate #{site_url} / entire_page: #{entire_page}, limit: #{limit || 'none'}"
13
+ invalid_contents = []
14
+ archive_url = File.join(site_url, 'archive')
15
+ agent = Mechanize.new
16
+ next_page_link = nil
17
+ counter = 0
18
+ catch(:exit_loop) do
19
+ begin
20
+ list_url = next_page_link ? next_page_link['href'] : archive_url
21
+ puts "Validating #{list_url}"
22
+ page = agent.get(list_url)
23
+ links = page.search('.entry-title-link')
24
+ links.each do |link|
25
+ over_limit = limit && (counter += 1) > limit
26
+ throw :exit_loop if over_limit
27
+
28
+ url = link['href']
29
+ invalid_contents += validate_entry(url)
30
+ sleep sleep_sec
31
+ end
32
+ next_page_link = page.search('.pager-next a')&.first
33
+ end while next_page_link
34
+ end
35
+ invalid_contents
36
+ end
37
+
38
+ VALIDATE_CONDITIONS = [
39
+ %w(img src),
40
+ %w(img srcset),
41
+ %w(source src),
42
+ %w(source srcset),
43
+ %w(script src),
44
+ %w(video src),
45
+ %w(audio src),
46
+ %w(iframe src),
47
+ %w(embed src),
48
+ %w(form action),
49
+ %w(object data),
50
+ %w(link href),
51
+ ]
52
+
53
+ def validate_entry(url)
54
+ puts "[#{Time.now.strftime("%H:%M:%S")}] Validate #{url}"
55
+
56
+ agent = Mechanize.new
57
+ page = agent.get(url)
58
+ root = entire_page ? '' : '.entry-content'
59
+ VALIDATE_CONDITIONS.flat_map do |tag, attr|
60
+ validator = ElementValidator.new(tag, attr, root)
61
+ validator.validate(page)
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,3 @@
1
+ module HatebloMixedContentsFinder
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,179 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hateblo-mixed-contents-finder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Junichi Ito
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-07-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mechanize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: hatenablog
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: thor
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.20'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.20'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.16'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.16'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: vcr
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '4.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '4.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: webmock
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '3.4'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '3.4'
125
+ description:
126
+ email:
127
+ - me@jnito.com
128
+ executables:
129
+ - hateblo_mixed_contents_finder
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".gitignore"
134
+ - ".rspec"
135
+ - ".ruby-version"
136
+ - CODE_OF_CONDUCT.md
137
+ - Gemfile
138
+ - Gemfile.lock
139
+ - LICENSE.txt
140
+ - README.md
141
+ - Rakefile
142
+ - bin/console
143
+ - bin/setup
144
+ - config.yml.sample
145
+ - exe/hateblo_mixed_contents_finder
146
+ - hateblo-mixed-contents-finder.gemspec
147
+ - invalid_entries.txt.sample
148
+ - lib/hateblo_mixed_contents_finder.rb
149
+ - lib/hateblo_mixed_contents_finder/cli.rb
150
+ - lib/hateblo_mixed_contents_finder/element_validator.rb
151
+ - lib/hateblo_mixed_contents_finder/hatena_client.rb
152
+ - lib/hateblo_mixed_contents_finder/invalid_content.rb
153
+ - lib/hateblo_mixed_contents_finder/mixed_contents_finder.rb
154
+ - lib/hateblo_mixed_contents_finder/version.rb
155
+ homepage: https://github.com/JunichiIto/hateblo-mixed-contents-finder
156
+ licenses:
157
+ - MIT
158
+ metadata: {}
159
+ post_install_message:
160
+ rdoc_options: []
161
+ require_paths:
162
+ - lib
163
+ required_ruby_version: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ version: '0'
168
+ required_rubygems_version: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ requirements: []
174
+ rubyforge_project:
175
+ rubygems_version: 2.7.6
176
+ signing_key:
177
+ specification_version: 4
178
+ summary: Helper tools for Hatena blog HTTPS migration
179
+ test_files: []