src/Tweeper.php: fix rendering Instagram images in some feed readers
[tweeper.git] / src / rss_converter_instagram.com.xsl
index 58cc19c..71b60a7 100644 (file)
@@ -1,7 +1,7 @@
 <!--
   Stylesheet to convert Instagram user timelines to RSS.
 
 <!--
   Stylesheet to convert Instagram user timelines to RSS.
 
-  Copyright (C) 2015  Antonio Ospite <ao2@ao2.it>
+  Copyright (C) 2015-2018  Antonio Ospite <ao2@ao2.it>
 
   This file is part of tweeper.
 
 
   This file is part of tweeper.
 
 <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:php="http://php.net/xsl"
 <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:php="http://php.net/xsl"
-    xsl:extension-element-prefixes="php"
     exclude-result-prefixes="php">
 
     <xsl:param name="generate-enclosure"/>
     exclude-result-prefixes="php">
 
     <xsl:param name="generate-enclosure"/>
+    <xsl:param name="show-usernames"/>
+    <xsl:param name="show-multimedia"/>
 
     <xsl:output method="xml" indent="yes"/>
 
 
     <xsl:output method="xml" indent="yes"/>
 
@@ -32,7 +33,7 @@
         <xsl:text>https://instagram.com</xsl:text>
     </xsl:variable>
 
         <xsl:text>https://instagram.com</xsl:text>
     </xsl:variable>
 
-    <xsl:variable name="user-name" select="//ProfilePage/user/username"/>
+    <xsl:variable name="user-name" select="//ProfilePage/graphql/user/username"/>
 
     <!--
          NOTE: some users do not specify the full name.
 
     <!--
          NOTE: some users do not specify the full name.
          Remember to handle this case when using it and fall-back to the plain
          user name when appropriate.
     -->
          Remember to handle this case when using it and fall-back to the plain
          user name when appropriate.
     -->
-    <xsl:variable name="full-name" select="//ProfilePage/user/full_name"/>
+    <xsl:variable name="full-name" select="//ProfilePage/graphql/user/full_name"/>
 
 
-    <xsl:variable name="location-name" select="//LocationsPage/location/name"/>
+    <xsl:variable name="location-name" select="//LocationsPage/graphql/location/name"/>
+
+    <xsl:variable name="hashtag-name" select="//TagPage/graphql/hashtag/name"/>
 
     <xsl:variable name="screen-name">
         <xsl:choose>
             <xsl:when test="$location-name != ''">
 
     <xsl:variable name="screen-name">
         <xsl:choose>
             <xsl:when test="$location-name != ''">
-                <xsl:variable name="location-latitude" select="//LocationsPage/location/lat"/>
-                <xsl:variable name="location-longitude" select="//LocationsPage/location/lng"/>
-                <xsl:value-of select="concat($location-name, ' (', $location-latitude, ', ', $location-longitude, ')')"/>
+                <xsl:value-of select="$location-name"/>
+            </xsl:when>
+            <xsl:when test="$hashtag-name != ''">
+                <xsl:value-of select="$hashtag-name"/>
             </xsl:when>
             <xsl:when test="$full-name != ''">
                 <xsl:value-of select="$full-name"/>
             </xsl:when>
             <xsl:when test="$full-name != ''">
                 <xsl:value-of select="$full-name"/>
         </xsl:choose>
     </xsl:variable>
 
         </xsl:choose>
     </xsl:variable>
 
-    <xsl:template match="//media/nodes">
-        <xsl:variable name="item-content-image" select="./display_src"/>
-        <xsl:variable name="item-content-caption" select="./caption"/>
-        <xsl:variable name="item-permalink" select="concat($BaseURL, '/p/', ./code, '/')"/>
+    <xsl:template match="//edges/node">
+        <xsl:variable name="item-content-image" select="./display_url"/>
+        <xsl:variable name="item-content-image-data" select="php:functionString('Tweeper\Tweeper::generateDataURL', $item-content-image)"/>
+        <xsl:variable name="item-content-caption" select="./edge_media_to_caption/edges/node/text"/>
+        <xsl:variable name="item-permalink" select="concat($BaseURL, '/p/', ./shortcode, '/')"/>
         <item>
             <title>
                 <xsl:variable name="title-length" select="140"/>
         <item>
             <title>
                 <xsl:variable name="title-length" select="140"/>
-                <xsl:variable name="item-content-title" select="normalize-space(concat($user-name, ': ', $item-content-caption))"/>
+                <xsl:variable name="item-content-title">
+                    <xsl:if test="$show-usernames = 1">
+                        <xsl:value-of select="concat($screen-name, ': ')"/>
+                    </xsl:if>
+                    <xsl:value-of select="normalize-space($item-content-caption)"/>
+                </xsl:variable>
                 <!-- ellipsize, inspired from http://stackoverflow.com/questions/13622338 -->
                 <xsl:choose>
                     <xsl:when test="string-length($item-content-title) > $title-length">
                 <!-- ellipsize, inspired from http://stackoverflow.com/questions/13622338 -->
                 <xsl:choose>
                     <xsl:when test="string-length($item-content-title) > $title-length">
@@ -87,7 +97,7 @@
                 <xsl:value-of select="$item-permalink"/>
             </guid>
             <pubDate>
                 <xsl:value-of select="$item-permalink"/>
             </guid>
             <pubDate>
