flickrie 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,254 @@
1
+ require 'flickrie/media/visibility'
2
+ require 'flickrie/media/note'
3
+ require 'flickrie/location'
4
+ require 'date'
5
+
6
+ module Flickrie
7
+ module Media
8
+ def id; @info['id'] end
9
+ def secret; @info['secret'] end
10
+ def server; @info['server'] end
11
+ def farm; @info['farm'] end
12
+ def title; @info['title'] end
13
+ def description; @info['description'] end
14
+ def tags; @info['tags'] end
15
+ def machine_tags; @info['machine_tags'] end
16
+ def media_status; @info['media_status'] end
17
+ def path_alias; @info['pathalias'] end
18
+
19
+ def views_count
20
+ @info['views'].to_i if @info['views']
21
+ end
22
+
23
+ def comments_count
24
+ @info['comments_count'].to_i if @info['comments_count']
25
+ end
26
+
27
+ def location
28
+ Location.new(@info['location']) if @info['location']
29
+ end
30
+
31
+ def geo_permissions
32
+ if @info['geoperms']
33
+ Visibility.new \
34
+ *[@info['geoperms']['ispublic'],
35
+ @info['geoperms']['isfriend'],
36
+ @info['geoperms']['isfamily'],
37
+ @info['geoperms']['iscontact']]
38
+ end
39
+ end
40
+
41
+ def license
42
+ License.new(@info['license']) if @info['license']
43
+ end
44
+
45
+ def posted_at; Time.at(@info['dates']['posted'].to_i) if @info['dates']['posted'] end
46
+ def uploaded_at; Time.at(@info['dates']['uploaded'].to_i) if @info['dates']['uploaded'] end
47
+ def updated_at; Time.at(@info['dates']['lastupdate'].to_i) if @info['dates']['lastupdate'] end
48
+ def taken_at; DateTime.parse(@info['dates']['taken']).to_time if @info['dates']['taken'] end
49
+
50
+ def taken_at_granularity
51
+ @info['dates']['takengranularity'].to_i if @info['dates']['takengranularity']
52
+ end
53
+
54
+ def owner
55
+ User.new(@info['owner']) if @info['owner']
56
+ end
57
+
58
+ def safety_level; @info['safety_level'].to_i if @info['safety_level'] end
59
+
60
+ def safe?; safety_level <= 1 if safety_level end
61
+ def moderate?; safety_level == 2 if safety_level end
62
+ def restricted?; safety_level == 3 if safety_level end
63
+
64
+ def url
65
+ if owner and id
66
+ "http://www.flickr.com/photos/#{owner.nsid}/#{id}"
67
+ end
68
+ end
69
+
70
+ def visibility
71
+ if @info['visibility']
72
+ Visibility.new \
73
+ *[@info['visibility']['ispublic'],
74
+ @info['visibility']['isfriend'],
75
+ @info['visibility']['isfamily']]
76
+ end
77
+ end
78
+
79
+ def primary?; @info['isprimary'].to_i == 1 if @info['isprimary'] end
80
+
81
+ def favorite?; @info['isfavorite'].to_i == 1 if @info['isfavorite'] end
82
+
83
+ def can_comment?; @info['editability']['cancomment'].to_i == 1 if @info['editability'] end
84
+ def can_add_meta?; @info['editability']['canaddmeta'].to_i == 1 if @info['editability'] end
85
+
86
+ def can_everyone_comment?
87
+ @info['publiceditability']['cancomment'].to_i == 1 if @info['publiceditability']
88
+ end
89
+
90
+ def can_everyone_add_meta?
91
+ @info['publiceditability']['canaddmeta'].to_i == 1 if @info['publiceditability']
92
+ end
93
+
94
+ def can_download?; @info['usage']['candownload'].to_i == 1 if @info['usage']['candownload'] end
95
+ def can_blog?; @info['usage']['canblog'].to_i == 1 if @info['usage']['canblog'] end
96
+ def can_print?; @info['usage']['canprint'].to_i == 1 if @info['usage']['canprint'] end
97
+ def can_share?; @info['usage']['canshare'].to_i == 1 if @info['usage']['canshare'] end
98
+
99
+ def has_people?; @info['people']['haspeople'].to_i == 1 if @info['people'] end
100
+
101
+ def notes
102
+ @info['notes']['note'].map { |hash| Note.new(hash) } if @info['notes']
103
+ end
104
+
105
+ def get_info(info = nil)
106
+ info ||= Flickrie.client.get_media_info(id).body['photo']
107
+
108
+ # Fixes
109
+ info['title'] = info['title']['_content']
110
+ info['description'] = info['description']['_content']
111
+ info['comments_count'] = info.delete('comments')['_content']
112
+ info['dates']['uploaded'] = info.delete('dateuploaded')
113
+ info['machine_tags'] = info['tags']['tag'].
114
+ select { |tag| tag['machine_tag'].to_i == 1 }.
115
+ map { |tag| tag['_content']}.join(' ')
116
+ info['tags'] = info['tags']['tag'].
117
+ select { |tag| tag['machine_tag'].to_i == 0 }.
118
+ map { |tag| tag['_content']}.join(' ')
119
+
120
+ @info.update(info)
121
+ self
122
+ end
123
+
124
+ def initialize(info = {})
125
+ @info = info
126
+ @info['dates'] ||= {}
127
+ @info['usage'] ||= {}
128
+ end
129
+
130
+ module ClassMethods
131
+ def from_set(hash)
132
+ hash['photo'].map do |info|
133
+ info['owner'] = {
134
+ 'nsid' => hash['owner'],
135
+ 'username' => hash['ownername'],
136
+ 'iconserver' => info.delete('iconserver'),
137
+ 'iconfarm' => info.delete('iconfarm')
138
+ }
139
+ if info['place_id']
140
+ geo_info = %w[latitude longitude accuracy context place_id woeid]
141
+ info['location'] = geo_info.inject({}) do |location, geo|
142
+ location.update(geo => info.delete(geo))
143
+ end
144
+ info['geoperms'] = {
145
+ 'isfamily' => info['geo_is_family'],
146
+ 'isfriend' => info['geo_is_friend'],
147
+ 'iscontact' => info['geo_is_contact'],
148
+ 'ispublic' => info['geo_is_public']
149
+ }
150
+ end
151
+ info['dates'] = {
152
+ 'uploaded' => info.delete('dateupload'),
153
+ 'lastupdate' => info.delete('lastupdate'),
154
+ 'taken' => info.delete('datetaken'),
155
+ 'takengranularity' => info.delete('datetakengranularity'),
156
+ }
157
+ info['usage'] = {}
158
+
159
+ new(info)
160
+ end
161
+ end
162
+
163
+ def from_info(info)
164
+ new('media' => info['media']).get_info(info)
165
+ end
166
+
167
+ def from_user(hash)
168
+ hash['owner'] = hash['photo'].first['owner']
169
+ hash['ownername'] = hash['photo'].first['ownername']
170
+ hash['photo'].each do |info|
171
+ info['visibility'] = {
172
+ 'ispublic' => info.delete('ispublic'),
173
+ 'isfriend' => info.delete('isfriend'),
174
+ 'isfamily' => info.delete('isfamily')
175
+ }
176
+ end
177
+
178
+ from_set(hash)
179
+ end
180
+
181
+ def from_sizes(info)
182
+ new.get_sizes(info)
183
+ end
184
+
185
+ def from_search(info)
186
+ from_user(info)
187
+ end
188
+ end
189
+ extend(ClassMethods)
190
+
191
+ def self.included(klass)
192
+ klass.extend(ClassMethods)
193
+ end
194
+
195
+ def self.new(info)
196
+ eval(info['media'].capitalize).new(info)
197
+ end
198
+ end
199
+ end
200
+
201
+ __END__
202
+
203
+
204
+ {
205
+ "id"=>"6923154272",
206
+ "secret"=>"5519fab554",
207
+ "server"=>"5279",
208
+ "farm"=>6,
209
+ "dateuploaded"=>"1334189525",
210
+ "isfavorite"=>0,
211
+ "license"=>"0",
212
+ "safety_level"=>"0",
213
+ "rotation"=>0,
214
+ "owner"=>
215
+ {"nsid"=>"67131352@N04",
216
+ "username"=>"Janko Marohnić",
217
+ "realname"=>"",
218
+ "location"=>"",
219
+ "iconserver"=>"0",
220
+ "iconfarm"=>0},
221
+ "title"=>{"_content"=>"David Belle - Canon commercial"},
222
+ "description"=>{"_content"=>""},
223
+ "visibility"=>{"ispublic"=>1, "isfriend"=>0, "isfamily"=>0},
224
+ "dates"=>
225
+ {"posted"=>"1334189525",
226
+ "taken"=>"2012-04-11 17:12:05",
227
+ "takengranularity"=>"0",
228
+ "lastupdate"=>"1334259651"},
229
+ "views"=>"1",
230
+ "editability"=>{"cancomment"=>0, "canaddmeta"=>0},
231
+ "publiceditability"=>{"cancomment"=>1, "canaddmeta"=>0},
232
+ "usage"=>{"candownload"=>1, "canblog"=>0, "canprint"=>0, "canshare"=>0},
233
+ "comments"=>{"_content"=>"0"},
234
+ "notes"=>{"note"=>[]},
235
+ "people"=>{"haspeople"=>0},
236
+ "tags"=>
237
+ {"tag"=>
238
+ [{"id"=>"67099213-6923154272-471",
239
+ "author"=>"67131352@N04",
240
+ "raw"=>"David",
241
+ "_content"=>"david",
242
+ "machine_tag"=>0},
243
+ {"id"=>"67099213-6923154272-18012",
244
+ "author"=>"67131352@N04",
245
+ "raw"=>"Belle",
246
+ "_content"=>"belle",
247
+ "machine_tag"=>0}]},
248
+ "location"=> {...}
249
+ "geoperms"=>{"ispublic"=>1, "iscontact"=>0, "isfriend"=>0, "isfamily"=>0},
250
+ "urls"=>
251
+ {"url"=>
252
+ [{"type"=>"photopage",
253
+ "_content"=>"http://www.flickr.com/photos/67131352@N04/6923154272/"}]},
254
+ }
@@ -0,0 +1,109 @@
1
+ module Flickrie
2
+ class Photo
3
+ include Media
4
+
5
+ attr_reader :size
6
+
7
+ SIZES = {
8
+ 'Square 75' => 'sq',
9
+ 'Square 150' => 'q',
10
+ 'Thumbnail' => 't',
11
+ 'Small 240' => 's',
12
+ 'Small 320' => 'n',
13
+ 'Medium 500' => 'm',
14
+ 'Medium 640' => 'z',
15
+ 'Medium 800' => 'c',
16
+ 'Large 1024' => 'l',
17
+ 'Original' => 'o'
18
+ }
19
+
20
+ def square!(number); @size = "Square #{number}"; self end
21
+ def thumbnail!; @size = "Thumbnail"; self end
22
+ def small!(number); @size = "Small #{number}"; self end
23
+ def medium!(number); @size = "Medium #{number}"; self end
24
+ def large!(number); @size = "Large #{number}"; self end
25
+ def original!; @size = "Original"; self end
26
+
27
+ def square(number); dup.square!(number) end
28
+ def thumbnail; dup.thumbnail! end
29
+ def small(number); dup.small!(number) end
30
+ def medium(number); dup.medium!(number) end
31
+ def large(number); dup.large!(number) end
32
+ def original; dup.original! end
33
+
34
+ [75, 150].each do |n|
35
+ define_method("square#{n}!") { square!(n) }
36
+ define_method("square#{n}") { square(n) }
37
+ end
38
+ [240, 320].each do |n|
39
+ define_method("small#{n}!") { small!(n) }
40
+ define_method("small#{n}") { small(n) }
41
+ end
42
+ [500, 640, 800].each do |n|
43
+ define_method("medium#{n}!") { medium!(n) }
44
+ define_method("medium#{n}") { medium(n) }
45
+ end
46
+ [1024].each do |n|
47
+ define_method("large#{n}!") { large!(n) }
48
+ define_method("large#{n}") { large(n) }
49
+ end
50
+
51
+ def largest!; @size = largest_size; self end
52
+ def largest; dup.largest! end
53
+
54
+ def available_sizes
55
+ SIZES.select { |_, s| @info["url_#{s}"] }.keys
56
+ end
57
+
58
+ def width; @info["width_#{size_abbr}"].to_i if @info["width_#{size_abbr}"] end
59
+ def height; @info["height_#{size_abbr}"].to_i if @info["height_#{size_abbr}"] end
60
+
61
+ def source_url; @info["url_#{size_abbr}"] end
62
+
63
+ def rotation; @info['rotation'].to_i if @info['rotation'] end
64
+
65
+ def get_sizes(info = nil)
66
+ info ||= Flickrie.client.get_media_sizes(id).body['sizes']
67
+ @info['usage'].update \
68
+ 'canblog' => info['canblog'],
69
+ 'canprint' => info['canprint'],
70
+ 'candownload' => info['candownload']
71
+ flickr_sizes = {
72
+ 'Square' => 'sq',
73
+ 'Large Square' => 'q',
74
+ 'Thumbnail' => 't',
75
+ 'Small' => 's',
76
+ 'Small 320' => 'n',
77
+ 'Medium' => 'm',
78
+ 'Medium 640' => 'z',
79
+ 'Medium 800' => 'c',
80
+ 'Large' => 'l',
81
+ 'Original' => 'o'
82
+ }
83
+ info['size'].each do |size_info|
84
+ size_abbr = flickr_sizes[size_info['label']]
85
+ @info["width_#{size_abbr}"] = size_info['width']
86
+ @info["height_#{size_abbr}"] = size_info['height']
87
+ @info["url_#{size_abbr}"] = size_info['source']
88
+ end
89
+
90
+ @size = largest_size
91
+ self
92
+ end
93
+
94
+ private
95
+
96
+ def initialize(info = {}, size = nil)
97
+ super(info)
98
+ @size = size || largest_size
99
+ end
100
+
101
+ def largest_size
102
+ available_sizes.last
103
+ end
104
+
105
+ def size_abbr
106
+ SIZES[size]
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,89 @@
1
+ module Flickrie
2
+ class Set
3
+ def id; @info['id'] end
4
+ def secret; @info['secret'] end
5
+ def server; @info['server'] end
6
+ def farm; @info['farm'] end
7
+ def title; @info['title'] end
8
+ def description; @info['description'] end
9
+
10
+ def primary_media_id; @info['primary'] end
11
+ alias primary_photo_id primary_media_id
12
+ alias primary_video_id primary_media_id
13
+
14
+ def views_count; @info['count_views'].to_i if @info['count_views'] end
15
+ def comments_count; @info['count_comments'].to_i if @info['count_comments'] end
16
+ def photos_count; @info['count_photos'].to_i if @info['count_photos'] end
17
+ def videos_count; @info['count_videos'].to_i if @info['count_videos'] end
18
+ def media_count; photos_count + videos_count rescue nil end
19
+
20
+ def owner; User.new('nsid' => @info['owner']) if @info['owner'] end
21
+
22
+ def photos; Flickrie.photos_from_set(id) end
23
+ def videos; Flickrie.videos_from_set(id) end
24
+ def media; Flickrie.media_from_set(id) end
25
+
26
+ def can_comment?; @info['can_comment'].to_i == 1 if @info['can_comment'] end
27
+
28
+ # TODO: Figure out what this is
29
+ def needs_interstitial?; @info['needs_interstitial'].to_i == 1 end
30
+ def visibility_can_see_set?; @info['visibility_can_see_set'].to_i == 1 end
31
+
32
+ def created_at; Time.at(@info['date_create'].to_i) end
33
+ def updated_at; Time.at(@info['date_update'].to_i) end
34
+
35
+ def url; "http://www.flickr.com/photos/#{owner.nsid}/sets/#{id}" end
36
+
37
+ def get_info(info = nil)
38
+ info ||= Flickrie.client.get_set_info(id).body['photoset']
39
+ info['title'] = info['title']['_content']
40
+ info['description'] = info['description']['_content']
41
+ @info.update(info)
42
+
43
+ self
44
+ end
45
+
46
+ private
47
+
48
+ def initialize(info = {})
49
+ @info = info
50
+ end
51
+
52
+ def self.from_info(info)
53
+ new.get_info(info)
54
+ end
55
+
56
+ def self.from_user(info, user_nsid)
57
+ info.map do |info|
58
+ info['count_photos'] = info.delete('photos')
59
+ info['count_videos'] = info.delete('videos')
60
+ info['title'] = info['title']['_content']
61
+ info['description'] = info['description']['_content']
62
+ info['owner'] = user_nsid
63
+
64
+ new(info)
65
+ end
66
+ end
67
+ end
68
+ end
69
+
70
+ __END__
71
+
72
+ {
73
+ "id"=>"72157629443464020",
74
+ "primary"=>"6913731566",
75
+ "secret"=>"23879c079a",
76
+ "server"=>"7130",
77
+ "farm"=>8,
78
+ "photos"=>"1",
79
+ "videos"=>0,
80
+ "title"=>{"_content"=>"Bla"},
81
+ "description"=>{"_content"=>""},
82
+ "needs_interstitial"=>0,
83
+ "visibility_can_see_set"=>1,
84
+ "count_views"=>"0",
85
+ "count_comments"=>"0",
86
+ "can_comment"=>0,
87
+ "date_create"=>"1334331151",
88
+ "date_update"=>"1334331155
89
+ }
@@ -0,0 +1,84 @@
1
+ require 'date'
2
+
3
+ module Flickrie
4
+ class User
5
+ def id; @info['id'] end
6
+ def nsid; @info['nsid'] end
7
+ def username; @info['username'] end
8
+ def real_name; @info['realname'] end
9
+ def location; @info['location'] end
10
+ def time_zone; @info['timezone'] end
11
+ def description; @info['description'] end
12
+ def profile_url; @info['profileurl'] end
13
+ def mobile_url; @info['mobileurl'] end
14
+ def photos_url; @info['photosurl'] end
15
+ def path_alias; @info['path_alias'] end
16
+ def icon_server; @info['iconserver'] end
17
+ def icon_farm; @info['iconfarm'] end
18
+
19
+ def buddy_icon_url
20
+ if icon_farm
21
+ if icon_server.to_i > 0
22
+ "http://farm{#{icon_farm}}.staticflickr.com/{#{icon_server}}/buddyicons/#{nsid}.jpg"
23
+ else
24
+ "http://www.flickr.com/images/buddyicon.jpg"
25
+ end
26
+ end
27
+ end
28
+
29
+ def first_taken
30
+ if @info['photos'] and @info['photos']['firstdatetaken']
31
+ DateTime.parse(@info['photos']['firstdatetaken']).to_time
32
+ end
33
+ end
34
+
35
+ def first_uploaded
36
+ if @info['photos'] and @info['photos']['firstdate']
37
+ Time.at(@info['photos']['firstdate'].to_i)
38
+ end
39
+ end
40
+
41
+ def media_count
42
+ if @info['photos'] and @info['photos']['count']
43
+ @info['photos']['count'].to_i
44
+ end
45
+ end
46
+
47
+ def public_photos; Flickrie.public_photos_from_user(nsid) end
48
+
49
+ def pro?
50
+ @info['ispro'].to_i == 1 if @info['ispro']
51
+ end
52
+
53
+ def get_info(info = nil)
54
+ info ||= Flickrie.client.get_user_info(nsid).body['person']
55
+ @info.update(info)
56
+
57
+ # Fixes
58
+ %w[username realname location description profileurl
59
+ mobileurl photosurl].each do |attribute|
60
+ @info[attribute] = @info[attribute]['_content']
61
+ end
62
+ %w[count firstdatetaken firstdate].each do |photo_attribute|
63
+ @info['photos'][photo_attribute] = @info['photos'][photo_attribute]['_content']
64
+ end
65
+
66
+ self
67
+ end
68
+
69
+ private
70
+
71
+ def initialize(info = {})
72
+ @info = info
73
+ end
74
+
75
+ def self.from_info(info)
76
+ new.get_info(info)
77
+ end
78
+
79
+ def self.from_find(info)
80
+ info['username'] = info['username']['_content']
81
+ new(info)
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,3 @@
1
+ module Flickrie
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,62 @@
1
+ module Flickrie
2
+ class Video
3
+ include Media
4
+
5
+ def ready?; @video['ready'].to_i == 1 if @video['ready'] end
6
+ def failed?; @video['failed'].to_i == 1 if @video['failed'] end
7
+ def pending?; @video['pending'].to_i == 1 if @video['pending'] end
8
+
9
+ def duration; @video['duration'].to_i if @video['duration'] end
10
+
11
+ def width; @video['width'].to_i if @video['width'] end
12
+ def height; @video['height'].to_i if @video['height'] end
13
+
14
+ def source_url; @video['source_url'] end
15
+ def download_url; @video['download_url'] end
16
+ def mobile_download_url; @video['mobile_download_url'] end
17
+
18
+ def get_sizes(info = nil)
19
+ info ||= Flickrie.client.get_media_sizes(id).body['sizes']
20
+ @info['usage'].update \
21
+ 'canblog' => info['canblog'],
22
+ 'canprint' => info['canprint'],
23
+ 'candownload' => info['candownload']
24
+ info['size'].each do |hash|
25
+ case hash['label']
26
+ when 'Video Player' then @video['source_url'] = hash['source']
27
+ when 'Site MP4' then @video['download_url'] = hash['source']
28
+ when 'Mobile MP4' then @video['mobile_download_url'] = hash['source']
29
+ end
30
+ end
31
+
32
+ self
33
+ end
34
+
35
+ def get_info(info = nil)
36
+ super
37
+ @video = @info['video']
38
+
39
+ self
40
+ end
41
+
42
+ private
43
+
44
+ def initialize(info = {})
45
+ super
46
+ @video = {}
47
+ end
48
+ end
49
+ end
50
+
51
+ __END__
52
+
53
+ {
54
+ ...
55
+ "video"=>
56
+ {"ready"=>1,
57
+ "failed"=>0,
58
+ "pending"=>0,
59
+ "duration"=>"34",
60
+ "width"=>"480",
61
+ "height"=>"360"}
62
+ }