-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgreadie.rb
120 lines (100 loc) · 3.01 KB
/
greadie.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
require 'google/reader'
require 'ruby-debug'
require 'active_support'
require 'nokogiri'
class GReadie
BASE_URL = "http://www.google.com/reader/api/0/"
READING_LIST_URL = BASE_URL + "stream/contents/user/-/state/com.google/reading-list"
UNREAD_LIST_URL = READING_LIST_URL + "?xt=user/-/state/com.google/read" #presently not used
EDIT_TAG_URL = BASE_URL + "edit-tag"
def initialize(in_username, in_password)
@username = in_username
@password = in_password
end
def reading_list(number_to_fetch = 20, continuation = nil)
response = json_reading_list({:n => number_to_fetch, :c => continuation})
list = response['items'].collect do |item_hash|
GReadie::Entry.new(item_hash) rescue nil
end.compact
[list, response['continuation']]
end
def token
@token || reset_token!
end
def reset_token!
@token = Google::Reader::Base.get_token
end
def share!(entry)
Google::Reader::Base.post GReadie::EDIT_TAG_URL, :form_data => {
:T => token,
:a => Google::Reader::State::BROADCAST,
:async => false,
:i => entry.google_item_id,
:s => entry.feed.google_feed_id
}
end
protected
def connect!
@connection ||= Google::Reader::Base.establish_connection @username, @password
end
def fetch(url, options)
raise "No url provided" unless url
raise "Couldn't connect to Google Reader" unless connect!
Google::Reader::Base.get url, options
end
def json_reading_list(options = {})
url = "#{READING_LIST_URL}"
# JSON.parse(fetch(url, :query_hash => options))
json_document = fetch(url, :query_hash => options)
ActiveSupport::JSON.decode json_document
end
end
class GReadie::Entry
attr_reader :title, :author, :href, :google_item_id, :feed, :categories, :body
def initialize(item)
raise "Title is nil" unless item['title']
@title = normalize item['title']
@author = normalize item['author']
@href = item['alternate'].first['href']
@google_item_id = item['id']
@published = item['published']
@updated = item['updated']
@body = normalize get_body(item)
@feed = GReadie::Feed.new(item['origin'])
end
def sort_by_time
updated_at || published_at
end
def published_at
Time.at @published rescue nil
end
def updated_at
Time.at @updated rescue nil
end
def body=(text)
# parse body text into tag collections
# isolate paragraphs, blockquotes, and images
# grab first three paragraphs
# take word count of these paragraphs, find cutoff point
# close tags and return
end
protected
def get_body(item_hash)
container = item_hash['content'] || item_hash['summary'] || item_hash['description']
return container['content']
rescue
nil
end
def normalize(text)
return text if text.nil?
Nokogiri::XML::DocumentFragment.parse(text).to_html
end
end
class GReadie::Feed
attr_reader :google_feed_id, :title, :href
def initialize(options)
@title = options['title']
@href = options['htmlUrl']
@google_feed_id = options['streamId']
end
end