Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completion + Configuration Script #6

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
12 changes: 11 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
.PHONY: install
.PHONY: install configure

configure:
@chmod +x scripts/configure.sh
@scripts/configure.sh -t $(THRESHOLD) -k $(API_KEY) -s $(API_SECRET) -u $(API_ENDPOINT) -h $(API_HOMEPAGE)

install:
@chmod +x scripts/install.sh
@scripts/install.sh

THRESHOLD ?= 0.9
API_KEY ?= username
API_SECRET ?= password
API_ENDPOINT ?= http://localhost:8080/xapi
API_HOMEPAGE ?= http://yetanalytics.com
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ An XAPI integrated VLC player

### Setup

0) (optional) `make configure` will setup the configuration files for the plugin. to override defaults, run the command with the appropriate overrides. Example being: `make configure THRESHOLD=0.87`. The following is a list of variables and what they mean.
- `THRESHOLD` - Decimal value between 0 and 1 that represents point in video considered a completion. Once a video reaches this threshold, the plugin will issue a completion statement
- `API_KEY` - API key for LRS
- `API_SECRET` - API Secret for LRS
- `API_ENDPOINT` - API Endpoint for LRS
- `API_HOMEPAGE` - system that identifies user. In the statement this is set at `actor.account.homePage`.

1) Run `make install` . This will move the plugin into the appropriate directory for VLC.
2) Open VLC. when you do, click on the view dropdown and click 'xAPI Integration.'
3) Enter the fields in the form:
Expand All @@ -23,4 +30,4 @@ All code is located at `xapi.lua`. In VLC you can go to the console log (via ctr

Copyright © 2024 Yet Analytics, Inc.

This module is licensed under the GNU Lesser General Public License 2.1 or later.
This module is licensed under the GNU Lesser General Public License 2.1 or later.
83 changes: 83 additions & 0 deletions scripts/configure.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/bin/bash

source "$(dirname "$0")/get-config-dir.sh"


delete_if_exists() {
local file_path="$1" # The file path is passed as an argument

# Check if the file exists
if [ -f "$file_path" ]; then
echo "File exists: $file_path"

# Delete the file
rm "$file_path"
echo "File deleted: $file_path"
else
echo "File does not exist: $file_path"
fi
}

configure() {
local xapi_config_file="$(get_vlc_config_directory)xapi-extension-config.txt"
local threshold_config_file="$(get_vlc_config_directory)xapi-threshold-config.txt"

delete_if_exists "$xapi_config_file"
delete_if_exists "$threshold_config_file"

# Variables to store the required options
local api_key=""
local api_secret=""
local api_endpoint=""
local threshold=""
local homepage=""

# Parse command line options
while getopts "k:s:u:t:h:" opt; do
case $opt in
k)
api_key="$OPTARG"
;;
s)
api_secret="$OPTARG"
;;
u)
api_endpoint="$OPTARG"
;;
t)
threshold="$OPTARG"
;;
h)
homepage="$OPTARG"
;;
*)
echo "Usage: $0 -k <api_key> -s <api_secret> -u <api_endpoint> -t <threshold> -h <homepage>"
exit 1
;;
esac
done


# Debugging: Output parsed values
echo "API Key: $api_key"
echo "API Secret: $api_secret"
echo "API URL: $api_endpoint"
echo "Threshold: $threshold"
echo "API Homepage: $homepage"


# Write the key-value pairs to the file, only if they exist
[[ -n "$api_key" ]] && echo "api_key = $api_key" >> "$xapi_config_file"
[[ -n "$api_secret" ]] && echo "api_secret = $api_secret" >> "$xapi_config_file"
[[ -n "$api_endpoint" ]] && echo "api_endpoint = $api_endpoint" >> "$xapi_config_file"
[[ -n "$threshold" ]] && echo "threshold = $threshold" >> "$threshold_config_file"
[[ -n "$homepage" ]] && echo "api_homepage = $homepage" >> "$xapi_config_file"

echo "config written to $xapi_config_file and threshold written to $threshold_config_file"

return 0
}

#echo "$(get_vlc_config_directory)"

configure "$@"
41 changes: 41 additions & 0 deletions scripts/get-config-dir.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash

