-
Notifications
You must be signed in to change notification settings - Fork 0
/
flickr_upload.rb
189 lines (137 loc) · 5.28 KB
/
flickr_upload.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#
# Automated upload to Flickr
# jssmk @ Helsinki Hacklab
#
# arranges photos in
# albums by date
#
# Start with editing your locals.rb file, use locals_default.rb as a template
require_relative 'locals'
pic_list = Dir[PIC_path+"**/**{.JPG,.jpg}"].reject { |p| p.index(PIC_exclude_prefix) }
png_list = Dir[PIC_path+"**/**{.PNG,.png}"].reject { |p| p.index(PIC_exclude_prefix) }
# if pic_list is empty, no need to do anything else, just exit
if pic_list.empty? and png_list.empty?
exit
end
require 'logger'
# Logfile
if not File.exists?(LOG_path+"flickr_upload_log.txt")
File.open(LOG_path+"flickr_upload_log.txt", "w") do |f| f.write "New log" end
end
$file_for_logging = File.open(LOG_path+"flickr_upload_log.txt", File::WRONLY || File::APPEND)
$mylog = Logger.new($file_for_logging)
$mylog.info("Start ---->")
$mylog.info("Number of new pics to upload: "+pic_list.length.to_s)
# Remove all pictures that are 300 days old
remove_time = Time.now()-60*60*24*300 # 300 days from here
pic_remove = Dir[PIC_path+"**/**{.JPG,.jpg}"].select { |p| File.mtime(p) < remove_time } # Filter by modified date
if pic_remove.any?
$mylog.info("Removing "+pic_remove.count.to_s+" files")
pic_remove.each do |p| File.delete(p) end # delete all old pictures
end
require 'flickraw'
require 'exifr'
require 'exifr/jpeg'
require 'lockfile'
# Try to make sure we upload photos only once
Lockfile.new ("/tmp/flickr_upload.lock") do
# API key
FlickRaw.api_key = MY_api_key
FlickRaw.shared_secret = MY_shared_secret
### uncomment below if you yet have no token
#token = flickr.get_request_token
#auth_url = flickr.get_authorize_url(token['oauth_token'], :perms => 'delete')
#
#puts "Open this url in your process to complete the authication process : #{auth_url}"
#puts "Copy here the number given when you complete the process."
#verify = gets.strip
#
#begin
# flickr.get_access_token(token['oauth_token'], token['oauth_token_secret'], verify)
# login = flickr.test.login
# puts "You are now authenticated as #{login.username} with token #{flickr.access_token} and secret #{flickr.access_secret}"
#rescue FlickRaw::FailedResponse => e
# puts "Authentication failed : #{e.msg}"
#end
############
### use when you have your token
flickr.access_token = MY_access_token
flickr.access_secret = MY_access_secret
# Login
begin
login = flickr.test.login
rescue TypeError, NameError => e
$mylog.error("Login failed: #{e}")
exit
end
$mylog.info("You are authenticated as #{login.username}")
# List of albums in my Flickr account
$album_list = nil
def refresh_album_list()
$album_list = flickr.photosets.getList
end
refresh_album_list()
def upload_pic(pic, pic_name, album_name)
pic_dirname = File.dirname(pic)
pic_basename = File.basename(pic)
## Upload
begin
# try uploading the pic
$mylog.info("Start uploading: "+pic_basename)
# upload_photo returns Flickr photo id
pic_result = flickr.upload_photo pic, :title => pic_name, :description => PIC_default_desc
rescue FlickRaw::FailedResponse => e
$mylog.error("Photo upload failed: #{e.msg}")
return
end
$mylog.info("Pic "+pic_basename+" uploaded")
# mark this file now uploaded using a prefix
File.rename(pic, pic_dirname+'/'+PIC_exclude_prefix+pic_basename)
## Tags
begin
# Add tags to newly uploaded photo
flickr.photos.addTags api_key: FlickRaw.api_key, photo_id: pic_result, tags: PIC_default_tags
rescue FlickRaw::FailedResponse => e
$mylog.error("Photo tagging failed: #{e.msg}")
end
## Handle albums
# return first occurrence of an album with title '%Y-%m-%d'
dest_album = $album_list.detect{|a| a.title == album_name}
if dest_album != nil
# album already exists
$mylog.info("Adding pic to album "+dest_album.title)
begin
flickr.photosets.addPhoto photoset_id: dest_album.id, photo_id: pic_result
rescue FlickRaw::FailedResponse => e
$mylog.error("Adding to album failed: #{e.msg}")
end
else
# album does not yet exist
$mylog.info("Creating new album: "+album_name)
begin
flickr.photosets.create api_key: FlickRaw.api_key, title: album_name, :primary_photo_id => pic_result
rescue FlickRaw::FailedResponse => e
$mylog.error("Creating album failed: #{e.msg}")
end
sleep(5) # It takes some time before the new album is available through API
refresh_album_list() # we now have a new album available, refresh the list
end
end
# Upload the pics so that photostream shows new photos first
pic_list.sort! {|left, right| EXIFR::JPEG.new(left).date_time <=> EXIFR::JPEG.new(right).date_time }
for pic in pic_list
# Use exif data to rename both picture and album titles
# Reduce 3h from album dates, so that pics taken before 3 am. are sorted in same Flickr album with pics taken at same evening/night
album_name = (EXIFR::JPEG.new(pic).date_time - (60 * 60 * 3)).strftime('%Y-%m-%d')
pic_name = EXIFR::JPEG.new(pic).date_time.strftime('%Y-%m-%d %H:%M:%S')
# Upload the pic, insert to album or create a new one if not existing
upload_pic(pic, pic_name, album_name)
end
for png in png_list
png_name = File.basename(png)
album_name = "Graphs"
# Upload the pic, insert to album or create a new one if not existing
upload_pic(png, png_name, album_name)
end
$mylog.info("----> End")
end # Release lockfile