Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 171 lines (140 sloc) 4.17 kb
8b923ae @funkaoshi No longer need to use vendor'd sinatra.
authored
1 require 'sinatra'
a1be805 @funkaoshi Initial commit. Sort of.
authored
2 require 'flickraw'
3 require 'haml'
4
b4a8681 Use the new 640px wide images when they are available.
Ramanan Sivaranjan authored
5 # Models and the like
5dc51cc @funkaoshi Created an ImageListing class to make controller cleaner.
authored
6
7 class String
8 def is_untitled?
9 return self.empty? || self == '.' || self =~ /IMG_/ || self =~ /DSC_/
566ec28 @funkaoshi Make nicer URLs using path_alias
authored
10 end
11 end
12
a1be805 @funkaoshi Initial commit. Sort of.
authored
13 # Some basic information about an image (on Flickr)
14 class Image
e445c95 @funkaoshi display photographer name and image title on page. tweak layout.
authored
15 attr_accessor :id, :img_url, :flickr_url, :title, :photographer
42afe55 @funkaoshi Basic support for viewing favs.
authored
16
e445c95 @funkaoshi display photographer name and image title on page. tweak layout.
authored
17 def initialize(photo)
b4a8681 Use the new 640px wide images when they are available.
Ramanan Sivaranjan authored
18 @id, @img_url, @flickr_url = photo.id, FlickRaw::url_z(photo), FlickRaw::url_photopage(photo)
e445c95 @funkaoshi display photographer name and image title on page. tweak layout.
authored
19 @photographer = "<a href='#{FlickRaw::url_photostream(photo)}'>#{photo.ownername}</a>"
5dc51cc @funkaoshi Created an ImageListing class to make controller cleaner.
authored
20 @title = photo.title.is_untitled? ? 'untitled' : photo.title
a1be805 @funkaoshi Initial commit. Sort of.
authored
21 end
22 end
23
5dc51cc @funkaoshi Created an ImageListing class to make controller cleaner.
authored
24 # All the information about a page of images that need to be displayed
25 class ImageListing
26 attr_accessor :flickr_id, :title, :page, :pages, :sequence
b4a8681 Use the new 640px wide images when they are available.
Ramanan Sivaranjan authored
27
5dc51cc @funkaoshi Created an ImageListing class to make controller cleaner.
authored
28 def initialize(id, title, page, pages, sequence)
29 @flickr_id, @title, @page, @pages, @sequence = id, title, page, pages, sequence
30 end
31 end
32
33
a1be805 @funkaoshi Initial commit. Sort of.
authored
34 # Sinatra !!
35
36 configure do
ff8e460 @funkaoshi Use my own API key.
authored
37 # Set API Key
38 FlickRaw.api_key = 'd14c1db0be6b1519a09274054a8be802'
39
a1be805 @funkaoshi Initial commit. Sort of.
authored
40 # use HTML5 when generating HTML
41 set :haml, :format => :html5
42
43 # set the last mod time to now, when the app starts up. Updated via /update/now
30cbcbc @funkaoshi remove trailing spaces, add maximum-scale, no more class variables.
authored
44 @last_mod_time = Time.now
42afe55 @funkaoshi Basic support for viewing favs.
authored
45
15ea2c6 @funkaoshi Add code to track stats with google analytics.
authored
46 ## for google analytics
30cbcbc @funkaoshi remove trailing spaces, add maximum-scale, no more class variables.
authored
47 @analytics_token = 'UA-2675737-8'
a1be805 @funkaoshi Initial commit. Sort of.
authored
48 end
49
50 before do
51 unless request.path_info =~ /update/
52 expires 300, :public, :must_revalidate # always cache for 5 minutes ...
30cbcbc @funkaoshi remove trailing spaces, add maximum-scale, no more class variables.
authored
53 last_modified(@last_mod_time) # ... and rely on 304 query after that
a1be805 @funkaoshi Initial commit. Sort of.
authored
54 end
42afe55 @funkaoshi Basic support for viewing favs.
authored
55 end
a1be805 @funkaoshi Initial commit. Sort of.
authored
56
b4a8681 Use the new 640px wide images when they are available.
Ramanan Sivaranjan authored
57 helpers do
5dc51cc @funkaoshi Created an ImageListing class to make controller cleaner.
authored
58 def set_paging(params, page)
59 params[:per_page] = 30 unless page == 0
60 params[:page] = page unless page == 0
61 end
b4a8681 Use the new 640px wide images when they are available.
Ramanan Sivaranjan authored
62
a1be805 @funkaoshi Initial commit. Sort of.
authored
63 # Loads 30 medium images from the flickr group
64 def load_group(group, page)
42afe55 @funkaoshi Basic support for viewing favs.
authored
65 params = { :group_id => group, :extras => 'path_alias, owner_name' }
5dc51cc @funkaoshi Created an ImageListing class to make controller cleaner.
authored
66 set_paging(params, page)
a1be805 @funkaoshi Initial commit. Sort of.
authored
67 begin
68 photos = flickr.groups.pools.getPhotos(params)
69 group_info = flickr.groups.getInfo(:group_id => group)
70 rescue FlickRaw::FailedResponse => e
71 halt 404
72 end
5dc51cc @funkaoshi Created an ImageListing class to make controller cleaner.
authored
73 @list = ImageListing.new(group, group_info.name, page.to_i, photos.pages, build_sequence(photos))
a1be805 @funkaoshi Initial commit. Sort of.
authored
74 end
75
5dc51cc @funkaoshi Created an ImageListing class to make controller cleaner.
authored
76 # Loads 30 medium images from a flickr user
42afe55 @funkaoshi Basic support for viewing favs.
authored
77 def load_favs(user_name, page)
78 user_id = flickr.people.findByUsername(:username => user_name).id
79 params = { :user_id => user_id, :extras => 'path_alias, owner_name' }
5dc51cc @funkaoshi Created an ImageListing class to make controller cleaner.
authored
80 set_paging(params, page)
42afe55 @funkaoshi Basic support for viewing favs.
authored
81 begin
82 photos = flickr.favorites.getPublicList(params)
83 rescue FlickRaw::FailedResponse => e
84 halt 404
85 end
5dc51cc @funkaoshi Created an ImageListing class to make controller cleaner.
authored
86 @list = ImageListing.new(user_name, "#{user_name}'s Favourites", page.to_i, photos.pages, build_sequence(photos))
42afe55 @funkaoshi Basic support for viewing favs.
authored
87 end
88
a1be805 @funkaoshi Initial commit. Sort of.
authored
89 # build list of images.
90 def build_sequence(photos)
91 return [] if photos.nil?
92 photos.map do |photo|
e445c95 @funkaoshi display photographer name and image title on page. tweak layout.
authored
93 Image.new(photo)
a1be805 @funkaoshi Initial commit. Sort of.
authored
94 end
95 end
96
97 def nav_links
4769548 @funkaoshi Get rid of @mode variable, figure out route directly.
authored
98 mode = request.path_info.split('/')[1]
42afe55 @funkaoshi Basic support for viewing favs.
authored
99 next_link =
5dc51cc @funkaoshi Created an ImageListing class to make controller cleaner.
authored
100 if @list.page == @list.pages
42afe55 @funkaoshi Basic support for viewing favs.
authored
101 "Next"
102 else
4769548 @funkaoshi Get rid of @mode variable, figure out route directly.
authored
103 "<a class='next_page' href='/#{mode}/#{@list.flickr_id}?pg=#{@list.page+1}'>Next</a>"
a1be805 @funkaoshi Initial commit. Sort of.
authored
104 end
42afe55 @funkaoshi Basic support for viewing favs.
authored
105 prev_link =
5dc51cc @funkaoshi Created an ImageListing class to make controller cleaner.
authored
106 case @list.page
a1be805 @funkaoshi Initial commit. Sort of.
authored
107 when 1
108 "Prev"
109 when 2
4769548 @funkaoshi Get rid of @mode variable, figure out route directly.
authored
110 "<a class='prev_page' href='/#{mode}/#{@list.flickr_id}'>Prev</a>"
a1be805 @funkaoshi Initial commit. Sort of.
authored
111 else
4769548 @funkaoshi Get rid of @mode variable, figure out route directly.
authored
112 "<a class='prev_page' href='/#{mode}/#{@list.flickr_id}?pg=#{@list.page-1}'>Prev</a>"
a1be805 @funkaoshi Initial commit. Sort of.
authored
113 end
5dc51cc @funkaoshi Created an ImageListing class to make controller cleaner.
authored
114 "#{prev_link} | #{@list.page} of #{@list.pages} | #{next_link}"
a1be805 @funkaoshi Initial commit. Sort of.
authored
115 end
116 end
117
118 # Routes
119
120 get '/' do
121 haml :index
122 end
123
42afe55 @funkaoshi Basic support for viewing favs.
authored
124 get '/favs/:user_id' do |user_name|
125 page = params['pg']
126 page ||= 1
127 load_favs(user_name, page)
128 haml :group
129 end
130
a1be805 @funkaoshi Initial commit. Sort of.
authored
131 get '/group/:group_id/?' do |group|
132 page = params['pg']
133 page ||= 1
134 load_group(group, page)
135 haml :group
136 end
137
138 get '/group/?' do
139 redirect '/'
140 end
141
142 post '/group' do
143 halt 404 unless params['url'] =~ /http:\/\/(?:www.)?flickr.com\/groups\/([\w@]*)\/?/
144 begin
145 group = flickr.urls.lookupGroup(:url => params['url'])
146 rescue FlickRaw::FailedResponse => e
147 halt 404
148 end
149 redirect "/group/#{group.id}"
150 end
151
152 # Lame web-cache thing which I don't think really works.
153
154 get '/update/now' do
30cbcbc @funkaoshi remove trailing spaces, add maximum-scale, no more class variables.
authored
155 @last_mod_time = Time.now
a1be805 @funkaoshi Initial commit. Sort of.
authored
156 end
157
158 get '/update/show' do
30cbcbc @funkaoshi remove trailing spaces, add maximum-scale, no more class variables.
authored
159 "Last-Update: #{@last_mod_time}"
a1be805 @funkaoshi Initial commit. Sort of.
authored
160 end
161
162 # Error Handlers in Production
163
164 not_found do
165 haml :wtf
166 end
167
168 error do
169 haml :wtf
170 end
Something went wrong with that request. Please try again.