GoogleReaderApi 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ pkg
@@ -0,0 +1,50 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{GoogleReaderApi}
8
+ s.version = "0.3.5"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Toon Willems"]
12
+ s.date = %q{2010-06-22}
13
+ s.description = %q{a google reader api (unofficial) written in ruby}
14
+ s.email = %q{willemstoon@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "README.mdown"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "GoogleReaderApi.gemspec",
21
+ "License",
22
+ "README.mdown",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "lib/google-reader-api/api.rb",
26
+ "lib/google-reader-api/cache.rb",
27
+ "lib/google-reader-api/entry.rb",
28
+ "lib/google-reader-api/feed.rb",
29
+ "lib/google-reader-api/rss_utils.rb",
30
+ "lib/google-reader-api/subscription_list.rb",
31
+ "lib/google-reader-api/user.rb",
32
+ "lib/google_reader_api.rb"
33
+ ]
34
+ s.homepage = %q{http://github.com/nudded/GoogleReaderAPI}
35
+ s.rdoc_options = ["--charset=UTF-8"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.7}
38
+ s.summary = %q{a google reader api (unofficial) written in ruby}
39
+
40
+ if s.respond_to? :specification_version then
41
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
42
+ s.specification_version = 3
43
+
44
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
45
+ else
46
+ end
47
+ else
48
+ end
49
+ end
50
+
data/License ADDED
@@ -0,0 +1,21 @@
1
+ the MIT License
2
+
3
+ Copyright (c) 2010 Toon Willems
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.mdown ADDED
@@ -0,0 +1,62 @@
1
+ GoogleReaderAPI
2
+ ===============
3
+
4
+ A Google Reader api. Programmed in ruby. This is an unofficial api.
5
+ Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
6
+
7
+ Usage
8
+ -----
9
+
10
+ If you would like to implement your own methods, you can quite easily use
11
+ the api class provided.
12
+
13
+ api = GoogleReaderApi::Api.new email, pass
14
+
15
+ and then you can perform requests using the `get_link` and `post_link` methods.
16
+
17
+ # this will give the user info.
18
+ api.get_link "api/0/user-info"
19
+
20
+ # this will add a feed
21
+ api.post_link 'api/0/subscription/edit', :s => "feed/#{url}" , :ac => :subscribe
22
+
23
+ The following methods all use the `Api` class, so there is no magic
24
+ involved.
25
+
26
+ # password should be asked by the app using the api
27
+ # you probably don't want to type that in cleartext here
28
+ user = GoogleReaderApi::User.new 'willemstoon@gmail.com' , password
29
+
30
+ you can access your feeds from there
31
+
32
+ user.feeds
33
+
34
+ which will return an array of GoogleReader::Feed objects.
35
+ then you can get the unread items, the read items, ...
36
+
37
+ hn = user.feeds.find {|feed| feed.title =~ /hacker news/i }
38
+ # return 3 unread items (ordered by date)
39
+ hn.unread_items(3)
40
+ # return all the read items
41
+ hn.read_items
42
+ # all the starred items
43
+ hn.starred_items
44
+
45
+ you get the idea.
46
+ you can like items, star items, and mark them as read or unread.
47
+
48
+ hn.all_unread_items.each {|item| item.toggle_read}
49
+
50
+ subscribing to new feeds is also easy
51
+
52
+ user.subscriptions.add "any feed url here"
53
+
54
+ unsubscribing is best done via `remove_if` method
55
+
56
+ user.subscriptions.remove_if {|feed| feed.title =~ /hacker news/i}
57
+
58
+ Todo
59
+ ----
60
+
61
+ * provide nicer convenience methods. (user.subscriptions.feeds is not a good way to access your feeds).
62
+ * labels should be part of the api.
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |gemspec|
4
+ gemspec.name = "GoogleReaderApi"
5
+ gemspec.summary = "a google reader api (unofficial) written in ruby"
6
+ gemspec.description = "a google reader api (unofficial) written in ruby"
7
+ gemspec.email = "willemstoon@gmail.com"
8
+ gemspec.homepage = "http://github.com/nudded/GoogleReaderAPI"
9
+ gemspec.authors = ["Toon Willems"]
10
+ end
11
+ rescue LoadError
12
+ puts "Jeweler not available. Install it with: gem install jeweler"
13
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.5
@@ -0,0 +1,107 @@
1
+ module GoogleReaderApi
2
+
3
+ class Api
4
+
5
+ require "cgi"
6
+ require "net/https"
7
+ require "uri"
8
+
9
+ BASE_URL = "http://www.google.com/reader/"
10
+
11
+ def initialize(email,password)
12
+ request_sid(email,password)
13
+ @cache = GoogleReaderApi::Cache.new(2)
14
+ end
15
+
16
+ # do a get request to the link
17
+ # args is a hash of values that should be used in the request
18
+ def get_link(link,args={})
19
+ link = BASE_URL + link
20
+ get_request(link,args)
21
+ end
22
+
23
+ def post_link(link,args={})
24
+ link = BASE_URL + link
25
+ post_request(link,args)
26
+ end
27
+
28
+ def cached_unread_count
29
+ @cache['unread-count'] ||= get_link 'api/0/unread-count', :output => :json
30
+ end
31
+
32
+ private
33
+
34
+ # url as a string
35
+ # the post data as a hash
36
+ def post_request(url,args)
37
+ uri = URI.parse(url)
38
+ args[:T] = token
39
+ req = Net::HTTP::Post.new(uri.path)
40
+ req.set_form_data(args)
41
+ request(uri,req)
42
+ end
43
+
44
+ # the url as a string and the args as a hash
45
+ # e.g. :allcomments => true etc...
46
+ def get_request(url,args)
47
+ uri = URI.parse url
48
+
49
+ # ck is the current unix timestamp
50
+ args[:ck] = Time.now.to_i unless args[:ck]
51
+
52
+ req = Net::HTTP::Get.new("#{uri.path}?#{argument_string(args)}")
53
+ request(uri,req)
54
+ end
55
+
56
+ def request(uri,request)
57
+ # add the cookie to the http header
58
+ request.add_field('Cookie',user_cookie)
59
+ res = Net::HTTP.start(uri.host,uri.port) do |http|
60
+ http.request(request)
61
+ end
62
+ # TODO: use better exception
63
+ if res.code != '200'
64
+ p res.body
65
+ raise "something went wrong"
66
+ end
67
+ res.body
68
+ end
69
+
70
+ # returns the argumentstring based on the hash it is given
71
+ def argument_string(args)
72
+ args.to_a.map { |v| v.join '=' }.join('&')
73
+ end
74
+
75
+ def token
76
+ url = URI.parse "http://www.google.com/reader/api/0/token"
77
+ res = Net::HTTP.start(url.host,url.port) do |http|
78
+ http.get(url.path,"Cookie" => user_cookie)
79
+ end
80
+ res.body
81
+ end
82
+
83
+ def user_cookie
84
+ CGI::Cookie::new('name' => 'SID' , 'value' => sid ,
85
+ 'path' => '/' , 'domain' => '.google.com').to_s
86
+ end
87
+
88
+ def sid
89
+ @sid
90
+ end
91
+
92
+ def request_sid(email,password)
93
+ password = CGI.escape(password)
94
+ email = CGI.escape(email)
95
+ url = URI.parse "https://www.google.com/accounts/ClientLogin?service=reader&Email=#{email}&Passwd=#{password}"
96
+ http = Net::HTTP.new(url.host,url.port)
97
+ http.use_ssl = true
98
+ res,data = http.get("#{url.path}?#{url.query}")
99
+
100
+ raise "could not authenticate" if res.code != "200"
101
+
102
+ @sid = data.match(/SID=(.+?)\n/)[1]
103
+ end
104
+
105
+ end
106
+
107
+ end
@@ -0,0 +1,30 @@
1
+ module GoogleReaderApi
2
+ class Cache
3
+
4
+ def initialize(time)
5
+ @time = time
6
+ @hash = {}
7
+ end
8
+
9
+ def [](key)
10
+ if cached?(key)
11
+ @hash[key].first
12
+ else
13
+ @hash[key] = nil
14
+ end
15
+ end
16
+
17
+ def []=(key,value)
18
+ @hash[key] = [value,Time.now.to_i]
19
+ end
20
+
21
+ def cached?(key)
22
+ if @hash[key]
23
+ Time.now.to_i - @hash[key][1] < @time
24
+ else
25
+ false
26
+ end
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,35 @@
1
+ module GoogleReaderApi
2
+ class Entry
3
+
4
+ attr_reader :entry
5
+
6
+ def initialize(api,entry)
7
+ @api, @entry = api, entry
8
+ end
9
+
10
+ def toggle_read
11
+ edit_tag 'user/-/state/com.google/read'
12
+ end
13
+
14
+ def toggle_like
15
+ edit_tag 'user/-/state/com.google/like'
16
+ end
17
+
18
+ def toggle_star
19
+ edit_tag 'user/-/state/com.google/starred'
20
+ end
21
+
22
+ def to_s
23
+ "<<Entry: #{@entry.title.content} >>"
24
+ end
25
+
26
+ private
27
+
28
+ def edit_tag(tag_identifier)
29
+ @api.post_link "api/0/edit-tag" , :a => tag_identifier ,
30
+ :s => entry.parent.id.content.to_s.scan(/feed\/.*/) ,
31
+ :i => entry.id.content.to_s
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,76 @@
1
+ module GoogleReaderApi
2
+
3
+ class Feed
4
+
5
+ include GoogleReaderApi::RssUtils
6
+
7
+ attr_reader :url, :title
8
+
9
+ def initialize(hash,api)
10
+ # strip the first 5 characters of the url (they are 'feed/')
11
+ @url = hash['id'][5..-1]
12
+ @title = hash['title']
13
+ # no idea what this is used for
14
+ @sortid = hash['sortid']
15
+ @categories = hash['categories']
16
+ @firstitemmsec = hash['firstitemmsec']
17
+
18
+ @api = api
19
+ end
20
+
21
+ def unsubscribe
22
+ @api.post_link 'api/0/subscription/edit' , :s => "feed/#{url}",
23
+ :ac => :unsubscribe
24
+ end
25
+
26
+ def unread_count
27
+ entry = JSON[@api.cached_unread_count]['unreadcounts'].find {|h| h['id'] == "feed/#{url}"}
28
+ entry ? entry['count'] : 0
29
+ end
30
+
31
+ # return count read items
32
+ def read_items(count=20)
33
+ create_entries get_user_items('read',:n => count)
34
+ end
35
+
36
+ def starred_items(count=20)
37
+ create_entries get_user_items('starred',:n => count)
38
+ end
39
+
40
+ # return the number of specified items. (read or not)
41
+ def items(count = 20)
42
+ create_entries get_feed_items(:n => count)
43
+ end
44
+
45
+ # return all the unread items in an array
46
+ def all_unread_items
47
+ unread_count > 0 ? unread_items(unread_count) : []
48
+ end
49
+
50
+ # will return an array of GoogleReader::Feed::Entry objects.
51
+ # will try to return the amount of unread items you specify. unless there are no more.
52
+ # will return 20 unread items by default.
53
+ def unread_items(count = 20)
54
+ create_entries get_feed_items(:n => count,:xt => 'user/-/state/com.google/read')
55
+ end
56
+
57
+ def inspect
58
+ to_s
59
+ end
60
+
61
+ def to_s
62
+ "<<Feed: #{title} url:#{url}>>"
63
+ end
64
+
65
+ private
66
+
67
+ def get_user_items(state,args={})
68
+ @api.get_link "atom/user/-/state/com.google/#{state}" , args
69
+ end
70
+
71
+ def get_feed_items(args={})
72
+ @api.get_link "atom/feed/#{url}" , args
73
+ end
74
+
75
+ end
76
+ end
@@ -0,0 +1,14 @@
1
+ module GoogleReaderApi
2
+ module RssUtils
3
+
4
+ require "rss/parser"
5
+ require "rss/atom"
6
+
7
+ private
8
+
9
+ def create_entries(atom_feed)
10
+ RSS::Parser.parse(atom_feed.force_encoding('utf-8')).entries.map {|e| GoogleReaderApi::Entry.new(@api,e) }
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,74 @@
1
+ module GoogleReaderApi
2
+
3
+ class SubscriptionList
4
+
5
+ require "cgi"
6
+
7
+ include GoogleReaderApi::RssUtils
8
+ include Enumerable
9
+
10
+ def initialize(api)
11
+ @api = api
12
+ update
13
+ end
14
+
15
+ # returns the total unread count
16
+ def total_unread
17
+ inject(0) {|i,j| i+j.unread_count}
18
+ end
19
+
20
+ # returns a hash
21
+ # with following pattern:
22
+ # feed => unread_count
23
+ def unread_count
24
+ hash = {}
25
+ each { |feed| hash[feed] = feed.unread_count }
26
+ hash
27
+ end
28
+
29
+ # yield each feed, if you return true
30
+ # the feed will be removed from your subscriptions
31
+ def remove_if
32
+ each { |feed| feed.unsubscribe if yield feed}
33
+ update
34
+ end
35
+
36
+ # subscribe to the given url
37
+ # google will set the title for you
38
+ def add(url)
39
+ @api.post_link 'api/0/subscription/edit', :s => "feed/#{url}" , :ac => :subscribe
40
+ update
41
+ end
42
+
43
+ # return an array of unread items
44
+ def unread_items
45
+ feeds.map(&:all_unread_items)
46
+ end
47
+
48
+ # will return an array of entries with label
49
+ def items_with_label(label)
50
+ create_entries(@api.get_link "atom/user/-/label/#{CGI::escape label}")
51
+ end
52
+
53
+ def feeds
54
+ @feeds
55
+ end
56
+
57
+ def each
58
+ @feeds.each {|feed| yield feed}
59
+ end
60
+
61
+ def update
62
+ fetch_list
63
+ end
64
+
65
+ private
66
+
67
+ def fetch_list
68
+ json = JSON[@api.get_link 'api/0/subscription/list', :output => :json]['subscriptions']
69
+ @feeds = json.map {|hash| GoogleReaderApi::Feed.new(hash,@api) }
70
+ end
71
+
72
+ end
73
+
74
+ end
@@ -0,0 +1,26 @@
1
+ module GoogleReaderApi
2
+
3
+ class User
4
+
5
+ require "json"
6
+ # maybe someone would like to access the api for a user
7
+ attr_reader :api
8
+
9
+ def initialize(email,password)
10
+ @api = GoogleReaderApi::Api::new email,password
11
+ end
12
+
13
+ def info
14
+ JSON[api.get_link "api/0/user-info"]
15
+ end
16
+
17
+ def subscriptions
18
+ @subscriptions ||= GoogleReaderApi::SubscriptionList.new @api
19
+ end
20
+
21
+ def feeds
22
+ subscriptions.feeds
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,7 @@
1
+ require "google-reader-api/rss_utils"
2
+ require "google-reader-api/api"
3
+ require "google-reader-api/cache"
4
+ require "google-reader-api/entry"
5
+ require "google-reader-api/feed"
6
+ require "google-reader-api/subscription_list"
7
+ require "google-reader-api/user"
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: GoogleReaderApi
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 3
9
+ - 5
10
+ version: 0.3.5
11
+ platform: ruby
12
+ authors:
13
+ - Toon Willems
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-06-22 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: a google reader api (unofficial) written in ruby
23
+ email: willemstoon@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README.mdown
30
+ files:
31
+ - .gitignore
32
+ - GoogleReaderApi.gemspec
33
+ - License
34
+ - README.mdown
35
+ - Rakefile
36
+ - VERSION
37
+ - lib/google-reader-api/api.rb
38
+ - lib/google-reader-api/cache.rb
39
+ - lib/google-reader-api/entry.rb
40
+ - lib/google-reader-api/feed.rb
41
+ - lib/google-reader-api/rss_utils.rb
42
+ - lib/google-reader-api/subscription_list.rb
43
+ - lib/google-reader-api/user.rb
44
+ - lib/google_reader_api.rb
45
+ has_rdoc: true
46
+ homepage: http://github.com/nudded/GoogleReaderAPI
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options:
51
+ - --charset=UTF-8
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ hash: 3
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ requirements: []
73
+
74
+ rubyforge_project:
75
+ rubygems_version: 1.3.7
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: a google reader api (unofficial) written in ruby
79
+ test_files: []
80
+