JDownloader Community - Appwork GmbH
 

Reply
 
Thread Tools Display Modes
  #1  
Old 24.11.2024, 16:26
wkearney99 wkearney99 is offline
DSL Light User
 
Join Date: May 2024
Posts: 33
Default Regex to remove multiple hashtags from filenames?

What's the 'right way' to remove multiple #hashtags from a filename?

Some sites are using #this #that #another #whatever and all kinds of other #hashtags as part of a download filename. This often results in excessively long filenames, which can cause problems with a resulting path that's too long for the filesystem.

I'd like to rename the file being grabbed to omit all of those #hashtags.

I believe this regex would do it?

/#[a-zA-Z0-9_]+/g

But I'm unclear on how to use to get all #hashtags removed from a filename.

And if not as a rule, any tips on how an Event Script would approach this?
Reply With Quote
  #2  
Old 25.11.2024, 10:51
Jiaz's Avatar
Jiaz Jiaz is offline
JD Manager
 
Join Date: Mar 2009
Location: Germany
Posts: 81,711
Default

@wkearney99: Please provide example links as it is important if those hashtags are already part of filename when they are in list or set before download.
Also helps us to provide better help on this when we can actual test it by ourselves.
__________________
JD-Dev & Server-Admin
Reply With Quote
  #3  
Old 25.11.2024, 22:12
wkearney99 wkearney99 is offline
DSL Light User
 
Join Date: May 2024
Posts: 33
Default

I don't know if this is the "best way" to do it, but I'm using this script on a packagizer hook event script:

Code:
if (linkcheckDone) {

    var myPackagizerLink = link;
    var fileName = myPackagizerLink.getName();
    var re, ext, newName;

   if (fileName !== null && fileName.includes("#")) {

    ext = fileName.slice((fileName.lastIndexOf(".") - 1 >>> 0) + 2);
    if (ext !== "") {
        ext = "." + ext.toLowerCase();
    }

    fileName = fileName.substr(0, (fileName.length - ext.length));

    rgx = new RegExp("#[a-zA-Z0-9_]+", "gi");
    newName = fileName.replace(rgx, "");

    rgx = new RegExp("\\s\\s+", "gi");
    newName = newName.replace(rgx, " ");

    newName = newName.trim();

    myPackagizerLink.setName(newName + ext);
}
}
It's grabbing the link when it's being put into a package. The filename is examined to see if it contains any #hashtag strings. If so it replaces ALL of them with an empty string. Then it looks for any duplicate space characters, does one last trim and then sets that as the new name for the file.

I thought using a 'new link has been added' hook would be better, but I wasn't sure how to get to the necessary variables. That is, the code didn't do anything when called from a new link hook.

I'd welcome feedback on whether this is the right place to have it run, or any code improvement suggestions.

I'd love to do this in just a packagizer rule, but just could not figure out the magic incantations to make it work.

Last edited by wkearney99; 26.11.2024 at 02:24. Reason: added a check for the filename including a # character
Reply With Quote
  #4  
Old 25.11.2024, 22:13
wkearney99 wkearney99 is offline
DSL Light User
 
Join Date: May 2024
Posts: 33
Default

It is REALLY opaque trying to figure out where/how scripts/rules run and what variables/contexts they have available.
Reply With Quote
  #5  
Old 25.11.2024, 22:55
Jiaz's Avatar
Jiaz Jiaz is offline
JD Manager
 
Join Date: Mar 2009
Location: Germany
Posts: 81,711
Default

@wkearney99:
You can see all available commands/objects/method in help section above the editor. You can code and then run script in test mode which will have dummy objects. You can fill those dummy objects and then use logging/alert to show results.

You can also make use of Settings->Advanced Settings->GeneralSettings.filenamereplacemap and define replace/matcher pattern that will be applied to filenames. That way you should be easily able to do what you want.
__________________
JD-Dev & Server-Admin
Reply With Quote
  #6  
Old 26.11.2024, 00:42
wkearney99 wkearney99 is offline
DSL Light User
 
Join Date: May 2024
Posts: 33
Default

Does filenamereplacemap handle Regexes? I have already edited my filenamereplacemap to handle some apostrophe variations.

Code:
{
 ":"    : "",
 "\\|"  : "¦",
 "<"    : "[",
 ">"    : "]",	
 "/"    : "\u2044",
 "\\\\" : "∖",
 "\\*"  : "#",
 "\\?"  : "¿",
 "\""   : "'",
 "‘"    : "'",
 "’"    : "'",
 "‘"    : "'",
 "\u2013" : "-",
 "\u2018" : "'"
}
The help text in the script editor is a barely formatted mess, pardon my bluntness.

Which logging? Which alerts? You get how finding the log and alert commands is a hassle in that long list, right? Yes, "it's in there" but that's it for documentation?

I know, it's free software, and it's GREAT software, but going beyond the basics of using it is like making an appointment to get your teeth pulled. You know it's going to take ALL DAY and be painful.

I swear, having an idea about adding something to how I use JD and carving out the time necessary to slog through forums to figure it out is nearly as bad as a root canal. Oh, and toss in a few "go use the search" or "read through the Event Scripter thread" posts from others do generally not speed the process.

Side rant: it would be GREAT to have the Event Scripter thread broken out into it's own sub-forum. I've suggested it here.

But hey, like I said, it's great software! I'm glad to have it, just a little annoyed by it sometimes.
Reply With Quote
  #7  
Old 26.11.2024, 11:13
Jiaz's Avatar
Jiaz Jiaz is offline
JD Manager
 
Join Date: Mar 2009
Location: Germany
Posts: 81,711
Default

Quote:
Originally Posted by wkearney99 View Post
Does filenamereplacemap handle Regexes?
Yes, those are regex and matcher

Quote:
Originally Posted by wkearney99 View Post
The help text in the script editor is a barely formatted mess, pardon my bluntness.
You get how finding the log and alert commands is a hassle in that long list, right? Yes, "it's in there" but that's it for documentation?
Everyone is welcome to help/contribute

Quote:
Originally Posted by wkearney99 View Post
Side rant: it would be GREAT to have the Event Scripter thread broken out into it's own sub-forum. I've suggested it here..
Working on it as we speak/write
Here it is, https://board.jdownloader.org/forumdisplay.php?f=52



Quote:
Originally Posted by wkearney99 View Post
I'd love to do this in just a packagizer rule, but just could not figure out the magic incantations to make it work.
Not possible with current search/matcher workflow of the Packagizer as this requires search/replace functionality
hence the recommendation to use replacemap feature. replacements are processed from top to bottom
__________________
JD-Dev & Server-Admin

Last edited by Jiaz; 26.11.2024 at 11:30.
Reply With Quote
  #8  
Old 26.11.2024, 11:41
pspzockerscene's Avatar
pspzockerscene pspzockerscene is offline
Community Manager
 
Join Date: Mar 2009
Location: Deutschland
Posts: 73,786
Default

Quote:
Originally Posted by wkearney99 View Post
Does filenamereplacemap handle Regexes?
Yes. The settings' description also says that.
Screenshot:
Spoiler:


Feel free to provide feedback for us to improve usability/settings/documentation.
__________________
JD Supporter, Plugin Dev. & Community Manager

Erste Schritte & Tutorials || JDownloader 2 Setup Download
Spoiler:

A users' JD crashes and the first thing to ask is:
Quote:
Originally Posted by Jiaz View Post
Do you have Nero installed?
Reply With Quote
  #9  
Old 26.11.2024, 21:36
wkearney99 wkearney99 is offline
DSL Light User
 
Join Date: May 2024
Posts: 33
Default

That wasn't my take-away from seeing the helper text. That and the default example only has single character matches presented.

I have since tried it with a regex and it does indeed work. Nice.

Though I think I'm going to stick with using an Event Script as it's easier to turn it on/off with a checkbox.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT +2. The time now is 13:41.
Provided By AppWork GmbH | Privacy | Imprint
Parts of the Design are used from Kirsch designed by Andrew & Austin
Powered by vBulletin® Version 3.8.10 Beta 1
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.