summaryrefslogtreecommitdiff
path: root/templates/www.youtube.com/video.xsl
blob: 6fe1282127a085280ec3ee5a652e5592584bf635 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:str="http://exslt.org/strings"
  xmlns:map="mapping"
  exclude-result-prefixes="str map">

<!-- Mapping from Youtube fmt parameter (key) to webvideo priority
     score (value) -->
<map:associativearray>
  <map:item key="17" value="40"/> <!-- 176x144 3GP   -->
  <map:item key="5" value="50"/>  <!-- 320x240 FLV   -->
  <map:item key="34" value="52"/> <!-- 640x360 FLV   -->
  <map:item key="35" value="54"/> <!-- 854x480 FLV   -->
  <map:item key="18" value="60"/> <!-- 640x360 MP4   -->
  <map:item key="43" value="65"/> <!-- 640x360 WebM  -->
  <map:item key="44" value="67"/> <!-- 854x480 WebM  -->
  <map:item key="22" value="70"/> <!-- 720p MP4      -->
  <map:item key="45" value="75"/> <!-- 720p WebM     -->
  <map:item key="37" value="80"/> <!-- 1080p MP4     -->
</map:associativearray>

<xsl:template name="fmturl">
  <xsl:variable name="fmt">
    <xsl:for-each select="str:tokenize(., '&amp;')">
      <xsl:if test="starts-with(., 'itag=')">
	<xsl:value-of select="substring-after(., 'itag=')"/>
      </xsl:if>
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="url">
    <xsl:for-each select="str:tokenize(., '&amp;')">
      <xsl:if test="starts-with(., 'url=')">
	<xsl:value-of select="str:decode-uri(substring-after(., 'url='))"/>
      </xsl:if>
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="signature">
    <xsl:for-each select="str:tokenize(., '&amp;')">
      <xsl:if test="starts-with(., 'sig=')">
	<xsl:value-of select="str:decode-uri(substring-after(., 'sig='))"/>
      </xsl:if>
    </xsl:for-each>
  </xsl:variable>

  <xsl:if test="$url">
    <url>
      <xsl:attribute name="priority">
	<xsl:choose>
	  <xsl:when test="document('')/*/map:associativearray/map:item[@key=$fmt]">
	    <xsl:value-of select="document('')/*/map:associativearray/map:item[@key=$fmt]/@value"/>
	  </xsl:when>
	  <xsl:otherwise>
	    <xsl:value-of select="50"/>
	  </xsl:otherwise>
	</xsl:choose>
      </xsl:attribute>
      <xsl:value-of select="concat($url, '&amp;signature=', $signature)"/>
    </url>
  </xsl:if>
</xsl:template>

<xsl:template name="mediaurl">
  <xsl:param name="videoinfo"/>

  <mediaurl>
    <title>
      <xsl:variable name="titleparam">
	<xsl:choose>
	  <xsl:when test="contains(substring-after($videoinfo, '&amp;title='), '&amp;')">
	    <xsl:value-of select="substring-before(substring-after($videoinfo, '&amp;title='), '&amp;')"/>
	  </xsl:when>
	  <xsl:otherwise>
	    <xsl:value-of select="substring-after($videoinfo, '&amp;title=')"/>
	  </xsl:otherwise>
	</xsl:choose>
      </xsl:variable>
      
      <xsl:value-of select="str:decode-uri(str:replace($titleparam, '+', ' '))"/>
    </title>

    <xsl:for-each select="str:split(str:decode-uri(substring-before(substring-after($videoinfo, '&amp;url_encoded_fmt_stream_map='), '&amp;')), ',')">
      <xsl:call-template name="fmturl"/>
    </xsl:for-each>

  </mediaurl>
</xsl:template>

<xsl:template name="main">
  <xsl:call-template name="mediaurl">
    <xsl:with-param name="videoinfo" select="."/>
  </xsl:call-template>
</xsl:template>

<xsl:template match="/">
  <xsl:call-template name="main"/>
</xsl:template>

</xsl:stylesheet>