From 01dfd03d97afd0d5d06a36af37d96892905340ad Mon Sep 17 00:00:00 2001 From: Antonio Ospite Date: Tue, 13 Nov 2018 16:14:09 +0100 Subject: [PATCH 1/1] Add option to enable or disable showing usernames in RSS items Tweeper shows usernames by default in items created from multi-user sites like Twitter or Instagram. This is because the main use case is to aggregate multiple feeds in the same viewer, and in this scenario having some info about where the messages is coming from can be useful. However sometimes tweeper can be used to track one single feed and in this case having always the same username repeated over and over is unnecessary. Make showing the username optional, but keep the current behavior as default. NOTE: for Twitter keep always showing the username in case of retweets ($screen-name != $user-name). --- src/Tweeper.php | 13 +++++++++++-- src/rss_converter_instagram.com.xsl | 8 +++++++- src/rss_converter_pump.io.xsl | 10 ++++++++-- src/rss_converter_twitter.com.xsl | 11 ++++++++--- tweeper.1.asciidoc | 4 ++++ tweeper.php | 28 +++++++++++++++++++++++----- 6 files changed, 61 insertions(+), 13 deletions(-) diff --git a/src/Tweeper.php b/src/Tweeper.php index f25ea25..cfa3812 100644 --- a/src/Tweeper.php +++ b/src/Tweeper.php @@ -39,10 +39,18 @@ class Tweeper { private static $userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:60.0) Gecko/20100101 Firefox/60.0"; /** - * Constructor sets up {@link $generate_enclosure}. + * Create a new Tweeper object controlling optional settings. + * + * @param bool $generate_enclosure + * Enables the creation of elements (disabled by default). + * @param bool $show_usernames + * Enables showing the username in front of the content for multi-user + * sites (enabled by default). Only some stylesheets supports this + * functionality (twitter, instagram, pump.io). */ - public function __construct($generate_enclosure = FALSE) { + public function __construct($generate_enclosure = FALSE, $show_usernames = TRUE) { $this->generate_enclosure = $generate_enclosure; + $this->show_usernames = $show_usernames; } /** @@ -319,6 +327,7 @@ class Tweeper { $xsltProcessor = new XSLTProcessor(); $xsltProcessor->registerPHPFunctions(); $xsltProcessor->setParameter('', 'generate-enclosure', $this->generate_enclosure); + $xsltProcessor->setParameter('', 'show-usernames', $this->show_usernames); $xsltProcessor->importStylesheet($xslDoc); return $xsltProcessor; diff --git a/src/rss_converter_instagram.com.xsl b/src/rss_converter_instagram.com.xsl index 2e427b4..01bd3d1 100644 --- a/src/rss_converter_instagram.com.xsl +++ b/src/rss_converter_instagram.com.xsl @@ -24,6 +24,7 @@ exclude-result-prefixes="php"> + @@ -69,7 +70,12 @@ <xsl:variable name="title-length" select="140"/> - <xsl:variable name="item-content-title" select="normalize-space(concat($screen-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"> diff --git a/src/rss_converter_pump.io.xsl b/src/rss_converter_pump.io.xsl index 8cdc3d8..42f8ac0 100644 --- a/src/rss_converter_pump.io.xsl +++ b/src/rss_converter_pump.io.xsl @@ -25,6 +25,7 @@ exclude-result-prefixes="php"> <xsl:param name="generate-enclosure"/> + <xsl:param name="show-usernames"/> <xsl:output method="xml" indent="yes"/> @@ -38,7 +39,10 @@ <xsl:variable name="item-permalink" select=".//p[@class='muted']/small/a/@href"/> <item> <title> - <xsl:value-of select="concat($user-name, ': ', normalize-space($item-content))"/> + <xsl:if test="$show-usernames = 1"> + <xsl:value-of select="concat($user-name, ': ')"/> + </xsl:if> + <xsl:value-of select="normalize-space($item-content)"/> @@ -50,8 +54,10 @@ - <![CDATA[ + + + ]]> diff --git a/src/rss_converter_twitter.com.xsl b/src/rss_converter_twitter.com.xsl index 1d98fa2..b59a97b 100644 --- a/src/rss_converter_twitter.com.xsl +++ b/src/rss_converter_twitter.com.xsl @@ -24,6 +24,7 @@ exclude-result-prefixes="php"> + @@ -127,7 +128,9 @@ - <xsl:value-of select="concat($user-name, ': ')"/> + <xsl:if test="($show-usernames = 1) or ($screen-name != $user-name)"> + <xsl:value-of select="concat($user-name, ': ')"/> + </xsl:if> <xsl:if test="$item-has-video"> <xsl:text>(Video) </xsl:text> </xsl:if> @@ -154,8 +157,10 @@ </pubDate> <description> <xsl:text disable-output-escaping="yes"><![CDATA[</xsl:text> - <xsl:value-of select="concat($user-name, ':')"/> - <xsl:element name="br"/> + <xsl:if test="($show-usernames = 1) or ($screen-name != $user-name)"> + <xsl:value-of select="concat($user-name, ':')"/> + <xsl:element name="br"/> + </xsl:if> <xsl:if test="$item-has-video"> <xsl:text> (Video)</xsl:text> <xsl:element name="br"/> diff --git a/tweeper.1.asciidoc b/tweeper.1.asciidoc index 82b3a43..8519d3a 100644 --- a/tweeper.1.asciidoc +++ b/tweeper.1.asciidoc @@ -45,6 +45,10 @@ OPTIONS *-e*:: show links to supported media files in the RSS <enclosure/> element +*-u <0|1>*:: + enable or disable showing usernames in front of the item for hosts which + supports it (Twitter.com/Instagram.com). Default is 1 (enable). + *-h, --help*:: show the help message diff --git a/tweeper.php b/tweeper.php index b1dd021..038f2b6 100644 --- a/tweeper.php +++ b/tweeper.php @@ -37,10 +37,10 @@ function is_cli() { */ function usage($argv) { if (is_cli()) { - $usage = "{$argv[0]} [-e|-h|--help] <src_url>\n"; + $usage = "{$argv[0]} [-e|-u <0|1>|-h|--help] <src_url>\n"; } else { - $usage = htmlentities("{$_SERVER['SCRIPT_NAME']}?src_url=<src_url>&generate_enclosure=<0|1>"); + $usage = htmlentities("{$_SERVER['SCRIPT_NAME']}?src_url=<src_url>&generate_enclosure=<0|1>&show_usernames=<0|1>"); } return "usage: $usage"; @@ -52,19 +52,30 @@ function usage($argv) { function parse_options_cli($argv, $argc) { $options = array( 'generate_enclosure' => FALSE, + 'show_usernames' => TRUE, ); if ($argc < 2) { return $options; } - $cli_options = getopt("eh", array("help")); + $cli_options = getopt("eu:h", array("help")); foreach ($cli_options as $opt => $val) { switch ($opt) { case 'e': $options['generate_enclosure'] = TRUE; break; + case 'u': + $ret = filter_var($val, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); + if (NULL === $ret) { + fwrite(STDERR, "Invalid argument for the -u option.\n"); + fwrite(STDERR, usage($argv)); + exit(1); + } + $options['show_usernames'] = $val; + break; + case 'h': case 'help': echo usage($argv); @@ -76,7 +87,9 @@ function parse_options_cli($argv, $argc) { } } - $options['src_url'] = $argv[count($cli_options) + 1]; + // For now assume that the URL is the lest argument, in the future we could + // switch to PHP >= 7.1 and use the $optind argument of getopt(). + $options['src_url'] = array_pop($argv); return $options; } @@ -87,6 +100,7 @@ function parse_options_cli($argv, $argc) { function parse_options_query_string() { $options = array( 'generate_enclosure' => FALSE, + 'show_usernames' => TRUE, ); if (isset($_GET['src_url'])) { @@ -97,6 +111,10 @@ function parse_options_query_string() { $options['generate_enclosure'] = $_GET['generate_enclosure'] == 1; } + if (isset($_GET['show_usernames'])) { + $options['show_usernames'] = $_GET['show_usernames'] != 0; + } + return $options; } @@ -114,7 +132,7 @@ if (!isset($options['src_url'])) { exit(1); } -$tweeper = new Tweeper($options['generate_enclosure']); +$tweeper = new Tweeper($options['generate_enclosure'], $options['show_usernames']); $output = $tweeper->tweep($options['src_url']); if (is_null($output)) { exit(1); -- 2.1.4