Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A Liquid tag for Jekyll sites that allows embedding Lychee albums. See https://tobrunet.ch/articles/jekyll-meets-lychee-a-liquid-tag-plugin/
# Connects Jekyll with Lychee (http://lychee.electerious.com/)
#
# # Features
#
# * Generate album overview and link to image
# * Caching of JSON data
#
# # Usage
#
# {% lychee_album <album_id> %}
# {% lychee_album_no_cache <album_id> %}
#
# # Example
#
# {% lychee_album 1 %}
# {% lychee_album_no_cache 1 %}
#
# # Default configuration (override in _config.yml)
#
# lychee:
# url: http://electerious.com/lychee_demo
# album_title_tag: h1
# link_big_to: lychee
# cache_folder: _lychee_cache
#
# Change at least "url" to your own Lychee installation
# album_title_tag: let's you chose which HTML tag to use around the album title
# link_big_to: choose "lychee" or "img".
# lychee: links the image to the Lychee image view
# img: links the image to it's original image
#
# # Author and license
#
# Tobias Brunner <tobias@tobru.ch> - https://tobrunet.ch
# License: MIT
require 'json'
require 'net/http'
require 'net/https'
require 'uri'
module Jekyll
class LycheeAlbumTag < Liquid::Tag
def initialize(tag_name, config, token)
super
# params coming from the liquid tag
@params = config.strip
# get config from _config.yml
@config = Jekyll.configuration({})['lychee'] || {}
# set default values
@config['album_title_tag'] ||= 'h1'
@config['link_big_to'] ||= 'lychee'
@config['url'] ||= 'http://electerious.com/lychee_demo'
@config['cache_folder'] ||= '_lychee_cache'
# construct class wide usable variables
@thumb_url = @config['url'] + "/"
@big_url = @config['url'] + "/"
@album_id = @params
# initialize caching
@cache_disabled = false
@cache_folder = File.expand_path "../#{@config['cache_folder']}", File.dirname(__FILE__)
FileUtils.mkdir_p @cache_folder
end
def render(context)
# initialize session with Lychee
api_url = @config['url'] + "/php/api.php"
uri = URI.parse(api_url)
@http = Net::HTTP.new(uri.host, uri.port)
@http.use_ssl = true
@request = Net::HTTP::Post.new(uri.request_uri)
@request['Cookie'] = init_lychee_session
album = cached_response(@album_id, 'album') || get_album(@album_id)
puts "[Lychee Tag] Processing Lychee album id #{@album_id}: '#{album['title']}'"
html = "<#{@config['album_title_tag']}>#{album['title']}</#{@config['album_title_tag']}>\n"
album_content = album['content']
album_content.each do |photo_id, photo_data|
big_href = case @config['link_big_to']
when "img"
photo_data = cached_response(photo_id, 'photo') || get_photo(@album_id, photo_id)
@big_url + photo_data['url']
when "lychee" then @config['url'] + "#" + @album_id + "/" + photo_id
else "#"
end
html << "<a href=\"#{big_href}\" title=\"#{photo_data['title']}\"><img src=\"#{@thumb_url}#{photo_data['thumbUrl']}\"/></a>\n"
end
return html
end
# Caching
def cache(id, type, data)
puts "[Lychee Tag] Caching Lychee #{type} id #{id}"
cache_file = cache_file_for(id, type)
File.open(cache_file, "w") do |f|
f.write(data)
end
end
def cache_file_for(id, type)
filename = "#{type}_#{id}"
File.join(@cache_folder, filename)
end
def cached_response(id, type)
return nil if @cache_disabled
cache_file = cache_file_for(id, type)
JSON.parse(File.read(cache_file)) if File.exist?(cache_file)
end
# Lychee API mapping
def init_lychee_session
# construct request
@request.set_form_data({'function' => 'Session::init'})
# send request now and save cookies
response = @http.request(@request)
return response.response['set-cookie']
end
def get_albums
@request.set_form_data({'function' => 'Album::getAll'})
return JSON.parse(@http.request(@request).body)
end
def get_album(id)
@request.set_form_data({'function' => 'Album::get', 'albumID' => id, 'password' => ''})
response = @http.request(@request).body
cache(id, 'album', response) unless @cache_disabled
return JSON.parse(response)
end
def get_photo(album_id, photo_id)
@request.set_form_data({'function' => 'Photo::get', 'albumID' => album_id, 'photoID' => photo_id, 'password' => ''})
response = @http.request(@request).body
cache(photo_id, 'photo', response) unless @cache_disabled
return JSON.parse(response)
end
end
class LycheeAlbumTagNoCache < LycheeAlbumTag
def initialize(tag_name, config, token)
super
@cache_disabled = true
end
end
end
Liquid::Template.register_tag('lychee_album', Jekyll::LycheeAlbumTag)
Liquid::Template.register_tag('lychee_album_no_cache', Jekyll::LycheeAlbumTagNoCache)

When retrieving the image (on line 91), the output URL contains uploads/thumb (or uploads/big) twice, which is problematic. You should use the following:

# for the big image
@config + photo_data['url']
# for the small image
@config + photo_data['thumbUrl']

instead of

# for the big image
@big_url + photo_data['url']
# for the small image
@thumb_url + photo_data['thumbUrl']

There is a problem with the plugin/API: when retrieving an image from an album, the response does not provide the photo properties (such as the description). How can it be done?

I got it: there is a function at the bottom of the Gist, called get_photo(), which allows me to retrieve photo information about the photo specifically.

So, at line 83, instead of getting the images from the get_album() function (which only returns basic information, not each photo description), we should use the get_photo() function, as:

album_content.each do |photo_id, photo_data|
photo_id = photo_data['id']
photo = get_photo(@album_id, photo_id)

n00tz commented Dec 8, 2016

Update line 72, for the current implementation of the api url.

api_url = @config['url'] + "/php/index.php"

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