From ef4f6c6fb10b121fa79a0d8b6ccd7930fec48591 Mon Sep 17 00:00:00 2001 From: Antonio Ospite <ospite@studenti.unina.it> Date: Sat, 27 Jul 2013 13:24:44 +0200 Subject: [PATCH] Make stylesheet file name parametric The host is encoded in the file name, this is in order to support more sites with no changes to the code, all that is needed is just new stylesheets with the host in their filename following the scheme will: rss_converter_HOST.xsl Where HOST has the meaning of the "host" field in the return value of the PHP parse_url() function. --- rss_converter_twitter.com.xsl | 79 +++++++++++++++++++++++++++++++++++++++++++ tweeper.php | 9 ++--- twitter_user_timeline2rss.xsl | 79 ------------------------------------------- 3 files changed, 81 insertions(+), 86 deletions(-) create mode 100644 rss_converter_twitter.com.xsl delete mode 100644 twitter_user_timeline2rss.xsl diff --git a/rss_converter_twitter.com.xsl b/rss_converter_twitter.com.xsl new file mode 100644 index 0000000..6d72ceb --- /dev/null +++ b/rss_converter_twitter.com.xsl @@ -0,0 +1,79 @@ +<!-- + Stylesheet to convert Twitter user timelines to RSS. + + Copyright (C) 2013 Antonio Ospite <ospite@studenti.unina.it> + + This file is part of tweeper. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +--> +<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"> + + <xsl:output method="xml" indent="yes"/> + + <xsl:variable name="twitterBaseURL"> + <xsl:text>https://twitter.com</xsl:text> + </xsl:variable> + + <xsl:variable name="screen-name" select="//div[@class='profile-card-inner']/@data-screen-name"/> + + <xsl:template match="//div[@id='timeline']//ol[@id='stream-items-id']//li[@data-item-type='tweet']"> + <xsl:variable name="tweet-text" select=".//p[@class='js-tweet-text tweet-text']"/> + <item> + <title> + <xsl:value-of select="concat($screen-name, ': ')"/> + <xsl:value-of select="$tweet-text"/> + </title> + <link> + <xsl:value-of select="$twitterBaseURL"/><xsl:value-of select=".//a[@class='details with-icn js-details']/@href"/> + </link> + <pubDate> + <xsl:value-of select="php:functionString('formatDate', .//small[@class='time']//span/@data-time)"/> + </pubDate> + <description> + <xsl:value-of select="concat($screen-name, ': ')"/> + <xsl:text disable-output-escaping="yes"><![CDATA[</xsl:text> + <xsl:copy-of select="$tweet-text/node()"/> + <xsl:text disable-output-escaping="yes">]]></xsl:text> + </description> + </item> + </xsl:template> + + <xsl:template match="/"> + + <rss version="2.0"> + <channel> + <generator>Tweeper</generator> + <title> + <xsl:text>Twitter / </xsl:text><xsl:value-of select="$screen-name"/> + </title> + <link> + <xsl:value-of select="//link[@rel='canonical']/@href"/> + </link> + <description> + <xsl:value-of select="//meta[@name='description']/@content"/> + </description> + <image> + <url> + <xsl:value-of select="//a[@class='profile-picture media-thumbnail']/@href"/> + </url> + </image> + <xsl:apply-templates select="//div[@id='timeline']//ol[@id='stream-items-id']//li[@data-item-type='tweet']"/> + </channel> + </rss> + </xsl:template> +</xsl:stylesheet> diff --git a/tweeper.php b/tweeper.php index a275a64..4050100 100755 --- a/tweeper.php +++ b/tweeper.php @@ -95,14 +95,9 @@ $url = parse_url($src_url); if (FALSE === $url || empty($url["host"])) die("Invalid url\n"); -$stylesheets = array( - "twitter.com" => "twitter_user_timeline2rss.xsl", -); - -if (FALSE === array_key_exists($url["host"], $stylesheets)) +$stylesheet = "rss_converter_" . $url["host"] . ".xsl"; +if (FALSE === file_exists($stylesheet)) die("Unsupported host\n"); -$stylesheet = $stylesheets[$url["host"]]; - $tweeper = new Tweeper($stylesheet); echo $tweeper->tweep($src_url); diff --git a/twitter_user_timeline2rss.xsl b/twitter_user_timeline2rss.xsl deleted file mode 100644 index 6d72ceb..0000000 --- a/twitter_user_timeline2rss.xsl +++ /dev/null @@ -1,79 +0,0 @@ -<!-- - Stylesheet to convert Twitter user timelines to RSS. - - Copyright (C) 2013 Antonio Ospite <ospite@studenti.unina.it> - - This file is part of tweeper. - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see <http://www.gnu.org/licenses/>. ---> -<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"> - - <xsl:output method="xml" indent="yes"/> - - <xsl:variable name="twitterBaseURL"> - <xsl:text>https://twitter.com</xsl:text> - </xsl:variable> - - <xsl:variable name="screen-name" select="//div[@class='profile-card-inner']/@data-screen-name"/> - - <xsl:template match="//div[@id='timeline']//ol[@id='stream-items-id']//li[@data-item-type='tweet']"> - <xsl:variable name="tweet-text" select=".//p[@class='js-tweet-text tweet-text']"/> - <item> - <title> - <xsl:value-of select="concat($screen-name, ': ')"/> - <xsl:value-of select="$tweet-text"/> - </title> - <link> - <xsl:value-of select="$twitterBaseURL"/><xsl:value-of select=".//a[@class='details with-icn js-details']/@href"/> - </link> - <pubDate> - <xsl:value-of select="php:functionString('formatDate', .//small[@class='time']//span/@data-time)"/> - </pubDate> - <description> - <xsl:value-of select="concat($screen-name, ': ')"/> - <xsl:text disable-output-escaping="yes"><![CDATA[</xsl:text> - <xsl:copy-of select="$tweet-text/node()"/> - <xsl:text disable-output-escaping="yes">]]></xsl:text> - </description> - </item> - </xsl:template> - - <xsl:template match="/"> - - <rss version="2.0"> - <channel> - <generator>Tweeper</generator> - <title> - <xsl:text>Twitter / </xsl:text><xsl:value-of select="$screen-name"/> - </title> - <link> - <xsl:value-of select="//link[@rel='canonical']/@href"/> - </link> - <description> - <xsl:value-of select="//meta[@name='description']/@content"/> - </description> - <image> - <url> - <xsl:value-of select="//a[@class='profile-picture media-thumbnail']/@href"/> - </url> - </image> - <xsl:apply-templates select="//div[@id='timeline']//ol[@id='stream-items-id']//li[@data-item-type='tweet']"/> - </channel> - </rss> - </xsl:template> -</xsl:stylesheet> -- 2.1.4