Difference between revisions of "Meta:Create YouTube playlists on-the-fly with semantic queries"

From protonation
Jump to navigation Jump to search
 
(One intermediate revision by the same user not shown)
Line 103: Line 103:
 
{{#replace:{{#explode:{{{1|}}}|,|1|2}}| |}}
 
{{#replace:{{#explode:{{{1|}}}|,|1|2}}| |}}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
==Wrap it all up==
 +
The widget call should look like this:
 +
<syntaxhighlight lang="html">
 +
{{#widget:YouTube
 +
| id={{first item of a comma separated list|{{#var:query_output}} }}
 +
| additionalids={{Comma separated list minus first item spaces removed|{{#var:query_output}} }}
 +
}}
 +
</syntaxhighlight>
 +
 +
Let's put them all together:
 +
 +
<syntaxhighlight lang="html">
 +
{{#vardefine:query_output
 +
| {{#ask: [[Category:YouTube videos]]
 +
  | mainlabel=-
 +
  | ?Has video ID=
 +
  | sort=Is published on
 +
  | order=descending
 +
  | limit=10
 +
  | searchlabel=
 +
  }}
 +
}}
 +
 +
{{#widget:YouTube
 +
| id={{first item of a comma separated list|{{#var:query_output}} }}
 +
| additionalids={{Comma separated list minus first item spaces removed|{{#var:query_output}} }}
 +
}}
 +
</syntaxhighlight>
 +
  
 
==References==
 
==References==
 
<references/>
 
<references/>
 +
 +
{{comments}}
  
  
 
[[Category:Semantic MediaWiki tips]]
 
[[Category:Semantic MediaWiki tips]]

Latest revision as of 05:06, 24 May 2020

VLC.svg Under construction

This page is under construction.


It is easy to create a YouTube playlist from a query. All you need is the (upgraded) Widget:YouTube from the MediaWiki Widgets extension.

In essence, what we describe here could be seen as a new semantic results format; we could call it the YouTube playlist format!

Rationale

YouTube allows the creation of playlists on the fly, i.e. without even logging in, just from the URL. All you need to do is provide the URL with a comma separated list of the video IDs you want to play sequentially.[1]

The URL must have the following form:

https://www.youtube.com/watch_videos?video_ids=VIDEO_ID_1,VIDEO_ID_2, … ,VIDEO_ID_n

Think of the above not so much as a URL but rather as a command to YouTube to create a playlist. When YouTube receives this "command" it automatically creates a playlist, as if it was created by a user, and then assigns it a playlist ID as well, which is permanent. Then it redirects you to a "proper" YouTube playlist URL.

For example, if you click this URL:

https://www.youtube.com/watch_videos?video_ids=ntpt0fq3Jn8,iZ9vkd7Rp-g,L4GrDOOIIbk

you will be redirected to this URL:

https://www.youtube.com/watch?v=ntpt0fq3Jn8&list=TLGGUeJrTjDLyMgyMjA1MjAyMA

where TLGGUeJrTjDLyMgyMjA1MjAyMA is the newly created playlist ID. This playlist ID is as good as any playlist created by a user. You may use it the same way you would use a "proper" playlist ID and it never expires.

Yeah, but how do I embed this?

If you click on the YouTube share button and then select the embed option you will get a simple iframe code with some control params such as dimensions etc and the embed URL which has the following form:

https://www.youtube.com/embed/VIDEO_ID

If you try to share a playlist you will get a slightly different URL:

https://www.youtube.com/embed?listType=playlist&list=PLAYLIST_ID

Note that you need to prepend the playlist ID with the letters PL as shown in the following example:

https://www.youtube.com/embed?listType=playlist&list=PLTLGGUeJrTjDLyMgyMjA1MjAyMA
https://www.youtube.com/embed/DoR08T26IPU?playlist=QyPRTblP2-4,OyiNc7zNN9c

[2]

Embeding YouTube player in MediaWiki

All the above refer to pure HTML webpages. In MediaWiki we cannot just use raw iframe code. Instead we can use the Widget:YouTube which does the embeding in a wiki-secure way.

So, to embed an on-the-fly playlist, along with the param id we use the extra param additionalids with a comma separated list of IDs, as follows:

{{#widget:YouTube|id=VIDEO_ID_1|additionalids=VIDEO_ID_2,VIDEO_ID_3, … ,VIDEO_ID_n}}

where id is the video ID of the first video and additionalids is a comma separated list of the additional video IDs, i.e. from the second to the n-th one.

For example:

{{#widget:YouTube|id=ntpt0fq3Jn8|additionalids=iZ9vkd7Rp-g,L4GrDOOIIbk}}

gives:

Boom! We've just embeded a playlist from three separate video IDs!

Get the video IDs from a semantic query

The query

Let's suppose that we have a Category:YouTube videos where each page stores the details of a YouTube video such as title (that could be the page's title), video ID, publication date, views etc. Note, that we can easily populate these pages with the video data via the YouTube API using the ExternalData extension.

So, a simple query to get a comma separated list of video IDs would look like this:

{{#ask: [[Category:YouTube videos]]
| mainlabel=-
| ?Has video ID=
| sort=Is published on
| order=descending
| limit=10
| searchlabel=
}}

Where:

  • mainlabel=- to omit the page titles from results. That way we get a list, otherwise we would get a table.
  • ?Has video ID=(nothing) so in the results we get only the video ID without the property name. Otherwise we would get: Has video ID:ntpt0fq3Jn8, Has video ID:iZ9vkd7Rp-g, etc
  • sort=Is published on and order=descending: We may sort this list any way that we want. In this example it is sorted by publication date in reverse order, i.e. latest videos first.
  • searchlabel=(nothing) to omit the ...further results message at the end of the list. We don't want to feed that too into YouTube URL!

A query like this would produce a comma separated list of plain video IDs such as: ntpt0fq3Jn8|additionalids=iZ9vkd7Rp-g,L4GrDOOIIbk, etc

Format the list

The problem now is that the list contains spaces between items. Moreover we need the first ID separate from the others.

There are two ways to deal with this:

  1. Using a template inside the query to manually format the result without the spaces. That way we will need two queries, one to get the first ID and one to get the list of the rest of the IDs.
  2. Using some other tool such as the StringFunctions extension to manipulate the query result. That way we only need one query.

I personally prefer the second method because it uses just one query. Imagine that all this will eventually end up inside a template, or more for that matter. In a big wiki, one query more could mean millions of extra queries in the long run. Which means more processing time, more errors, more maintenance, and so one. In a semantic wiki it is essential to keep the least number of queries possible. That's why I have come to appreciate the power of the Variables[3] extension and use it almost everywhere. I will use it here as well.

Let's begin with storing the query output into a variable named query_output:

{{#vardefine:query_output|{{#ask: … }} }}

First template

Create a template to get the first item of a comma separated list. The StringFunctions[4] extension is required. I actually use this template on this wiki.

Template:First item of a comma separated list [view code]

{{#explode:{{{1|}}}|,|0}}

Second template

Create another template to get the rest of the items of a comma separated list, i.e. minus the first item, and remove spaces.

Template:Comma separated list minus first item spaces removed [view code]

{{#replace:{{#explode:{{{1|}}}|,|1|2}}| |}}

Wrap it all up

The widget call should look like this:

{{#widget:YouTube
 | id={{first item of a comma separated list|{{#var:query_output}} }}
 | additionalids={{Comma separated list minus first item spaces removed|{{#var:query_output}} }}
}}

Let's put them all together:

{{#vardefine:query_output
 | {{#ask: [[Category:YouTube videos]]
   | mainlabel=-
   | ?Has video ID=
   | sort=Is published on
   | order=descending
   | limit=10
   | searchlabel=
  }}
}}

{{#widget:YouTube
 | id={{first item of a comma separated list|{{#var:query_output}} }}
 | additionalids={{Comma separated list minus first item spaces removed|{{#var:query_output}} }}
}}


References

Comments

blog comments powered by Disqus