benschwarz-flickr-wrapper 0.1.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.
data/README ADDED
@@ -0,0 +1,7 @@
1
+ A grassroots readonly flickr wrapper - Strictly for convenience
2
+ Not a full API, no groups, no community, who needs friends anyway?
3
+
4
+ If you want a full API, use the `flickr` gem hosted on Rubyforge, its
5
+ old but it was nailed with its 1.0 release.
6
+
7
+ Check the specs for usage
@@ -0,0 +1,15 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "flickr-wrapper"
3
+ s.version = "0.1.0"
4
+ s.date = "2008-05-10"
5
+ s.summary = "A grassroots wrapper for flickr"
6
+ s.email = "ben@germanforblack.com"
7
+ s.homepage = "http://github.com/benschwarz/flickr-wrapper"
8
+ s.description = "A grassroots wrapper around flickr - Strictly for convenience"
9
+ s.authors = ["Ben Schwarz"]
10
+ s.files = ["README", "flickr-wrapper.gemspec", "lib/flickr-wrapper.rb", "lib/flickr-wrapper/base.rb", "lib/flickr-wrapper/machine_tag.rb", "lib/flickr-wrapper/photo.rb", "lib/flickr-wrapper/photoset.rb", "lib/flickr-wrapper/tag.rb", "lib/flickr-wrapper/user.rb"]
11
+ s.require_path = "lib"
12
+
13
+ # Deps
14
+ s.add_dependency("flickr-rest", ">= 0.0.1")
15
+ end
@@ -0,0 +1,30 @@
1
+ class Flickr::Base
2
+ attr_reader :user_id
3
+
4
+ def initialize(user_id)
5
+ @user_id = user_id
6
+ end
7
+
8
+ # Add some convenience class methods, lovley and general; sets, photos, tags and machine tags
9
+
10
+ # List all photo sets
11
+ def sets
12
+ Flickr::PhotoSet.list(self.user_id)
13
+ end
14
+
15
+ # This is a cheat, we're just going to search with no params.
16
+ # Maximum amount of photos flickr will let me return is 500
17
+ def photos
18
+ Flickr::Photo.list(self.user_id)
19
+ end
20
+
21
+ # List all tags
22
+ def tags
23
+ Flickr::Tag.list(self.user_id)
24
+ end
25
+
26
+ # List all machine tags
27
+ def machine_tags
28
+ Flickr::MachineTag.list(self.user_id)
29
+ end
30
+ end
@@ -0,0 +1,24 @@
1
+ class Flickr::MachineTag < Flickr::Base #:nodoc
2
+ class Invalid < StandardError; end;
3
+
4
+ include Validatable
5
+ validates_presence_of :namespace, :predicate, :value
6
+
7
+ attr_accessor :namespace, :predicate, :value
8
+
9
+ def initialize(namespace, predicate, value)
10
+ @namespace, @predicate, @value = namespace, predicate, value
11
+ end
12
+
13
+ def self.list(user_id)
14
+ (Flickr::Query.new(user_id).execute('flickr.tags.getListUser')/:tag).map {|tag| self.from_s tag.inner_text.to_s }.compact
15
+ end
16
+
17
+ def self.from_s(string)
18
+ new($1, $2, $3) if string =~ /(.*)\:(.*)\=(.*)/
19
+ end
20
+
21
+ def to_s
22
+ "#{namespace}:#{predicate}=#{value}"
23
+ end
24
+ end
@@ -0,0 +1,52 @@
1
+ class Flickr::Photo < Flickr::Base
2
+ class Size < Struct.new :width, :height, :source, :url; end
3
+
4
+ attr_accessor :id, :title, :description, :tags, :machine_tags, :taken, :posted, :caller
5
+
6
+ def initialize(id, title, description)
7
+ @id, @title, @description = id, title, description
8
+ end
9
+
10
+ def self.list(user_id)
11
+ search(user_id)
12
+ end
13
+
14
+ # Find will grab all sizes of images, process the tags and standard attributes of a photo
15
+ def self.find(user_id, id)
16
+ photo_call = Flickr::Query.new(user_id).execute('flickr.photos.getInfo', :photo_id => id)
17
+
18
+ # Set basic attributes
19
+ photo = self.new(id, *%w(title description).map {|a| (photo_call/a).inner_text })
20
+ photo.taken = Time.parse(photo_call.at(:dates)['taken'])
21
+ photo.posted = Time.at(photo_call.at(:dates)['posted'].to_i)
22
+
23
+ # Set tags for photo
24
+ photo.tags = (photo_call/"tag[@machine_tag=0]").map{|tag| Flickr::Tag.new tag['raw'] }
25
+ photo.machine_tags = (photo_call/"tag[@machine_tag=1]").map{|tag| Flickr::MachineTag.from_s tag['raw'] }
26
+
27
+ return photo
28
+ end
29
+
30
+ # Find a collection of photos by text
31
+ # Search options should be hashes with string values or arrays for multiple values
32
+ def self.search(user_id, search_options={})
33
+ parse(user_id, Flickr::Query.new(user_id).execute('flickr.photos.search', search_options))
34
+ end
35
+
36
+ def sizes
37
+ hash = {}
38
+ (Flickr::Query.new(user_id).execute('flickr.photos.getSizes', :photo_id => id)/:size).each do |size|
39
+ hash[size['label'].downcase.to_sym] = Size.new(*%w(width height source url).map{|a| size[a]})
40
+ end
41
+ hash
42
+ end
43
+
44
+ private
45
+ # Parse applies Hpricot to the photos and maps them to a Photo class instance
46
+ def self.parse(user_id, collection)
47
+ photos = (collection/:photo)
48
+ photos.empty? ? [] : photos.map do |photo|
49
+ self.find(user_id, photo[:id])
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,78 @@
1
+ class Flickr::PhotoSet < Flickr::Base
2
+ attr_accessor :id, :title, :description, :photo_count
3
+
4
+ def initialize id, title, description, photo_count
5
+ @id, @title, @description, @photo_count = id, title, description, photo_count
6
+ end
7
+
8
+ # Class methods require scope of the user, it is to be sent into the method
9
+ def self.list user_id
10
+ query = Flickr::Query.new user_id
11
+ parse(query.execute('flickr.photosets.getList'))
12
+ end
13
+
14
+ # Get information regarding set by searching with the set's ID
15
+ def self.find user_id, id
16
+ # Keep scope of the user that made the call
17
+ @user_id = user_id
18
+
19
+ query = Flickr::Query.new(@user_id).execute('flickr.photosets.getInfo', :photoset_id => id)
20
+ # Find should return a single entity
21
+ parse(query).first
22
+ end
23
+
24
+ # Get the photos within a set
25
+ # Queries:
26
+ # > photosets.getPhotos (1 call)
27
+ # > Photo.find ID (n calls)
28
+ def photos
29
+ (Flickr::Query.new(user_id).execute('flickr.photosets.getPhotos', :photoset_id => id)/:photo).map do |photo|
30
+ Flickr::Photo.find user_id, photo[:id]
31
+ end
32
+ end
33
+
34
+ # Return a list of tags for the set
35
+ # Queries:
36
+ # > photosets.getPhotos (1 call)
37
+ # > Photo.find ID (n calls)
38
+ def tags
39
+ tags = []
40
+ photos.each do |photo|
41
+ photo.tags.each do |tag|
42
+ tags << tag.value
43
+ end
44
+ end
45
+ tags.uniq
46
+ end
47
+
48
+ # Return a list of machine_tags for the set
49
+ # Queries:
50
+ # > photosets.getPhotos (1 call)
51
+ # > Photo.find ID (n calls)
52
+ def machine_tags
53
+ tags = []
54
+ photos.each do |photo|
55
+ photo.machine_tags.each do |tag|
56
+ tags << tag
57
+ end
58
+ end
59
+ tags.uniq
60
+ end
61
+
62
+ # Search for photosets that have similar tags
63
+ def similar
64
+ sets = []
65
+ Flickr::PhotoSet.list(user_id).each do |set|
66
+ sets << set unless (self.tags & set.tags).empty? or set == self
67
+ end
68
+ sets.uniq
69
+ end
70
+
71
+ private
72
+ def self.parse collection
73
+ photosets = (collection/:photoset)
74
+ photosets.empty? ? [] : photosets.map do |set|
75
+ new(set[:id], (set/:title).inner_text, (set/:description).inner_text, set[:photos])
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,13 @@
1
+ class Flickr::Tag < Flickr::Base
2
+ attr_accessor :value
3
+
4
+ def initialize(tag)
5
+ @value = tag
6
+ end
7
+
8
+ def self.list(user_id)
9
+ (Flickr::Query.new(user_id).execute('flickr.tags.getListUser')/:tag).map { |tag|
10
+ self.new(tag.inner_text.to_s) if tag.inner_text.to_s =~ /^([a-zA-Z0-9_-]+)$/
11
+ }.compact
12
+ end
13
+ end
@@ -0,0 +1,40 @@
1
+ class Flickr::User < Flickr::Base
2
+ attr_accessor :username, :nsid
3
+
4
+ def initialize username, nsid
5
+ @username, @nsid = username, nsid
6
+ end
7
+
8
+ #
9
+ # ==== Class methods
10
+ #
11
+
12
+ def self.find_by_email email
13
+ result = Flickr::Query.new('').execute('flickr.people.findByEmail', :find_email => email)
14
+ return Flickr::User.new(result.at(:username).inner_text, result.at(:user)['nsid'])
15
+ end
16
+
17
+ #
18
+ # ==== Instance methods
19
+ #
20
+
21
+ def realname
22
+ get_info.at(:realname).inner_text
23
+ end
24
+
25
+ # The psyical location string as entered in flickr
26
+ def location
27
+ get_info.at(:location).inner_text || "Unknown"
28
+ end
29
+
30
+ #
31
+ # ==== Private methods
32
+ #
33
+
34
+ private
35
+ # Get info for the user, once and once only
36
+ def get_info
37
+ @info = Flickr::Query.new(@nsid).execute('flickr.people.getInfo', :user_id => nsid) unless @info
38
+ @info
39
+ end
40
+ end
@@ -0,0 +1,11 @@
1
+ # Dependencies
2
+ require 'rubygems'
3
+ require 'flickr-rest'
4
+ require 'time'
5
+ require 'validatable'
6
+
7
+ # Namespace junkie
8
+ module Flickr; end
9
+
10
+ # Classes
11
+ %w(base photoset photo tag machine_tag user).each {|r| require File.join(File.dirname(__FILE__), 'flickr-wrapper', r)}
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: benschwarz-flickr-wrapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ben Schwarz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-05-10 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: flickr-rest
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.0.1
23
+ version:
24
+ description: A grassroots wrapper around flickr - Strictly for convenience
25
+ email: ben@germanforblack.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files: []
31
+
32
+ files:
33
+ - README
34
+ - flickr-wrapper.gemspec
35
+ - lib/flickr-wrapper.rb
36
+ - lib/flickr-wrapper/base.rb
37
+ - lib/flickr-wrapper/machine_tag.rb
38
+ - lib/flickr-wrapper/photo.rb
39
+ - lib/flickr-wrapper/photoset.rb
40
+ - lib/flickr-wrapper/tag.rb
41
+ - lib/flickr-wrapper/user.rb
42
+ has_rdoc: false
43
+ homepage: http://github.com/benschwarz/flickr-wrapper
44
+ post_install_message:
45
+ rdoc_options: []
46
+
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.0.1
65
+ signing_key:
66
+ specification_version: 2
67
+ summary: A grassroots wrapper for flickr
68
+ test_files: []
69
+