Use Hazel to Automatically Upload Photos/Videos to Flickr

From your noodle to other noodles. Talk about ways to get the most from Hazel. Even exchange recipes for the cool rules you've thought up. DO NOT POST YOUR QUESTIONS HERE.

Moderators: Mr_Noodle, Moderators

The following explains how to use Hazel to automatically upload your photos and videos to Flickr. There is some setup involved and you will need to edit some script files so please make sure you are comfortable doing so before you embark.

Pre-requisites:
1) Python must be installed on your Mac
2) You must have a Flickr account (ok, so obviously)

Part 1: Installing the necessary files

Key script files: Download and extract the following zip which includes two python files, upload.py and xmltramp.py. My suggestion is that that you place them in a location where you will also want to store a few other output files that the script generates. For instance, I have a Scripts folder for all my external Hazel scripts where i stored these in a subfolder called Flickr. To work with Hazel, you will also need to copy the xmltramp.py file to the /Library/Python/[your python version - in my case 2.7]/site-packages folder.

Other files: In order to make the script upload video, you will need to download and install Qt-Tools. This program enables the script to check for the length on the video file since Flickr only allows 3 minute videos or less to be uploaded. Once you download it, copy the contents to a folder on your desktop and run the install.sh script. You can do this by opening a terminal window, cd to the location of the script you just copied, and typing: sudo sh install.sh.

Part 2: Getting your authentication keys from Flickr API

Creating the Keys: In order to be able to upload content to Flickr via the API, you need to get a Secret and API key along with an authentication token. This is actually very easy to do. The first step is to tell Flickr you are creating an app so it can give you the two keys. First log into your Flickr account and then go to this address http://www.flickr.com/services/apps/create/noncommercial/ where you will be presented with a screen called the App Garden. Give your app any name and then just type anything for the "What are you building" section. Click both checkboxes and the hit Submit. Flickr now presents you with your Key and Secret. Copy these somewhere as you will need them shortly.

Part 3: Authenticating and creating your token for the first time

Editing your uploadr.py file: Locate the upload.py file you downloaded earlier and open it in a your favorite script editing program (i use TextWrangler). Notice about 50 lines down the section called "Items You will want to change". Here you need to edit the variables to fit your needs. I will briefly go through them -- first the code with the explanation after:
Code: Select all
####################################################################
##  Items you will want to change
####################################################################

#
# Locations to scan for new images and videos
#   
IMAGE_DIRS = [ "" ]
# File we keep the history of uploaded images in.
HISTORY_FILE = "ENTER_PATH_FOR_HISTORY_FILE/uploadr.history"

# SET YOUR FLICKR API INFO. YOU CAN GET YOUR OWN API KEY AND SECRET BY GOING HERE: http://www.flickr.com/services/apps/create/noncommercial/
SECRET_SET = "ENTER_SECRET"
API_KEY_SET = "ENTER_API_KEY"

# Set location of your token file after authenticated for first time
TOKEN_FILE_SET = "ENTER_PATH_FOR_TOKEN_FILE/flickrToken.txt"
          
# The maximum length of a video allowed by flickr in seconds
# video longer than this will not be uploaded.
FLICKR_MOVIE_MAX_LENGTH = 180

# Enable Video Upload
VIDEO_UPLOAD_ENABLED = 1

# Path to QT info app
qtinfo = "/usr/local/bin/qt_info"

# Flickr settings
#
FLICKR = {"title": "",
        "description": "",
        "tags": "",
        "is_public": "0",
        "is_friend": "0",
        "is_family": "1" }
#
#   How often to check for new images to upload  (in seconds )
#
# SLEEP_TIME = 1 * 60

#
#  log file information
#
logger = logging.getLogger("uploadr")
logger.setLevel(logging.DEBUG)
#create file handler and set level to debug
fh = logging.FileHandler("ENTER_PATH_FOR_LOG_FILE/uploadr_run_results.log")
fh.setLevel(logging.DEBUG)

ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)


logger.addHandler(fh)
logger.addHandler(ch)
###################################################################

IMAGE_DIRS -- Leave this blank, as is
HISTORY_FILE -- This file stores the uploaded images and videos so once they have uploaded, they will not be re-uploaded again. Where you see ENTER_PATH_FOR_HISTORY_FILE you want to replace with the directory you want to store this in. Again, I would make the same folder as the uploadr.py file. Leave the /uploadr.history at the end as this is the creation of the file.
SECRET_SET and API_KEY_SET -- Here is where you want to enter the two keys you just created on Flickr.
TOKEN_FILE_SET -- the location of storing the token file. Again, reference the HISTORY_FILE above to create it properly.
FLICKR_MOVIE_MAX_LENGTH = 180 -- Leave this alone
VIDEO_UPLOAD_ENABLED = 1 -- Set to 0 if you do not want to upload video
Under the Flickr Settings, you can set defaults for each file uploaded (tags, description, etc). Also the sharing preferences are available to set as well. Set to 0 to make private and 1 to share with respective group.
fh = logging.FileHandler -- Again, set the path for the logging file. Reference HISTORY_FILE above.

Once you have set these settings above, save the upload.py file. No we need to run for the first time in a terminal window to create the token. Open a terminal window, cd to the location of the uploadr.py file, and type: python upload.py and hit enter. Terminal should display a message that says "Getting new Token" then a browser window should pop up, opening a Flickr page and asking you to confirm authentication of the app you created in the step above. Click Ok, "Ill Authorize IT" and then once you receive confirmation that it has been done, head back to your terminal window where you will see another message waiting that says: "Have you authenticated this application? (Y/N):" Type: Y and hit return. Now, in the location where you set the parameters above in the uploadr.py file, you will see some files created including flickrToken.txt and upload.history.db. You have authenticated and now we can move to Hazel.

