# Album Plugin:	format/opengraph
# For info:	'album -plugin_info format/opengraph'
# For usage:	'album -plugin_usage format/opengraph'
# FB Debugger:  https://developers.facebook.com/tools/debug/og/object/

# Version history:
# v1.01 David Ljung Madison <contactRequest at GetDave.com>
# 			+ Merged code from the FB plugins so this now all works together
# v1.00 David Ljung Madison <contactRequest at GetDave.com>
#       + released
use strict;

my $DESCRIPTION = << 'DESCRIPTION';
Adds opengraph tags to the <head> section of album pages.
DESCRIPTION

sub start_plugin {
	my ($opt) = @_;

	# -album_url
	#album::add_option(1, 'album_url', album::OPTION_STR, usage=>"The full URL for the top album [required]");

	# Setup the hooks
	album::hook($opt, 'write_index', \&add_index_opengraph);
  album::hook($opt, 'end_handle_file',\&add_page_info);

	return {
		author => 'David Ljung Madison',
		href => 'http://MarginalHacks.com/',
		version => '1.01',
		description => $DESCRIPTION,
	};
}

# Our {URL} stuff has single quotes around it
sub unquote {
	my ($url) = @_;
	$url = $1 if $url =~ /^'(.+)'$/;
	$url;
}

sub html_clean {
	my ($str) = @_;
	$str =~ s/&/&amp;/g;
	$str =~ s/"/&quot;/g;
	$str =~ s/([^a-z0-9 \t])/"&#".ord($1).";"/ieg;
	$str;
}

sub album_url {
	my ($opt) = @_;
	my $album_url = album::option($opt, 'album_url') ||
		album::option($opt, 'format/opengraph:album_url') ||
		album::option($opt, 'social/facebook-comments:album_url') ||
		album::option($opt, 'social/facebook-like:album_url');
	album::usage($opt,"Plugin format/opengraph needs you to specify:\n  -album_url")
		unless $album_url;
	$album_url =~ s|/+$||;
	$album_url;
}

sub add_index_opengraph {
	my ($opt,$data, undef,  $dir, $album) = @_;

	# Get the directory info (like the dir_thumb)
	my ($dir_tn_path,$dir_url,$dir_full_path,$dir_file) = album::dir_info($opt,$data,'.');

	# Top level url for the album?
	my $album_url = album_url($opt);

	# Figure out URLs
	my $my_url = $album_url.'/'.$album.'/';
	$my_url =~ s|/\./|/|g;
	my $image = $my_url.$dir_file;

	# A bit kludgy way to get some sort of caption/name for the directory
	# It would be nice to parse all the object info and get the {name} and {cap},
	# but that's too much work for now...
	my ($my_path,$my_dir) = album::split_path($opt,$album);
	my $cap = album::read_captions($opt,'..');
	my $title = album::clean_name($opt, $my_dir, $cap->{'vid_20170316_202301.mp4'}{name});
	$title = html_clean($title);

	# And the long description can be the header.txt, if it exists.
	# This means reading it twice, but we didn't save it...
	my $desc = '';
	if (-f "$dir/$opt->{header}" && (open(HEADER,"<$dir/$opt->{header}"))) {
		while(<HEADER>) { $desc .= $_; }
	}
	$desc = html_clean($desc);

	# Add to head (but don't add it twice if we've used another plugin for this)
	album::add_head_index($opt,$data, <<HEAD);
		<!-- BEGIN format/opengraph -->
		<meta property="og:type" content="article" />
		<meta property="og:url" content="$my_url" />
		<meta property="og:image" content="$image" />
		<meta property="og:title" content="$title" />
		<meta property="og:description" content="$desc" />
		<!-- END format/opengraph -->
HEAD

	# Do the album!
	return 0;
}

sub add_page_info {
  my ($opt, $data, $hookname, $dir, $pic, $obj, $path) = @_;

  # We need to figure out the full URL to the image and to the page,
  # which is tricky because 'album' doesn't need it or calculate it.

  # 1) Top level url for the album?
	my $album_url = album_url($opt);

  # 2) The album directory.  Either album_path if it's available (we've done
  # calc_paths() or else we use $dir and remove -topdir (which is uglier)
  my $album_path = $data->{paths}{album_path};
  unless ($album_path) {
    $album_path = $dir;
    my $topdir = album::option($opt,'topdir');
    $album_path =~ s/^$topdir//;
  }
  $album_path =~ s|^/||;

  # 3) URL for the image page
  my $album_page_to_img_page = unquote($obj->{URL}{album_page}{image});
  my $image_page_url = "$album_url/$album_path/$album_page_to_img_page";

  # 4) URL for the image on the page (either medium or full)
  my $img_page_to_img = unquote($obj->{URL}{image_page}{image_src});

  my $album_page_to_tn = $album_page_to_img_page;
  $album_page_to_tn =~ s|/[^/]+$||;
  my $image_url = "$album_url/$album_path/$album_page_to_tn/$img_page_to_img";

	# 4.1) If we have a snapshot, use that instead, because most
	#      places (like FB) can't do a movie for og:image
	my $video_url;
	if ($obj->{snapshot} && $obj->{snapshot}{file}) {
  	$video_url = $image_url;
  	$image_url = "$album_url/$album_path/$album_page_to_tn/$obj->{snapshot}{file}";
	}

  # Figure out the rest of the opengraph info
  my $title = $data->{obj}{$pic}{name} || $pic;
	$title = html_clean($title);
  my $desc = $data->{obj}{$pic}{cap};
	$desc = html_clean($desc);

	album::add_head_image($opt,$data, $pic, <<PICHEAD);
		<!-- BEGIN image format/opengraph -->
    <meta property="og:type" content="article" />
    <meta property="og:url" content="$image_page_url" />
    <meta property="og:image" content="$image_url" />
    <meta property="og:title" content="$title" />
    <meta property="og:description" content="$desc" />
PICHEAD

	album::add_head_image($opt,$data, $pic, <<VIDEOHEAD) if $video_url;
    <meta property="og:video" content="$video_url" />
    <meta property="og:video:width" content="$obj->{full}{x}" />
    <meta property="og:video:height" content="$obj->{full}{y}" />
VIDEOHEAD

	album::add_head_image($opt,$data, $pic, <<ENDPICHEAD);
		<!-- END image format/opengraph -->
ENDPICHEAD

	# Save image_page_url for any other plugins that require opengraph
	$obj->{opengraph_image_url} = $image_page_url;
}

# Plugins always end with:
1;
