1

I'm a complete python noob, so please go easy.

I'm currently hacking/editing a kodi plugin called pseudo library, so that it cleans up the titles of the streams I'm grabbing so that I can put it into a better looking EPG.

Currently they look like this:

[COLOR white]3E (Now - 07 - 30 That '70s Show) - .strm

I've identified the code that produces this here:

FleName = (title + ' - ' + eptitle + '.strm').replace(":"," - ")
FleName = re.sub('[\/:*?<>|!@#$/:]', '', FleName)

and edited as follows (messy I know and I'm sure there is a much better way, as I said above I'm a noob!)

FleName = (title + '.strm').replace(":"," - ").replace("[COLOR white]","").replace("[COLOR blue]","")
FleName = re.sub('[\/:*?<>|!@#$/:]', '', FleName)

This then changes the above title to:

3E (Now - 07 - 30 That '70s Show).strm

What I really want the output to be is:

3E.strm

The closest answer I can find to my problem is here:

https://stackoverflow.com/a/14599280

However I also have parentheses within parentheses to remove and the above does not solve that e.g.

Zee Cinema (Now - 19 - 15 Baazigar (1993)).strm

I've looked at strip to remove all characters after and including "(Now" but can't quite work it out. Please can someone provide a universal solution to my problem above so that whether the title is

[COLOR white]3E (Now - 07 - 30 That '70s Show) - .strm OR

[COLOR white]Zee Cinema (Now - 19 - 15 Baazigar (1993)).strm

that it outputs just the title and .strm. So in the examples above:

3E.strm
Zee Cinema.strm

Many thanks for looking and for hopefully helping me resolve my issue.

Community
  • 1
  • 1
user1358120
  • 193
  • 1
  • 4
  • 18

3 Answers3

4

FileName.split(']')[1].split('(')[0].strip() + ".strm"

Craig Burgler
  • 1,749
  • 10
  • 19
2

So you essentially you have something of the form [something]text you want (something else).strm? The easiest way to solve this is to just ignore everything after the opening ( and before the extension:

re.sub(r"^[^\]]+\]([^(]+) \(.*\.strm$",r"\1.strm",FleName)

Be aware of failure modes, however. This will fail for improperly formatted filenames by not changing them at all in most cases. Craig's will fail with an exception in most cases. It's possible that a more complex solution could be made to raise an exception for a wider range of improperly formatted filename, but neither of these solutions do.

cge
  • 9,552
  • 3
  • 32
  • 51
0

Based on the pattern of the original titles it seems like you need to get the text between the first '](' pair, strip the whitespace and append the extension. Here's an example:

originalFileName = "[COLOR white]3E (Now - 07 - 30 That '70s Show) - .strm"
fileName, fileExt = originalFileName.split(".")
newFileName = ".".join([re.search("\](.*?)\(", fileName).groups()[0].strip(), fileExt])