You probably shouldn't use this repo.
If it has any use it's probably to show you how to interface with the Flickr API using the NodeJS Flickr SDK.
I created this because I upload photos from my iPhone to Flickr using the Flickr iOS app, except it doesn't always work. Sometimes Flickr will re-upload the same image. Other times I fatfinger the upload and upload dupes.
I wanted an app to:
- Dedupe a single photoset
- Reorder the photos by most recently taken
- Automatically grab the URLs of the largest size of all photos in the photoset
- Run as a background service so it can be automatically run every night
Below I explain how to set up a connection with Flickr using the NodeJS Flickr SDK, I'd recommend just using that. But if you're feeling bold and want to try the exact code I wrote for myself, here's a quick overview of the architecture and endpoints.
The main app is an Express server designed to run on your local machine or in a Docker container. It has a bunch of endpoints that receive HTTP requests and interact with the Flickr data in some way.
The other section of code generates local TLS certs you'll need to complete the Oauth process, then runs the code necessary to retrieve the access codes. It's a one-time setup that you'll never need to repeat.
- Register for an account with Flickr if you haven't already, and log in.
- Go to Flickr's "App Garden" and create an app by clicking "apply for a non-commercial key"
- Fill out the form, check the boxes
, hit submit
- Copy down the Key and Secret provided to you and store them somewhere safe
- While logged into Flickr.com, go to the photo album you want to work with and look at the URL. It should be something like
https://www.flickr.com/photos/123456789@N05/albums/123456789123456
. In this example123456789@N05
is the user id and123456789123456
is the album ID. Write yours down - Copy
.env.example
in the root folder to.env
- Open
.env
in a text editorFLICKR_CONSUMER_KEY=tbd FLICKR_CONSUMER_SECRET=tbd FLICKR_REQUEST_TOKEN=tbd FLICKR_REQUEST_TOKEN_SECRET=tbd FLICKR_OAUTH_TOKEN=tbd FLICKR_OAUTH_TOKEN_SECRET=tbd FLICKR_OAUTH_TOKEN_VERIFIER=tbd FLICKR_ALBUM_ID=tbd FLICKR_PRIMARY_PHOTO_ID=tbd USER_ID=tbd LOCAL_KEY_FILE=key.pem LOCAL_CERT_FILE=cert.pem
FLICKR_CONSUMER_KEY
andFLICKR_CONSUMER_SECRET
are the values you just obtained from Flickr- The next 5 (
FLICKR_REQUEST_TOKEN
toFLICKR_OAUTH_TOKEN_VERIFIER
) will be obtained from the app. Leave themtbd
for now - Enter your
FLICKR_ALBUM_ID
andUSER_ID
obtained above - Choose the primary photo for the album (or use the existing one) by navigating to it via a web browser and saving the photo ID in
FLICKR_PRIMARY_PHOTO_ID
- Leave
LOCAL_KEY_FILE
andLOCAL_CERT_FILE
alone
- Ensure you have NodeJS installed (instructions)
- Optional: install
nvm
(instructions) and runnvm use
to use the version of NodeJS I specified in the.nvmrc
file - Install the dependencies by running
npm i
- Here's where things start to get dicey so buckle up.
- Flickr has a fairly thorough overview of its Oauth process but it's not the most user-friendly walkthrough I've ever seen.
- To authorize your app (yes, that's required as part of the Oauth process, generating the key and secret is not enough by itself) you'll need to run a server on your local machine, open that in your web browser, then click the authorize button. The app will record the credentials produced by that process and save them for you in the
.env
file - To make the above more complicated, the process requires the local server be HTTPS, HTTP will not do. So you need to generate a self-signed certificate locally to produce
key.pem
andcert.pem
files - I have a bash script that will generate those files for you (
generate-cert.sh
) but we can kill two birds with one one by just runningnpm run oauth
, which will run that bash script and then start up the server. So let's do that and see what that looks like - If succesful you should see this in your terminal
- As the terminal says, open your web browser and navigate to https://localhost:3000.
- You'll get a warning that it's unsafe. That's expected, because the cert is self-signed. Click "Advanced" and "Proceed to localhost (unsafe)"
- You should see the authorization screen in your browser. Click "OK, I'LL AUTHORIZE IT"
- If it worked, 3 things will happen. First, you will be redirected in your browser to some JSON output that will (if parsed via a Chrome extension) look like this
- Second, the app should have overwritten your
.env
file to populate all the values like so - But maybe that didn't happen, because maybe the process doesn't have write permissions to that folder, or some other reason. As a backup the values should all be outputted to the terminal, where you can manually enter them into your
.env
file - You should be able to now interface with the Flickr API! And if the credentials are stored to that
.env
file, you'll never need to repeat that process. But let's test it out
- Hit
CTRL-C
to interrupt that process and bring you back to the terminal. Now let's run the main app by typingnpm start
. You should seeThere's no saved data file because you haven't loaded any data yet much less saved it to disk.
- Let's test the API credentials by opening up your browser and going to http://localhost:3000/test-login. If that worked you should see
That's it! You now have a working set of credentials to engage with the Flickr API and can write your own programs.
If you want to use the endpoints programmed into this app, here they are.
URL | What it does |
---|---|
http://localhost:3000/test-login | tests credentials by attempting to log in |
http://localhost:3000/get-photos | obtains all photo data from album listed in .env file |
http://localhost:3000/get-photosets | lists all photosets associated with user |
http://localhost:3000/dedupe | dangerous! deletes duplicate photos in the specified album |
http://localhost:3000/info | return the current data set |
http://localhost:3000/reorder | attempts to re-sort album by most recently taken photo |
http://localhost:3000/get-sizes | gets the largest file sizes for photos in the album |
http://localhost:3000/save-data | saves obtained data to your computer |
http://localhost:3000/ugly | a POST endpoint to record ugly photos |
http://localhost:3000/download-photos | download to disk all photo URLs from /get-sizes |
/dedupe
is dangerous. It can and will delete photos from your Flickr account permanently, so I don't recommend you use it unless you've already stored copies of all the photos (which you can do via /get-photos
, /get-sizes
, then /download-photos
).
If you want to save the data to disk so you don't have to refetch all of it on every run just hit /save-data
. It will save your data to src/data
.
Generally tho I don't recommend you use these endpoints but instead write your own interaction with the Flickr API.
There's a Dockerfile in this repo that builds this app and runs it in a container, and then uses [forever](https://www.npmjs.com/package/forever to run the server as a background daemon.
I run this app in a Docker container on a NAS, then run a cron job on my NAS to copy the data output to a webserver. But that's why flickr-cron
is in root as well as the Dockerfile
.
That's it! Good luck.