livejournal2 0.4.0
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.
- checksums.yaml +7 -0
- data/Changes +24 -0
- data/LICENSE +17 -0
- data/README.md +92 -0
- data/Rakefile +39 -0
- data/VERSION +1 -0
- data/lib/livejournal.rb +6 -0
- data/lib/livejournal/basic.rb +98 -0
- data/lib/livejournal/comment.rb +84 -0
- data/lib/livejournal/comments-xml.rb +161 -0
- data/lib/livejournal/database.rb +308 -0
- data/lib/livejournal/entry.rb +385 -0
- data/lib/livejournal/friends.rb +137 -0
- data/lib/livejournal/login.rb +46 -0
- data/lib/livejournal/logjam.rb +78 -0
- data/lib/livejournal/request.rb +142 -0
- data/lib/livejournal/sync.rb +187 -0
- data/sample/export +144 -0
- data/sample/fuse +198 -0
- data/sample/graph +208 -0
- data/sample/progressbar.rb +45 -0
- data/setup.rb +1585 -0
- data/test/checkfriends.rb +29 -0
- data/test/comments-xml.rb +110 -0
- data/test/database.rb +54 -0
- data/test/login.rb +19 -0
- data/test/roundtrip.rb +46 -0
- data/test/time.rb +49 -0
- metadata +74 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 70cf259619d7fdcfaa042318210d463f4dfdbaca194a3634bd24d8182a6df1a3
|
4
|
+
data.tar.gz: c8bdc3f44a4f54f47aed9fe1281654195e739e0da8756e283ad7bb651446b96f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d6676ce797ec2e5b7e32624a303e7176a8ba3c819c3571eafb2c34e9aaa4b71335b2b01523d1d996964c44e9881ee5bea9689724eca5bd67d0d0abb67ef8345c
|
7
|
+
data.tar.gz: 0ae722f3c4af8c3548a4b02e35d475246191000e61ac3aa2663eed93169aa3b662b56e161ec485df9e4b0ff5ce11d8813d778289060b285b38a30da801642783
|
data/Changes
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
0.4.0: Sat, Mar 27 2021
|
2
|
+
- forked version
|
3
|
+
- added HTTPS support
|
4
|
+
- added support for alternative servers
|
5
|
+
|
6
|
+
0.3.7: Fri, Feb 10 2012
|
7
|
+
- give_features support (Alexander Logunov)
|
8
|
+
|
9
|
+
0.3.6: Fri, Jan 20 2012
|
10
|
+
- Ruby 1.9.3 compatibility (Aleksandr Zykov)
|
11
|
+
|
12
|
+
0.3.5: Thu, Jan 19 2012
|
13
|
+
- Send User-Agent header (Aleksandr Zykov)
|
14
|
+
|
15
|
+
0.3.3: Sat, Apr 23 2011
|
16
|
+
- Populate the list of journals the user has posting access to.
|
17
|
+
|
18
|
+
0.3.2: Mon, Nov 22 2010
|
19
|
+
- Support for the new LiveJournal entry properties
|
20
|
+
|
21
|
+
0.3.1: Sat, 06 Jan 2007 10:21:05 -0800
|
22
|
+
- Don't die on the "useragent" prop.
|
23
|
+
- Add a :strict => false option to GetEvents so we won't die on any new prop.
|
24
|
+
see the GetEvents documentation for details.
|
data/LICENSE
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
2
|
+
of this software and associated documentation files (the "Software"), to deal
|
3
|
+
in the Software without restriction, including without limitation the rights
|
4
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
5
|
+
copies of the Software, and to permit persons to whom the Software is
|
6
|
+
furnished to do so, subject to the following conditions:
|
7
|
+
|
8
|
+
The above copyright notice and this permission notice shall be included in
|
9
|
+
all copies or substantial portions of the Software.
|
10
|
+
|
11
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
12
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
13
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
14
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
15
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
16
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
17
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
# LiveJournal API client
|
2
|
+
|
3
|
+
This is a clone of the original [livejournal](https://github.com/romanbsd/livejournal) gem. A few minor fixes
|
4
|
+
and refactorings made. New functionality includes support for alternative
|
5
|
+
servers compatible with LiveJournal API (e.g. Dreamwidth.org).
|
6
|
+
|
7
|
+
## How to use
|
8
|
+
|
9
|
+
Add to your `Gemfile`:
|
10
|
+
|
11
|
+
gem "livejournal2"
|
12
|
+
|
13
|
+
(Please note the name of the directory to require is still
|
14
|
+
`livejournal`, not `livejournal2`)
|
15
|
+
|
16
|
+
## Example
|
17
|
+
|
18
|
+
require 'livejournal'
|
19
|
+
|
20
|
+
dreamwidth = LiveJournal::Server.new("Dreamwidth", "https://www.dreamwidth.org")
|
21
|
+
user = LiveJournal::User.new('test', 'testkey', dreamwidth)
|
22
|
+
login = LiveJournal::Request::Login.new(user)
|
23
|
+
login.run
|
24
|
+
|
25
|
+
NOTE: For LiveJournal you use your actual password, but for Dreamwidth instead of
|
26
|
+
password you need to provide an API key that can be generated here: https://www.dreamwidth.org/manage/emailpost
|
27
|
+
|
28
|
+
---
|
29
|
+
|
30
|
+
Original README below:
|
31
|
+
|
32
|
+
---
|
33
|
+
|
34
|
+
# ljrb: LiveJournal Ruby module
|
35
|
+
|
36
|
+
Copyright: Copyright (C) 2005 Evan Martin <martine@danga.com>
|
37
|
+
|
38
|
+
Website: http://neugierig.org/software/livejournal/ruby
|
39
|
+
|
40
|
+
Documentation: http://rubydoc.info/gems/livejournal/
|
41
|
+
|
42
|
+
Example usage:
|
43
|
+
require 'livejournal/login'
|
44
|
+
|
45
|
+
puts "Logging in..."
|
46
|
+
user = LiveJournal::User.new('test', 'test')
|
47
|
+
login = LiveJournal::Request::Login.new(user)
|
48
|
+
login.run
|
49
|
+
|
50
|
+
puts "Login response:"
|
51
|
+
login.dumpresponse
|
52
|
+
|
53
|
+
puts "User's full name: #{user.fullname}"
|
54
|
+
|
55
|
+
## LiveJournal Datatypes
|
56
|
+
* LiveJournal::Server
|
57
|
+
* LiveJournal::User
|
58
|
+
* LiveJournal::Entry
|
59
|
+
* LiveJournal::Comment
|
60
|
+
* LiveJournal::Friend
|
61
|
+
|
62
|
+
## Implemented Requests
|
63
|
+
|
64
|
+
### Login Requests
|
65
|
+
* LiveJournal::Request::Login
|
66
|
+
|
67
|
+
### Friend Requests
|
68
|
+
* LiveJournal::Request::Friends
|
69
|
+
* LiveJournal::Request::FriendOfs
|
70
|
+
* LiveJournal::Request::CheckFriends
|
71
|
+
|
72
|
+
### Entry Requests
|
73
|
+
* LiveJournal::Request::PostEvent
|
74
|
+
* LiveJournal::Request::GetEvents
|
75
|
+
* LiveJournal::Request::EditEvent
|
76
|
+
|
77
|
+
### Comments Requests
|
78
|
+
* Livejournal::Request::GetComments
|
79
|
+
* Livejournal::Request::GetRecentComments
|
80
|
+
|
81
|
+
## Journal Offline Synchronization
|
82
|
+
* LiveJournal::Sync::Entries
|
83
|
+
* LiveJournal::Sync::Comments
|
84
|
+
See samples/export for an example of how to use these.
|
85
|
+
|
86
|
+
## SQLite3 Support
|
87
|
+
* LiveJournal::Database -- storing/loading entries+comments with SQLite3
|
88
|
+
Integrates well with syncing. See samples/export.
|
89
|
+
|
90
|
+
## Other Features
|
91
|
+
* LiveJournal::LogJam -- interface with LogJam (http://logjam.danga.com) 's
|
92
|
+
journal exports. (XXX currently broken)
|
data/Rakefile
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'rdoc/task'
|
2
|
+
require 'rake/testtask'
|
3
|
+
|
4
|
+
FILES = FileList[
|
5
|
+
'Rakefile', 'README.md', 'Changes', 'LICENSE', 'setup.rb', 'VERSION',
|
6
|
+
'lib/**/*', 'sample/*', 'test/*'
|
7
|
+
]
|
8
|
+
|
9
|
+
begin
|
10
|
+
require 'jeweler'
|
11
|
+
Jeweler::Tasks.new do |gem|
|
12
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
13
|
+
gem.name = 'livejournal'
|
14
|
+
gem.summary = 'Module for interacting with livejournal'
|
15
|
+
gem.description = %q{LiveJournal module. Post to livejournal, retrieve friends lists, edit entries, sync journal to an offline database.}
|
16
|
+
gem.email = 'romanbsd@yahoo.com'
|
17
|
+
gem.homepage = 'http://neugierig.org/software/livejournal/ruby/'
|
18
|
+
gem.authors = ['Evan Martin', 'Roman Shterenzon']
|
19
|
+
gem.files = FILES.to_a
|
20
|
+
end
|
21
|
+
Jeweler::GemcutterTasks.new
|
22
|
+
rescue LoadError
|
23
|
+
puts "Jeweler not available. Install it with: gem install jeweler"
|
24
|
+
end
|
25
|
+
|
26
|
+
desc 'Generate RDoc'
|
27
|
+
Rake::RDocTask.new :rdoc do |rd|
|
28
|
+
rd.title = "ljrb (LiveJournal Ruby module) Documentation"
|
29
|
+
rd.rdoc_dir = 'doc'
|
30
|
+
rd.rdoc_files.add 'lib/livejournal/*.rb', 'README.md', 'LICENSE'
|
31
|
+
rd.main = 'README.md'
|
32
|
+
end
|
33
|
+
|
34
|
+
desc 'Run Tests'
|
35
|
+
Rake::TestTask.new :test do |t|
|
36
|
+
t.test_files = FileList['test/*.rb']
|
37
|
+
end
|
38
|
+
|
39
|
+
# vim: set ts=2 sw=2 et :
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.4.0
|
data/lib/livejournal.rb
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
#--
|
3
|
+
# ljrb -- LiveJournal Ruby module
|
4
|
+
# Copyright (c) 2005 Evan Martin <martine@danga.com>
|
5
|
+
#
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
# of this software and associated documentation files (the "Software"), to deal
|
8
|
+
# in the Software without restriction, including without limitation the rights
|
9
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
# copies of the Software, and to permit persons to whom the Software is
|
11
|
+
# furnished to do so, subject to the following conditions:
|
12
|
+
#
|
13
|
+
# The above copyright notice and this permission notice shall be included in
|
14
|
+
# all copies or substantial portions of the Software.
|
15
|
+
#
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22
|
+
# SOFTWARE.
|
23
|
+
#++
|
24
|
+
|
25
|
+
module LiveJournal
|
26
|
+
# A LiveJournal server. name is currently unused.
|
27
|
+
class Server
|
28
|
+
attr_accessor :name, :url
|
29
|
+
|
30
|
+
def initialize(name, url)
|
31
|
+
@name = name
|
32
|
+
@url = url
|
33
|
+
end
|
34
|
+
|
35
|
+
def host
|
36
|
+
uri.host
|
37
|
+
end
|
38
|
+
|
39
|
+
def port
|
40
|
+
uri.port
|
41
|
+
end
|
42
|
+
|
43
|
+
def use_ssl
|
44
|
+
uri.scheme == "https"
|
45
|
+
end
|
46
|
+
|
47
|
+
def ==(server)
|
48
|
+
host == server.host
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
def uri
|
53
|
+
URI.parse(url)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
DEFAULT_SERVER = Server.new("LiveJournal.com", "http://www.livejournal.com")
|
57
|
+
|
58
|
+
# A LiveJournal user. Given a username, password, and server, running a
|
59
|
+
# LiveJournal::Request::Login will fill in the other fields.
|
60
|
+
class User
|
61
|
+
# parameter when creating a User
|
62
|
+
attr_accessor :username, :password, :server
|
63
|
+
# Set usejournal to log in as user username but act as user usejournal.
|
64
|
+
# For example, to work with a community you own.
|
65
|
+
attr_accessor :usejournal
|
66
|
+
# User's self-reported name, as retrieved by LiveJournal::Request::Login
|
67
|
+
attr_accessor :fullname
|
68
|
+
# Journals the user has posting access to
|
69
|
+
attr_accessor :journals
|
70
|
+
|
71
|
+
def initialize(username=nil, password=nil, server=nil)
|
72
|
+
@username = username
|
73
|
+
@password = password
|
74
|
+
@usejournal = nil
|
75
|
+
@server = server || LiveJournal::DEFAULT_SERVER
|
76
|
+
end
|
77
|
+
def journal
|
78
|
+
@usejournal || @username
|
79
|
+
end
|
80
|
+
def to_s
|
81
|
+
"#{@username}: '#{@fullname}'"
|
82
|
+
end
|
83
|
+
def canonical_journal_name
|
84
|
+
journal.tr("_", "-")
|
85
|
+
end
|
86
|
+
def journal_url
|
87
|
+
klass = server.use_ssl ? URI::HTTPS : URI::HTTP
|
88
|
+
host = server.host.gsub(/^www\./, "")
|
89
|
+
if journal[0] == "_"
|
90
|
+
klass.build(host: "users.#{host}", port: server.port, path: "/" + canonical_journal_name).to_s
|
91
|
+
else
|
92
|
+
klass.build(host: "#{canonical_journal_name}.#{host}", port: server.port).to_s
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
# vim: ts=2 sw=2 et :
|
@@ -0,0 +1,84 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
#--
|
3
|
+
# ljrb -- LiveJournal Ruby module
|
4
|
+
# Copyright (c) 2005 Evan Martin <martine@danga.com>
|
5
|
+
#
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
# of this software and associated documentation files (the "Software"), to deal
|
8
|
+
# in the Software without restriction, including without limitation the rights
|
9
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
# copies of the Software, and to permit persons to whom the Software is
|
11
|
+
# furnished to do so, subject to the following conditions:
|
12
|
+
#
|
13
|
+
# The above copyright notice and this permission notice shall be included in
|
14
|
+
# all copies or substantial portions of the Software.
|
15
|
+
#
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22
|
+
# SOFTWARE.
|
23
|
+
#++
|
24
|
+
#
|
25
|
+
# LiveJournal comments.
|
26
|
+
|
27
|
+
# http://www.livejournal.com/developer/exporting.bml
|
28
|
+
|
29
|
+
module LiveJournal
|
30
|
+
class Comment
|
31
|
+
attr_accessor :commentid, :posterid, :itemid, :parentid
|
32
|
+
# State of the comment. Possible values: {+:active+, +:screened+, +:deleted+}
|
33
|
+
attr_accessor :state
|
34
|
+
attr_accessor :subject, :body
|
35
|
+
# a Ruby Time object
|
36
|
+
attr_reader :time
|
37
|
+
|
38
|
+
def initialize
|
39
|
+
@commentid = @posterid = @itemid = @parentid = nil
|
40
|
+
@subject = @body = nil
|
41
|
+
@time = nil
|
42
|
+
@state = :active
|
43
|
+
end
|
44
|
+
|
45
|
+
# Convert a state to the string representation used by LiveJournal.
|
46
|
+
def self.state_from_string(str)
|
47
|
+
case str
|
48
|
+
when nil; :active
|
49
|
+
when 'A'; :active
|
50
|
+
when 'D'; :deleted
|
51
|
+
when 'S'; :screened
|
52
|
+
when 'F'; :frozen
|
53
|
+
else raise ArgumentError, "Invalid comment state: #{str.inspect}"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# Convert a state from the string representation used by LiveJournal.
|
58
|
+
def self.state_to_string state
|
59
|
+
case state
|
60
|
+
when nil; nil
|
61
|
+
when :active; nil
|
62
|
+
when :deleted; 'D'
|
63
|
+
when :screened; 'S'
|
64
|
+
when :frozen; 'F'
|
65
|
+
else raise ArgumentError, "Invalid comment state: #{state.inspect}"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def time=(time)
|
70
|
+
raise RuntimeError, "Must use GMT times everywhere to reduce confusion. See LiveJournal::coerce_gmt for details." unless time.gmt?
|
71
|
+
@time = time
|
72
|
+
end
|
73
|
+
|
74
|
+
def ==(other)
|
75
|
+
[:commentid, :posterid, :state, :itemid, :parentid,
|
76
|
+
:subject, :body, :time].each do |attr|
|
77
|
+
return false if send(attr) != other.send(attr)
|
78
|
+
end
|
79
|
+
return true
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# vim: ts=2 sw=2 et :
|
@@ -0,0 +1,161 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
#--
|
3
|
+
# ljrb -- LiveJournal Ruby module
|
4
|
+
# Copyright (c) 2005 Evan Martin <martine@danga.com>
|
5
|
+
#
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
# of this software and associated documentation files (the "Software"), to deal
|
8
|
+
# in the Software without restriction, including without limitation the rights
|
9
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
# copies of the Software, and to permit persons to whom the Software is
|
11
|
+
# furnished to do so, subject to the following conditions:
|
12
|
+
#
|
13
|
+
# The above copyright notice and this permission notice shall be included in
|
14
|
+
# all copies or substantial portions of the Software.
|
15
|
+
#
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22
|
+
# SOFTWARE.
|
23
|
+
#++
|
24
|
+
#
|
25
|
+
# REXML is pleasant to use but hella slow, so we allow using the expat-based
|
26
|
+
# parser as well.
|
27
|
+
|
28
|
+
require 'livejournal/comment'
|
29
|
+
require 'time' # parsing xmlschema times
|
30
|
+
|
31
|
+
module LiveJournal
|
32
|
+
begin
|
33
|
+
require 'xml/parser'
|
34
|
+
HAVE_XML_PARSER = true
|
35
|
+
rescue Exception
|
36
|
+
require 'rexml/document'
|
37
|
+
HAVE_XML_PARSER = false
|
38
|
+
end
|
39
|
+
|
40
|
+
module Sync
|
41
|
+
module CommentsXML
|
42
|
+
def self.optional_int_string(x)
|
43
|
+
return nil unless x
|
44
|
+
x.to_i
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.load_comment_from_attrs(comment, attrs)
|
48
|
+
comment.commentid = attrs['id'].to_i
|
49
|
+
comment.posterid = CommentsXML::optional_int_string attrs['posterid']
|
50
|
+
comment.itemid = CommentsXML::optional_int_string attrs['jitemid']
|
51
|
+
comment.parentid = CommentsXML::optional_int_string attrs['parentid']
|
52
|
+
statestr = attrs['state']
|
53
|
+
comment.state = LiveJournal::Comment::state_from_string(statestr) if statestr
|
54
|
+
end
|
55
|
+
|
56
|
+
class Base
|
57
|
+
attr_reader :maxid, :comments, :usermap
|
58
|
+
def initialize(data=nil)
|
59
|
+
@maxid = nil
|
60
|
+
@comments = {}
|
61
|
+
@usermap = {}
|
62
|
+
parse data if data
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
class WithREXML < Base
|
67
|
+
def parse(data)
|
68
|
+
doc = REXML::Document.new(data)
|
69
|
+
root = doc.root
|
70
|
+
|
71
|
+
root.elements.each('maxid') { |e| @maxid = e.text.to_i }
|
72
|
+
|
73
|
+
root.elements.each('comments/comment') do |e|
|
74
|
+
id = e.attributes['id'].to_i
|
75
|
+
comment = @comments[id] || Comment.new
|
76
|
+
CommentsXML::load_comment_from_attrs(comment, e.attributes)
|
77
|
+
e.elements.each('subject') { |s| comment.subject = s.text }
|
78
|
+
e.elements.each('body') { |s| comment.body = s.text }
|
79
|
+
e.elements.each('date') { |s| comment.time = Time::xmlschema s.text }
|
80
|
+
@comments[id] = comment
|
81
|
+
end
|
82
|
+
|
83
|
+
root.elements.each('usermaps/usermap') do |e|
|
84
|
+
id = e.attributes['id'].to_i
|
85
|
+
user = e.attributes['user']
|
86
|
+
@usermap[id] = user
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
if HAVE_XML_PARSER
|
92
|
+
class WithExpat < Base
|
93
|
+
class Parser < XMLParser
|
94
|
+
attr_reader :maxid, :comments, :usermap
|
95
|
+
def initialize
|
96
|
+
super
|
97
|
+
@maxid = nil
|
98
|
+
@cur_comment = nil
|
99
|
+
@comments = {}
|
100
|
+
@usermap = {}
|
101
|
+
@content = nil
|
102
|
+
end
|
103
|
+
def startElement(name, attrs)
|
104
|
+
case name
|
105
|
+
when 'maxid'
|
106
|
+
@content = ''
|
107
|
+
when 'comment'
|
108
|
+
id = attrs['id'].to_i
|
109
|
+
@cur_comment = @comments[id] || Comment.new
|
110
|
+
@comments[id] = @cur_comment
|
111
|
+
CommentsXML::load_comment_from_attrs(@cur_comment, attrs)
|
112
|
+
when 'usermap'
|
113
|
+
id = attrs['id'].to_i
|
114
|
+
user = attrs['user']
|
115
|
+
@usermap[id] = user
|
116
|
+
when 'date'
|
117
|
+
@content = ''
|
118
|
+
when 'subject'
|
119
|
+
@content = ''
|
120
|
+
when 'body'
|
121
|
+
@content = ''
|
122
|
+
end
|
123
|
+
end
|
124
|
+
def character(data)
|
125
|
+
@content << data if @content
|
126
|
+
end
|
127
|
+
def endElement(name)
|
128
|
+
return unless @content
|
129
|
+
case name
|
130
|
+
when 'maxid'
|
131
|
+
@maxid = @content.to_i
|
132
|
+
when 'date'
|
133
|
+
@cur_comment.time = Time::xmlschema(@content)
|
134
|
+
when 'subject'
|
135
|
+
@cur_comment.subject = @content
|
136
|
+
when 'body'
|
137
|
+
@cur_comment.body = @content
|
138
|
+
end
|
139
|
+
@content = nil
|
140
|
+
end
|
141
|
+
end
|
142
|
+
def parse(data)
|
143
|
+
parser = Parser.new
|
144
|
+
parser.parse(data)
|
145
|
+
@maxid = parser.maxid
|
146
|
+
@comments = parser.comments
|
147
|
+
@usermap = parser.usermap
|
148
|
+
end
|
149
|
+
end # class WithExpat
|
150
|
+
end # if HAVE_XML_PARSER
|
151
|
+
|
152
|
+
if HAVE_XML_PARSER
|
153
|
+
Parser = WithExpat
|
154
|
+
else
|
155
|
+
Parser = WithREXML
|
156
|
+
end
|
157
|
+
end # module CommentsXML
|
158
|
+
end # module Sync
|
159
|
+
end # module LiveJournal
|
160
|
+
|
161
|
+
# vim: ts=2 sw=2 et :
|