1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-04-16 18:15:48 +02:00

Include tweet author and quality in image download filename

This commit is contained in:
chylex 2017-08-05 21:32:07 +02:00
parent 37c5fba162
commit 48c38f6e1d
4 changed files with 32 additions and 16 deletions
Core
Resources/Scripts

View File

@ -14,11 +14,12 @@ sealed class TweetDeckBridge{
public static string LastRightClickedImage = string.Empty; public static string LastRightClickedImage = string.Empty;
public static string LastHighlightedTweet = string.Empty; public static string LastHighlightedTweet = string.Empty;
public static string LastHighlightedQuotedTweet = string.Empty; public static string LastHighlightedQuotedTweet = string.Empty;
public static string LastHighlightedTweetAuthor = string.Empty;
public static string[] LastHighlightedTweetImages = StringUtils.EmptyArray; public static string[] LastHighlightedTweetImages = StringUtils.EmptyArray;
public static Dictionary<string, string> SessionData = new Dictionary<string, string>(2); public static Dictionary<string, string> SessionData = new Dictionary<string, string>(2);
public static void ResetStaticProperties(){ public static void ResetStaticProperties(){
LastRightClickedLink = LastRightClickedImage = LastHighlightedTweet = LastHighlightedQuotedTweet = string.Empty; LastRightClickedLink = LastRightClickedImage = LastHighlightedTweet = LastHighlightedQuotedTweet = LastHighlightedTweetAuthor = string.Empty;
LastHighlightedTweetImages = StringUtils.EmptyArray; LastHighlightedTweetImages = StringUtils.EmptyArray;
} }
@ -63,10 +64,11 @@ public void SetLastRightClickedImage(string link){
form.InvokeAsyncSafe(() => LastRightClickedImage = link); form.InvokeAsyncSafe(() => LastRightClickedImage = link);
} }
public void SetLastHighlightedTweet(string link, string quotedLink, string imageList){ public void SetLastHighlightedTweet(string link, string quotedLink, string author, string imageList){
form.InvokeAsyncSafe(() => { form.InvokeAsyncSafe(() => {
LastHighlightedTweet = link; LastHighlightedTweet = link;
LastHighlightedQuotedTweet = quotedLink; LastHighlightedQuotedTweet = quotedLink;
LastHighlightedTweetAuthor = author;
LastHighlightedTweetImages = imageList.Split(';'); LastHighlightedTweetImages = imageList.Split(';');
}); });
} }

View File

@ -32,6 +32,7 @@ private static string GetImage(IContextMenuParams parameters){
private readonly Form form; private readonly Form form;
private string lastHighlightedTweetAuthor;
private string[] lastHighlightedTweetImageList; private string[] lastHighlightedTweetImageList;
protected ContextMenuBase(Form form){ protected ContextMenuBase(Form form){
@ -40,9 +41,11 @@ protected ContextMenuBase(Form form){
public virtual void OnBeforeContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model){ public virtual void OnBeforeContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model){
bool hasTweetImage = !string.IsNullOrEmpty(TweetDeckBridge.LastRightClickedImage); bool hasTweetImage = !string.IsNullOrEmpty(TweetDeckBridge.LastRightClickedImage);
lastHighlightedTweetAuthor = TweetDeckBridge.LastHighlightedTweetAuthor;
lastHighlightedTweetImageList = TweetDeckBridge.LastHighlightedTweetImages; lastHighlightedTweetImageList = TweetDeckBridge.LastHighlightedTweetImages;
if (!TwitterUtils.IsTweetDeckWebsite(frame) || browser.IsLoading){ if (!TwitterUtils.IsTweetDeckWebsite(frame) || browser.IsLoading){
lastHighlightedTweetAuthor = string.Empty;
lastHighlightedTweetImageList = StringUtils.EmptyArray; lastHighlightedTweetImageList = StringUtils.EmptyArray;
} }
@ -88,11 +91,11 @@ public virtual bool OnContextMenuCommand(IWebBrowser browserControl, IBrowser br
break; break;
case MenuSaveImage: case MenuSaveImage:
TwitterUtils.DownloadImage(GetImage(parameters), ImageQuality); TwitterUtils.DownloadImage(GetImage(parameters), lastHighlightedTweetAuthor, ImageQuality);
break; break;
case MenuSaveAllImages: case MenuSaveAllImages:
TwitterUtils.DownloadImages(lastHighlightedTweetImageList, ImageQuality); TwitterUtils.DownloadImages(lastHighlightedTweetImageList, lastHighlightedTweetAuthor, ImageQuality);
break; break;
case MenuCopyImageUrl: case MenuCopyImageUrl:

View File

@ -2,6 +2,7 @@
using CefSharp; using CefSharp;
using System.Drawing; using System.Drawing;
using System.IO; using System.IO;
using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Windows.Forms; using System.Windows.Forms;
using TweetDuck.Core.Other; using TweetDuck.Core.Other;
@ -52,23 +53,32 @@ public static string GetImageLink(string url, ImageQuality quality){
} }
} }
public static void DownloadImage(string url, ImageQuality quality){ public static void DownloadImage(string url, string username, ImageQuality quality){
DownloadImages(new string[]{ url }, quality); DownloadImages(new string[]{ url }, username, quality);
} }
public static void DownloadImages(string[] urls, ImageQuality quality){ public static void DownloadImages(string[] urls, string username, ImageQuality quality){
if (urls.Length == 0){ if (urls.Length == 0){
return; return;
} }
string file = BrowserUtils.GetFileNameFromUrl(ExtractImageBaseLink(urls[0])); string firstImageLink = GetImageLink(urls[0], quality);
int qualityIndex = firstImageLink.LastIndexOf(':');
string file = BrowserUtils.GetFileNameFromUrl(ExtractImageBaseLink(firstImageLink));
string ext = Path.GetExtension(file); // includes dot string ext = Path.GetExtension(file); // includes dot
string[] fileNameParts = {
username,
Path.ChangeExtension(file, null),
qualityIndex == -1 ? string.Empty : firstImageLink.Substring(qualityIndex+1)
};
using(SaveFileDialog dialog = new SaveFileDialog{ using(SaveFileDialog dialog = new SaveFileDialog{
AutoUpgradeEnabled = true, AutoUpgradeEnabled = true,
OverwritePrompt = urls.Length == 1, OverwritePrompt = urls.Length == 1,
Title = "Save image", Title = "Save image",
FileName = file, FileName = $"{string.Join(" ", fileNameParts.Where(part => part.Length > 0))}{ext}",
Filter = (urls.Length == 1 ? "Image" : "Images")+(string.IsNullOrEmpty(ext) ? " (unknown)|*.*" : $" (*{ext})|*{ext}") Filter = (urls.Length == 1 ? "Image" : "Images")+(string.IsNullOrEmpty(ext) ? " (unknown)|*.*" : $" (*{ext})|*{ext}")
}){ }){
if (dialog.ShowDialog() == DialogResult.OK){ if (dialog.ShowDialog() == DialogResult.OK){
@ -77,14 +87,14 @@ void OnFailure(Exception ex){
} }
if (urls.Length == 1){ if (urls.Length == 1){
BrowserUtils.DownloadFileAsync(GetImageLink(urls[0], quality), dialog.FileName, null, OnFailure); BrowserUtils.DownloadFileAsync(firstImageLink, dialog.FileName, null, OnFailure);
} }
else{ else{
string pathBase = Path.ChangeExtension(dialog.FileName, null); string pathBase = Path.ChangeExtension(dialog.FileName, null);
string pathExt = Path.GetExtension(dialog.FileName); string pathExt = Path.GetExtension(dialog.FileName);
for(int index = 0; index < urls.Length; index++){ for(int index = 0; index < urls.Length; index++){
BrowserUtils.DownloadFileAsync(GetImageLink(urls[index], quality), pathBase+(index+1)+pathExt, null, OnFailure); BrowserUtils.DownloadFileAsync(GetImageLink(urls[index], quality), $"{pathBase} {index+1}{pathExt}", null, OnFailure);
} }
} }
} }

View File

@ -391,12 +391,12 @@
return !!highlightedColumnObj; return !!highlightedColumnObj;
}; };
var updateHighlightedTweet = function(ele, obj, link, embeddedLink, imageList){ var updateHighlightedTweet = function(ele, obj, link, embeddedLink, author, imageList){
highlightedTweetEle = ele; highlightedTweetEle = ele;
highlightedTweetObj = obj; highlightedTweetObj = obj;
if (lastTweet !== link){ if (lastTweet !== link){
$TD.setLastHighlightedTweet(link, embeddedLink, imageList); $TD.setLastHighlightedTweet(link, embeddedLink, author, imageList);
lastTweet = link; lastTweet = link;
} }
}; };
@ -426,18 +426,19 @@
if (tweet.chirpType === TD.services.ChirpBase.TWEET){ if (tweet.chirpType === TD.services.ChirpBase.TWEET){
let link = tweet.getChirpURL(); let link = tweet.getChirpURL();
let embedded = tweet.quotedTweet ? tweet.quotedTweet.getChirpURL() : ""; let embedded = tweet.quotedTweet ? tweet.quotedTweet.getChirpURL() : "";
let username = tweet.getMainUser().screenName;
let images = tweet.hasImage() ? tweet.getMedia().filter(item => !item.isAnimatedGif).map(item => item.entity.media_url_https+":small").join(";") : ""; let images = tweet.hasImage() ? tweet.getMedia().filter(item => !item.isAnimatedGif).map(item => item.entity.media_url_https+":small").join(";") : "";
// TODO maybe handle embedded images too? // TODO maybe handle embedded images too?
updateHighlightedTweet(me, tweet, link || "", embedded || "", images); updateHighlightedTweet(me, tweet, link || "", embedded || "", username, images);
} }
else{ else{
updateHighlightedTweet(me, tweet, "", "", ""); updateHighlightedTweet(me, tweet, "", "", "", "");
} }
} }
} }
else if (e.type === "mouseleave"){ else if (e.type === "mouseleave"){
updateHighlightedTweet(null, null, "", "", ""); updateHighlightedTweet(null, null, "", "", "", "");
} }
}); });
})(); })();