Skip to content

Instantly share code, notes, and snippets.

@hcooper
Last active March 5, 2021 14:47
Show Gist options
  • Save hcooper/71dc46d292ecfdc37e4e to your computer and use it in GitHub Desktop.
Save hcooper/71dc46d292ecfdc37e4e to your computer and use it in GitHub Desktop.
Upload photos to a koken gallery from the command line
# v1.0 of a command line uploader for the koken gallery software.
# The only non-standard requirement is pyton-requests.
import json
import requests
import os
from urllib import quote as quote
api_url = "http://yourdomain/api.php?"
common_headers = {'X-Koken-Auth':'cookie'}
email = quote('your@email.com')
password = quote('YourPassword')
# Establish an authenticated session
auth = requests.Session()
auth.post(api_url + '/sessions',
data={'email': email, 'password': password},
headers=common_headers)
def create_album(title):
res = auth.post(api_url + '/albums',
data={'title': title, 'album_type': '0', 'listed': '1'},
headers=common_headers)
id = res.json['id']
log('Created album: %s, id: %s' % (title, id))
return(id)
def upload_localfile(file):
res = auth.post(api_url + '/content',
data={'localfile': file, 'visibility': 'public'},
headers=common_headers)
id = res.json['id']
log('Uploaded localfile %s, id: %s' % (file, id))
return(id)
def add_content_to_album(content_id, album_id):
res = auth.post(api_url + '/albums/%s/content/%s' % (album_id, content_id),
data={'localfile': file, 'visibility': 'public'},
headers=common_headers)
log('Added %s to album %s' % (content_id, album_id))
return
def log(msg):
print(msg)
# Create an album named the same as your current working directory,
# and upload each file within to the new album.
cwd = os.getcwd()
dname = cwd.split('/')[-1]
album_id = create_album(dname)
for file in os.listdir(cwd):
content_id = upload_localfile(cwd + '/' + file)
add_content_to_album(content_id, album_id)
@mowog78
Copy link

mowog78 commented Jul 30, 2018

HI hcooper!
Did you have the opportunity to build upon your script ? I am working on a similar project, with the intention to replace my Lightroom Publish plugin with a home-made automated synchronization (in a Linux environment). I am still at the beginning of my research, and struggling to get any documentation on the Koken API...
Do you have any insights or documentation that would be helpful ?
Best regards.
basile

@benchonaut
Copy link

@mowog78 @hcooper and all the internetz stopping by :

There is an improved version with local upload/remote download that asks for your password and is scriptable here..

https://gitlab.com/the-foundation/koken-python-api-client/

@benchonaut
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment