Add option to enable or disable showing usernames in RSS items
authorAntonio Ospite <ao2@ao2.it>
Tue, 13 Nov 2018 15:14:09 +0000 (16:14 +0100)
committerAntonio Ospite <ao2@ao2.it>
Fri, 16 Nov 2018 09:28:16 +0000 (10:28 +0100)
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
src/rss_converter_instagram.com.xsl
src/rss_converter_pump.io.xsl
src/rss_converter_twitter.com.xsl
tweeper.1.asciidoc
tweeper.php

index f25ea25..cfa3812 100644 (file)
@@ -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 <enclosure/> 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;
index 2e427b4..01bd3d1 100644 (file)
@@ -24,6 +24,7 @@
     exclude-result-prefixes="php">
 
     <xsl:param name="generate-enclosure"/>
+    <xsl:param name="show-usernames"/>
 
     <xsl:output method="xml" indent="yes"/>
 
         <item>
             <title>
                 <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">
index 8cdc3d8..42f8ac0 100644 (file)
@@ -25,6 +25,7 @@
     exclude-result-prefixes="php">
 
     <xsl:param name="generate-enclosure"/>
+    <xsl:param name="show-usernames"/>
 
     <xsl:output method="xml" indent="yes"/>
 
         <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)"/>
             </title>
             <link>
                 <xsl:value-of select="$item-permalink"/>
                 <xsl:value-of select="php:functionString('Tweeper\Tweeper::strToRssDate', .//abbr[@class='easydate']/@title)"/>
             </pubDate>
             <description>
-                <xsl:value-of select="concat($user-name, ': ')"/>
                 <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
+                <xsl:if test="$show-usernames = 1">
+                    <xsl:value-of select="concat($user-name, ': ')"/>
+                </xsl:if>
                 <xsl:copy-of select="$item-content/node()"/>
                 <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
             </description>
index 1d98fa2..b59a97b 100644 (file)
@@ -24,6 +24,7 @@
     exclude-result-prefixes="php">
 
     <xsl:param name="generate-enclosure"/>
+    <xsl:param name="show-usernames"/>
 
     <xsl:output method="xml" indent="yes"/>
 
         <xsl:variable name="item-has-gif" select="$item-media//*[contains(@class, 'PlayableMedia--gif')]"/>
         <item>
             <title>
-                <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>
             </pubDate>
             <description>
                 <xsl:text disable-output-escaping="yes">&lt;![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"/>
index 82b3a43..8519d3a 100644 (file)
@@ -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
 
index b1dd021..038f2b6 100644 (file)
@@ -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);