Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Filter out file extension
#1
Hi all,
Am new here so please be nice :). Also new to python coding and am still learning with a lot more learning to be done.

I have coded a simple Python script which takes an m3u playlist and converts it into an XML that is playable with livestreamer pro addon in kodi, thing is, I can't figure out how to pass .TS links thru f4mproxy only and not any .Mp4 / .Mkv links etc

[color=#000000]import os, re

## Enter file location here ie: C:/temp/PythonTests
dir = ' '

## Enter name of playlist less .m3u extension
input_file = ' '
input_extn = '.m3u'
output_file = input_file+'.xml'

in_path = (dir)+'\\'+(input_file)+input_extn
out_path = (dir)+'\\'+(output_file)

in_file = open(in_path, 'r')
in_txt = in_file.read()

out_file = open(out_path, 'w')
m3u_regex = '#.+,(.+?)\n(.+?)\n'


link = in_txt
match = re.compile(m3u_regex).findall(link)
out_file.write ('<streamingInfos>\n\n')
for title, url in match:
    url = url.replace('&', '&amp;').replace('rtmp://$OPT:rtmp-raw=', '').strip()
    title = title.strip()
    out_file.write('<item>\n<title>' + title + '</title>\n<link>plugin://plugin.video.f4mTester/?streamtype=TSDOWNLOADER;name=' + title + '&amp;url=' + url + '</link>\n<thumbnail>' + ' ' + '</thumbnail>\n</item>\n\n')
    # print('<item>\n<title>' + title + '</title>\n<link>plugin://plugin.video.f4mTester/?streamtype=TSDOWNLOADER&amp;url=' + url + '</link>\n<thumbnail>' + ' ' + '</thumbnail>\n</item>\n\n')

out_file.close()
in_file.close()
[/color]
Reply
#2
you could just use splitext to split off the extension and check against an ignore set.

import os


files = ["some_file.png", "some_file.jpg", "some_file.bat",
         "some_file.exe", "some_file.dat"]

ignore = {".jpg", ".png"}


for filename in files:
    name, ext = os.path.splitext(filename)
    if ext not in ignore:
        print(filename)
Reply
#3
(Jan-06-2017, 01:15 AM)Mekire Wrote: you could just use splitext to split off the extension and check against an ignore set.

import os


files = ["some_file.png", "some_file.jpg", "some_file.bat",
         "some_file.exe", "some_file.dat"]

ignore = {".jpg", ".png"}


for filename in files:
    name, ext = os.path.splitext(filename)
    if ext not in ignore:
        print(filename)

How would this work with .'s in the url.  Http://someurl.com:8000/username/password/file.ts

It would look for the etxn being after the first dot . Yes?  And not all URLs would be the same either.  Some maybe an IP address which would have more dots  .'s in it.  Ie xxx.xxx.xxx.xxx:8080/username/password/file.ts
So I'd need to apply the filter only after the last / ?
Reply
#4
(Jan-06-2017, 02:45 AM)Kiwi_man82 Wrote: How would this work with .'s in the url. Http://someurl.com:8000/username/password/file.ts

It would look for the etxn being after the first dot . Yes? And not all URLs would be the same either. Some maybe an IP address which would have more dots .'s in it. Ie xxx.xxx.xxx.xxx:8080/username/password/file.ts
So I'd need to apply the filter only after the last / ?

os.path.splitext splits on the last dot, not the first.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Or os.path.basename to not get a tuple output.
>>> import os
>>> os.path.basename('Http://someurl.com:8000/username/password/file.ts')
'file.ts'
>>> os.path.basename('xxx.xxx.xxx.xxx:8080/username/password/file.ts')
'file.ts'
Reply
#6
Awesome thanks guys.  I'll give it a go and see how I get on.  Any more questions I know we're to come too.  Thanks heaps ??
Reply
#7
Thanks guys, finally got it figured out.
Here is the final code


import os, re


## Enter file location here ie: C:/temp/PythonTests
dir = ' '

## Enter name of playlist less .m3u extension
input_file = ' '
input_extn = '.m3u'
output_file = input_file+'.xml'


in_path = (dir)+'\\'+(input_file)+input_extn
out_path = (dir)+'\\'+(output_file)

in_file = open(in_path, 'r')
in_txt = in_file.read()

out_file = open(out_path, 'w')
m3u_regex = '#.+,(.+?)\n(.+?)\n'

ignore = {'.mp4', '.mkv'}


link = in_txt
match = re.compile(m3u_regex).findall(link)
out_file.write ('<streamingInfos>\n\n')
for title, url in match:
   url = url.replace('&', '&amp;').replace('rtmp://$OPT:rtmp-raw=', '').strip()
   title = title.strip()
   ignore = {".mp4", ".mkv"}
   name, ext = os.path.splitext(url)
   if ext not in ignore:
       print('<item>\n<title>' + title + '</title>\n<link>plugin://plugin.video.f4mTester/?streamtype=TSDOWNLOADER&amp;url=' + url + '</link>\n<thumbnail>' + ' ' + '</thumbnail>\n</item>\n\n')
   else:
       print('<item>\n<title>' + title + '</title>\n<link>' + url + '</link>\n<thumbnail>' + ' ' + '</thumbnail>\n</item>\n\n')
out_file.close()
in_file.close()
Reply
#8
Could I also use the same method to pull out say, all the sports links only and use that to create a sports XML that only contains sports links? Having fun learning python and it's capabilities. (I know it's not related to my op but didn't want to start a new thread for a very similar question)
Reply
#9
It's possible, but it depends on the links. Is there something in the links that identifies them as sports links? Is there something at the destination of the link that identifies it as a sports link? If there is, you could use that to gather only the sports links.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#10
(Jan-08-2017, 04:09 PM)ichabod801 Wrote: It's possible, but it depends on the links. Is there something in the links that identifies them as sports links? Is there something at the destination of the link that identifies it as a sports link? If there is, you could use that to gather only the sports links.

I actually ended up playing with a regex and managed to do it that way :)
Thanks for the reply all the same
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  filter every 24 days file (considering file name) RolanRoll 5 2,203 Jun-23-2022, 11:41 AM
Last Post: RolanRoll
  Filter Excel and Convert an Excel File giddyhead 0 2,211 May-13-2021, 06:31 PM
Last Post: giddyhead
  find file not knowing the extension Leon79 6 3,065 Jul-07-2020, 04:44 PM
Last Post: Leon79
  Can't open/read txt file in C extension for Python Rad226 8 4,754 Jun-26-2020, 04:08 PM
Last Post: Rad226
  how to just filter .mp4 in the text file jacklee26 1 1,962 Mar-22-2020, 06:59 AM
Last Post: buran
  How to get file name without the full path details and without extension aruncom2006 1 5,899 Jan-13-2020, 07:37 AM
Last Post: Larz60+
  How to fetch file from a list which match file extension suddhasilsarkar 1 2,796 Jul-30-2019, 11:54 AM
Last Post: Malt
  .py File extension now working Pronger05 5 4,323 Sep-26-2017, 08:50 PM
Last Post: nilamo
  Unable to open files without file extension pstarrett 3 7,220 Dec-25-2016, 09:15 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020