Part 4: Setting up Hazel to automatically upload to Flickr

Create your Hazel rule: The next step really depends on your workflow and you can set up any way you want. For these purposes, you can test by adding a test folder with an image and a video to Hazel. Then create a new rule and for the condition, just do: Date Added is after Date Last Matched. Now when that matches, you want to set the following embedded shell script to run. Choose Run shell script and embedded script. Within the embedded script, set the shell to: /usr/bin/python. Now re-open the upload.py script in your editor and select the entire script, copy it, and paste it into the main body of the embedded script. Lastly, we need to make one update to the script within Hazel. The variable:
Code: Select all
IMAGE_DIRS = [ "" ]

you want to change to

Code: Select all
IMAGE_DIRS = [ os.path.dirname(os.path.abspath(sys.argv[1])) ]

This makes Hazel act on the files as they are dropped into the folder vs a hard-coded folder.

Thats it folks. You can obviously use this in a number of ways but now you can leverage Hazel to kick off the automatic uploads (i.e., if you use the Dropbox auto import photos and videos you could also have these files auto-added to Flickr. One note. Be careful with this as if you are going to monitor a bunch of folders, make sure you rule is such that once you create this, Hazel doesn't start auto uploading all your previous images you may have already uploaded to Flickr.
Last edited by dhy8386 on Fri Feb 14, 2014 8:28 am, edited 1 time in total.
dhy8386
 
Posts: 94
Joined: Tue Nov 09, 2010 12:19 pm

FYI - the link to the scripts is actually to a .tiff file.
ilium007
 
Posts: 23
Joined: Sun May 01, 2011 5:42 pm

ilium007 wrote:FYI - the link to the scripts is actually to a .tiff file.


Fixed sorry. Let me know how it goes for you.
dhy8386
 
Posts: 94
Joined: Tue Nov 09, 2010 12:19 pm

I'm kind of a newbie with Hazel and Python. The python script works perfectly for me when I run it from the terminal, but I can't get it to work from Hazel. Here is what the Hazel log says:

2014-04-23 11:33:52.409 hazelworker[9118] [Error] Shell script failed: Error processing shell script on file /Users/Jackie/Desktop/test/IMG_0799.JPG.
2014-04-23 11:33:52.409 hazelworker[9118] Shellscript exited with non-successful status code: 1

Any ideas on what the problem is?
JackieC
 
Posts: 1
Joined: Wed Apr 23, 2014 2:54 pm

Same problem here. The log states:
2015-02-04 19:13:45.540 hazelworker[704] Processing folder flickr_test (forced)
2015-02-04 19:13:47.544 hazelworker[704] test_photo.jpg: Rule Upload_to_Flickr matched.
2015-02-04 19:13:47.939 hazelworker[704] [Error] Shell script failed: Error processing shell script on file /Users/Rich/flickr_test/test_photo.jpg.
2015-02-04 19:13:47.940 hazelworker[704] Shellscript exited with non-successful status code: 1

The uploadr_run_results.log file shows that uploader.py is starting, but the only text is:
*********************************************************
Starting Flickr Uploader 2015-02-04 19:13:47
*********************************************************

JackieC - were you able to resolve this?
richwoodham
 
Posts: 3
Joined: Thu Sep 20, 2012 1:03 pm

Hang on - changing the shell to
/usr/local/bin/python
seems to have done the trick. Log now states:
*********************************************************
Starting Flickr Uploader 2015-02-04 19:52:57
*********************************************************
2 files to upload.
Uploading /Users/Rich/flickr_test/test_photo.jpg at 2015-02-04 19:52:58...
Uploading /Users/Rich/flickr_test/test_vid.mov at 2015-02-04 19:52:59...
Flickr Uploadr Complete!

although I'm yet to see the files in Flickr itself...
richwoodham
 
Posts: 3
Joined: Thu Sep 20, 2012 1:03 pm

Just had another go at this on a different Mac. Similar problem - needed to change the shell to
/usr/local/bin/python
to get it to start uploading files, but files are still not successfully uploading. Hazel log states:
2015-03-05 20:51:23.273 hazelworker[712] DEBUG: == script output ==
*********************************************************
Starting Flickr Uploader 2015-03-05 20:51:10
*********************************************************
2 files to upload.
Uploading /Users/richardwoodham/Desktop/Flickr_test/test_file.jpg at 2015-03-05 20:51:13...
Uploading /Users/richardwoodham/Desktop/Flickr_test/test_file2.jpg at 2015-03-05 20:51:19...
Flickr Uploadr Complete!
(<class 'urllib2.HTTPError'>, HTTPError(), <traceback object at 0x104fe0830>)
(<class 'urllib2.HTTPError'>, HTTPError(), <traceback object at 0x104fe0998>)

== End script output ==
2015-03-05 20:51:23.275 hazelworker[712] DEBUG: Rule requires extra matching pass.

Does anyone have any suggestions?
richwoodham
 
Posts: 3
Joined: Thu Sep 20, 2012 1:03 pm

I think the Python script uses the Flickr Authentication API, which Flickr has now replaced with OAuth, so the script will no longer work. Does anyone have a Python script that uses OAuth instead?
nickg
 
Posts: 5
Joined: Fri May 18, 2018 7:20 am


Return to Tips & Tricks - DO NOT POST QUESTIONS