get_vlc_config_directory() {
local config_dir=""

# Check the OS type using uname
if [[ "$(uname)" == "Darwin" ]]; then
# macOS
if [[ -n "$HOME" ]]; then
config_dir="$HOME/Library/Preferences/org.videolan.vlc/"
fi
elif [[ "$(uname)" == "Linux" ]]; then
# Linux
if [[ -n "$HOME" ]]; then
config_dir="$HOME/.config/vlc/"
fi
elif [[ "$(uname -o 2>/dev/null)" == "Cygwin" || "$(uname -o 2>/dev/null)" == "Msys" || "$(uname -o 2>/dev/null)" == "MINGW32_NT" || "$(uname -o 2>/dev/null)" == "MINGW64_NT" ]]; then
# Windows (Cygwin or WSL environments can also be checked)
if [[ -n "$APPDATA" ]]; then
config_dir="$APPDATA/vlc/"
fi
fi

# Check if config directory was determined
if [[ -z "$config_dir" ]]; then
echo "Could not determine VLC configuration directory." >&2
return 1
fi

# Ensure the directory exists
if [[ ! -d "$config_dir" ]]; then
mkdir -p "$config_dir"
if [[ $? -ne 0 ]]; then
echo "Failed to create directory: $config_dir" >&2
return 1
fi
fi

echo "$config_dir"
return 0
}
44 changes: 41 additions & 3 deletions xapi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@ local api_homepage = ""
local api_userid = ""
local b='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
local config_file_path = ""

local threshold_file_path = ""
local threshold = 0.9
local is_completed = false
-- *************** Events ************

function activate()
api_userid = get_uid()
config_file_path = get_vlc_config_directory() .. "config.txt"
config_file_path = get_vlc_config_directory() .. "xapi-extension-config.txt"
threshold_file_path = get_vlc_config_directory() .. "xapi-threshold-config.txt"
load_config(config_file_path)
load_threshold_config(threshold_file_path)
vlc.msg.info("threshold value is: "..threshold)
vlc.msg.info("config_file_path: "..config_file_path)
vlc.msg.info("UID is: " .. api_userid)
show_api_settings_dialog()
Expand All @@ -38,6 +43,7 @@ end

function input_changed()
vlc.msg.info("[Now Playing] input_changed")
is_completed = false
end

function playing_changed()
Expand Down Expand Up @@ -169,6 +175,29 @@ function load_config(file_path)
end
end

function load_threshold_config(file_path)
local config = read_config(file_path)
if config then
for key, value in pairs(config) do
if key == "threshold" then
threshold = tonumber(value)

-- if someone sets the threshold too high it may never issue a completion
if threshold >= 0.98 then
threshold = 0.98
end

else
vlc.msg.err("Unknown threshold config. setting threshold to default (0.9)")
threshold = 0.9
end
end
else
vlc.msg.err("Unable to load threshold config. setting threshold to default (0.9)")
threshold = 0.9
end
end

function write_config(config, file_path)
-- Open the file for writing ("w" overwrites the file)
local file = io.open(file_path, "w")
Expand Down Expand Up @@ -282,17 +311,24 @@ function form_statement(args)
local title = args.title
local status = args.status
local duration = args.duration
local progress = args.progress
local progress = tonumber(args.progress)
local current_time = args.current_time
local base_url = "https://yet.systems/xapi/profiles/vlc"
local verb = base_url .. "/verbs/" .. status

-- Override verb if we get a completion
if is_completed == false and progress >= threshold then
verb = base_url .. "/verbs/complete"
is_completed = true
end
local activity_url = base_url .. "/activity"
local video_url = activity_url .. "/video"
local object = video_url .. "/" .. title
local extension_url = base_url .. "/extensions/"
local duration_url = extension_url .. "duration"
local progress_url = extension_url .. "progress"
local current_time_url = extension_url .. "currentTime"
local status_url = extension_url .. "status"

-- Manually construct the JSON string with results
local json_statement =
Expand All @@ -315,6 +351,7 @@ function form_statement(args)
'"extensions": {' ..
'"' .. duration_url .. '": ' .. duration .. ',' ..
'"' .. progress_url .. '": ' .. progress .. ',' ..
'"' .. status_url .. '": "' .. status .. '",' ..
'"' .. current_time_url .. '": ' .. current_time ..
'}' ..
'}' ..
Expand Down Expand Up @@ -342,6 +379,7 @@ function post_request(json_body)
-- Encode API key and secret as Base64 for Basic Auth
local auth = "Basic " .. base64_encode(api_key .. ":" .. api_secret)
-- Construct the curl command to make the HTTP POST request

local command = 'curl -X POST ' .. api_endpoint .. '/statements '
.. '-H "Content-Type: application/json" '
.. '-H "Authorization: ' .. auth .. '" '
Expand Down