:- module(ugm_ingest, []). :- use_module(library(http/http_dispatch)). :- use_module(library(http/http_parameters)). :- use_module(library(http/http_path)). :- use_module(library(http/http_json)). :- use_module(library(http/json)). :- use_module(library(http/json_convert)). :- use_module(library(rdf_write)). :- use_module(library(semweb/rdf_db)). :- use_module(library(semweb/rdf_label)). :- use_module(library(yaz_util)). :- use_module(library(video_annotation)). :- use_module(library(download_video)). :- http_handler(yaz(api/ugmingest), http_api_ugm_ingest, []). :- debug(ugm_ingest). :- debug(stream_download). :- rdf_meta rdf_cond_assert(r,r,o,+). %% local_video_file(+StreamURL, -File) % % Create a local filename for a stream url. local_video_file(URL, File) :- www_form_encode(URL, File0), absolute_file_name(video(File0), File). %% http_api_tag_ingest(+Request) % % Handler for the tag ingestion service. http_api_ugm_ingest(Request) :- http_parameters(Request, [ videoURL(VideoURL, [uri, description('URL of the video')]), videoId(VideoId, [optional(true), description('Identifier of the video')]), videoTitle(Title, [default(''), description('Title of the video')]), videoDuration(Duration, [default(0), description('Duration of the video in seconds')]), videoDescription(Desc, [optional(true), description('Description of the video')]), slug(Slug, [optional(true), description('Slug to identify the video')]), streamURL(StreamURL, [description('URL of the video stream')]), entries(Entries, [json_tag_entries, description('JSON object with the annotations')]) ]), length(Entries, EntryCount), ( rdf(VideoURL, rdf:type, pprime:'Video') -> rdf_transaction(rdf_retractall(_,_,_,VideoURL)) ; true % for the test bed we have the local copies already %local_video_file(StreamURL, File), %save_video_stream(StreamURL, File) ), rdf_transaction(( import_video(VideoURL, VideoId, StreamURL, Title, Desc, Duration, Slug), debug(ugm_ingest, 'added video ~w', [VideoURL, StreamURL]), import_entries(Entries, VideoURL), debug(ugm_ingest, 'added ~w tag entries', [EntryCount]) )), reply_json(json([videoURL=VideoURL,entryCount=EntryCount])). http:convert_parameter(json_tag_entries, Atom, Term) :- atom_json_term(Atom, JSON, []), json_to_prolog(JSON, Term). :- json_object tag_entry(tag:atom, time:number), tag_entry(id:number, game_id:number, createdOn:number, tag:atom, player_id:number, time:number, typingDuration:number, score:number). %% import_video(+VideoURL, +Id, +StreamURL, +Title, +Desc, %% +Duration) % % Add a Video to the RDF database and store it's metadata % properties % % @see video_property/2 import_video(URL, Id, Stream, Title, Desc, Duration0, Slug) :- get_time(Now), rdf_atom_object(Duration0, Duration), rdf_cond_assert(URL, dc:id, literal(Id), URL), rdf_cond_assert(URL, rdf:type, pprime:'Video', URL), rdf_cond_assert(URL, pprime:source, Stream, URL), rdf_cond_assert(URL, dc:title, literal(Title), URL), rdf_cond_assert(URL, dc:description, literal(Desc), URL), rdf_cond_assert(URL, pprime:duration, literal(Duration), URL), rdf_cond_assert(URL, pprime:slug, literal(Slug), URL), rdf_cond_assert(URL, pprime:ingestedAt, literal(Now), URL). rdf_cond_assert(S,P,O,G) :- ground(O), !, rdf_assert(S,P,O,G). rdf_cond_assert(_,_,_,_). %% import_entries(+TagEntryList, +VideoId) % % Add the annotations for VideoId to the RDF db. import_entries([], _). import_entries([TagEntry|Es], VideoURL) :- import_tag_entry(TagEntry, VideoURL), import_entries(Es, VideoURL). import_tag_entry(tag_entry(Tag, Playhead0), VideoURL) :- rdf_bnode(URL), rdf_atom_object(Playhead0, Playhead), rdf_assert(VideoURL, pprime:hasAnnotation, URL, VideoURL), rdf_assert(URL, rdf:type, pprime:'TagEntry', VideoURL), rdf_assert(URL, rdf:value, literal(Tag), VideoURL), rdf_assert(URL, pprime:videoPlayhead, literal(Playhead), VideoURL). import_tag_entry(tag_entry(Id,GameId,CreationTime, Tag,PlayerId,Playhead0,TypingDuration0,Score0), VideoURL) :- pprime_url(Id, entry, URL), pprime_url(GameId, game, GameURL), pprime_url(PlayerId, player, PlayerURL), rdf_atom_object(Playhead0, Playhead), rdf_atom_object(TypingDuration0, TypingDuration), rdf_atom_object(Score0, Score), rdf_assert(VideoURL, pprime:hasAnnotation, URL, VideoURL), rdf_assert(URL, rdf:type, pprime:'TagEntry', VideoURL), rdf_assert(URL, opmv:used, GameURL, VideoURL), rdf_assert(URL, opmv:wasPerformedAt, literal(CreationTime), VideoURL), rdf_assert(URL, rdf:value, literal(Tag), VideoURL), rdf_assert(URL, pprime:videoPlayhead, literal(Playhead), VideoURL), rdf_assert(URL, pprime:creator, PlayerURL, VideoURL), rdf_assert(URL, pprime:score, literal(Score), VideoURL), rdf_assert(URL, pprime:typingDuration, literal(TypingDuration), VideoURL). %% pprime_url(+Id, +Specifier, -URL) % % URL in prestoprime namespace. pprime_url(Id, Spec, URL) :- atom_concat(Spec, Id, Local), rdf_global_id(pprime:Local, URL). rdf_atom_object(N, A) :- number(N), !, atom_number(A, N). rdf_atom_object(A, A).