From 22a01f10937f13ebfd710e3509ccb34c20698abc Mon Sep 17 00:00:00 2001
From: somini <somini@users.noreply.github.com>
Date: Wed, 10 Jun 2020 21:39:36 +0100
Subject: [PATCH] [Twitter] Fix Twitter bridge images and add other media types
 (#1595)

* Keep old URI structure

Use the username, not the user ID.

* Fix Twitter bridge images

Credit to @kinoushe

See https://github.com/RSS-Bridge/rss-bridge/issues/1562#issuecomment-639393175

* Include Videos and "Animated GIF" as twit enclosures

Credit to @kinoushe for digging into the API docs.

https://github.com/RSS-Bridge/rss-bridge/issues/1562#issuecomment-640320688

* Calculate the highest bitrate video

Include that on the enclosure.

* Appease linter

* Appease linter, again

* Remove surrounding link from videos

Add it on a smaller link besides it.

See
https://github.com/RSS-Bridge/rss-bridge/pull/1595#issuecomment-640989208

* Include video poster on the enclosures.
---
 bridges/TwitterBridge.php | 51 +++++++++++++++++++++++++++++++++------
 1 file changed, 43 insertions(+), 8 deletions(-)

diff --git a/bridges/TwitterBridge.php b/bridges/TwitterBridge.php
index 6ba1626a..210f5d01 100644
--- a/bridges/TwitterBridge.php
+++ b/bridges/TwitterBridge.php
@@ -232,7 +232,7 @@ EOD
 			$item['avatar'] = $user_info->profile_image_url_https;
 
 			$item['id'] = $tweet->id_str;
-			$item['uri'] = self::URI . $tweet->user_id_str . '/status/' . $item['id'];
+			$item['uri'] = self::URI . $item['username'] . '/status/' . $item['id'];
 			// extract tweet timestamp
 			$item['timestamp'] = $tweet->created_at;
 
@@ -255,15 +255,17 @@ EOD;
 			}
 
 			// Get images
-			$image_html = '';
+			$media_html = '';
 			if(isset($tweet->extended_entities->media) && !$this->getInput('noimg')) {
 				foreach($tweet->extended_entities->media as $media) {
-					$image = $media->media_url_https;
-					$display_image = $media->media_url;
-					// add enclosures
-					$item['enclosures'][] = $image;
+					switch($media->type) {
+					case 'photo':
+						$image = $media->media_url_https . '?name=orig';
+						$display_image = $media->media_url_https;
+						// add enclosures
+						$item['enclosures'][] = $image;
 
-					$image_html .= <<<EOD
+						$media_html .= <<<EOD
 <a href="{$image}">
 <img
 	style="align:top; max-width:558px; border:1px solid black;"
@@ -271,6 +273,39 @@ EOD;
 	src="{$display_image}" />
 </a>
 EOD;
+						break;
+					case 'video':
+					case 'animated_gif':
+						if(isset($media->video_info)) {
+							$link = $media->expanded_url;
+							$poster = $media->media_url_https;
+							$video = null;
+							$maxBitrate = -1;
+							foreach($media->video_info->variants as $variant) {
+								$bitRate = isset($variant->bitrate) ? $variant->bitrate : -100;
+								if ($bitRate > $maxBitrate) {
+									$maxBitrate = $bitRate;
+									$video = $variant->url;
+								}
+							}
+							if(!is_null($video)) {
+								// add enclosures
+								$item['enclosures'][] = $video;
+								$item['enclosures'][] = $poster;
+
+								$media_html .= <<<EOD
+<a href="{$link}">Video</a>
+<video
+	style="align:top; max-width:558px; border:1px solid black;"
+	referrerpolicy="no-referrer"
+	src="{$video}" poster="{$poster}" />
+EOD;
+							}
+						}
+						break;
+					default:
+						Debug::log('Missing support for media type: ' . $media->type);
+					}
 				}
 			}
 
@@ -294,7 +329,7 @@ EOD;
 	<blockquote>{$cleanedTweet}</blockquote>
 </div>
 <div style="display: block; vertical-align: top;">
-	<blockquote>{$image_html}</blockquote>
+	<blockquote>{$media_html}</blockquote>
 </div>
 EOD;