-                <xsl:variable name="timestamp" select="./date"/>
+                <xsl:variable name="timestamp" select="./taken_at_timestamp"/>
                 <xsl:value-of select="php:functionString('Tweeper\Tweeper::epochToRssDate', $timestamp)"/>
             </pubDate>
             <description>
                 <xsl:value-of select="php:functionString('Tweeper\Tweeper::epochToRssDate', $timestamp)"/>
             </pubDate>
             <description>
                     </xsl:if>
                     <xsl:value-of select="$item-content-caption"/>
                 </p><br />
                     </xsl:if>
                     <xsl:value-of select="$item-content-caption"/>
                 </p><br />
-                <a href="{$item-permalink}"><img src="{$item-content-image}" style="max-width: 100%"/></a>
+                <xsl:if test="$show-multimedia = 1">
+                    <a href="{$item-permalink}"><img src="{$item-content-image-data}" style="max-width: 100%"/></a>
+                </xsl:if>
                 <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
             </description>
             <xsl:if test="$generate-enclosure = 1">
                 <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
             </description>
             <xsl:if test="$generate-enclosure = 1">
         <xsl:variable name="channel-link">
             <xsl:choose>
                 <xsl:when test="$location-name != ''">
         <xsl:variable name="channel-link">
             <xsl:choose>
                 <xsl:when test="$location-name != ''">
-                    <xsl:variable name="location-id" select="//LocationsPage/location/id"/>
+                    <xsl:variable name="location-id" select="//LocationsPage/graphql/location/id"/>
                     <xsl:value-of select="concat($BaseURL, '/explore/locations/', $location-id)"/>
                 </xsl:when>
                     <xsl:value-of select="concat($BaseURL, '/explore/locations/', $location-id)"/>
                 </xsl:when>
+                <xsl:when test="$hashtag-name != ''">
+                    <xsl:value-of select="concat($BaseURL, '/explore/tags/', $hashtag-name)"/>
+                </xsl:when>
                 <xsl:otherwise>
                     <xsl:value-of select="concat($BaseURL, '/', $user-name)"/>
                 </xsl:otherwise>
             </xsl:choose>
         </xsl:variable>
                 <xsl:otherwise>
                     <xsl:value-of select="concat($BaseURL, '/', $user-name)"/>
                 </xsl:otherwise>
             </xsl:choose>
         </xsl:variable>
+        <xsl:variable name="channel-image" select="//profile_pic_url"/>
 
         <rss version="2.0">
             <xsl:attribute name="xml:base"><xsl:value-of select="$BaseURL" /></xsl:attribute>
 
         <rss version="2.0">
             <xsl:attribute name="xml:base"><xsl:value-of select="$BaseURL" /></xsl:attribute>
                 </link>
                 <description>
                     <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
                 </link>
                 <description>
                     <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
-                    <xsl:value-of select="normalize-space(concat($screen-name, '. ', //user/biography))"/>
-                    <xsl:variable name="external-url" select="//user/external_url"/>
-                    <xsl:if test="$external-url != ''">
-                        <xsl:text> </xsl:text><a href="{$external-url}"><xsl:value-of select="$external-url"/></a>
-                    </xsl:if>
+                    <xsl:choose>
+                        <xsl:when test="$location-name != ''">
+                            <xsl:variable name="location-latitude" select="//LocationsPage/graphql/location/lat"/>
+                            <xsl:variable name="location-longitude" select="//LocationsPage/graphql/location/lng"/>
+                            <xsl:value-of select="concat($location-name, ' (', $location-latitude, ', ', $location-longitude, ')')"/>
+                        </xsl:when>
+                        <xsl:when test="$hashtag-name != ''">
+                            <xsl:value-of select="concat('#', $hashtag-name)"/>
+                        </xsl:when>
+                        <xsl:otherwise>
+                            <xsl:value-of select="normalize-space(concat($screen-name, '. ', //user/biography))"/>
+                            <xsl:variable name="external-url" select="//user/external_url"/>
+                            <xsl:if test="$external-url != ''">
+                                <xsl:text> </xsl:text><a href="{$external-url}"><xsl:value-of select="$external-url"/></a>
+                            </xsl:if>
+                        </xsl:otherwise>
+                    </xsl:choose>
                     <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
                 </description>
                     <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
                 </description>
-                <image>
-                    <title>
-                        <xsl:value-of select="$channel-title"/>
-                    </title>
-                    <link>
-                        <xsl:value-of select="$channel-link"/>
-                    </link>
-                    <url>
-                        <xsl:value-of select="//ProfilePage/user/profile_pic_url"/>
-                    </url>
-                </image>
-                <xsl:apply-templates select="//ProfilePage/user/media/nodes|//LocationsPage/location/media/nodes"/>
+                <xsl:if test="$channel-image != ''">
+                    <image>
+                        <title>
+                            <xsl:value-of select="$channel-title"/>
+                        </title>
+                        <link>
+                            <xsl:value-of select="$channel-link"/>
+                        </link>
+                        <url>
+                            <xsl:value-of select="$channel-image"/>
+                        </url>
+                    </image>
+                </xsl:if>
+                <xsl:apply-templates select="//ProfilePage/graphql/user/edge_owner_to_timeline_media/edges/node|//LocationsPage/graphql/location/edge_location_to_media/edges/node|//TagPage/graphql/hashtag/edge_hashtag_to_media/edges/node"/>
             </channel>
         </rss>
     </xsl:template>
             </channel>
         </rss>
     </xsl:template>