Here’s a video:
So basically the program indexes every single file on the SD card and then starts a scheduled interval process to upload them every single upload to flickr every 20 seconds. From there, it creates a set and indexes all of the upload responses and then walks though that list and adds each photo to that list. Then it emails the set URL with some other data.
I had a lot of problems with the flickr API timing out, and solved this problem by using APscheduler (easily my favorite package). You can see a more in depth chronicling my struggle on my twitter account.
I really want this project to be polished so I probably won’t be done with it for a while. I want to like 3D print a really nice looking enclosure and use a lot of panel mount components.
Here’s the python script:
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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
#GPIO setup import RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) in_flickr = 11 GPIO.setup(in_flickr, GPIO.IN) in_hdd = 13 GPIO.setup(in_hdd, GPIO.IN) button = 16 GPIO.setup(button, GPIO.IN) flickr_LED = 3 GPIO.setup(flickr_LED, GPIO.OUT) both_LED = 5 GPIO.setup(both_LED, GPIO.OUT) hdd_LED = 7 GPIO.setup(hdd_LED, GPIO.OUT) uploading_LED = 26 GPIO.setup(uploading_LED, GPIO.OUT) ready_LED = 24 GPIO.setup(ready_LED, GPIO.OUT) stat_LED = 22 GPIO.setup(stat_LED, GPIO.OUT) import os from apscheduler.scheduler import Scheduler import logging logging.basicConfig() import time #Flickr Setup import flickrapi api_key = '' api_secret = '' flickr = flickrapi.FlickrAPI(api_key, api_secret, format='json', cache=True) (token, frob) = flickr.get_token_part_one(perms='write') if not token: raw_input("Press ENTER after you authorized this program") flickr.get_token_part_two((token, frob)) #email setup import smtplib def sd_walk(): global file_list global file_number global filename global file_current print '-Starting File Index' for path, subdirs, files in os.walk(image_dir): for filename in files: if os.path.splitext(filename)[1].lower() in ('.jpg','jpeg'): listfiles = os.path.join(path, filename) file_list.insert(file_number,listfiles) print '--File: ' + str(file_number+1) + ' Added To List: ' + str(listfiles) file_number = file_number + 1 print '-Indexing Completed \n' print '---Starting Upload' def upload_file(): global file_list global file_number global filename global listfiles global file_current if file_current == int(len(file_list)): print '\n----All Files Dealt With...Terminating \n' global scheduler scheduler.shutdown(shutdown_threadpool=False) else: print '----Uploaing File: ' + str(file_current+1) + ' Of: ' + str(len(file_list)) + ' : ' + file_list[file_current] upload_response = flickr.upload(filename = file_list[file_current], format='etree') upload_ID = upload_response.find('photoid').text print '------Uploaded - Photo ID: ' + upload_ID id_list.insert(file_current,upload_ID) file_current = file_current + 1 def flickr_upload(): global id_list id_list = [] global file_list file_list = [] global file_current file_current = 0 global file_number file_number = 0 sd_walk() global scheduler scheduler = Scheduler(standalone=True) scheduler.add_interval_job(upload_file,seconds=20) scheduler.start() print '---Uploading completed - Adding Files To Set' set_name = 'Uploaded At ' + time.strftime('%m-%d-%y_%H-%M-%S') print '\n----Creating Set: ' + set_name print '----Primary Photo: ' + id_list[0] json_string = flickr.photosets_create(title=set_name, primary_photo_id=id_list[0]) global set_id set_id = json_string.split('"')[5] print '----Set Created: ' + set_id print '----Adding Files To list' global flickr_setno flickr_setno = 0 global scheduler scheduler = Scheduler(standalone=True) scheduler.add_interval_job(flickr_addset,seconds=1) scheduler.start() print '---All Photos Added, Flickr Process Complete \n' flickr_email(set_id,file_current,set_name) def flickr_addset(): global id_list global set_id global flickr_setno if flickr_setno == int(len(id_list)): print '\n----All IDs Dealt With...Terminating \n' global scheduler scheduler.shutdown(shutdown_threadpool=False) else: flickr.photosets_addPhoto(photoset_id=set_id, photo_id=id_list[flickr_setno]) print '-----Photo: ' + str(flickr_setno+1) + ' Of ' + str(len(id_list)) + ' Added To Set: ' + str(set_id) + ' ID: ' + str(id_list[flickr_setno]) flickr_setno = flickr_setno + 1 def flickr_email(idd,total_files,name): fromaddr = '' toaddrs = '' username = '' password = '' server = smtplib.SMTP('smtp.gmail.com:587') server.ehlo() server.starttls() server.ehlo() server.login(username,password) print 'Sending Email' SUBJECT = 'Your Photos Have Been Uploaded!' TEXT = 'Hello!\n\nYou Uploaded a total of: ' + str(total_files) + '\nYour Set is Named: "' + str(name) + '" \nYou can View These Photos Here: \n\n http://www.flickr.com/photos/99154806@N04/sets/' + str(idd) msg = 'Subject: %s\n\n%s' % (SUBJECT, TEXT) server.sendmail(fromaddr, toaddrs, msg) time.sleep(10) server.quit print 'Email Sent \n' while 1: GPIO.output(ready_LED, True) GPIO.output(uploading_LED, False) GPIO.output(stat_LED, False) if GPIO.input(in_flickr): #print "left" GPIO.output(flickr_LED, True) GPIO.output(both_LED, False) GPIO.output(hdd_LED, False) elif GPIO.input(in_hdd): #print "right" GPIO.output(flickr_LED, False) GPIO.output(both_LED, False) GPIO.output(hdd_LED, True) else: #print "mid" GPIO.output(flickr_LED, False) GPIO.output(both_LED, True) GPIO.output(hdd_LED, False) if GPIO.input(button): GPIO.output(uploading_LED, True) GPIO.output(ready_LED, False) print '======Start=====' print 'Mounting SD' time.sleep(1) global image_dir image_dir = '/mnt/SD/' os.system('mount -t vfat /dev/sda1/ ' + image_dir) print 'SD Mounted' if GPIO.input(in_flickr): flickr_upload() print 'SD Unmouting' time.sleep(10) os.system('umount -t vfat /dev/sda1/ ' + image_dir) print 'SD Unounted' print '======End======' GPIO.output(uploading_LED, False) GPIO.output(ready_LED, True) elif GPIO.input(in_hdd): hdd_upload() print 'SD Unmouting' time.sleep(10) os.system('umount -t vfat /dev/sda1/ ' + image_dir) print 'SD Unounted' print '======End======' GPIO.output(uploading_LED, False) GPIO.output(ready_LED, True) else: both_upload() print 'SD Unmouting' time.sleep(10) os.system('umount -t vfat /dev/sda1/ ' + image_dir) print 'SD Unounted' print '======End======' GPIO.output(uploading_LED, False) GPIO.output(ready_LED, True) |
Thanks for reading!