mixi-community 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1 +1,2 @@
1
1
  Gemfile.lock
2
+ *.gem
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2013 todesking
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # mixi-community
2
+
3
+ Access to Mixi community.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'mixi-community'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install mixi-community
18
+
19
+ ## Usage
20
+
21
+ require 'mixi/community'
22
+
23
+ fetcher = Mixi::Community::Fetcher.new('your_email', 'password')
24
+
25
+ community = Mixi::Community.new('community id') # http://mixi.jp/view_community.pl?id={community id}
26
+
27
+ community.fetch(fetcher)
28
+
29
+ community.recent_bbses.each do|bbs|
30
+ bbs.fetch(fetcher)
31
+
32
+ bbs.recent_comments.each do|comment|
33
+ puts "#{thread.title} #{comment.user_name} #{comment.body_text}"
34
+ end
35
+ end
36
+
37
+ See source for details.
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create new Pull Request
46
+
47
+ ## Changes
48
+
49
+ ### 0.0.2
50
+
51
+ * Add readme
52
+ * Add license
53
+ * Fix error when Encoding.default_internal is ASCII_8BIT
54
+
55
+ ### 0.0.1
56
+
57
+ * Initial release.
@@ -50,14 +50,13 @@ module Mixi
50
50
  page = fetcher.get(uri)
51
51
  @title = page.at('.bbsTitle .title').text
52
52
  @recent_comments = page.at('#bbsComment').at('dl.commentList01').children.select{|e|%w(dt dd).include? e.name}.each_slice(2).map {|dt,dd|
53
- puts dt.at('a').attr(:href)
54
53
  user_uri = URI.parse(dd.at('dl.commentContent01 dt a').attr(:href))
55
54
  user_id = Hash[user_uri.query.split('&').map{|kv|kv.split('=')}]['content_id']
56
55
  user_name = dd.at('dl.commentContent01 dt a').text
57
- body_text = dd.at('dl.commentContent01 dd').text.strip.gsub(/\n\n返信$/, '').gsub(/\r/,"\n")
56
+ body_text = resolve_encoding(dd.at('dl.commentContent01 dd').text){|t|t.strip.gsub(/\n\n返信$/, '').gsub(/\r/,"\n")}
58
57
  comment_id = dt.at('.senderId a').attr(:name).gsub(/^comment_id_(\d+)$/, '\1')
59
58
  comment_num = dt.at('.senderId a').text.gsub(/^\[(\d+)\]$/, '\1')
60
- time = Time.strptime(dt.at('.date').text, '%Y年%m月%d日 %H:%M')
59
+ time = resolve_encoding(dt.at('.date').text){|t| Time.strptime(t, '%Y年%m月%d日 %H:%M') }
61
60
 
62
61
  Comment.new(
63
62
  comment_id,
@@ -70,6 +69,24 @@ module Mixi
70
69
  }
71
70
  end
72
71
 
72
+ def resolve_encoding(text, &block)
73
+ # Nokogiriの返す文字列のエンコーディングはEncoding.default_internalに影響される。
74
+ # これがUTF-8以外だと正規表現によるマッチに失敗するため対策する必要がある。
75
+ # 当面必要ないのでASCII_8BITの場合以外は対処してない
76
+ if text.encoding == Encoding::UTF_8
77
+ block.call(text)
78
+ elsif text.encoding == Encoding::ASCII_8BIT
79
+ ret = block.call(text.force_encoding(Encoding::UTF_8))
80
+ if String === ret
81
+ ret.force_encoding(Encoding::ASCII_8BIT)
82
+ else
83
+ ret
84
+ end
85
+ else
86
+ raise "Unsupported encoding: #{text.encoding}"
87
+ end
88
+ end
89
+
73
90
  class Comment
74
91
  def initialize(id, params)
75
92
  @id = id
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = "mixi-community"
3
- gem.version = "0.0.1"
3
+ gem.version = "0.0.2"
4
4
  gem.authors = ["todesking"]
5
5
  gem.email = ["discommunicative@gmail.com"]
6
6
  gem.summary = %q{Access to Mixi community.}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mixi-community
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-13 00:00:00.000000000 Z
12
+ date: 2013-01-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mechanize
@@ -36,6 +36,8 @@ extra_rdoc_files: []
36
36
  files:
37
37
  - .gitignore
38
38
  - Gemfile
39
+ - LICENSE
40
+ - README.md
39
41
  - lib/mixi-community.rb
40
42
  - mixi-community.gemspec
41
43
  homepage: https://github.com/todesking/mixi-community
@@ -63,4 +65,3 @@ signing_key:
63
65
  specification_version: 3
64
66
  summary: Access to Mixi community.
65
67
  test_files: []
66
- has_rdoc: